use cmd_sfunc_ftype and cmd_cfunc_ftype more
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
ecd75fc8 3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
0e9f083f 39#include <string.h>
50f182aa 40#include "gdb-demangle.h"
0ba1096a 41#include "filenames.h"
c906108c
SS
42#include "annotate.h"
43#include "symfile.h"
44#include "objfiles.h"
0378c332 45#include "source.h"
c5f0f3d0 46#include "linespec.h"
c94fdfd0 47#include "completer.h"
5b7f31a4 48#include "gdb.h"
8b93c638 49#include "ui-out.h"
e1507482 50#include "cli/cli-script.h"
0225421b 51#include "gdb_assert.h"
fe898f56 52#include "block.h"
a77053c2 53#include "solib.h"
84acb35a
JJ
54#include "solist.h"
55#include "observer.h"
60250e8b 56#include "exceptions.h"
765dc015 57#include "memattr.h"
f7f9143b 58#include "ada-lang.h"
d1aa2f50 59#include "top.h"
79a45b7d 60#include "valprint.h"
4efc6507 61#include "jit.h"
a96d9b2e 62#include "xml-syscall.h"
65d79d4b 63#include "parser-defs.h"
55aa24fb
SDJ
64#include "gdb_regex.h"
65#include "probe.h"
e9cafbcc 66#include "cli/cli-utils.h"
be34f849 67#include "continuations.h"
1bfeeb0f
JL
68#include "stack.h"
69#include "skip.h"
b775012e 70#include "ax-gdb.h"
e2e4d78b 71#include "dummy-frame.h"
c906108c 72
d3ce09f5
SS
73#include "format.h"
74
1042e4c0
SS
75/* readline include files */
76#include "readline/readline.h"
77#include "readline/history.h"
78
79/* readline defines this. */
80#undef savestring
81
034dad6f 82#include "mi/mi-common.h"
6dddc817 83#include "extension.h"
104c1213 84
e7e8980f
YQ
85/* Enums for exception-handling support. */
86enum exception_event_kind
87{
88 EX_EVENT_THROW,
591f19e8 89 EX_EVENT_RETHROW,
e7e8980f
YQ
90 EX_EVENT_CATCH
91};
92
4a64f543 93/* Prototypes for local functions. */
c906108c 94
a14ed312 95static void enable_delete_command (char *, int);
c906108c 96
a14ed312 97static void enable_once_command (char *, int);
c906108c 98
816338b5
SS
99static void enable_count_command (char *, int);
100
a14ed312 101static void disable_command (char *, int);
c906108c 102
a14ed312 103static void enable_command (char *, int);
c906108c 104
95a42b64
TT
105static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
106 void *),
107 void *);
c906108c 108
a14ed312 109static void ignore_command (char *, int);
c906108c 110
4efb68b1 111static int breakpoint_re_set_one (void *);
c906108c 112
348d480f
PA
113static void breakpoint_re_set_default (struct breakpoint *);
114
983af33b
SDJ
115static void create_sals_from_address_default (char **,
116 struct linespec_result *,
117 enum bptype, char *,
118 char **);
119
120static void create_breakpoints_sal_default (struct gdbarch *,
121 struct linespec_result *,
e7e0cddf 122 char *, char *, enum bptype,
983af33b
SDJ
123 enum bpdisp, int, int,
124 int,
125 const struct breakpoint_ops *,
44f238bb 126 int, int, int, unsigned);
983af33b
SDJ
127
128static void decode_linespec_default (struct breakpoint *, char **,
129 struct symtabs_and_lines *);
130
a14ed312 131static void clear_command (char *, int);
c906108c 132
a14ed312 133static void catch_command (char *, int);
c906108c 134
a9634178 135static int can_use_hardware_watchpoint (struct value *);
c906108c 136
98deb0da 137static void break_command_1 (char *, int, int);
c906108c 138
a14ed312 139static void mention (struct breakpoint *);
c906108c 140
348d480f
PA
141static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
142 enum bptype,
c0a91b2b 143 const struct breakpoint_ops *);
3742cc8b
YQ
144static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
145 const struct symtab_and_line *);
146
4a64f543
MS
147/* This function is used in gdbtk sources and thus can not be made
148 static. */
63c252f8 149struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 150 struct symtab_and_line,
c0a91b2b
TT
151 enum bptype,
152 const struct breakpoint_ops *);
c906108c 153
06edf0c0
PA
154static struct breakpoint *
155 momentary_breakpoint_from_master (struct breakpoint *orig,
156 enum bptype type,
a1aa2221
LM
157 const struct breakpoint_ops *ops,
158 int loc_enabled);
06edf0c0 159
76897487
KB
160static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
161
a6d9a66e
UW
162static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
163 CORE_ADDR bpaddr,
88f7da05 164 enum bptype bptype);
76897487 165
6c95b8df
PA
166static void describe_other_breakpoints (struct gdbarch *,
167 struct program_space *, CORE_ADDR,
5af949e3 168 struct obj_section *, int);
c906108c 169
85d721b8
PA
170static int watchpoint_locations_match (struct bp_location *loc1,
171 struct bp_location *loc2);
172
f1310107
TJB
173static int breakpoint_location_address_match (struct bp_location *bl,
174 struct address_space *aspace,
175 CORE_ADDR addr);
176
a14ed312 177static void breakpoints_info (char *, int);
c906108c 178
d77f58be
SS
179static void watchpoints_info (char *, int);
180
e5a67952
MS
181static int breakpoint_1 (char *, int,
182 int (*) (const struct breakpoint *));
c906108c 183
4efb68b1 184static int breakpoint_cond_eval (void *);
c906108c 185
4efb68b1 186static void cleanup_executing_breakpoints (void *);
c906108c 187
a14ed312 188static void commands_command (char *, int);
c906108c 189
a14ed312 190static void condition_command (char *, int);
c906108c 191
c5aa993b
JM
192typedef enum
193 {
194 mark_inserted,
195 mark_uninserted
196 }
197insertion_state_t;
c906108c 198
0bde7532 199static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 200static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 201
e514a9d6 202static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 203
4efb68b1 204static int watchpoint_check (void *);
c906108c 205
a14ed312 206static void maintenance_info_breakpoints (char *, int);
c906108c 207
a14ed312 208static int hw_breakpoint_used_count (void);
c906108c 209
a1398e0c
PA
210static int hw_watchpoint_use_count (struct breakpoint *);
211
212static int hw_watchpoint_used_count_others (struct breakpoint *except,
213 enum bptype type,
214 int *other_type_used);
c906108c 215
a14ed312 216static void hbreak_command (char *, int);
c906108c 217
a14ed312 218static void thbreak_command (char *, int);
c906108c 219
816338b5
SS
220static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
221 int count);
c906108c 222
a14ed312 223static void stop_command (char *arg, int from_tty);
7a292a7a 224
a14ed312 225static void stopin_command (char *arg, int from_tty);
7a292a7a 226
a14ed312 227static void stopat_command (char *arg, 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
ef370185
JB
233static int find_single_step_breakpoint (struct address_space *aspace,
234 CORE_ADDR pc);
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
558a9d82
YQ
262static void set_tracepoint_count (int num);
263
9c06b0b4
TJB
264static int is_masked_watchpoint (const struct breakpoint *b);
265
b775012e
LM
266static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
267
983af33b
SDJ
268/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
269 otherwise. */
270
271static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 272
2060206e
PA
273/* The abstract base class all breakpoint_ops structures inherit
274 from. */
ab04a2af 275struct breakpoint_ops base_breakpoint_ops;
2060206e
PA
276
277/* The breakpoint_ops structure to be inherited by all breakpoint_ops
278 that are implemented on top of software or hardware breakpoints
279 (user breakpoints, internal and momentary breakpoints, etc.). */
280static struct breakpoint_ops bkpt_base_breakpoint_ops;
281
282/* Internal breakpoints class type. */
06edf0c0 283static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
284
285/* Momentary breakpoints class type. */
06edf0c0
PA
286static struct breakpoint_ops momentary_breakpoint_ops;
287
e2e4d78b
JK
288/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
289static struct breakpoint_ops longjmp_breakpoint_ops;
290
2060206e
PA
291/* The breakpoint_ops structure to be used in regular user created
292 breakpoints. */
293struct breakpoint_ops bkpt_breakpoint_ops;
294
55aa24fb
SDJ
295/* Breakpoints set on probes. */
296static struct breakpoint_ops bkpt_probe_breakpoint_ops;
297
e7e0cddf 298/* Dynamic printf class type. */
c5867ab6 299struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 300
9d497a19
PA
301/* One (or perhaps two) breakpoints used for software single
302 stepping. */
303
304static void *single_step_breakpoints[2];
305static struct gdbarch *single_step_gdbarch[2];
306
d3ce09f5
SS
307/* The style in which to perform a dynamic printf. This is a user
308 option because different output options have different tradeoffs;
309 if GDB does the printing, there is better error handling if there
310 is a problem with any of the arguments, but using an inferior
311 function lets you have special-purpose printers and sending of
312 output to the same place as compiled-in print functions. */
313
314static const char dprintf_style_gdb[] = "gdb";
315static const char dprintf_style_call[] = "call";
316static const char dprintf_style_agent[] = "agent";
317static const char *const dprintf_style_enums[] = {
318 dprintf_style_gdb,
319 dprintf_style_call,
320 dprintf_style_agent,
321 NULL
322};
323static const char *dprintf_style = dprintf_style_gdb;
324
325/* The function to use for dynamic printf if the preferred style is to
326 call into the inferior. The value is simply a string that is
327 copied into the command, so it can be anything that GDB can
328 evaluate to a callable address, not necessarily a function name. */
329
330static char *dprintf_function = "";
331
332/* The channel to use for dynamic printf if the preferred style is to
333 call into the inferior; if a nonempty string, it will be passed to
334 the call as the first argument, with the format string as the
335 second. As with the dprintf function, this can be anything that
336 GDB knows how to evaluate, so in addition to common choices like
337 "stderr", this could be an app-specific expression like
338 "mystreams[curlogger]". */
339
340static char *dprintf_channel = "";
341
342/* True if dprintf commands should continue to operate even if GDB
343 has disconnected. */
344static int disconnected_dprintf = 1;
345
5cea2a26
PA
346/* A reference-counted struct command_line. This lets multiple
347 breakpoints share a single command list. */
348struct counted_command_line
349{
350 /* The reference count. */
351 int refc;
352
353 /* The command list. */
354 struct command_line *commands;
355};
356
357struct command_line *
358breakpoint_commands (struct breakpoint *b)
359{
360 return b->commands ? b->commands->commands : NULL;
361}
3daf8fe5 362
f3b1572e
PA
363/* Flag indicating that a command has proceeded the inferior past the
364 current breakpoint. */
365
366static int breakpoint_proceeded;
367
956a9fb9 368const char *
2cec12e5
AR
369bpdisp_text (enum bpdisp disp)
370{
4a64f543
MS
371 /* NOTE: the following values are a part of MI protocol and
372 represent values of 'disp' field returned when inferior stops at
373 a breakpoint. */
bc043ef3 374 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 375
2cec12e5
AR
376 return bpdisps[(int) disp];
377}
c906108c 378
4a64f543 379/* Prototypes for exported functions. */
c906108c 380/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 381 if such is available. */
c906108c
SS
382static int can_use_hw_watchpoints;
383
920d2a44
AC
384static void
385show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
386 struct cmd_list_element *c,
387 const char *value)
388{
3e43a32a
MS
389 fprintf_filtered (file,
390 _("Debugger's willingness to use "
391 "watchpoint hardware is %s.\n"),
920d2a44
AC
392 value);
393}
394
fa8d40ab
JJ
395/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
396 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 397 for unrecognized breakpoint locations.
fa8d40ab
JJ
398 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
399static enum auto_boolean pending_break_support;
920d2a44
AC
400static void
401show_pending_break_support (struct ui_file *file, int from_tty,
402 struct cmd_list_element *c,
403 const char *value)
404{
3e43a32a
MS
405 fprintf_filtered (file,
406 _("Debugger's behavior regarding "
407 "pending breakpoints is %s.\n"),
920d2a44
AC
408 value);
409}
fa8d40ab 410
765dc015 411/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 412 set with "break" but falling in read-only memory.
765dc015
VP
413 If 0, gdb will warn about such breakpoints, but won't automatically
414 use hardware breakpoints. */
415static int automatic_hardware_breakpoints;
416static void
417show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
418 struct cmd_list_element *c,
419 const char *value)
420{
3e43a32a
MS
421 fprintf_filtered (file,
422 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
423 value);
424}
425
33e5cbd6
PA
426/* If on, gdb will keep breakpoints inserted even as inferior is
427 stopped, and immediately insert any new breakpoints. If off, gdb
428 will insert breakpoints into inferior only when resuming it, and
429 will remove breakpoints upon stop. If auto, GDB will behave as ON
430 if in non-stop mode, and as OFF if all-stop mode.*/
431
72d0e2c5
YQ
432static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
433
33e5cbd6 434static void
74960c60 435show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 436 struct cmd_list_element *c, const char *value)
74960c60 437{
72d0e2c5 438 if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
3e43a32a
MS
439 fprintf_filtered (file,
440 _("Always inserted breakpoint "
441 "mode is %s (currently %s).\n"),
33e5cbd6
PA
442 value,
443 breakpoints_always_inserted_mode () ? "on" : "off");
444 else
3e43a32a
MS
445 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
446 value);
74960c60
VP
447}
448
33e5cbd6
PA
449int
450breakpoints_always_inserted_mode (void)
451{
72d0e2c5
YQ
452 return (always_inserted_mode == AUTO_BOOLEAN_TRUE
453 || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
33e5cbd6 454}
765dc015 455
b775012e
LM
456static const char condition_evaluation_both[] = "host or target";
457
458/* Modes for breakpoint condition evaluation. */
459static const char condition_evaluation_auto[] = "auto";
460static const char condition_evaluation_host[] = "host";
461static const char condition_evaluation_target[] = "target";
462static const char *const condition_evaluation_enums[] = {
463 condition_evaluation_auto,
464 condition_evaluation_host,
465 condition_evaluation_target,
466 NULL
467};
468
469/* Global that holds the current mode for breakpoint condition evaluation. */
470static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
471
472/* Global that we use to display information to the user (gets its value from
473 condition_evaluation_mode_1. */
474static const char *condition_evaluation_mode = condition_evaluation_auto;
475
476/* Translate a condition evaluation mode MODE into either "host"
477 or "target". This is used mostly to translate from "auto" to the
478 real setting that is being used. It returns the translated
479 evaluation mode. */
480
481static const char *
482translate_condition_evaluation_mode (const char *mode)
483{
484 if (mode == condition_evaluation_auto)
485 {
486 if (target_supports_evaluation_of_breakpoint_conditions ())
487 return condition_evaluation_target;
488 else
489 return condition_evaluation_host;
490 }
491 else
492 return mode;
493}
494
495/* Discovers what condition_evaluation_auto translates to. */
496
497static const char *
498breakpoint_condition_evaluation_mode (void)
499{
500 return translate_condition_evaluation_mode (condition_evaluation_mode);
501}
502
503/* Return true if GDB should evaluate breakpoint conditions or false
504 otherwise. */
505
506static int
507gdb_evaluates_breakpoint_condition_p (void)
508{
509 const char *mode = breakpoint_condition_evaluation_mode ();
510
511 return (mode == condition_evaluation_host);
512}
513
a14ed312 514void _initialize_breakpoint (void);
c906108c 515
c906108c
SS
516/* Are we executing breakpoint commands? */
517static int executing_breakpoint_commands;
518
c02f5703
MS
519/* Are overlay event breakpoints enabled? */
520static int overlay_events_enabled;
521
e09342b5
TJB
522/* See description in breakpoint.h. */
523int target_exact_watchpoints = 0;
524
c906108c 525/* Walk the following statement or block through all breakpoints.
e5dd4106 526 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 527 current breakpoint. */
c906108c 528
5c44784c 529#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 530
5c44784c
JM
531#define ALL_BREAKPOINTS_SAFE(B,TMP) \
532 for (B = breakpoint_chain; \
533 B ? (TMP=B->next, 1): 0; \
534 B = TMP)
c906108c 535
4a64f543
MS
536/* Similar iterator for the low-level breakpoints. SAFE variant is
537 not provided so update_global_location_list must not be called
538 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 539
876fa593
JK
540#define ALL_BP_LOCATIONS(B,BP_TMP) \
541 for (BP_TMP = bp_location; \
542 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
543 BP_TMP++)
7cc221ef 544
b775012e
LM
545/* Iterates through locations with address ADDRESS for the currently selected
546 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
547 to where the loop should start from.
548 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
549 appropriate location to start with. */
550
551#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
552 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
553 BP_LOCP_TMP = BP_LOCP_START; \
554 BP_LOCP_START \
555 && (BP_LOCP_TMP < bp_location + bp_location_count \
556 && (*BP_LOCP_TMP)->address == ADDRESS); \
557 BP_LOCP_TMP++)
558
1042e4c0
SS
559/* Iterator for tracepoints only. */
560
561#define ALL_TRACEPOINTS(B) \
562 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 563 if (is_tracepoint (B))
1042e4c0 564
7cc221ef 565/* Chains of all breakpoints defined. */
c906108c
SS
566
567struct breakpoint *breakpoint_chain;
568
876fa593
JK
569/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
570
571static struct bp_location **bp_location;
572
573/* Number of elements of BP_LOCATION. */
574
575static unsigned bp_location_count;
576
4a64f543
MS
577/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
578 ADDRESS for the current elements of BP_LOCATION which get a valid
579 result from bp_location_has_shadow. You can use it for roughly
580 limiting the subrange of BP_LOCATION to scan for shadow bytes for
581 an address you need to read. */
876fa593
JK
582
583static CORE_ADDR bp_location_placed_address_before_address_max;
584
4a64f543
MS
585/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
586 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
587 BP_LOCATION which get a valid result from bp_location_has_shadow.
588 You can use it for roughly limiting the subrange of BP_LOCATION to
589 scan for shadow bytes for an address you need to read. */
876fa593
JK
590
591static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 592
4a64f543
MS
593/* The locations that no longer correspond to any breakpoint, unlinked
594 from bp_location array, but for which a hit may still be reported
595 by a target. */
20874c92
VP
596VEC(bp_location_p) *moribund_locations = NULL;
597
c906108c
SS
598/* Number of last breakpoint made. */
599
95a42b64
TT
600static int breakpoint_count;
601
86b17b60
PA
602/* The value of `breakpoint_count' before the last command that
603 created breakpoints. If the last (break-like) command created more
604 than one breakpoint, then the difference between BREAKPOINT_COUNT
605 and PREV_BREAKPOINT_COUNT is more than one. */
606static int prev_breakpoint_count;
c906108c 607
1042e4c0
SS
608/* Number of last tracepoint made. */
609
95a42b64 610static int tracepoint_count;
1042e4c0 611
6149aea9
PA
612static struct cmd_list_element *breakpoint_set_cmdlist;
613static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 614struct cmd_list_element *save_cmdlist;
6149aea9 615
468d015d
JJ
616/* Return whether a breakpoint is an active enabled breakpoint. */
617static int
618breakpoint_enabled (struct breakpoint *b)
619{
0d381245 620 return (b->enable_state == bp_enabled);
468d015d
JJ
621}
622
c906108c
SS
623/* Set breakpoint count to NUM. */
624
95a42b64 625static void
fba45db2 626set_breakpoint_count (int num)
c906108c 627{
86b17b60 628 prev_breakpoint_count = breakpoint_count;
c906108c 629 breakpoint_count = num;
4fa62494 630 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
631}
632
86b17b60
PA
633/* Used by `start_rbreak_breakpoints' below, to record the current
634 breakpoint count before "rbreak" creates any breakpoint. */
635static int rbreak_start_breakpoint_count;
636
95a42b64
TT
637/* Called at the start an "rbreak" command to record the first
638 breakpoint made. */
86b17b60 639
95a42b64
TT
640void
641start_rbreak_breakpoints (void)
642{
86b17b60 643 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
644}
645
646/* Called at the end of an "rbreak" command to record the last
647 breakpoint made. */
86b17b60 648
95a42b64
TT
649void
650end_rbreak_breakpoints (void)
651{
86b17b60 652 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
653}
654
4a64f543 655/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
656
657void
fba45db2 658clear_breakpoint_hit_counts (void)
c906108c
SS
659{
660 struct breakpoint *b;
661
662 ALL_BREAKPOINTS (b)
663 b->hit_count = 0;
664}
665
9add0f1b
TT
666/* Allocate a new counted_command_line with reference count of 1.
667 The new structure owns COMMANDS. */
668
669static struct counted_command_line *
670alloc_counted_command_line (struct command_line *commands)
671{
672 struct counted_command_line *result
673 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 674
9add0f1b
TT
675 result->refc = 1;
676 result->commands = commands;
677 return result;
678}
679
680/* Increment reference count. This does nothing if CMD is NULL. */
681
682static void
683incref_counted_command_line (struct counted_command_line *cmd)
684{
685 if (cmd)
686 ++cmd->refc;
687}
688
689/* Decrement reference count. If the reference count reaches 0,
690 destroy the counted_command_line. Sets *CMDP to NULL. This does
691 nothing if *CMDP is NULL. */
692
693static void
694decref_counted_command_line (struct counted_command_line **cmdp)
695{
696 if (*cmdp)
697 {
698 if (--(*cmdp)->refc == 0)
699 {
700 free_command_lines (&(*cmdp)->commands);
701 xfree (*cmdp);
702 }
703 *cmdp = NULL;
704 }
705}
706
707/* A cleanup function that calls decref_counted_command_line. */
708
709static void
710do_cleanup_counted_command_line (void *arg)
711{
712 decref_counted_command_line (arg);
713}
714
715/* Create a cleanup that calls decref_counted_command_line on the
716 argument. */
717
718static struct cleanup *
719make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
720{
721 return make_cleanup (do_cleanup_counted_command_line, cmdp);
722}
723
c906108c 724\f
48cb2d85
VP
725/* Return the breakpoint with the specified number, or NULL
726 if the number does not refer to an existing breakpoint. */
727
728struct breakpoint *
729get_breakpoint (int num)
730{
731 struct breakpoint *b;
732
733 ALL_BREAKPOINTS (b)
734 if (b->number == num)
735 return b;
736
737 return NULL;
738}
5c44784c 739
c906108c 740\f
adc36818 741
b775012e
LM
742/* Mark locations as "conditions have changed" in case the target supports
743 evaluating conditions on its side. */
744
745static void
746mark_breakpoint_modified (struct breakpoint *b)
747{
748 struct bp_location *loc;
749
750 /* This is only meaningful if the target is
751 evaluating conditions and if the user has
752 opted for condition evaluation on the target's
753 side. */
754 if (gdb_evaluates_breakpoint_condition_p ()
755 || !target_supports_evaluation_of_breakpoint_conditions ())
756 return;
757
758 if (!is_breakpoint (b))
759 return;
760
761 for (loc = b->loc; loc; loc = loc->next)
762 loc->condition_changed = condition_modified;
763}
764
765/* Mark location as "conditions have changed" in case the target supports
766 evaluating conditions on its side. */
767
768static void
769mark_breakpoint_location_modified (struct bp_location *loc)
770{
771 /* This is only meaningful if the target is
772 evaluating conditions and if the user has
773 opted for condition evaluation on the target's
774 side. */
775 if (gdb_evaluates_breakpoint_condition_p ()
776 || !target_supports_evaluation_of_breakpoint_conditions ())
777
778 return;
779
780 if (!is_breakpoint (loc->owner))
781 return;
782
783 loc->condition_changed = condition_modified;
784}
785
786/* Sets the condition-evaluation mode using the static global
787 condition_evaluation_mode. */
788
789static void
790set_condition_evaluation_mode (char *args, int from_tty,
791 struct cmd_list_element *c)
792{
b775012e
LM
793 const char *old_mode, *new_mode;
794
795 if ((condition_evaluation_mode_1 == condition_evaluation_target)
796 && !target_supports_evaluation_of_breakpoint_conditions ())
797 {
798 condition_evaluation_mode_1 = condition_evaluation_mode;
799 warning (_("Target does not support breakpoint condition evaluation.\n"
800 "Using host evaluation mode instead."));
801 return;
802 }
803
804 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
805 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
806
abf1152a
JK
807 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
808 settings was "auto". */
809 condition_evaluation_mode = condition_evaluation_mode_1;
810
b775012e
LM
811 /* Only update the mode if the user picked a different one. */
812 if (new_mode != old_mode)
813 {
814 struct bp_location *loc, **loc_tmp;
815 /* If the user switched to a different evaluation mode, we
816 need to synch the changes with the target as follows:
817
818 "host" -> "target": Send all (valid) conditions to the target.
819 "target" -> "host": Remove all the conditions from the target.
820 */
821
b775012e
LM
822 if (new_mode == condition_evaluation_target)
823 {
824 /* Mark everything modified and synch conditions with the
825 target. */
826 ALL_BP_LOCATIONS (loc, loc_tmp)
827 mark_breakpoint_location_modified (loc);
828 }
829 else
830 {
831 /* Manually mark non-duplicate locations to synch conditions
832 with the target. We do this to remove all the conditions the
833 target knows about. */
834 ALL_BP_LOCATIONS (loc, loc_tmp)
835 if (is_breakpoint (loc->owner) && loc->inserted)
836 loc->needs_update = 1;
837 }
838
839 /* Do the update. */
840 update_global_location_list (1);
841 }
842
843 return;
844}
845
846/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
847 what "auto" is translating to. */
848
849static void
850show_condition_evaluation_mode (struct ui_file *file, int from_tty,
851 struct cmd_list_element *c, const char *value)
852{
853 if (condition_evaluation_mode == condition_evaluation_auto)
854 fprintf_filtered (file,
855 _("Breakpoint condition evaluation "
856 "mode is %s (currently %s).\n"),
857 value,
858 breakpoint_condition_evaluation_mode ());
859 else
860 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
861 value);
862}
863
864/* A comparison function for bp_location AP and BP that is used by
865 bsearch. This comparison function only cares about addresses, unlike
866 the more general bp_location_compare function. */
867
868static int
869bp_location_compare_addrs (const void *ap, const void *bp)
870{
871 struct bp_location *a = *(void **) ap;
872 struct bp_location *b = *(void **) bp;
873
874 if (a->address == b->address)
875 return 0;
876 else
877 return ((a->address > b->address) - (a->address < b->address));
878}
879
880/* Helper function to skip all bp_locations with addresses
881 less than ADDRESS. It returns the first bp_location that
882 is greater than or equal to ADDRESS. If none is found, just
883 return NULL. */
884
885static struct bp_location **
886get_first_locp_gte_addr (CORE_ADDR address)
887{
888 struct bp_location dummy_loc;
889 struct bp_location *dummy_locp = &dummy_loc;
890 struct bp_location **locp_found = NULL;
891
892 /* Initialize the dummy location's address field. */
893 memset (&dummy_loc, 0, sizeof (struct bp_location));
894 dummy_loc.address = address;
895
896 /* Find a close match to the first location at ADDRESS. */
897 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
898 sizeof (struct bp_location **),
899 bp_location_compare_addrs);
900
901 /* Nothing was found, nothing left to do. */
902 if (locp_found == NULL)
903 return NULL;
904
905 /* We may have found a location that is at ADDRESS but is not the first in the
906 location's list. Go backwards (if possible) and locate the first one. */
907 while ((locp_found - 1) >= bp_location
908 && (*(locp_found - 1))->address == address)
909 locp_found--;
910
911 return locp_found;
912}
913
adc36818
PM
914void
915set_breakpoint_condition (struct breakpoint *b, char *exp,
916 int from_tty)
917{
3a5c3e22
PA
918 xfree (b->cond_string);
919 b->cond_string = NULL;
adc36818 920
3a5c3e22 921 if (is_watchpoint (b))
adc36818 922 {
3a5c3e22
PA
923 struct watchpoint *w = (struct watchpoint *) b;
924
925 xfree (w->cond_exp);
926 w->cond_exp = NULL;
927 }
928 else
929 {
930 struct bp_location *loc;
931
932 for (loc = b->loc; loc; loc = loc->next)
933 {
934 xfree (loc->cond);
935 loc->cond = NULL;
b775012e
LM
936
937 /* No need to free the condition agent expression
938 bytecode (if we have one). We will handle this
939 when we go through update_global_location_list. */
3a5c3e22 940 }
adc36818 941 }
adc36818
PM
942
943 if (*exp == 0)
944 {
945 if (from_tty)
946 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
947 }
948 else
949 {
bbc13ae3 950 const char *arg = exp;
cc59ec59 951
adc36818
PM
952 /* I don't know if it matters whether this is the string the user
953 typed in or the decompiled expression. */
954 b->cond_string = xstrdup (arg);
955 b->condition_not_parsed = 0;
956
957 if (is_watchpoint (b))
958 {
3a5c3e22
PA
959 struct watchpoint *w = (struct watchpoint *) b;
960
adc36818
PM
961 innermost_block = NULL;
962 arg = exp;
1bb9788d 963 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
964 if (*arg)
965 error (_("Junk at end of expression"));
3a5c3e22 966 w->cond_exp_valid_block = innermost_block;
adc36818
PM
967 }
968 else
969 {
3a5c3e22
PA
970 struct bp_location *loc;
971
adc36818
PM
972 for (loc = b->loc; loc; loc = loc->next)
973 {
974 arg = exp;
975 loc->cond =
1bb9788d
TT
976 parse_exp_1 (&arg, loc->address,
977 block_for_pc (loc->address), 0);
adc36818
PM
978 if (*arg)
979 error (_("Junk at end of expression"));
980 }
981 }
982 }
b775012e
LM
983 mark_breakpoint_modified (b);
984
8d3788bd 985 observer_notify_breakpoint_modified (b);
adc36818
PM
986}
987
d55637df
TT
988/* Completion for the "condition" command. */
989
990static VEC (char_ptr) *
6f937416
PA
991condition_completer (struct cmd_list_element *cmd,
992 const char *text, const char *word)
d55637df 993{
6f937416 994 const char *space;
d55637df 995
6f937416
PA
996 text = skip_spaces_const (text);
997 space = skip_to_space_const (text);
d55637df
TT
998 if (*space == '\0')
999 {
1000 int len;
1001 struct breakpoint *b;
1002 VEC (char_ptr) *result = NULL;
1003
1004 if (text[0] == '$')
1005 {
1006 /* We don't support completion of history indices. */
1007 if (isdigit (text[1]))
1008 return NULL;
1009 return complete_internalvar (&text[1]);
1010 }
1011
1012 /* We're completing the breakpoint number. */
1013 len = strlen (text);
1014
1015 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1016 {
1017 char number[50];
1018
1019 xsnprintf (number, sizeof (number), "%d", b->number);
1020
1021 if (strncmp (number, text, len) == 0)
1022 VEC_safe_push (char_ptr, result, xstrdup (number));
1023 }
d55637df
TT
1024
1025 return result;
1026 }
1027
1028 /* We're completing the expression part. */
6f937416 1029 text = skip_spaces_const (space);
d55637df
TT
1030 return expression_completer (cmd, text, word);
1031}
1032
c906108c
SS
1033/* condition N EXP -- set break condition of breakpoint N to EXP. */
1034
1035static void
fba45db2 1036condition_command (char *arg, int from_tty)
c906108c 1037{
52f0bd74 1038 struct breakpoint *b;
c906108c 1039 char *p;
52f0bd74 1040 int bnum;
c906108c
SS
1041
1042 if (arg == 0)
e2e0b3e5 1043 error_no_arg (_("breakpoint number"));
c906108c
SS
1044
1045 p = arg;
1046 bnum = get_number (&p);
5c44784c 1047 if (bnum == 0)
8a3fe4f8 1048 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1049
1050 ALL_BREAKPOINTS (b)
1051 if (b->number == bnum)
2f069f6f 1052 {
6dddc817
DE
1053 /* Check if this breakpoint has a "stop" method implemented in an
1054 extension language. This method and conditions entered into GDB
1055 from the CLI are mutually exclusive. */
1056 const struct extension_language_defn *extlang
1057 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1058
1059 if (extlang != NULL)
1060 {
1061 error (_("Only one stop condition allowed. There is currently"
1062 " a %s stop condition defined for this breakpoint."),
1063 ext_lang_capitalized_name (extlang));
1064 }
2566ad2d 1065 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1066
1067 if (is_breakpoint (b))
1068 update_global_location_list (1);
1069
2f069f6f
JB
1070 return;
1071 }
c906108c 1072
8a3fe4f8 1073 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1074}
1075
a7bdde9e
VP
1076/* Check that COMMAND do not contain commands that are suitable
1077 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1078 Throw if any such commands is found. */
1079
a7bdde9e
VP
1080static void
1081check_no_tracepoint_commands (struct command_line *commands)
1082{
1083 struct command_line *c;
cc59ec59 1084
a7bdde9e
VP
1085 for (c = commands; c; c = c->next)
1086 {
1087 int i;
1088
1089 if (c->control_type == while_stepping_control)
3e43a32a
MS
1090 error (_("The 'while-stepping' command can "
1091 "only be used for tracepoints"));
a7bdde9e
VP
1092
1093 for (i = 0; i < c->body_count; ++i)
1094 check_no_tracepoint_commands ((c->body_list)[i]);
1095
1096 /* Not that command parsing removes leading whitespace and comment
4a64f543 1097 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1098 command directly. */
1099 if (strstr (c->line, "collect ") == c->line)
1100 error (_("The 'collect' command can only be used for tracepoints"));
1101
51661e93
VP
1102 if (strstr (c->line, "teval ") == c->line)
1103 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1104 }
1105}
1106
d77f58be
SS
1107/* Encapsulate tests for different types of tracepoints. */
1108
d9b3f62e
PA
1109static int
1110is_tracepoint_type (enum bptype type)
1111{
1112 return (type == bp_tracepoint
1113 || type == bp_fast_tracepoint
1114 || type == bp_static_tracepoint);
1115}
1116
a7bdde9e 1117int
d77f58be 1118is_tracepoint (const struct breakpoint *b)
a7bdde9e 1119{
d9b3f62e 1120 return is_tracepoint_type (b->type);
a7bdde9e 1121}
d9b3f62e 1122
e5dd4106 1123/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1124 breakpoint. This function will throw an exception if a problem is
1125 found. */
48cb2d85 1126
95a42b64
TT
1127static void
1128validate_commands_for_breakpoint (struct breakpoint *b,
1129 struct command_line *commands)
48cb2d85 1130{
d77f58be 1131 if (is_tracepoint (b))
a7bdde9e 1132 {
c9a6ce02 1133 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1134 struct command_line *c;
1135 struct command_line *while_stepping = 0;
c9a6ce02
PA
1136
1137 /* Reset the while-stepping step count. The previous commands
1138 might have included a while-stepping action, while the new
1139 ones might not. */
1140 t->step_count = 0;
1141
1142 /* We need to verify that each top-level element of commands is
1143 valid for tracepoints, that there's at most one
1144 while-stepping element, and that the while-stepping's body
1145 has valid tracing commands excluding nested while-stepping.
1146 We also need to validate the tracepoint action line in the
1147 context of the tracepoint --- validate_actionline actually
1148 has side effects, like setting the tracepoint's
1149 while-stepping STEP_COUNT, in addition to checking if the
1150 collect/teval actions parse and make sense in the
1151 tracepoint's context. */
a7bdde9e
VP
1152 for (c = commands; c; c = c->next)
1153 {
a7bdde9e
VP
1154 if (c->control_type == while_stepping_control)
1155 {
1156 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1157 error (_("The 'while-stepping' command "
1158 "cannot be used for fast tracepoint"));
0fb4aa4b 1159 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1160 error (_("The 'while-stepping' command "
1161 "cannot be used for static tracepoint"));
a7bdde9e
VP
1162
1163 if (while_stepping)
3e43a32a
MS
1164 error (_("The 'while-stepping' command "
1165 "can be used only once"));
a7bdde9e
VP
1166 else
1167 while_stepping = c;
1168 }
c9a6ce02
PA
1169
1170 validate_actionline (c->line, b);
a7bdde9e
VP
1171 }
1172 if (while_stepping)
1173 {
1174 struct command_line *c2;
1175
1176 gdb_assert (while_stepping->body_count == 1);
1177 c2 = while_stepping->body_list[0];
1178 for (; c2; c2 = c2->next)
1179 {
a7bdde9e
VP
1180 if (c2->control_type == while_stepping_control)
1181 error (_("The 'while-stepping' command cannot be nested"));
1182 }
1183 }
1184 }
1185 else
1186 {
1187 check_no_tracepoint_commands (commands);
1188 }
95a42b64
TT
1189}
1190
0fb4aa4b
PA
1191/* Return a vector of all the static tracepoints set at ADDR. The
1192 caller is responsible for releasing the vector. */
1193
1194VEC(breakpoint_p) *
1195static_tracepoints_here (CORE_ADDR addr)
1196{
1197 struct breakpoint *b;
1198 VEC(breakpoint_p) *found = 0;
1199 struct bp_location *loc;
1200
1201 ALL_BREAKPOINTS (b)
1202 if (b->type == bp_static_tracepoint)
1203 {
1204 for (loc = b->loc; loc; loc = loc->next)
1205 if (loc->address == addr)
1206 VEC_safe_push(breakpoint_p, found, b);
1207 }
1208
1209 return found;
1210}
1211
95a42b64 1212/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1213 validate that only allowed commands are included. */
95a42b64
TT
1214
1215void
4a64f543
MS
1216breakpoint_set_commands (struct breakpoint *b,
1217 struct command_line *commands)
95a42b64
TT
1218{
1219 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1220
9add0f1b
TT
1221 decref_counted_command_line (&b->commands);
1222 b->commands = alloc_counted_command_line (commands);
8d3788bd 1223 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1224}
1225
45a43567
TT
1226/* Set the internal `silent' flag on the breakpoint. Note that this
1227 is not the same as the "silent" that may appear in the breakpoint's
1228 commands. */
1229
1230void
1231breakpoint_set_silent (struct breakpoint *b, int silent)
1232{
1233 int old_silent = b->silent;
1234
1235 b->silent = silent;
1236 if (old_silent != silent)
8d3788bd 1237 observer_notify_breakpoint_modified (b);
45a43567
TT
1238}
1239
1240/* Set the thread for this breakpoint. If THREAD is -1, make the
1241 breakpoint work for any thread. */
1242
1243void
1244breakpoint_set_thread (struct breakpoint *b, int thread)
1245{
1246 int old_thread = b->thread;
1247
1248 b->thread = thread;
1249 if (old_thread != thread)
8d3788bd 1250 observer_notify_breakpoint_modified (b);
45a43567
TT
1251}
1252
1253/* Set the task for this breakpoint. If TASK is 0, make the
1254 breakpoint work for any task. */
1255
1256void
1257breakpoint_set_task (struct breakpoint *b, int task)
1258{
1259 int old_task = b->task;
1260
1261 b->task = task;
1262 if (old_task != task)
8d3788bd 1263 observer_notify_breakpoint_modified (b);
45a43567
TT
1264}
1265
95a42b64
TT
1266void
1267check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1268{
1269 struct breakpoint *b = closure;
cc59ec59 1270
6f937416 1271 validate_actionline (line, b);
a7bdde9e
VP
1272}
1273
95a42b64
TT
1274/* A structure used to pass information through
1275 map_breakpoint_numbers. */
1276
1277struct commands_info
1278{
1279 /* True if the command was typed at a tty. */
1280 int from_tty;
86b17b60
PA
1281
1282 /* The breakpoint range spec. */
1283 char *arg;
1284
95a42b64
TT
1285 /* Non-NULL if the body of the commands are being read from this
1286 already-parsed command. */
1287 struct command_line *control;
86b17b60 1288
95a42b64
TT
1289 /* The command lines read from the user, or NULL if they have not
1290 yet been read. */
1291 struct counted_command_line *cmd;
1292};
1293
1294/* A callback for map_breakpoint_numbers that sets the commands for
1295 commands_command. */
1296
c906108c 1297static void
95a42b64 1298do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1299{
95a42b64 1300 struct commands_info *info = data;
c906108c 1301
95a42b64
TT
1302 if (info->cmd == NULL)
1303 {
1304 struct command_line *l;
5c44784c 1305
95a42b64
TT
1306 if (info->control != NULL)
1307 l = copy_command_lines (info->control->body_list[0]);
1308 else
86b17b60
PA
1309 {
1310 struct cleanup *old_chain;
1311 char *str;
c5aa993b 1312
3e43a32a
MS
1313 str = xstrprintf (_("Type commands for breakpoint(s) "
1314 "%s, one per line."),
86b17b60
PA
1315 info->arg);
1316
1317 old_chain = make_cleanup (xfree, str);
1318
1319 l = read_command_lines (str,
1320 info->from_tty, 1,
d77f58be 1321 (is_tracepoint (b)
86b17b60
PA
1322 ? check_tracepoint_command : 0),
1323 b);
1324
1325 do_cleanups (old_chain);
1326 }
a7bdde9e 1327
95a42b64
TT
1328 info->cmd = alloc_counted_command_line (l);
1329 }
1330
1331 /* If a breakpoint was on the list more than once, we don't need to
1332 do anything. */
1333 if (b->commands != info->cmd)
1334 {
1335 validate_commands_for_breakpoint (b, info->cmd->commands);
1336 incref_counted_command_line (info->cmd);
1337 decref_counted_command_line (&b->commands);
1338 b->commands = info->cmd;
8d3788bd 1339 observer_notify_breakpoint_modified (b);
c5aa993b 1340 }
95a42b64
TT
1341}
1342
1343static void
4a64f543
MS
1344commands_command_1 (char *arg, int from_tty,
1345 struct command_line *control)
95a42b64
TT
1346{
1347 struct cleanup *cleanups;
1348 struct commands_info info;
1349
1350 info.from_tty = from_tty;
1351 info.control = control;
1352 info.cmd = NULL;
1353 /* If we read command lines from the user, then `info' will hold an
1354 extra reference to the commands that we must clean up. */
1355 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1356
1357 if (arg == NULL || !*arg)
1358 {
86b17b60 1359 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1360 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1361 breakpoint_count);
95a42b64
TT
1362 else if (breakpoint_count > 0)
1363 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1364 else
1365 {
1366 /* So that we don't try to free the incoming non-NULL
1367 argument in the cleanup below. Mapping breakpoint
1368 numbers will fail in this case. */
1369 arg = NULL;
1370 }
95a42b64 1371 }
9766ced4
SS
1372 else
1373 /* The command loop has some static state, so we need to preserve
1374 our argument. */
1375 arg = xstrdup (arg);
86b17b60
PA
1376
1377 if (arg != NULL)
1378 make_cleanup (xfree, arg);
1379
1380 info.arg = arg;
95a42b64
TT
1381
1382 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1383
1384 if (info.cmd == NULL)
1385 error (_("No breakpoints specified."));
1386
1387 do_cleanups (cleanups);
1388}
1389
1390static void
1391commands_command (char *arg, int from_tty)
1392{
1393 commands_command_1 (arg, from_tty, NULL);
c906108c 1394}
40c03ae8
EZ
1395
1396/* Like commands_command, but instead of reading the commands from
1397 input stream, takes them from an already parsed command structure.
1398
1399 This is used by cli-script.c to DTRT with breakpoint commands
1400 that are part of if and while bodies. */
1401enum command_control_type
1402commands_from_control_command (char *arg, struct command_line *cmd)
1403{
95a42b64
TT
1404 commands_command_1 (arg, 0, cmd);
1405 return simple_control;
40c03ae8 1406}
876fa593
JK
1407
1408/* Return non-zero if BL->TARGET_INFO contains valid information. */
1409
1410static int
1411bp_location_has_shadow (struct bp_location *bl)
1412{
1413 if (bl->loc_type != bp_loc_software_breakpoint)
1414 return 0;
1415 if (!bl->inserted)
1416 return 0;
1417 if (bl->target_info.shadow_len == 0)
e5dd4106 1418 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1419 return 0;
1420 return 1;
1421}
1422
9d497a19
PA
1423/* Update BUF, which is LEN bytes read from the target address
1424 MEMADDR, by replacing a memory breakpoint with its shadowed
1425 contents.
1426
1427 If READBUF is not NULL, this buffer must not overlap with the of
1428 the breakpoint location's shadow_contents buffer. Otherwise, a
1429 failed assertion internal error will be raised. */
1430
1431static void
1432one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1433 const gdb_byte *writebuf_org,
1434 ULONGEST memaddr, LONGEST len,
1435 struct bp_target_info *target_info,
1436 struct gdbarch *gdbarch)
1437{
1438 /* Now do full processing of the found relevant range of elements. */
1439 CORE_ADDR bp_addr = 0;
1440 int bp_size = 0;
1441 int bptoffset = 0;
1442
1443 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1444 current_program_space->aspace, 0))
1445 {
1446 /* The breakpoint is inserted in a different address space. */
1447 return;
1448 }
1449
1450 /* Addresses and length of the part of the breakpoint that
1451 we need to copy. */
1452 bp_addr = target_info->placed_address;
1453 bp_size = target_info->shadow_len;
1454
1455 if (bp_addr + bp_size <= memaddr)
1456 {
1457 /* The breakpoint is entirely before the chunk of memory we are
1458 reading. */
1459 return;
1460 }
1461
1462 if (bp_addr >= memaddr + len)
1463 {
1464 /* The breakpoint is entirely after the chunk of memory we are
1465 reading. */
1466 return;
1467 }
1468
1469 /* Offset within shadow_contents. */
1470 if (bp_addr < memaddr)
1471 {
1472 /* Only copy the second part of the breakpoint. */
1473 bp_size -= memaddr - bp_addr;
1474 bptoffset = memaddr - bp_addr;
1475 bp_addr = memaddr;
1476 }
1477
1478 if (bp_addr + bp_size > memaddr + len)
1479 {
1480 /* Only copy the first part of the breakpoint. */
1481 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1482 }
1483
1484 if (readbuf != NULL)
1485 {
1486 /* Verify that the readbuf buffer does not overlap with the
1487 shadow_contents buffer. */
1488 gdb_assert (target_info->shadow_contents >= readbuf + len
1489 || readbuf >= (target_info->shadow_contents
1490 + target_info->shadow_len));
1491
1492 /* Update the read buffer with this inserted breakpoint's
1493 shadow. */
1494 memcpy (readbuf + bp_addr - memaddr,
1495 target_info->shadow_contents + bptoffset, bp_size);
1496 }
1497 else
1498 {
1499 const unsigned char *bp;
1500 CORE_ADDR placed_address = target_info->placed_address;
1501 int placed_size = target_info->placed_size;
1502
1503 /* Update the shadow with what we want to write to memory. */
1504 memcpy (target_info->shadow_contents + bptoffset,
1505 writebuf_org + bp_addr - memaddr, bp_size);
1506
1507 /* Determine appropriate breakpoint contents and size for this
1508 address. */
1509 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1510
1511 /* Update the final write buffer with this inserted
1512 breakpoint's INSN. */
1513 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1514 }
1515}
1516
8defab1a 1517/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1518 by replacing any memory breakpoints with their shadowed contents.
1519
35c63cd8
JB
1520 If READBUF is not NULL, this buffer must not overlap with any of
1521 the breakpoint location's shadow_contents buffers. Otherwise,
1522 a failed assertion internal error will be raised.
1523
876fa593 1524 The range of shadowed area by each bp_location is:
35df4500
TJB
1525 bl->address - bp_location_placed_address_before_address_max
1526 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1527 The range we were requested to resolve shadows for is:
1528 memaddr ... memaddr + len
1529 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1530 memaddr + len <= (bl->address
1531 - bp_location_placed_address_before_address_max)
876fa593 1532 and:
35df4500 1533 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1534
8defab1a 1535void
f0ba3972
PA
1536breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1537 const gdb_byte *writebuf_org,
1538 ULONGEST memaddr, LONGEST len)
c906108c 1539{
4a64f543
MS
1540 /* Left boundary, right boundary and median element of our binary
1541 search. */
876fa593 1542 unsigned bc_l, bc_r, bc;
9d497a19 1543 size_t i;
876fa593 1544
4a64f543
MS
1545 /* Find BC_L which is a leftmost element which may affect BUF
1546 content. It is safe to report lower value but a failure to
1547 report higher one. */
876fa593
JK
1548
1549 bc_l = 0;
1550 bc_r = bp_location_count;
1551 while (bc_l + 1 < bc_r)
1552 {
35df4500 1553 struct bp_location *bl;
876fa593
JK
1554
1555 bc = (bc_l + bc_r) / 2;
35df4500 1556 bl = bp_location[bc];
876fa593 1557
4a64f543
MS
1558 /* Check first BL->ADDRESS will not overflow due to the added
1559 constant. Then advance the left boundary only if we are sure
1560 the BC element can in no way affect the BUF content (MEMADDR
1561 to MEMADDR + LEN range).
876fa593 1562
4a64f543
MS
1563 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1564 offset so that we cannot miss a breakpoint with its shadow
1565 range tail still reaching MEMADDR. */
c5aa993b 1566
35df4500
TJB
1567 if ((bl->address + bp_location_shadow_len_after_address_max
1568 >= bl->address)
1569 && (bl->address + bp_location_shadow_len_after_address_max
1570 <= memaddr))
876fa593
JK
1571 bc_l = bc;
1572 else
1573 bc_r = bc;
1574 }
1575
128070bb
PA
1576 /* Due to the binary search above, we need to make sure we pick the
1577 first location that's at BC_L's address. E.g., if there are
1578 multiple locations at the same address, BC_L may end up pointing
1579 at a duplicate location, and miss the "master"/"inserted"
1580 location. Say, given locations L1, L2 and L3 at addresses A and
1581 B:
1582
1583 L1@A, L2@A, L3@B, ...
1584
1585 BC_L could end up pointing at location L2, while the "master"
1586 location could be L1. Since the `loc->inserted' flag is only set
1587 on "master" locations, we'd forget to restore the shadow of L1
1588 and L2. */
1589 while (bc_l > 0
1590 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1591 bc_l--;
1592
876fa593
JK
1593 /* Now do full processing of the found relevant range of elements. */
1594
1595 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1596 {
35df4500 1597 struct bp_location *bl = bp_location[bc];
876fa593
JK
1598 CORE_ADDR bp_addr = 0;
1599 int bp_size = 0;
1600 int bptoffset = 0;
1601
35df4500
TJB
1602 /* bp_location array has BL->OWNER always non-NULL. */
1603 if (bl->owner->type == bp_none)
8a3fe4f8 1604 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1605 bl->owner->number);
ffce0d52 1606
e5dd4106 1607 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1608 content. */
1609
35df4500
TJB
1610 if (bl->address >= bp_location_placed_address_before_address_max
1611 && memaddr + len <= (bl->address
1612 - bp_location_placed_address_before_address_max))
876fa593
JK
1613 break;
1614
35df4500 1615 if (!bp_location_has_shadow (bl))
c5aa993b 1616 continue;
6c95b8df 1617
9d497a19
PA
1618 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1619 memaddr, len, &bl->target_info, bl->gdbarch);
1620 }
c5aa993b 1621
9d497a19
PA
1622 /* Now process single-step breakpoints. These are not found in the
1623 bp_location array. */
1624 for (i = 0; i < 2; i++)
1625 {
1626 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
c5aa993b 1627
9d497a19
PA
1628 if (bp_tgt != NULL)
1629 {
1630 struct gdbarch *gdbarch = single_step_gdbarch[i];
c5aa993b 1631
9d497a19
PA
1632 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1633 memaddr, len, bp_tgt, gdbarch);
1634 }
1635 }
c906108c 1636}
9d497a19 1637
c906108c 1638\f
c5aa993b 1639
b775012e
LM
1640/* Return true if BPT is either a software breakpoint or a hardware
1641 breakpoint. */
1642
1643int
1644is_breakpoint (const struct breakpoint *bpt)
1645{
1646 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1647 || bpt->type == bp_hardware_breakpoint
1648 || bpt->type == bp_dprintf);
b775012e
LM
1649}
1650
60e1c644
PA
1651/* Return true if BPT is of any hardware watchpoint kind. */
1652
a5606eee 1653static int
d77f58be 1654is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1655{
1656 return (bpt->type == bp_hardware_watchpoint
1657 || bpt->type == bp_read_watchpoint
1658 || bpt->type == bp_access_watchpoint);
1659}
7270d8f2 1660
60e1c644
PA
1661/* Return true if BPT is of any watchpoint kind, hardware or
1662 software. */
1663
3a5c3e22 1664int
d77f58be 1665is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1666{
1667 return (is_hardware_watchpoint (bpt)
1668 || bpt->type == bp_watchpoint);
1669}
1670
3a5c3e22
PA
1671/* Returns true if the current thread and its running state are safe
1672 to evaluate or update watchpoint B. Watchpoints on local
1673 expressions need to be evaluated in the context of the thread that
1674 was current when the watchpoint was created, and, that thread needs
1675 to be stopped to be able to select the correct frame context.
1676 Watchpoints on global expressions can be evaluated on any thread,
1677 and in any state. It is presently left to the target allowing
1678 memory accesses when threads are running. */
f6bc2008
PA
1679
1680static int
3a5c3e22 1681watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1682{
d0d8b0c6
JK
1683 return (b->base.pspace == current_program_space
1684 && (ptid_equal (b->watchpoint_thread, null_ptid)
1685 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1686 && !is_executing (inferior_ptid))));
f6bc2008
PA
1687}
1688
d0fb5eae
JK
1689/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1690 associated bp_watchpoint_scope breakpoint. */
1691
1692static void
3a5c3e22 1693watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1694{
3a5c3e22 1695 struct breakpoint *b = &w->base;
d0fb5eae
JK
1696
1697 if (b->related_breakpoint != b)
1698 {
1699 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1700 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1701 b->related_breakpoint->disposition = disp_del_at_next_stop;
1702 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1703 b->related_breakpoint = b;
1704 }
1705 b->disposition = disp_del_at_next_stop;
1706}
1707
567e1b4e
JB
1708/* Assuming that B is a watchpoint:
1709 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1710 - Evaluate expression and store the result in B->val
567e1b4e
JB
1711 - Evaluate the condition if there is one, and store the result
1712 in b->loc->cond.
a5606eee
VP
1713 - Update the list of values that must be watched in B->loc.
1714
4a64f543
MS
1715 If the watchpoint disposition is disp_del_at_next_stop, then do
1716 nothing. If this is local watchpoint that is out of scope, delete
1717 it.
1718
1719 Even with `set breakpoint always-inserted on' the watchpoints are
1720 removed + inserted on each stop here. Normal breakpoints must
1721 never be removed because they might be missed by a running thread
1722 when debugging in non-stop mode. On the other hand, hardware
1723 watchpoints (is_hardware_watchpoint; processed here) are specific
1724 to each LWP since they are stored in each LWP's hardware debug
1725 registers. Therefore, such LWP must be stopped first in order to
1726 be able to modify its hardware watchpoints.
1727
1728 Hardware watchpoints must be reset exactly once after being
1729 presented to the user. It cannot be done sooner, because it would
1730 reset the data used to present the watchpoint hit to the user. And
1731 it must not be done later because it could display the same single
1732 watchpoint hit during multiple GDB stops. Note that the latter is
1733 relevant only to the hardware watchpoint types bp_read_watchpoint
1734 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1735 not user-visible - its hit is suppressed if the memory content has
1736 not changed.
1737
1738 The following constraints influence the location where we can reset
1739 hardware watchpoints:
1740
1741 * target_stopped_by_watchpoint and target_stopped_data_address are
1742 called several times when GDB stops.
1743
1744 [linux]
1745 * Multiple hardware watchpoints can be hit at the same time,
1746 causing GDB to stop. GDB only presents one hardware watchpoint
1747 hit at a time as the reason for stopping, and all the other hits
1748 are presented later, one after the other, each time the user
1749 requests the execution to be resumed. Execution is not resumed
1750 for the threads still having pending hit event stored in
1751 LWP_INFO->STATUS. While the watchpoint is already removed from
1752 the inferior on the first stop the thread hit event is kept being
1753 reported from its cached value by linux_nat_stopped_data_address
1754 until the real thread resume happens after the watchpoint gets
1755 presented and thus its LWP_INFO->STATUS gets reset.
1756
1757 Therefore the hardware watchpoint hit can get safely reset on the
1758 watchpoint removal from inferior. */
a79d3c27 1759
b40ce68a 1760static void
3a5c3e22 1761update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1762{
a5606eee 1763 int within_current_scope;
a5606eee 1764 struct frame_id saved_frame_id;
66076460 1765 int frame_saved;
a5606eee 1766
f6bc2008
PA
1767 /* If this is a local watchpoint, we only want to check if the
1768 watchpoint frame is in scope if the current thread is the thread
1769 that was used to create the watchpoint. */
1770 if (!watchpoint_in_thread_scope (b))
1771 return;
1772
3a5c3e22 1773 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1774 return;
1775
66076460 1776 frame_saved = 0;
a5606eee
VP
1777
1778 /* Determine if the watchpoint is within scope. */
1779 if (b->exp_valid_block == NULL)
1780 within_current_scope = 1;
1781 else
1782 {
b5db5dfc
UW
1783 struct frame_info *fi = get_current_frame ();
1784 struct gdbarch *frame_arch = get_frame_arch (fi);
1785 CORE_ADDR frame_pc = get_frame_pc (fi);
1786
1787 /* If we're in a function epilogue, unwinding may not work
1788 properly, so do not attempt to recreate locations at this
1789 point. See similar comments in watchpoint_check. */
1790 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1791 return;
66076460
DJ
1792
1793 /* Save the current frame's ID so we can restore it after
1794 evaluating the watchpoint expression on its own frame. */
1795 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1796 took a frame parameter, so that we didn't have to change the
1797 selected frame. */
1798 frame_saved = 1;
1799 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1800
a5606eee
VP
1801 fi = frame_find_by_id (b->watchpoint_frame);
1802 within_current_scope = (fi != NULL);
1803 if (within_current_scope)
1804 select_frame (fi);
1805 }
1806
b5db5dfc
UW
1807 /* We don't free locations. They are stored in the bp_location array
1808 and update_global_location_list will eventually delete them and
1809 remove breakpoints if needed. */
3a5c3e22 1810 b->base.loc = NULL;
b5db5dfc 1811
a5606eee
VP
1812 if (within_current_scope && reparse)
1813 {
bbc13ae3 1814 const char *s;
d63d0675 1815
a5606eee
VP
1816 if (b->exp)
1817 {
1818 xfree (b->exp);
1819 b->exp = NULL;
1820 }
d63d0675 1821 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1822 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1823 /* If the meaning of expression itself changed, the old value is
1824 no longer relevant. We don't want to report a watchpoint hit
1825 to the user when the old value and the new value may actually
1826 be completely different objects. */
1827 value_free (b->val);
fa4727a6
DJ
1828 b->val = NULL;
1829 b->val_valid = 0;
60e1c644
PA
1830
1831 /* Note that unlike with breakpoints, the watchpoint's condition
1832 expression is stored in the breakpoint object, not in the
1833 locations (re)created below. */
3a5c3e22 1834 if (b->base.cond_string != NULL)
60e1c644
PA
1835 {
1836 if (b->cond_exp != NULL)
1837 {
1838 xfree (b->cond_exp);
1839 b->cond_exp = NULL;
1840 }
1841
3a5c3e22 1842 s = b->base.cond_string;
1bb9788d 1843 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1844 }
a5606eee 1845 }
a5606eee
VP
1846
1847 /* If we failed to parse the expression, for example because
1848 it refers to a global variable in a not-yet-loaded shared library,
1849 don't try to insert watchpoint. We don't automatically delete
1850 such watchpoint, though, since failure to parse expression
1851 is different from out-of-scope watchpoint. */
e8369a73 1852 if (!target_has_execution)
2d134ed3
PA
1853 {
1854 /* Without execution, memory can't change. No use to try and
1855 set watchpoint locations. The watchpoint will be reset when
1856 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1857 if (!can_use_hw_watchpoints)
1858 {
1859 if (b->base.ops->works_in_software_mode (&b->base))
1860 b->base.type = bp_watchpoint;
1861 else
638aa5a1
AB
1862 error (_("Can't set read/access watchpoint when "
1863 "hardware watchpoints are disabled."));
e8369a73 1864 }
2d134ed3
PA
1865 }
1866 else if (within_current_scope && b->exp)
a5606eee 1867 {
0cf6dd15 1868 int pc = 0;
fa4727a6 1869 struct value *val_chain, *v, *result, *next;
2d134ed3 1870 struct program_space *frame_pspace;
a5606eee 1871
3a1115a0 1872 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1873
a5606eee
VP
1874 /* Avoid setting b->val if it's already set. The meaning of
1875 b->val is 'the last value' user saw, and we should update
1876 it only if we reported that last value to user. As it
9c06b0b4
TJB
1877 happens, the code that reports it updates b->val directly.
1878 We don't keep track of the memory value for masked
1879 watchpoints. */
3a5c3e22 1880 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1881 {
1882 b->val = v;
1883 b->val_valid = 1;
1884 }
a5606eee 1885
2d134ed3
PA
1886 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1887
a5606eee 1888 /* Look at each value on the value chain. */
9fa40276 1889 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1890 {
1891 /* If it's a memory location, and GDB actually needed
1892 its contents to evaluate the expression, then we
fa4727a6
DJ
1893 must watch it. If the first value returned is
1894 still lazy, that means an error occurred reading it;
1895 watch it anyway in case it becomes readable. */
a5606eee 1896 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1897 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1898 {
1899 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1900
a5606eee
VP
1901 /* We only watch structs and arrays if user asked
1902 for it explicitly, never if they just happen to
1903 appear in the middle of some value chain. */
fa4727a6 1904 if (v == result
a5606eee
VP
1905 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1906 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1907 {
1908 CORE_ADDR addr;
744a8059 1909 int type;
a5606eee
VP
1910 struct bp_location *loc, **tmp;
1911
42ae5230 1912 addr = value_address (v);
a5606eee 1913 type = hw_write;
3a5c3e22 1914 if (b->base.type == bp_read_watchpoint)
a5606eee 1915 type = hw_read;
3a5c3e22 1916 else if (b->base.type == bp_access_watchpoint)
a5606eee 1917 type = hw_access;
3a5c3e22
PA
1918
1919 loc = allocate_bp_location (&b->base);
1920 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1921 ;
1922 *tmp = loc;
a6d9a66e 1923 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1924
1925 loc->pspace = frame_pspace;
a5606eee 1926 loc->address = addr;
744a8059 1927 loc->length = TYPE_LENGTH (value_type (v));
a5606eee
VP
1928 loc->watchpoint_type = type;
1929 }
1930 }
9fa40276
TJB
1931 }
1932
1933 /* Change the type of breakpoint between hardware assisted or
1934 an ordinary watchpoint depending on the hardware support
1935 and free hardware slots. REPARSE is set when the inferior
1936 is started. */
a9634178 1937 if (reparse)
9fa40276 1938 {
e09342b5 1939 int reg_cnt;
9fa40276
TJB
1940 enum bp_loc_type loc_type;
1941 struct bp_location *bl;
a5606eee 1942
a9634178 1943 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1944
1945 if (reg_cnt)
9fa40276
TJB
1946 {
1947 int i, target_resources_ok, other_type_used;
a1398e0c 1948 enum bptype type;
9fa40276 1949
a9634178
TJB
1950 /* Use an exact watchpoint when there's only one memory region to be
1951 watched, and only one debug register is needed to watch it. */
1952 b->exact = target_exact_watchpoints && reg_cnt == 1;
1953
9fa40276 1954 /* We need to determine how many resources are already
e09342b5
TJB
1955 used for all other hardware watchpoints plus this one
1956 to see if we still have enough resources to also fit
a1398e0c
PA
1957 this watchpoint in as well. */
1958
1959 /* If this is a software watchpoint, we try to turn it
1960 to a hardware one -- count resources as if B was of
1961 hardware watchpoint type. */
1962 type = b->base.type;
1963 if (type == bp_watchpoint)
1964 type = bp_hardware_watchpoint;
1965
1966 /* This watchpoint may or may not have been placed on
1967 the list yet at this point (it won't be in the list
1968 if we're trying to create it for the first time,
1969 through watch_command), so always account for it
1970 manually. */
1971
1972 /* Count resources used by all watchpoints except B. */
1973 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1974
1975 /* Add in the resources needed for B. */
1976 i += hw_watchpoint_use_count (&b->base);
1977
1978 target_resources_ok
1979 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1980 if (target_resources_ok <= 0)
a9634178 1981 {
3a5c3e22 1982 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1983
1984 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1985 error (_("Target does not support this type of "
1986 "hardware watchpoint."));
9c06b0b4
TJB
1987 else if (target_resources_ok < 0 && !sw_mode)
1988 error (_("There are not enough available hardware "
1989 "resources for this watchpoint."));
a1398e0c
PA
1990
1991 /* Downgrade to software watchpoint. */
1992 b->base.type = bp_watchpoint;
1993 }
1994 else
1995 {
1996 /* If this was a software watchpoint, we've just
1997 found we have enough resources to turn it to a
1998 hardware watchpoint. Otherwise, this is a
1999 nop. */
2000 b->base.type = type;
a9634178 2001 }
9fa40276 2002 }
3a5c3e22 2003 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2004 {
2005 if (!can_use_hw_watchpoints)
2006 error (_("Can't set read/access watchpoint when "
2007 "hardware watchpoints are disabled."));
2008 else
2009 error (_("Expression cannot be implemented with "
2010 "read/access watchpoint."));
2011 }
9fa40276 2012 else
3a5c3e22 2013 b->base.type = bp_watchpoint;
9fa40276 2014
3a5c3e22 2015 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2016 : bp_loc_hardware_watchpoint);
3a5c3e22 2017 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2018 bl->loc_type = loc_type;
2019 }
2020
2021 for (v = val_chain; v; v = next)
2022 {
a5606eee
VP
2023 next = value_next (v);
2024 if (v != b->val)
2025 value_free (v);
2026 }
2027
c7437ca6
PA
2028 /* If a software watchpoint is not watching any memory, then the
2029 above left it without any location set up. But,
2030 bpstat_stop_status requires a location to be able to report
2031 stops, so make sure there's at least a dummy one. */
3a5c3e22 2032 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2033 {
3a5c3e22
PA
2034 struct breakpoint *base = &b->base;
2035 base->loc = allocate_bp_location (base);
2036 base->loc->pspace = frame_pspace;
2037 base->loc->address = -1;
2038 base->loc->length = -1;
2039 base->loc->watchpoint_type = -1;
c7437ca6 2040 }
a5606eee
VP
2041 }
2042 else if (!within_current_scope)
7270d8f2 2043 {
ac74f770
MS
2044 printf_filtered (_("\
2045Watchpoint %d deleted because the program has left the block\n\
2046in which its expression is valid.\n"),
3a5c3e22 2047 b->base.number);
d0fb5eae 2048 watchpoint_del_at_next_stop (b);
7270d8f2 2049 }
a5606eee
VP
2050
2051 /* Restore the selected frame. */
66076460
DJ
2052 if (frame_saved)
2053 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2054}
2055
a5606eee 2056
74960c60 2057/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2058 inserted in the inferior. We don't differentiate the type of BL's owner
2059 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2060 breakpoint_ops is not defined, because in insert_bp_location,
2061 tracepoint's insert_location will not be called. */
74960c60 2062static int
35df4500 2063should_be_inserted (struct bp_location *bl)
74960c60 2064{
35df4500 2065 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2066 return 0;
2067
35df4500 2068 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2069 return 0;
2070
35df4500 2071 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2072 return 0;
2073
f8eba3c6
TT
2074 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2075 return 0;
2076
56710373
PA
2077 /* This is set for example, when we're attached to the parent of a
2078 vfork, and have detached from the child. The child is running
2079 free, and we expect it to do an exec or exit, at which point the
2080 OS makes the parent schedulable again (and the target reports
2081 that the vfork is done). Until the child is done with the shared
2082 memory region, do not insert breakpoints in the parent, otherwise
2083 the child could still trip on the parent's breakpoints. Since
2084 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2085 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2086 return 0;
2087
31e77af2
PA
2088 /* Don't insert a breakpoint if we're trying to step past its
2089 location. */
2090 if ((bl->loc_type == bp_loc_software_breakpoint
2091 || bl->loc_type == bp_loc_hardware_breakpoint)
2092 && stepping_past_instruction_at (bl->pspace->aspace,
2093 bl->address))
2094 return 0;
2095
74960c60
VP
2096 return 1;
2097}
2098
934709f0
PW
2099/* Same as should_be_inserted but does the check assuming
2100 that the location is not duplicated. */
2101
2102static int
2103unduplicated_should_be_inserted (struct bp_location *bl)
2104{
2105 int result;
2106 const int save_duplicate = bl->duplicate;
2107
2108 bl->duplicate = 0;
2109 result = should_be_inserted (bl);
2110 bl->duplicate = save_duplicate;
2111 return result;
2112}
2113
b775012e
LM
2114/* Parses a conditional described by an expression COND into an
2115 agent expression bytecode suitable for evaluation
2116 by the bytecode interpreter. Return NULL if there was
2117 any error during parsing. */
2118
2119static struct agent_expr *
2120parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2121{
2122 struct agent_expr *aexpr = NULL;
b775012e
LM
2123 volatile struct gdb_exception ex;
2124
2125 if (!cond)
2126 return NULL;
2127
2128 /* We don't want to stop processing, so catch any errors
2129 that may show up. */
2130 TRY_CATCH (ex, RETURN_MASK_ERROR)
2131 {
2132 aexpr = gen_eval_for_expr (scope, cond);
2133 }
2134
2135 if (ex.reason < 0)
2136 {
2137 /* If we got here, it means the condition could not be parsed to a valid
2138 bytecode expression and thus can't be evaluated on the target's side.
2139 It's no use iterating through the conditions. */
2140 return NULL;
2141 }
2142
2143 /* We have a valid agent expression. */
2144 return aexpr;
2145}
2146
2147/* Based on location BL, create a list of breakpoint conditions to be
2148 passed on to the target. If we have duplicated locations with different
2149 conditions, we will add such conditions to the list. The idea is that the
2150 target will evaluate the list of conditions and will only notify GDB when
2151 one of them is true. */
2152
2153static void
2154build_target_condition_list (struct bp_location *bl)
2155{
2156 struct bp_location **locp = NULL, **loc2p;
2157 int null_condition_or_parse_error = 0;
2158 int modified = bl->needs_update;
2159 struct bp_location *loc;
2160
8b4f3082
PA
2161 /* Release conditions left over from a previous insert. */
2162 VEC_free (agent_expr_p, bl->target_info.conditions);
2163
b775012e
LM
2164 /* This is only meaningful if the target is
2165 evaluating conditions and if the user has
2166 opted for condition evaluation on the target's
2167 side. */
2168 if (gdb_evaluates_breakpoint_condition_p ()
2169 || !target_supports_evaluation_of_breakpoint_conditions ())
2170 return;
2171
2172 /* Do a first pass to check for locations with no assigned
2173 conditions or conditions that fail to parse to a valid agent expression
2174 bytecode. If any of these happen, then it's no use to send conditions
2175 to the target since this location will always trigger and generate a
2176 response back to GDB. */
2177 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2178 {
2179 loc = (*loc2p);
2180 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2181 {
2182 if (modified)
2183 {
2184 struct agent_expr *aexpr;
2185
2186 /* Re-parse the conditions since something changed. In that
2187 case we already freed the condition bytecodes (see
2188 force_breakpoint_reinsertion). We just
2189 need to parse the condition to bytecodes again. */
2190 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2191 loc->cond_bytecode = aexpr;
2192
2193 /* Check if we managed to parse the conditional expression
2194 correctly. If not, we will not send this condition
2195 to the target. */
2196 if (aexpr)
2197 continue;
2198 }
2199
2200 /* If we have a NULL bytecode expression, it means something
2201 went wrong or we have a null condition expression. */
2202 if (!loc->cond_bytecode)
2203 {
2204 null_condition_or_parse_error = 1;
2205 break;
2206 }
2207 }
2208 }
2209
2210 /* If any of these happened, it means we will have to evaluate the conditions
2211 for the location's address on gdb's side. It is no use keeping bytecodes
2212 for all the other duplicate locations, thus we free all of them here.
2213
2214 This is so we have a finer control over which locations' conditions are
2215 being evaluated by GDB or the remote stub. */
2216 if (null_condition_or_parse_error)
2217 {
2218 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2219 {
2220 loc = (*loc2p);
2221 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2222 {
2223 /* Only go as far as the first NULL bytecode is
2224 located. */
2225 if (!loc->cond_bytecode)
2226 return;
2227
2228 free_agent_expr (loc->cond_bytecode);
2229 loc->cond_bytecode = NULL;
2230 }
2231 }
2232 }
2233
2234 /* No NULL conditions or failed bytecode generation. Build a condition list
2235 for this location's address. */
2236 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2237 {
2238 loc = (*loc2p);
2239 if (loc->cond
2240 && is_breakpoint (loc->owner)
2241 && loc->pspace->num == bl->pspace->num
2242 && loc->owner->enable_state == bp_enabled
2243 && loc->enabled)
2244 /* Add the condition to the vector. This will be used later to send the
2245 conditions to the target. */
2246 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2247 loc->cond_bytecode);
2248 }
2249
2250 return;
2251}
2252
d3ce09f5
SS
2253/* Parses a command described by string CMD into an agent expression
2254 bytecode suitable for evaluation by the bytecode interpreter.
2255 Return NULL if there was any error during parsing. */
2256
2257static struct agent_expr *
2258parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2259{
2260 struct cleanup *old_cleanups = 0;
2261 struct expression *expr, **argvec;
2262 struct agent_expr *aexpr = NULL;
d3ce09f5 2263 volatile struct gdb_exception ex;
bbc13ae3
KS
2264 const char *cmdrest;
2265 const char *format_start, *format_end;
d3ce09f5
SS
2266 struct format_piece *fpieces;
2267 int nargs;
2268 struct gdbarch *gdbarch = get_current_arch ();
2269
2270 if (!cmd)
2271 return NULL;
2272
2273 cmdrest = cmd;
2274
2275 if (*cmdrest == ',')
2276 ++cmdrest;
bbc13ae3 2277 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2278
2279 if (*cmdrest++ != '"')
2280 error (_("No format string following the location"));
2281
2282 format_start = cmdrest;
2283
2284 fpieces = parse_format_string (&cmdrest);
2285
2286 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2287
2288 format_end = cmdrest;
2289
2290 if (*cmdrest++ != '"')
2291 error (_("Bad format string, non-terminated '\"'."));
2292
bbc13ae3 2293 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2294
2295 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2296 error (_("Invalid argument syntax"));
2297
2298 if (*cmdrest == ',')
2299 cmdrest++;
bbc13ae3 2300 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2301
2302 /* For each argument, make an expression. */
2303
2304 argvec = (struct expression **) alloca (strlen (cmd)
2305 * sizeof (struct expression *));
2306
2307 nargs = 0;
2308 while (*cmdrest != '\0')
2309 {
bbc13ae3 2310 const char *cmd1;
d3ce09f5
SS
2311
2312 cmd1 = cmdrest;
2313 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2314 argvec[nargs++] = expr;
2315 cmdrest = cmd1;
2316 if (*cmdrest == ',')
2317 ++cmdrest;
2318 }
2319
2320 /* We don't want to stop processing, so catch any errors
2321 that may show up. */
2322 TRY_CATCH (ex, RETURN_MASK_ERROR)
2323 {
2324 aexpr = gen_printf (scope, gdbarch, 0, 0,
2325 format_start, format_end - format_start,
2326 fpieces, nargs, argvec);
2327 }
2328
752eb8b4
TT
2329 do_cleanups (old_cleanups);
2330
d3ce09f5
SS
2331 if (ex.reason < 0)
2332 {
2333 /* If we got here, it means the command could not be parsed to a valid
2334 bytecode expression and thus can't be evaluated on the target's side.
2335 It's no use iterating through the other commands. */
2336 return NULL;
2337 }
2338
d3ce09f5
SS
2339 /* We have a valid agent expression, return it. */
2340 return aexpr;
2341}
2342
2343/* Based on location BL, create a list of breakpoint commands to be
2344 passed on to the target. If we have duplicated locations with
2345 different commands, we will add any such to the list. */
2346
2347static void
2348build_target_command_list (struct bp_location *bl)
2349{
2350 struct bp_location **locp = NULL, **loc2p;
2351 int null_command_or_parse_error = 0;
2352 int modified = bl->needs_update;
2353 struct bp_location *loc;
2354
8b4f3082
PA
2355 /* Release commands left over from a previous insert. */
2356 VEC_free (agent_expr_p, bl->target_info.tcommands);
2357
41fac0cf 2358 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2359 return;
2360
41fac0cf
PA
2361 /* For now, limit to agent-style dprintf breakpoints. */
2362 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2363 return;
2364
41fac0cf
PA
2365 /* For now, if we have any duplicate location that isn't a dprintf,
2366 don't install the target-side commands, as that would make the
2367 breakpoint not be reported to the core, and we'd lose
2368 control. */
2369 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2370 {
2371 loc = (*loc2p);
2372 if (is_breakpoint (loc->owner)
2373 && loc->pspace->num == bl->pspace->num
2374 && loc->owner->type != bp_dprintf)
2375 return;
2376 }
2377
d3ce09f5
SS
2378 /* Do a first pass to check for locations with no assigned
2379 conditions or conditions that fail to parse to a valid agent expression
2380 bytecode. If any of these happen, then it's no use to send conditions
2381 to the target since this location will always trigger and generate a
2382 response back to GDB. */
2383 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2384 {
2385 loc = (*loc2p);
2386 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2387 {
2388 if (modified)
2389 {
2390 struct agent_expr *aexpr;
2391
2392 /* Re-parse the commands since something changed. In that
2393 case we already freed the command bytecodes (see
2394 force_breakpoint_reinsertion). We just
2395 need to parse the command to bytecodes again. */
2396 aexpr = parse_cmd_to_aexpr (bl->address,
2397 loc->owner->extra_string);
2398 loc->cmd_bytecode = aexpr;
2399
2400 if (!aexpr)
2401 continue;
2402 }
2403
2404 /* If we have a NULL bytecode expression, it means something
2405 went wrong or we have a null command expression. */
2406 if (!loc->cmd_bytecode)
2407 {
2408 null_command_or_parse_error = 1;
2409 break;
2410 }
2411 }
2412 }
2413
2414 /* If anything failed, then we're not doing target-side commands,
2415 and so clean up. */
2416 if (null_command_or_parse_error)
2417 {
2418 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2419 {
2420 loc = (*loc2p);
2421 if (is_breakpoint (loc->owner)
2422 && loc->pspace->num == bl->pspace->num)
2423 {
2424 /* Only go as far as the first NULL bytecode is
2425 located. */
40fb6c5e 2426 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2427 return;
2428
40fb6c5e
HZ
2429 free_agent_expr (loc->cmd_bytecode);
2430 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2431 }
2432 }
2433 }
2434
2435 /* No NULL commands or failed bytecode generation. Build a command list
2436 for this location's address. */
2437 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2438 {
2439 loc = (*loc2p);
2440 if (loc->owner->extra_string
2441 && is_breakpoint (loc->owner)
2442 && loc->pspace->num == bl->pspace->num
2443 && loc->owner->enable_state == bp_enabled
2444 && loc->enabled)
2445 /* Add the command to the vector. This will be used later
2446 to send the commands to the target. */
2447 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2448 loc->cmd_bytecode);
2449 }
2450
2451 bl->target_info.persist = 0;
2452 /* Maybe flag this location as persistent. */
2453 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2454 bl->target_info.persist = 1;
2455}
2456
35df4500
TJB
2457/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2458 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2459 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2460 Returns 0 for success, 1 if the bp_location type is not supported or
2461 -1 for failure.
879bfdc2 2462
4a64f543
MS
2463 NOTE drow/2003-09-09: This routine could be broken down to an
2464 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2465static int
35df4500 2466insert_bp_location (struct bp_location *bl,
26bb91f3 2467 struct ui_file *tmp_error_stream,
3fbb6ffa 2468 int *disabled_breaks,
dd61ec5c
MW
2469 int *hw_breakpoint_error,
2470 int *hw_bp_error_explained_already)
879bfdc2 2471{
0000e5cc
PA
2472 enum errors bp_err = GDB_NO_ERROR;
2473 const char *bp_err_message = NULL;
c90a6fb7 2474 volatile struct gdb_exception e;
879bfdc2 2475
b775012e 2476 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2477 return 0;
2478
35c63cd8
JB
2479 /* Note we don't initialize bl->target_info, as that wipes out
2480 the breakpoint location's shadow_contents if the breakpoint
2481 is still inserted at that location. This in turn breaks
2482 target_read_memory which depends on these buffers when
2483 a memory read is requested at the breakpoint location:
2484 Once the target_info has been wiped, we fail to see that
2485 we have a breakpoint inserted at that address and thus
2486 read the breakpoint instead of returning the data saved in
2487 the breakpoint location's shadow contents. */
35df4500
TJB
2488 bl->target_info.placed_address = bl->address;
2489 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2490 bl->target_info.length = bl->length;
8181d85f 2491
b775012e
LM
2492 /* When working with target-side conditions, we must pass all the conditions
2493 for the same breakpoint address down to the target since GDB will not
2494 insert those locations. With a list of breakpoint conditions, the target
2495 can decide when to stop and notify GDB. */
2496
2497 if (is_breakpoint (bl->owner))
2498 {
2499 build_target_condition_list (bl);
d3ce09f5
SS
2500 build_target_command_list (bl);
2501 /* Reset the modification marker. */
b775012e
LM
2502 bl->needs_update = 0;
2503 }
2504
35df4500
TJB
2505 if (bl->loc_type == bp_loc_software_breakpoint
2506 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2507 {
35df4500 2508 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2509 {
2510 /* If the explicitly specified breakpoint type
2511 is not hardware breakpoint, check the memory map to see
2512 if the breakpoint address is in read only memory or not.
4a64f543 2513
765dc015
VP
2514 Two important cases are:
2515 - location type is not hardware breakpoint, memory
2516 is readonly. We change the type of the location to
2517 hardware breakpoint.
4a64f543
MS
2518 - location type is hardware breakpoint, memory is
2519 read-write. This means we've previously made the
2520 location hardware one, but then the memory map changed,
2521 so we undo.
765dc015 2522
4a64f543
MS
2523 When breakpoints are removed, remove_breakpoints will use
2524 location types we've just set here, the only possible
2525 problem is that memory map has changed during running
2526 program, but it's not going to work anyway with current
2527 gdb. */
765dc015 2528 struct mem_region *mr
35df4500 2529 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2530
2531 if (mr)
2532 {
2533 if (automatic_hardware_breakpoints)
2534 {
765dc015
VP
2535 enum bp_loc_type new_type;
2536
2537 if (mr->attrib.mode != MEM_RW)
2538 new_type = bp_loc_hardware_breakpoint;
2539 else
2540 new_type = bp_loc_software_breakpoint;
2541
35df4500 2542 if (new_type != bl->loc_type)
765dc015
VP
2543 {
2544 static int said = 0;
cc59ec59 2545
35df4500 2546 bl->loc_type = new_type;
765dc015
VP
2547 if (!said)
2548 {
3e43a32a
MS
2549 fprintf_filtered (gdb_stdout,
2550 _("Note: automatically using "
2551 "hardware breakpoints for "
2552 "read-only addresses.\n"));
765dc015
VP
2553 said = 1;
2554 }
2555 }
2556 }
35df4500 2557 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2558 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2559 warning (_("cannot set software breakpoint "
2560 "at readonly address %s"),
35df4500 2561 paddress (bl->gdbarch, bl->address));
765dc015
VP
2562 }
2563 }
2564
879bfdc2
DJ
2565 /* First check to see if we have to handle an overlay. */
2566 if (overlay_debugging == ovly_off
35df4500
TJB
2567 || bl->section == NULL
2568 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2569 {
2570 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2571 TRY_CATCH (e, RETURN_MASK_ALL)
2572 {
0000e5cc
PA
2573 int val;
2574
dd61ec5c 2575 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2576 if (val)
2577 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2578 }
2579 if (e.reason < 0)
2580 {
0000e5cc
PA
2581 bp_err = e.error;
2582 bp_err_message = e.message;
dd61ec5c 2583 }
879bfdc2
DJ
2584 }
2585 else
2586 {
4a64f543 2587 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2588 Shall we set a breakpoint at the LMA? */
2589 if (!overlay_events_enabled)
2590 {
2591 /* Yes -- overlay event support is not active,
2592 so we must try to set a breakpoint at the LMA.
2593 This will not work for a hardware breakpoint. */
35df4500 2594 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2595 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2596 bl->owner->number);
879bfdc2
DJ
2597 else
2598 {
35df4500
TJB
2599 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2600 bl->section);
879bfdc2 2601 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2602 bl->overlay_target_info = bl->target_info;
2603 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2604
2605 /* No overlay handling: just set the breakpoint. */
2606 TRY_CATCH (e, RETURN_MASK_ALL)
2607 {
2608 int val;
2609
2610 val = target_insert_breakpoint (bl->gdbarch,
2611 &bl->overlay_target_info);
2612 if (val)
2613 bp_err = GENERIC_ERROR;
2614 }
2615 if (e.reason < 0)
2616 {
2617 bp_err = e.error;
2618 bp_err_message = e.message;
2619 }
2620
2621 if (bp_err != GDB_NO_ERROR)
99361f52 2622 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2623 "Overlay breakpoint %d "
2624 "failed: in ROM?\n",
35df4500 2625 bl->owner->number);
879bfdc2
DJ
2626 }
2627 }
2628 /* Shall we set a breakpoint at the VMA? */
35df4500 2629 if (section_is_mapped (bl->section))
879bfdc2
DJ
2630 {
2631 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2632 TRY_CATCH (e, RETURN_MASK_ALL)
2633 {
0000e5cc
PA
2634 int val;
2635
dd61ec5c 2636 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2637 if (val)
2638 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2639 }
2640 if (e.reason < 0)
2641 {
0000e5cc
PA
2642 bp_err = e.error;
2643 bp_err_message = e.message;
dd61ec5c 2644 }
879bfdc2
DJ
2645 }
2646 else
2647 {
2648 /* No. This breakpoint will not be inserted.
2649 No error, but do not mark the bp as 'inserted'. */
2650 return 0;
2651 }
2652 }
2653
0000e5cc 2654 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2655 {
2656 /* Can't set the breakpoint. */
0000e5cc
PA
2657
2658 /* In some cases, we might not be able to insert a
2659 breakpoint in a shared library that has already been
2660 removed, but we have not yet processed the shlib unload
2661 event. Unfortunately, some targets that implement
076855f9
PA
2662 breakpoint insertion themselves can't tell why the
2663 breakpoint insertion failed (e.g., the remote target
2664 doesn't define error codes), so we must treat generic
2665 errors as memory errors. */
0000e5cc 2666 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2667 && bl->loc_type == bp_loc_software_breakpoint
08351840 2668 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2669 || shared_objfile_contains_address_p (bl->pspace,
2670 bl->address)))
879bfdc2 2671 {
4a64f543 2672 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2673 bl->shlib_disabled = 1;
8d3788bd 2674 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2675 if (!*disabled_breaks)
2676 {
2677 fprintf_unfiltered (tmp_error_stream,
2678 "Cannot insert breakpoint %d.\n",
2679 bl->owner->number);
2680 fprintf_unfiltered (tmp_error_stream,
2681 "Temporarily disabling shared "
2682 "library breakpoints:\n");
2683 }
2684 *disabled_breaks = 1;
879bfdc2 2685 fprintf_unfiltered (tmp_error_stream,
35df4500 2686 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2687 return 0;
879bfdc2
DJ
2688 }
2689 else
879bfdc2 2690 {
35df4500 2691 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2692 {
0000e5cc
PA
2693 *hw_breakpoint_error = 1;
2694 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2695 fprintf_unfiltered (tmp_error_stream,
2696 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2697 bl->owner->number, bp_err_message ? ":" : ".\n");
2698 if (bp_err_message != NULL)
2699 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2700 }
2701 else
2702 {
0000e5cc
PA
2703 if (bp_err_message == NULL)
2704 {
2705 char *message
2706 = memory_error_message (TARGET_XFER_E_IO,
2707 bl->gdbarch, bl->address);
2708 struct cleanup *old_chain = make_cleanup (xfree, message);
2709
2710 fprintf_unfiltered (tmp_error_stream,
2711 "Cannot insert breakpoint %d.\n"
2712 "%s\n",
2713 bl->owner->number, message);
2714 do_cleanups (old_chain);
2715 }
2716 else
2717 {
2718 fprintf_unfiltered (tmp_error_stream,
2719 "Cannot insert breakpoint %d: %s\n",
2720 bl->owner->number,
2721 bp_err_message);
2722 }
879bfdc2 2723 }
0000e5cc 2724 return 1;
879bfdc2
DJ
2725
2726 }
2727 }
2728 else
35df4500 2729 bl->inserted = 1;
879bfdc2 2730
0000e5cc 2731 return 0;
879bfdc2
DJ
2732 }
2733
35df4500 2734 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2735 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2736 watchpoints. It's not clear that it's necessary... */
35df4500 2737 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2738 {
0000e5cc
PA
2739 int val;
2740
77b06cd7
TJB
2741 gdb_assert (bl->owner->ops != NULL
2742 && bl->owner->ops->insert_location != NULL);
2743
2744 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2745
2746 /* If trying to set a read-watchpoint, and it turns out it's not
2747 supported, try emulating one with an access watchpoint. */
35df4500 2748 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2749 {
2750 struct bp_location *loc, **loc_temp;
2751
2752 /* But don't try to insert it, if there's already another
2753 hw_access location that would be considered a duplicate
2754 of this one. */
2755 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2756 if (loc != bl
85d721b8 2757 && loc->watchpoint_type == hw_access
35df4500 2758 && watchpoint_locations_match (bl, loc))
85d721b8 2759 {
35df4500
TJB
2760 bl->duplicate = 1;
2761 bl->inserted = 1;
2762 bl->target_info = loc->target_info;
2763 bl->watchpoint_type = hw_access;
85d721b8
PA
2764 val = 0;
2765 break;
2766 }
2767
2768 if (val == 1)
2769 {
77b06cd7
TJB
2770 bl->watchpoint_type = hw_access;
2771 val = bl->owner->ops->insert_location (bl);
2772
2773 if (val)
2774 /* Back to the original value. */
2775 bl->watchpoint_type = hw_read;
85d721b8
PA
2776 }
2777 }
2778
35df4500 2779 bl->inserted = (val == 0);
879bfdc2
DJ
2780 }
2781
35df4500 2782 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2783 {
0000e5cc
PA
2784 int val;
2785
77b06cd7
TJB
2786 gdb_assert (bl->owner->ops != NULL
2787 && bl->owner->ops->insert_location != NULL);
2788
2789 val = bl->owner->ops->insert_location (bl);
2790 if (val)
2791 {
2792 bl->owner->enable_state = bp_disabled;
2793
2794 if (val == 1)
2795 warning (_("\
2796Error inserting catchpoint %d: Your system does not support this type\n\
2797of catchpoint."), bl->owner->number);
2798 else
2799 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2800 }
2801
2802 bl->inserted = (val == 0);
1640b821
DJ
2803
2804 /* We've already printed an error message if there was a problem
2805 inserting this catchpoint, and we've disabled the catchpoint,
2806 so just return success. */
2807 return 0;
879bfdc2
DJ
2808 }
2809
2810 return 0;
2811}
2812
6c95b8df
PA
2813/* This function is called when program space PSPACE is about to be
2814 deleted. It takes care of updating breakpoints to not reference
2815 PSPACE anymore. */
2816
2817void
2818breakpoint_program_space_exit (struct program_space *pspace)
2819{
2820 struct breakpoint *b, *b_temp;
876fa593 2821 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2822
2823 /* Remove any breakpoint that was set through this program space. */
2824 ALL_BREAKPOINTS_SAFE (b, b_temp)
2825 {
2826 if (b->pspace == pspace)
2827 delete_breakpoint (b);
2828 }
2829
2830 /* Breakpoints set through other program spaces could have locations
2831 bound to PSPACE as well. Remove those. */
876fa593 2832 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2833 {
2834 struct bp_location *tmp;
2835
2836 if (loc->pspace == pspace)
2837 {
2bdf28a0 2838 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2839 if (loc->owner->loc == loc)
2840 loc->owner->loc = loc->next;
2841 else
2842 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2843 if (tmp->next == loc)
2844 {
2845 tmp->next = loc->next;
2846 break;
2847 }
2848 }
2849 }
2850
2851 /* Now update the global location list to permanently delete the
2852 removed locations above. */
2853 update_global_location_list (0);
2854}
2855
74960c60
VP
2856/* Make sure all breakpoints are inserted in inferior.
2857 Throws exception on any error.
2858 A breakpoint that is already inserted won't be inserted
2859 again, so calling this function twice is safe. */
2860void
2861insert_breakpoints (void)
2862{
2863 struct breakpoint *bpt;
2864
2865 ALL_BREAKPOINTS (bpt)
2866 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2867 {
2868 struct watchpoint *w = (struct watchpoint *) bpt;
2869
2870 update_watchpoint (w, 0 /* don't reparse. */);
2871 }
74960c60 2872
b60e7edf 2873 update_global_location_list (1);
74960c60 2874
c35b1492
PA
2875 /* update_global_location_list does not insert breakpoints when
2876 always_inserted_mode is not enabled. Explicitly insert them
2877 now. */
2878 if (!breakpoints_always_inserted_mode ())
74960c60
VP
2879 insert_breakpoint_locations ();
2880}
2881
20388dd6
YQ
2882/* Invoke CALLBACK for each of bp_location. */
2883
2884void
2885iterate_over_bp_locations (walk_bp_location_callback callback)
2886{
2887 struct bp_location *loc, **loc_tmp;
2888
2889 ALL_BP_LOCATIONS (loc, loc_tmp)
2890 {
2891 callback (loc, NULL);
2892 }
2893}
2894
b775012e
LM
2895/* This is used when we need to synch breakpoint conditions between GDB and the
2896 target. It is the case with deleting and disabling of breakpoints when using
2897 always-inserted mode. */
2898
2899static void
2900update_inserted_breakpoint_locations (void)
2901{
2902 struct bp_location *bl, **blp_tmp;
2903 int error_flag = 0;
2904 int val = 0;
2905 int disabled_breaks = 0;
2906 int hw_breakpoint_error = 0;
dd61ec5c 2907 int hw_bp_details_reported = 0;
b775012e
LM
2908
2909 struct ui_file *tmp_error_stream = mem_fileopen ();
2910 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2911
2912 /* Explicitly mark the warning -- this will only be printed if
2913 there was an error. */
2914 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2915
2916 save_current_space_and_thread ();
2917
2918 ALL_BP_LOCATIONS (bl, blp_tmp)
2919 {
2920 /* We only want to update software breakpoints and hardware
2921 breakpoints. */
2922 if (!is_breakpoint (bl->owner))
2923 continue;
2924
2925 /* We only want to update locations that are already inserted
2926 and need updating. This is to avoid unwanted insertion during
2927 deletion of breakpoints. */
2928 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2929 continue;
2930
2931 switch_to_program_space_and_thread (bl->pspace);
2932
2933 /* For targets that support global breakpoints, there's no need
2934 to select an inferior to insert breakpoint to. In fact, even
2935 if we aren't attached to any process yet, we should still
2936 insert breakpoints. */
f5656ead 2937 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2938 && ptid_equal (inferior_ptid, null_ptid))
2939 continue;
2940
2941 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 2942 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2943 if (val)
2944 error_flag = val;
2945 }
2946
2947 if (error_flag)
2948 {
2949 target_terminal_ours_for_output ();
2950 error_stream (tmp_error_stream);
2951 }
2952
2953 do_cleanups (cleanups);
2954}
2955
c30eee59 2956/* Used when starting or continuing the program. */
c906108c 2957
74960c60
VP
2958static void
2959insert_breakpoint_locations (void)
c906108c 2960{
a5606eee 2961 struct breakpoint *bpt;
35df4500 2962 struct bp_location *bl, **blp_tmp;
eacd795a 2963 int error_flag = 0;
c906108c 2964 int val = 0;
3fbb6ffa 2965 int disabled_breaks = 0;
81d0cc19 2966 int hw_breakpoint_error = 0;
dd61ec5c 2967 int hw_bp_error_explained_already = 0;
c906108c 2968
81d0cc19 2969 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2970 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2971
81d0cc19
GS
2972 /* Explicitly mark the warning -- this will only be printed if
2973 there was an error. */
2974 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2975
2976 save_current_space_and_thread ();
2977
35df4500 2978 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2979 {
b775012e 2980 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2981 continue;
2982
4a64f543
MS
2983 /* There is no point inserting thread-specific breakpoints if
2984 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2985 has BL->OWNER always non-NULL. */
35df4500
TJB
2986 if (bl->owner->thread != -1
2987 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2988 continue;
2989
35df4500 2990 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2991
2992 /* For targets that support global breakpoints, there's no need
2993 to select an inferior to insert breakpoint to. In fact, even
2994 if we aren't attached to any process yet, we should still
2995 insert breakpoints. */
f5656ead 2996 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
2997 && ptid_equal (inferior_ptid, null_ptid))
2998 continue;
2999
3fbb6ffa 3000 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3001 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3002 if (val)
eacd795a 3003 error_flag = val;
879bfdc2 3004 }
c906108c 3005
4a64f543
MS
3006 /* If we failed to insert all locations of a watchpoint, remove
3007 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3008 ALL_BREAKPOINTS (bpt)
3009 {
3010 int some_failed = 0;
3011 struct bp_location *loc;
3012
3013 if (!is_hardware_watchpoint (bpt))
3014 continue;
3015
d6b74ac4 3016 if (!breakpoint_enabled (bpt))
a5606eee 3017 continue;
74960c60
VP
3018
3019 if (bpt->disposition == disp_del_at_next_stop)
3020 continue;
a5606eee
VP
3021
3022 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3023 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3024 {
3025 some_failed = 1;
3026 break;
3027 }
3028 if (some_failed)
3029 {
3030 for (loc = bpt->loc; loc; loc = loc->next)
3031 if (loc->inserted)
3032 remove_breakpoint (loc, mark_uninserted);
3033
3034 hw_breakpoint_error = 1;
3035 fprintf_unfiltered (tmp_error_stream,
3036 "Could not insert hardware watchpoint %d.\n",
3037 bpt->number);
eacd795a 3038 error_flag = -1;
a5606eee
VP
3039 }
3040 }
3041
eacd795a 3042 if (error_flag)
81d0cc19
GS
3043 {
3044 /* If a hardware breakpoint or watchpoint was inserted, add a
3045 message about possibly exhausted resources. */
dd61ec5c 3046 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3047 {
c6510018
MS
3048 fprintf_unfiltered (tmp_error_stream,
3049 "Could not insert hardware breakpoints:\n\
3050You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3051 }
81d0cc19
GS
3052 target_terminal_ours_for_output ();
3053 error_stream (tmp_error_stream);
3054 }
f7545552
TT
3055
3056 do_cleanups (cleanups);
c906108c
SS
3057}
3058
c30eee59
TJB
3059/* Used when the program stops.
3060 Returns zero if successful, or non-zero if there was a problem
3061 removing a breakpoint location. */
3062
c906108c 3063int
fba45db2 3064remove_breakpoints (void)
c906108c 3065{
35df4500 3066 struct bp_location *bl, **blp_tmp;
3a1bae8e 3067 int val = 0;
c906108c 3068
35df4500 3069 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3070 {
1e4d1764 3071 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3072 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3073 }
3a1bae8e 3074 return val;
c906108c
SS
3075}
3076
49fa26b0
PA
3077/* When a thread exits, remove breakpoints that are related to
3078 that thread. */
3079
3080static void
3081remove_threaded_breakpoints (struct thread_info *tp, int silent)
3082{
3083 struct breakpoint *b, *b_tmp;
3084
3085 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3086 {
96181529 3087 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3088 {
3089 b->disposition = disp_del_at_next_stop;
3090
3091 printf_filtered (_("\
46ecd527 3092Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3093 b->number, tp->num);
3094
3095 /* Hide it from the user. */
3096 b->number = 0;
3097 }
3098 }
3099}
3100
6c95b8df
PA
3101/* Remove breakpoints of process PID. */
3102
3103int
3104remove_breakpoints_pid (int pid)
3105{
35df4500 3106 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3107 int val;
3108 struct inferior *inf = find_inferior_pid (pid);
3109
35df4500 3110 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3111 {
35df4500 3112 if (bl->pspace != inf->pspace)
6c95b8df
PA
3113 continue;
3114
d3ce09f5
SS
3115 if (bl->owner->type == bp_dprintf)
3116 continue;
3117
35df4500 3118 if (bl->inserted)
6c95b8df 3119 {
35df4500 3120 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3121 if (val != 0)
3122 return val;
3123 }
3124 }
3125 return 0;
3126}
3127
c906108c 3128int
fba45db2 3129reattach_breakpoints (int pid)
c906108c 3130{
6c95b8df 3131 struct cleanup *old_chain;
35df4500 3132 struct bp_location *bl, **blp_tmp;
c906108c 3133 int val;
86b887df 3134 struct ui_file *tmp_error_stream;
dd61ec5c 3135 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3136 struct inferior *inf;
3137 struct thread_info *tp;
3138
3139 tp = any_live_thread_of_process (pid);
3140 if (tp == NULL)
3141 return 1;
3142
3143 inf = find_inferior_pid (pid);
3144 old_chain = save_inferior_ptid ();
3145
3146 inferior_ptid = tp->ptid;
a4954f26 3147
86b887df 3148 tmp_error_stream = mem_fileopen ();
a4954f26 3149 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3150
35df4500 3151 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3152 {
35df4500 3153 if (bl->pspace != inf->pspace)
6c95b8df
PA
3154 continue;
3155
35df4500 3156 if (bl->inserted)
c5aa993b 3157 {
35df4500 3158 bl->inserted = 0;
dd61ec5c 3159 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3160 if (val != 0)
3161 {
ce696e05 3162 do_cleanups (old_chain);
c5aa993b
JM
3163 return val;
3164 }
3165 }
3166 }
ce696e05 3167 do_cleanups (old_chain);
c906108c
SS
3168 return 0;
3169}
3170
e58b0e63
PA
3171static int internal_breakpoint_number = -1;
3172
84f4c1fe
PM
3173/* Set the breakpoint number of B, depending on the value of INTERNAL.
3174 If INTERNAL is non-zero, the breakpoint number will be populated
3175 from internal_breakpoint_number and that variable decremented.
e5dd4106 3176 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3177 breakpoint_count and that value incremented. Internal breakpoints
3178 do not set the internal var bpnum. */
3179static void
3180set_breakpoint_number (int internal, struct breakpoint *b)
3181{
3182 if (internal)
3183 b->number = internal_breakpoint_number--;
3184 else
3185 {
3186 set_breakpoint_count (breakpoint_count + 1);
3187 b->number = breakpoint_count;
3188 }
3189}
3190
e62c965a 3191static struct breakpoint *
a6d9a66e 3192create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3193 CORE_ADDR address, enum bptype type,
c0a91b2b 3194 const struct breakpoint_ops *ops)
e62c965a 3195{
e62c965a
PP
3196 struct symtab_and_line sal;
3197 struct breakpoint *b;
3198
4a64f543 3199 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3200
3201 sal.pc = address;
3202 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3203 sal.pspace = current_program_space;
e62c965a 3204
06edf0c0 3205 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3206 b->number = internal_breakpoint_number--;
3207 b->disposition = disp_donttouch;
3208
3209 return b;
3210}
3211
17450429
PP
3212static const char *const longjmp_names[] =
3213 {
3214 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3215 };
3216#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3217
3218/* Per-objfile data private to breakpoint.c. */
3219struct breakpoint_objfile_data
3220{
3221 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3222 struct bound_minimal_symbol overlay_msym;
17450429
PP
3223
3224 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3225 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3226
28106bc2
SDJ
3227 /* True if we have looked for longjmp probes. */
3228 int longjmp_searched;
3229
3230 /* SystemTap probe points for longjmp (if any). */
3231 VEC (probe_p) *longjmp_probes;
3232
17450429 3233 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3234 struct bound_minimal_symbol terminate_msym;
17450429
PP
3235
3236 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3237 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3238
3239 /* True if we have looked for exception probes. */
3240 int exception_searched;
3241
3242 /* SystemTap probe points for unwinding (if any). */
3243 VEC (probe_p) *exception_probes;
17450429
PP
3244};
3245
3246static const struct objfile_data *breakpoint_objfile_key;
3247
3248/* Minimal symbol not found sentinel. */
3249static struct minimal_symbol msym_not_found;
3250
3251/* Returns TRUE if MSYM point to the "not found" sentinel. */
3252
3253static int
3254msym_not_found_p (const struct minimal_symbol *msym)
3255{
3256 return msym == &msym_not_found;
3257}
3258
3259/* Return per-objfile data needed by breakpoint.c.
3260 Allocate the data if necessary. */
3261
3262static struct breakpoint_objfile_data *
3263get_breakpoint_objfile_data (struct objfile *objfile)
3264{
3265 struct breakpoint_objfile_data *bp_objfile_data;
3266
3267 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3268 if (bp_objfile_data == NULL)
3269 {
3270 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3271 sizeof (*bp_objfile_data));
3272
3273 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3274 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3275 }
3276 return bp_objfile_data;
3277}
3278
28106bc2
SDJ
3279static void
3280free_breakpoint_probes (struct objfile *obj, void *data)
3281{
3282 struct breakpoint_objfile_data *bp_objfile_data = data;
3283
3284 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3285 VEC_free (probe_p, bp_objfile_data->exception_probes);
3286}
3287
e62c965a 3288static void
af02033e 3289create_overlay_event_breakpoint (void)
e62c965a 3290{
69de3c6a 3291 struct objfile *objfile;
af02033e 3292 const char *const func_name = "_ovly_debug_event";
e62c965a 3293
69de3c6a
PP
3294 ALL_OBJFILES (objfile)
3295 {
3296 struct breakpoint *b;
17450429
PP
3297 struct breakpoint_objfile_data *bp_objfile_data;
3298 CORE_ADDR addr;
69de3c6a 3299
17450429
PP
3300 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3301
3b7344d5 3302 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3303 continue;
3304
3b7344d5 3305 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3306 {
3b7344d5 3307 struct bound_minimal_symbol m;
17450429
PP
3308
3309 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3310 if (m.minsym == NULL)
17450429
PP
3311 {
3312 /* Avoid future lookups in this objfile. */
3b7344d5 3313 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3314 continue;
3315 }
3316 bp_objfile_data->overlay_msym = m;
3317 }
e62c965a 3318
77e371c0 3319 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3320 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3321 bp_overlay_event,
3322 &internal_breakpoint_ops);
69de3c6a 3323 b->addr_string = xstrdup (func_name);
e62c965a 3324
69de3c6a
PP
3325 if (overlay_debugging == ovly_auto)
3326 {
3327 b->enable_state = bp_enabled;
3328 overlay_events_enabled = 1;
3329 }
3330 else
3331 {
3332 b->enable_state = bp_disabled;
3333 overlay_events_enabled = 0;
3334 }
e62c965a
PP
3335 }
3336 update_global_location_list (1);
3337}
3338
0fd8e87f 3339static void
af02033e 3340create_longjmp_master_breakpoint (void)
0fd8e87f 3341{
6c95b8df 3342 struct program_space *pspace;
6c95b8df
PA
3343 struct cleanup *old_chain;
3344
3345 old_chain = save_current_program_space ();
0fd8e87f 3346
6c95b8df 3347 ALL_PSPACES (pspace)
af02033e
PP
3348 {
3349 struct objfile *objfile;
3350
3351 set_current_program_space (pspace);
3352
3353 ALL_OBJFILES (objfile)
0fd8e87f 3354 {
af02033e
PP
3355 int i;
3356 struct gdbarch *gdbarch;
17450429 3357 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3358
af02033e 3359 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3360
17450429
PP
3361 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3362
28106bc2
SDJ
3363 if (!bp_objfile_data->longjmp_searched)
3364 {
25f9533e
SDJ
3365 VEC (probe_p) *ret;
3366
3367 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3368 if (ret != NULL)
3369 {
3370 /* We are only interested in checking one element. */
3371 struct probe *p = VEC_index (probe_p, ret, 0);
3372
3373 if (!can_evaluate_probe_arguments (p))
3374 {
3375 /* We cannot use the probe interface here, because it does
3376 not know how to evaluate arguments. */
3377 VEC_free (probe_p, ret);
3378 ret = NULL;
3379 }
3380 }
3381 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3382 bp_objfile_data->longjmp_searched = 1;
3383 }
3384
3385 if (bp_objfile_data->longjmp_probes != NULL)
3386 {
3387 int i;
3388 struct probe *probe;
3389 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3390
3391 for (i = 0;
3392 VEC_iterate (probe_p,
3393 bp_objfile_data->longjmp_probes,
3394 i, probe);
3395 ++i)
3396 {
3397 struct breakpoint *b;
3398
729662a5
TT
3399 b = create_internal_breakpoint (gdbarch,
3400 get_probe_address (probe,
3401 objfile),
28106bc2
SDJ
3402 bp_longjmp_master,
3403 &internal_breakpoint_ops);
3404 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3405 b->enable_state = bp_disabled;
3406 }
3407
3408 continue;
3409 }
3410
0569175e
TSD
3411 if (!gdbarch_get_longjmp_target_p (gdbarch))
3412 continue;
3413
17450429 3414 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3415 {
3416 struct breakpoint *b;
af02033e 3417 const char *func_name;
17450429 3418 CORE_ADDR addr;
6c95b8df 3419
3b7344d5 3420 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3421 continue;
0fd8e87f 3422
17450429 3423 func_name = longjmp_names[i];
3b7344d5 3424 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3425 {
3b7344d5 3426 struct bound_minimal_symbol m;
17450429
PP
3427
3428 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3429 if (m.minsym == NULL)
17450429
PP
3430 {
3431 /* Prevent future lookups in this objfile. */
3b7344d5 3432 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3433 continue;
3434 }
3435 bp_objfile_data->longjmp_msym[i] = m;
3436 }
3437
77e371c0 3438 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3439 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3440 &internal_breakpoint_ops);
af02033e
PP
3441 b->addr_string = xstrdup (func_name);
3442 b->enable_state = bp_disabled;
3443 }
0fd8e87f 3444 }
af02033e 3445 }
0fd8e87f 3446 update_global_location_list (1);
6c95b8df
PA
3447
3448 do_cleanups (old_chain);
0fd8e87f
UW
3449}
3450
af02033e 3451/* Create a master std::terminate breakpoint. */
aa7d318d 3452static void
af02033e 3453create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3454{
3455 struct program_space *pspace;
aa7d318d 3456 struct cleanup *old_chain;
af02033e 3457 const char *const func_name = "std::terminate()";
aa7d318d
TT
3458
3459 old_chain = save_current_program_space ();
3460
3461 ALL_PSPACES (pspace)
17450429
PP
3462 {
3463 struct objfile *objfile;
3464 CORE_ADDR addr;
3465
3466 set_current_program_space (pspace);
3467
aa7d318d
TT
3468 ALL_OBJFILES (objfile)
3469 {
3470 struct breakpoint *b;
17450429 3471 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3472
17450429 3473 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3474
3b7344d5 3475 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3476 continue;
3477
3b7344d5 3478 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3479 {
3b7344d5 3480 struct bound_minimal_symbol m;
17450429
PP
3481
3482 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3483 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3484 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3485 {
3486 /* Prevent future lookups in this objfile. */
3b7344d5 3487 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3488 continue;
3489 }
3490 bp_objfile_data->terminate_msym = m;
3491 }
aa7d318d 3492
77e371c0 3493 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3494 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3495 bp_std_terminate_master,
3496 &internal_breakpoint_ops);
aa7d318d
TT
3497 b->addr_string = xstrdup (func_name);
3498 b->enable_state = bp_disabled;
3499 }
17450429
PP
3500 }
3501
aa7d318d
TT
3502 update_global_location_list (1);
3503
3504 do_cleanups (old_chain);
3505}
3506
186c406b
TT
3507/* Install a master breakpoint on the unwinder's debug hook. */
3508
70221824 3509static void
186c406b
TT
3510create_exception_master_breakpoint (void)
3511{
3512 struct objfile *objfile;
17450429 3513 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3514
3515 ALL_OBJFILES (objfile)
3516 {
17450429
PP
3517 struct breakpoint *b;
3518 struct gdbarch *gdbarch;
3519 struct breakpoint_objfile_data *bp_objfile_data;
3520 CORE_ADDR addr;
3521
3522 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3523
28106bc2
SDJ
3524 /* We prefer the SystemTap probe point if it exists. */
3525 if (!bp_objfile_data->exception_searched)
3526 {
25f9533e
SDJ
3527 VEC (probe_p) *ret;
3528
3529 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3530
3531 if (ret != NULL)
3532 {
3533 /* We are only interested in checking one element. */
3534 struct probe *p = VEC_index (probe_p, ret, 0);
3535
3536 if (!can_evaluate_probe_arguments (p))
3537 {
3538 /* We cannot use the probe interface here, because it does
3539 not know how to evaluate arguments. */
3540 VEC_free (probe_p, ret);
3541 ret = NULL;
3542 }
3543 }
3544 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3545 bp_objfile_data->exception_searched = 1;
3546 }
3547
3548 if (bp_objfile_data->exception_probes != NULL)
3549 {
3550 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3551 int i;
3552 struct probe *probe;
3553
3554 for (i = 0;
3555 VEC_iterate (probe_p,
3556 bp_objfile_data->exception_probes,
3557 i, probe);
3558 ++i)
3559 {
3560 struct breakpoint *b;
3561
729662a5
TT
3562 b = create_internal_breakpoint (gdbarch,
3563 get_probe_address (probe,
3564 objfile),
28106bc2
SDJ
3565 bp_exception_master,
3566 &internal_breakpoint_ops);
3567 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3568 b->enable_state = bp_disabled;
3569 }
3570
3571 continue;
3572 }
3573
3574 /* Otherwise, try the hook function. */
3575
3b7344d5 3576 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3577 continue;
3578
3579 gdbarch = get_objfile_arch (objfile);
186c406b 3580
3b7344d5 3581 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3582 {
3b7344d5 3583 struct bound_minimal_symbol debug_hook;
186c406b 3584
17450429 3585 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3586 if (debug_hook.minsym == NULL)
17450429 3587 {
3b7344d5 3588 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3589 continue;
3590 }
3591
3592 bp_objfile_data->exception_msym = debug_hook;
186c406b 3593 }
17450429 3594
77e371c0 3595 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3596 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3597 &current_target);
06edf0c0
PA
3598 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3599 &internal_breakpoint_ops);
17450429
PP
3600 b->addr_string = xstrdup (func_name);
3601 b->enable_state = bp_disabled;
186c406b
TT
3602 }
3603
3604 update_global_location_list (1);
3605}
3606
c906108c 3607void
fba45db2 3608update_breakpoints_after_exec (void)
c906108c 3609{
35df4500 3610 struct breakpoint *b, *b_tmp;
876fa593 3611 struct bp_location *bploc, **bplocp_tmp;
c906108c 3612
25b22b0a
PA
3613 /* We're about to delete breakpoints from GDB's lists. If the
3614 INSERTED flag is true, GDB will try to lift the breakpoints by
3615 writing the breakpoints' "shadow contents" back into memory. The
3616 "shadow contents" are NOT valid after an exec, so GDB should not
3617 do that. Instead, the target is responsible from marking
3618 breakpoints out as soon as it detects an exec. We don't do that
3619 here instead, because there may be other attempts to delete
3620 breakpoints after detecting an exec and before reaching here. */
876fa593 3621 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3622 if (bploc->pspace == current_program_space)
3623 gdb_assert (!bploc->inserted);
c906108c 3624
35df4500 3625 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3626 {
6c95b8df
PA
3627 if (b->pspace != current_program_space)
3628 continue;
3629
4a64f543 3630 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3631 if (b->type == bp_shlib_event)
3632 {
3633 delete_breakpoint (b);
3634 continue;
3635 }
c906108c 3636
4a64f543 3637 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3638 if (b->type == bp_jit_event)
3639 {
3640 delete_breakpoint (b);
3641 continue;
3642 }
3643
1900040c 3644 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3645 as must overlay event and longjmp master breakpoints. */
3646 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3647 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3648 || b->type == bp_exception_master)
c4093a6a
JM
3649 {
3650 delete_breakpoint (b);
3651 continue;
3652 }
3653
4a64f543 3654 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3655 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3656 {
3657 delete_breakpoint (b);
3658 continue;
3659 }
3660
611c83ae
PA
3661 /* Longjmp and longjmp-resume breakpoints are also meaningless
3662 after an exec. */
186c406b 3663 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3664 || b->type == bp_longjmp_call_dummy
186c406b 3665 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3666 {
3667 delete_breakpoint (b);
3668 continue;
3669 }
3670
ce78b96d
JB
3671 if (b->type == bp_catchpoint)
3672 {
3673 /* For now, none of the bp_catchpoint breakpoints need to
3674 do anything at this point. In the future, if some of
3675 the catchpoints need to something, we will need to add
3676 a new method, and call this method from here. */
3677 continue;
3678 }
3679
c5aa993b
JM
3680 /* bp_finish is a special case. The only way we ought to be able
3681 to see one of these when an exec() has happened, is if the user
3682 caught a vfork, and then said "finish". Ordinarily a finish just
3683 carries them to the call-site of the current callee, by setting
3684 a temporary bp there and resuming. But in this case, the finish
3685 will carry them entirely through the vfork & exec.
3686
3687 We don't want to allow a bp_finish to remain inserted now. But
3688 we can't safely delete it, 'cause finish_command has a handle to
3689 the bp on a bpstat, and will later want to delete it. There's a
3690 chance (and I've seen it happen) that if we delete the bp_finish
3691 here, that its storage will get reused by the time finish_command
3692 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3693 We really must allow finish_command to delete a bp_finish.
3694
e5dd4106 3695 In the absence of a general solution for the "how do we know
53a5351d
JM
3696 it's safe to delete something others may have handles to?"
3697 problem, what we'll do here is just uninsert the bp_finish, and
3698 let finish_command delete it.
3699
3700 (We know the bp_finish is "doomed" in the sense that it's
3701 momentary, and will be deleted as soon as finish_command sees
3702 the inferior stopped. So it doesn't matter that the bp's
3703 address is probably bogus in the new a.out, unlike e.g., the
3704 solib breakpoints.) */
c5aa993b 3705
c5aa993b
JM
3706 if (b->type == bp_finish)
3707 {
3708 continue;
3709 }
3710
3711 /* Without a symbolic address, we have little hope of the
3712 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3713 a.out. */
c5aa993b
JM
3714 if (b->addr_string == NULL)
3715 {
3716 delete_breakpoint (b);
3717 continue;
3718 }
c5aa993b 3719 }
1900040c 3720 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3721 create_overlay_event_breakpoint ();
3722 create_longjmp_master_breakpoint ();
3723 create_std_terminate_master_breakpoint ();
186c406b 3724 create_exception_master_breakpoint ();
c906108c
SS
3725}
3726
3727int
d80ee84f 3728detach_breakpoints (ptid_t ptid)
c906108c 3729{
35df4500 3730 struct bp_location *bl, **blp_tmp;
3a1bae8e 3731 int val = 0;
ce696e05 3732 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3733 struct inferior *inf = current_inferior ();
c5aa993b 3734
dfd4cc63 3735 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3736 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3737
6c95b8df 3738 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3739 inferior_ptid = ptid;
35df4500 3740 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3741 {
35df4500 3742 if (bl->pspace != inf->pspace)
6c95b8df
PA
3743 continue;
3744
bd9673a4
PW
3745 /* This function must physically remove breakpoints locations
3746 from the specified ptid, without modifying the breakpoint
3747 package's state. Locations of type bp_loc_other are only
3748 maintained at GDB side. So, there is no need to remove
3749 these bp_loc_other locations. Moreover, removing these
3750 would modify the breakpoint package's state. */
3751 if (bl->loc_type == bp_loc_other)
3752 continue;
3753
35df4500
TJB
3754 if (bl->inserted)
3755 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3756 }
d03285ec
UW
3757
3758 /* Detach single-step breakpoints as well. */
3759 detach_single_step_breakpoints ();
3760
ce696e05 3761 do_cleanups (old_chain);
3a1bae8e 3762 return val;
c906108c
SS
3763}
3764
35df4500 3765/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3766 Note that this is used to detach breakpoints from a child fork.
3767 When we get here, the child isn't in the inferior list, and neither
3768 do we have objects to represent its address space --- we should
35df4500 3769 *not* look at bl->pspace->aspace here. */
6c95b8df 3770
c906108c 3771static int
35df4500 3772remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3773{
3774 int val;
c5aa993b 3775
35df4500
TJB
3776 /* BL is never in moribund_locations by our callers. */
3777 gdb_assert (bl->owner != NULL);
2bdf28a0 3778
35df4500 3779 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3780 /* Permanent breakpoints cannot be inserted or removed. */
3781 return 0;
3782
74960c60
VP
3783 /* The type of none suggests that owner is actually deleted.
3784 This should not ever happen. */
35df4500 3785 gdb_assert (bl->owner->type != bp_none);
0bde7532 3786
35df4500
TJB
3787 if (bl->loc_type == bp_loc_software_breakpoint
3788 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3789 {
c02f5703
MS
3790 /* "Normal" instruction breakpoint: either the standard
3791 trap-instruction bp (bp_breakpoint), or a
3792 bp_hardware_breakpoint. */
3793
3794 /* First check to see if we have to handle an overlay. */
3795 if (overlay_debugging == ovly_off
35df4500
TJB
3796 || bl->section == NULL
3797 || !(section_is_overlay (bl->section)))
c02f5703
MS
3798 {
3799 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3800
3801 /* If we're trying to uninsert a memory breakpoint that we
3802 know is set in a dynamic object that is marked
3803 shlib_disabled, then either the dynamic object was
3804 removed with "remove-symbol-file" or with
3805 "nosharedlibrary". In the former case, we don't know
3806 whether another dynamic object might have loaded over the
3807 breakpoint's address -- the user might well let us know
3808 about it next with add-symbol-file (the whole point of
d03de421 3809 add-symbol-file is letting the user manually maintain a
08351840
PA
3810 list of dynamically loaded objects). If we have the
3811 breakpoint's shadow memory, that is, this is a software
3812 breakpoint managed by GDB, check whether the breakpoint
3813 is still inserted in memory, to avoid overwriting wrong
3814 code with stale saved shadow contents. Note that HW
3815 breakpoints don't have shadow memory, as they're
3816 implemented using a mechanism that is not dependent on
3817 being able to modify the target's memory, and as such
3818 they should always be removed. */
3819 if (bl->shlib_disabled
3820 && bl->target_info.shadow_len != 0
3821 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3822 val = 0;
3823 else
3824 val = bl->owner->ops->remove_location (bl);
c02f5703 3825 }
c906108c
SS
3826 else
3827 {
4a64f543 3828 /* This breakpoint is in an overlay section.
c02f5703
MS
3829 Did we set a breakpoint at the LMA? */
3830 if (!overlay_events_enabled)
3831 {
3832 /* Yes -- overlay event support is not active, so we
3833 should have set a breakpoint at the LMA. Remove it.
3834 */
c02f5703
MS
3835 /* Ignore any failures: if the LMA is in ROM, we will
3836 have already warned when we failed to insert it. */
35df4500
TJB
3837 if (bl->loc_type == bp_loc_hardware_breakpoint)
3838 target_remove_hw_breakpoint (bl->gdbarch,
3839 &bl->overlay_target_info);
c02f5703 3840 else
35df4500
TJB
3841 target_remove_breakpoint (bl->gdbarch,
3842 &bl->overlay_target_info);
c02f5703
MS
3843 }
3844 /* Did we set a breakpoint at the VMA?
3845 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3846 if (bl->inserted)
c906108c 3847 {
c02f5703
MS
3848 /* Yes -- remove it. Previously we did not bother to
3849 remove the breakpoint if the section had been
3850 unmapped, but let's not rely on that being safe. We
3851 don't know what the overlay manager might do. */
aa67235e
UW
3852
3853 /* However, we should remove *software* breakpoints only
3854 if the section is still mapped, or else we overwrite
3855 wrong code with the saved shadow contents. */
348d480f
PA
3856 if (bl->loc_type == bp_loc_hardware_breakpoint
3857 || section_is_mapped (bl->section))
3858 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3859 else
3860 val = 0;
c906108c 3861 }
c02f5703
MS
3862 else
3863 {
3864 /* No -- not inserted, so no need to remove. No error. */
3865 val = 0;
3866 }
c906108c 3867 }
879d1e6b 3868
08351840
PA
3869 /* In some cases, we might not be able to remove a breakpoint in
3870 a shared library that has already been removed, but we have
3871 not yet processed the shlib unload event. Similarly for an
3872 unloaded add-symbol-file object - the user might not yet have
3873 had the chance to remove-symbol-file it. shlib_disabled will
3874 be set if the library/object has already been removed, but
3875 the breakpoint hasn't been uninserted yet, e.g., after
3876 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3877 always-inserted mode. */
076855f9 3878 if (val
08351840
PA
3879 && (bl->loc_type == bp_loc_software_breakpoint
3880 && (bl->shlib_disabled
3881 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3882 || shared_objfile_contains_address_p (bl->pspace,
3883 bl->address))))
879d1e6b
UW
3884 val = 0;
3885
c906108c
SS
3886 if (val)
3887 return val;
35df4500 3888 bl->inserted = (is == mark_inserted);
c906108c 3889 }
35df4500 3890 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3891 {
77b06cd7
TJB
3892 gdb_assert (bl->owner->ops != NULL
3893 && bl->owner->ops->remove_location != NULL);
3894
35df4500 3895 bl->inserted = (is == mark_inserted);
77b06cd7 3896 bl->owner->ops->remove_location (bl);
2e70b7b9 3897
c906108c 3898 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3899 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3900 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3901 bl->owner->number);
c906108c 3902 }
35df4500
TJB
3903 else if (bl->owner->type == bp_catchpoint
3904 && breakpoint_enabled (bl->owner)
3905 && !bl->duplicate)
ce78b96d 3906 {
77b06cd7
TJB
3907 gdb_assert (bl->owner->ops != NULL
3908 && bl->owner->ops->remove_location != NULL);
ce78b96d 3909
77b06cd7 3910 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3911 if (val)
3912 return val;
77b06cd7 3913
35df4500 3914 bl->inserted = (is == mark_inserted);
ce78b96d 3915 }
c906108c
SS
3916
3917 return 0;
3918}
3919
6c95b8df 3920static int
35df4500 3921remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
3922{
3923 int ret;
3924 struct cleanup *old_chain;
3925
35df4500
TJB
3926 /* BL is never in moribund_locations by our callers. */
3927 gdb_assert (bl->owner != NULL);
2bdf28a0 3928
35df4500 3929 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
3930 /* Permanent breakpoints cannot be inserted or removed. */
3931 return 0;
3932
3933 /* The type of none suggests that owner is actually deleted.
3934 This should not ever happen. */
35df4500 3935 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
3936
3937 old_chain = save_current_space_and_thread ();
3938
35df4500 3939 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3940
35df4500 3941 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
3942
3943 do_cleanups (old_chain);
3944 return ret;
3945}
3946
c906108c
SS
3947/* Clear the "inserted" flag in all breakpoints. */
3948
25b22b0a 3949void
fba45db2 3950mark_breakpoints_out (void)
c906108c 3951{
35df4500 3952 struct bp_location *bl, **blp_tmp;
c906108c 3953
35df4500
TJB
3954 ALL_BP_LOCATIONS (bl, blp_tmp)
3955 if (bl->pspace == current_program_space)
3956 bl->inserted = 0;
c906108c
SS
3957}
3958
53a5351d
JM
3959/* Clear the "inserted" flag in all breakpoints and delete any
3960 breakpoints which should go away between runs of the program.
c906108c
SS
3961
3962 Plus other such housekeeping that has to be done for breakpoints
3963 between runs.
3964
53a5351d
JM
3965 Note: this function gets called at the end of a run (by
3966 generic_mourn_inferior) and when a run begins (by
4a64f543 3967 init_wait_for_inferior). */
c906108c
SS
3968
3969
3970
3971void
fba45db2 3972breakpoint_init_inferior (enum inf_context context)
c906108c 3973{
35df4500
TJB
3974 struct breakpoint *b, *b_tmp;
3975 struct bp_location *bl, **blp_tmp;
1c5cfe86 3976 int ix;
6c95b8df 3977 struct program_space *pspace = current_program_space;
c906108c 3978
50c71eaf
PA
3979 /* If breakpoint locations are shared across processes, then there's
3980 nothing to do. */
f5656ead 3981 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3982 return;
3983
35df4500 3984 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3985 {
35df4500
TJB
3986 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3987 if (bl->pspace == pspace
3988 && bl->owner->enable_state != bp_permanent)
3989 bl->inserted = 0;
6c95b8df 3990 }
075f6582 3991
35df4500 3992 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3993 {
6c95b8df
PA
3994 if (b->loc && b->loc->pspace != pspace)
3995 continue;
3996
c5aa993b
JM
3997 switch (b->type)
3998 {
3999 case bp_call_dummy:
e2e4d78b 4000 case bp_longjmp_call_dummy:
c906108c 4001
c5aa993b 4002 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4003 cause problems when the inferior is rerun, so we better get
4004 rid of it. */
4005
4006 case bp_watchpoint_scope:
4007
4008 /* Also get rid of scope breakpoints. */
4009
4010 case bp_shlib_event:
4011
4012 /* Also remove solib event breakpoints. Their addresses may
4013 have changed since the last time we ran the program.
4014 Actually we may now be debugging against different target;
4015 and so the solib backend that installed this breakpoint may
4016 not be used in by the target. E.g.,
4017
4018 (gdb) file prog-linux
4019 (gdb) run # native linux target
4020 ...
4021 (gdb) kill
4022 (gdb) file prog-win.exe
4023 (gdb) tar rem :9999 # remote Windows gdbserver.
4024 */
c906108c 4025
f59f708a
PA
4026 case bp_step_resume:
4027
4028 /* Also remove step-resume breakpoints. */
4029
c5aa993b
JM
4030 delete_breakpoint (b);
4031 break;
c906108c 4032
c5aa993b
JM
4033 case bp_watchpoint:
4034 case bp_hardware_watchpoint:
4035 case bp_read_watchpoint:
4036 case bp_access_watchpoint:
3a5c3e22
PA
4037 {
4038 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4039
3a5c3e22
PA
4040 /* Likewise for watchpoints on local expressions. */
4041 if (w->exp_valid_block != NULL)
4042 delete_breakpoint (b);
4043 else if (context == inf_starting)
4044 {
4045 /* Reset val field to force reread of starting value in
4046 insert_breakpoints. */
4047 if (w->val)
4048 value_free (w->val);
4049 w->val = NULL;
4050 w->val_valid = 0;
c860120c 4051 }
3a5c3e22 4052 }
c5aa993b
JM
4053 break;
4054 default:
c5aa993b
JM
4055 break;
4056 }
4057 }
1c5cfe86
PA
4058
4059 /* Get rid of the moribund locations. */
35df4500
TJB
4060 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4061 decref_bp_location (&bl);
1c5cfe86 4062 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4063}
4064
6c95b8df
PA
4065/* These functions concern about actual breakpoints inserted in the
4066 target --- to e.g. check if we need to do decr_pc adjustment or if
4067 we need to hop over the bkpt --- so we check for address space
4068 match, not program space. */
4069
c2c6d25f
JM
4070/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4071 exists at PC. It returns ordinary_breakpoint_here if it's an
4072 ordinary breakpoint, or permanent_breakpoint_here if it's a
4073 permanent breakpoint.
4074 - When continuing from a location with an ordinary breakpoint, we
4075 actually single step once before calling insert_breakpoints.
e5dd4106 4076 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4077 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4078 the target, to advance the PC past the breakpoint. */
c906108c 4079
c2c6d25f 4080enum breakpoint_here
6c95b8df 4081breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4082{
35df4500 4083 struct bp_location *bl, **blp_tmp;
c2c6d25f 4084 int any_breakpoint_here = 0;
c906108c 4085
35df4500 4086 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4087 {
35df4500
TJB
4088 if (bl->loc_type != bp_loc_software_breakpoint
4089 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4090 continue;
4091
f1310107 4092 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
4093 if ((breakpoint_enabled (bl->owner)
4094 || bl->owner->enable_state == bp_permanent)
f1310107 4095 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4096 {
4097 if (overlay_debugging
35df4500
TJB
4098 && section_is_overlay (bl->section)
4099 && !section_is_mapped (bl->section))
075f6582 4100 continue; /* unmapped overlay -- can't be a match */
35df4500 4101 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
4102 return permanent_breakpoint_here;
4103 else
4104 any_breakpoint_here = 1;
4105 }
4106 }
c906108c 4107
c2c6d25f 4108 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4109}
4110
1c5cfe86
PA
4111/* Return true if there's a moribund breakpoint at PC. */
4112
4113int
6c95b8df 4114moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4115{
4116 struct bp_location *loc;
4117 int ix;
4118
4119 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4120 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4121 return 1;
4122
4123 return 0;
4124}
c2c6d25f 4125
c36b740a 4126/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
4127 inserted using regular breakpoint_chain / bp_location array
4128 mechanism. This does not check for single-step breakpoints, which
4129 are inserted and removed using direct target manipulation. */
c906108c
SS
4130
4131int
4a64f543
MS
4132regular_breakpoint_inserted_here_p (struct address_space *aspace,
4133 CORE_ADDR pc)
c906108c 4134{
35df4500 4135 struct bp_location *bl, **blp_tmp;
c906108c 4136
35df4500 4137 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4138 {
35df4500
TJB
4139 if (bl->loc_type != bp_loc_software_breakpoint
4140 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4141 continue;
4142
35df4500 4143 if (bl->inserted
f1310107 4144 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4145 {
4146 if (overlay_debugging
35df4500
TJB
4147 && section_is_overlay (bl->section)
4148 && !section_is_mapped (bl->section))
075f6582
DJ
4149 continue; /* unmapped overlay -- can't be a match */
4150 else
4151 return 1;
4152 }
c5aa993b 4153 }
c36b740a
VP
4154 return 0;
4155}
4156
4157/* Returns non-zero iff there's either regular breakpoint
4158 or a single step breakpoint inserted at PC. */
4159
4160int
6c95b8df 4161breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 4162{
6c95b8df 4163 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 4164 return 1;
c906108c 4165
6c95b8df 4166 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4167 return 1;
4168
c906108c
SS
4169 return 0;
4170}
4171
ef370185
JB
4172/* Ignoring deprecated raw breakpoints, return non-zero iff there is a
4173 software breakpoint inserted at PC. */
4fa8626c 4174
ef370185
JB
4175static struct bp_location *
4176find_non_raw_software_breakpoint_inserted_here (struct address_space *aspace,
4177 CORE_ADDR pc)
4fa8626c 4178{
35df4500 4179 struct bp_location *bl, **blp_tmp;
4fa8626c 4180
35df4500 4181 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 4182 {
35df4500 4183 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4184 continue;
4185
35df4500
TJB
4186 if (bl->inserted
4187 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 4188 aspace, pc))
4fa8626c
DJ
4189 {
4190 if (overlay_debugging
35df4500
TJB
4191 && section_is_overlay (bl->section)
4192 && !section_is_mapped (bl->section))
4fa8626c
DJ
4193 continue; /* unmapped overlay -- can't be a match */
4194 else
ef370185 4195 return bl;
4fa8626c
DJ
4196 }
4197 }
4198
ef370185
JB
4199 return NULL;
4200}
4201
4202/* This function returns non-zero iff there is a software breakpoint
4203 inserted at PC. */
4204
4205int
4206software_breakpoint_inserted_here_p (struct address_space *aspace,
4207 CORE_ADDR pc)
4208{
4209 if (find_non_raw_software_breakpoint_inserted_here (aspace, pc) != NULL)
4210 return 1;
4211
1aafd4da 4212 /* Also check for software single-step breakpoints. */
6c95b8df 4213 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4214 return 1;
4215
4fa8626c
DJ
4216 return 0;
4217}
4218
9093389c
PA
4219int
4220hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4221 CORE_ADDR addr, ULONGEST len)
4222{
4223 struct breakpoint *bpt;
4224
4225 ALL_BREAKPOINTS (bpt)
4226 {
4227 struct bp_location *loc;
4228
4229 if (bpt->type != bp_hardware_watchpoint
4230 && bpt->type != bp_access_watchpoint)
4231 continue;
4232
4233 if (!breakpoint_enabled (bpt))
4234 continue;
4235
4236 for (loc = bpt->loc; loc; loc = loc->next)
4237 if (loc->pspace->aspace == aspace && loc->inserted)
4238 {
4239 CORE_ADDR l, h;
4240
4241 /* Check for intersection. */
4242 l = max (loc->address, addr);
4243 h = min (loc->address + loc->length, addr + len);
4244 if (l < h)
4245 return 1;
4246 }
4247 }
4248 return 0;
4249}
4250
075f6582
DJ
4251/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4252 PC is valid for process/thread PTID. */
c906108c
SS
4253
4254int
6c95b8df
PA
4255breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4256 ptid_t ptid)
c906108c 4257{
35df4500 4258 struct bp_location *bl, **blp_tmp;
4a306c9a 4259 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4260 int thread = -1;
4a306c9a 4261 int task = 0;
a6f1cd96 4262
35df4500 4263 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4264 {
35df4500
TJB
4265 if (bl->loc_type != bp_loc_software_breakpoint
4266 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4267 continue;
4268
35df4500
TJB
4269 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4270 if (!breakpoint_enabled (bl->owner)
4271 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4272 continue;
4273
f1310107 4274 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4275 continue;
4276
35df4500 4277 if (bl->owner->thread != -1)
075f6582 4278 {
a6f1cd96
JB
4279 /* This is a thread-specific breakpoint. Check that ptid
4280 matches that thread. If thread hasn't been computed yet,
4281 it is now time to do so. */
4282 if (thread == -1)
4283 thread = pid_to_thread_id (ptid);
35df4500 4284 if (bl->owner->thread != thread)
a6f1cd96 4285 continue;
075f6582 4286 }
a6f1cd96 4287
35df4500 4288 if (bl->owner->task != 0)
4a306c9a
JB
4289 {
4290 /* This is a task-specific breakpoint. Check that ptid
4291 matches that task. If task hasn't been computed yet,
4292 it is now time to do so. */
4293 if (task == 0)
4294 task = ada_get_task_number (ptid);
35df4500 4295 if (bl->owner->task != task)
4a306c9a
JB
4296 continue;
4297 }
4298
a6f1cd96 4299 if (overlay_debugging
35df4500
TJB
4300 && section_is_overlay (bl->section)
4301 && !section_is_mapped (bl->section))
a6f1cd96
JB
4302 continue; /* unmapped overlay -- can't be a match */
4303
4304 return 1;
c5aa993b 4305 }
c906108c
SS
4306
4307 return 0;
4308}
c906108c 4309\f
c5aa993b 4310
c906108c
SS
4311/* bpstat stuff. External routines' interfaces are documented
4312 in breakpoint.h. */
4313
4314int
c326b90e 4315is_catchpoint (struct breakpoint *ep)
c906108c 4316{
533be4dd 4317 return (ep->type == bp_catchpoint);
c906108c
SS
4318}
4319
f431efe5
PA
4320/* Frees any storage that is part of a bpstat. Does not walk the
4321 'next' chain. */
4322
4323static void
198757a8
VP
4324bpstat_free (bpstat bs)
4325{
4326 if (bs->old_val != NULL)
4327 value_free (bs->old_val);
9add0f1b 4328 decref_counted_command_line (&bs->commands);
f431efe5 4329 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4330 xfree (bs);
4331}
4332
c906108c
SS
4333/* Clear a bpstat so that it says we are not at any breakpoint.
4334 Also free any storage that is part of a bpstat. */
4335
4336void
fba45db2 4337bpstat_clear (bpstat *bsp)
c906108c
SS
4338{
4339 bpstat p;
4340 bpstat q;
4341
4342 if (bsp == 0)
4343 return;
4344 p = *bsp;
4345 while (p != NULL)
4346 {
4347 q = p->next;
198757a8 4348 bpstat_free (p);
c906108c
SS
4349 p = q;
4350 }
4351 *bsp = NULL;
4352}
4353
4354/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4355 is part of the bpstat is copied as well. */
4356
4357bpstat
fba45db2 4358bpstat_copy (bpstat bs)
c906108c
SS
4359{
4360 bpstat p = NULL;
4361 bpstat tmp;
4362 bpstat retval = NULL;
4363
4364 if (bs == NULL)
4365 return bs;
4366
4367 for (; bs != NULL; bs = bs->next)
4368 {
4369 tmp = (bpstat) xmalloc (sizeof (*tmp));
4370 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4371 incref_counted_command_line (tmp->commands);
f431efe5 4372 incref_bp_location (tmp->bp_location_at);
31cc81e9 4373 if (bs->old_val != NULL)
3c3185ac
JK
4374 {
4375 tmp->old_val = value_copy (bs->old_val);
4376 release_value (tmp->old_val);
4377 }
31cc81e9 4378
c906108c
SS
4379 if (p == NULL)
4380 /* This is the first thing in the chain. */
4381 retval = tmp;
4382 else
4383 p->next = tmp;
4384 p = tmp;
4385 }
4386 p->next = NULL;
4387 return retval;
4388}
4389
4a64f543 4390/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4391
4392bpstat
fba45db2 4393bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4394{
c5aa993b
JM
4395 if (bsp == NULL)
4396 return NULL;
c906108c 4397
c5aa993b
JM
4398 for (; bsp != NULL; bsp = bsp->next)
4399 {
f431efe5 4400 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4401 return bsp;
4402 }
c906108c
SS
4403 return NULL;
4404}
4405
ab04a2af
TT
4406/* See breakpoint.h. */
4407
47591c29 4408int
427cd150 4409bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4410{
ab04a2af
TT
4411 for (; bsp != NULL; bsp = bsp->next)
4412 {
427cd150
TT
4413 if (bsp->breakpoint_at == NULL)
4414 {
4415 /* A moribund location can never explain a signal other than
4416 GDB_SIGNAL_TRAP. */
4417 if (sig == GDB_SIGNAL_TRAP)
47591c29 4418 return 1;
427cd150
TT
4419 }
4420 else
47591c29
PA
4421 {
4422 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4423 sig))
4424 return 1;
4425 }
ab04a2af
TT
4426 }
4427
47591c29 4428 return 0;
ab04a2af
TT
4429}
4430
4a64f543
MS
4431/* Put in *NUM the breakpoint number of the first breakpoint we are
4432 stopped at. *BSP upon return is a bpstat which points to the
4433 remaining breakpoints stopped at (but which is not guaranteed to be
4434 good for anything but further calls to bpstat_num).
4435
8671a17b
PA
4436 Return 0 if passed a bpstat which does not indicate any breakpoints.
4437 Return -1 if stopped at a breakpoint that has been deleted since
4438 we set it.
4439 Return 1 otherwise. */
c906108c
SS
4440
4441int
8671a17b 4442bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4443{
4444 struct breakpoint *b;
4445
4446 if ((*bsp) == NULL)
4447 return 0; /* No more breakpoint values */
8671a17b 4448
4a64f543
MS
4449 /* We assume we'll never have several bpstats that correspond to a
4450 single breakpoint -- otherwise, this function might return the
4451 same number more than once and this will look ugly. */
f431efe5 4452 b = (*bsp)->breakpoint_at;
8671a17b
PA
4453 *bsp = (*bsp)->next;
4454 if (b == NULL)
4455 return -1; /* breakpoint that's been deleted since */
4456
4457 *num = b->number; /* We have its number */
4458 return 1;
c906108c
SS
4459}
4460
e93ca019 4461/* See breakpoint.h. */
c906108c
SS
4462
4463void
e93ca019 4464bpstat_clear_actions (void)
c906108c 4465{
e93ca019
JK
4466 struct thread_info *tp;
4467 bpstat bs;
4468
4469 if (ptid_equal (inferior_ptid, null_ptid))
4470 return;
4471
4472 tp = find_thread_ptid (inferior_ptid);
4473 if (tp == NULL)
4474 return;
4475
4476 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4477 {
9add0f1b 4478 decref_counted_command_line (&bs->commands);
abf85f46 4479
c906108c
SS
4480 if (bs->old_val != NULL)
4481 {
4482 value_free (bs->old_val);
4483 bs->old_val = NULL;
4484 }
4485 }
4486}
4487
f3b1572e
PA
4488/* Called when a command is about to proceed the inferior. */
4489
4490static void
4491breakpoint_about_to_proceed (void)
4492{
4493 if (!ptid_equal (inferior_ptid, null_ptid))
4494 {
4495 struct thread_info *tp = inferior_thread ();
4496
4497 /* Allow inferior function calls in breakpoint commands to not
4498 interrupt the command list. When the call finishes
4499 successfully, the inferior will be standing at the same
4500 breakpoint as if nothing happened. */
16c381f0 4501 if (tp->control.in_infcall)
f3b1572e
PA
4502 return;
4503 }
4504
4505 breakpoint_proceeded = 1;
4506}
4507
4a64f543
MS
4508/* Stub for cleaning up our state if we error-out of a breakpoint
4509 command. */
c906108c 4510static void
4efb68b1 4511cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4512{
4513 executing_breakpoint_commands = 0;
4514}
4515
abf85f46
JK
4516/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4517 or its equivalent. */
4518
4519static int
4520command_line_is_silent (struct command_line *cmd)
4521{
4522 return cmd && (strcmp ("silent", cmd->line) == 0
4523 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4524}
4525
4a64f543
MS
4526/* Execute all the commands associated with all the breakpoints at
4527 this location. Any of these commands could cause the process to
4528 proceed beyond this point, etc. We look out for such changes by
4529 checking the global "breakpoint_proceeded" after each command.
c906108c 4530
347bddb7
PA
4531 Returns true if a breakpoint command resumed the inferior. In that
4532 case, it is the caller's responsibility to recall it again with the
4533 bpstat of the current thread. */
4534
4535static int
4536bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4537{
4538 bpstat bs;
4539 struct cleanup *old_chain;
347bddb7 4540 int again = 0;
c906108c
SS
4541
4542 /* Avoid endless recursion if a `source' command is contained
4543 in bs->commands. */
4544 if (executing_breakpoint_commands)
347bddb7 4545 return 0;
c906108c
SS
4546
4547 executing_breakpoint_commands = 1;
4548 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4549
cf6c5ffb
TT
4550 prevent_dont_repeat ();
4551
4a64f543 4552 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4553 bs = *bsp;
4554
4555 breakpoint_proceeded = 0;
4556 for (; bs != NULL; bs = bs->next)
4557 {
9add0f1b 4558 struct counted_command_line *ccmd;
6c50ab1c
JB
4559 struct command_line *cmd;
4560 struct cleanup *this_cmd_tree_chain;
4561
4562 /* Take ownership of the BSP's command tree, if it has one.
4563
4564 The command tree could legitimately contain commands like
4565 'step' and 'next', which call clear_proceed_status, which
4566 frees stop_bpstat's command tree. To make sure this doesn't
4567 free the tree we're executing out from under us, we need to
4568 take ownership of the tree ourselves. Since a given bpstat's
4569 commands are only executed once, we don't need to copy it; we
4570 can clear the pointer in the bpstat, and make sure we free
4571 the tree when we're done. */
9add0f1b
TT
4572 ccmd = bs->commands;
4573 bs->commands = NULL;
abf85f46
JK
4574 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4575 cmd = ccmd ? ccmd->commands : NULL;
4576 if (command_line_is_silent (cmd))
4577 {
4578 /* The action has been already done by bpstat_stop_status. */
4579 cmd = cmd->next;
4580 }
6c50ab1c 4581
c906108c
SS
4582 while (cmd != NULL)
4583 {
4584 execute_control_command (cmd);
4585
4586 if (breakpoint_proceeded)
4587 break;
4588 else
4589 cmd = cmd->next;
4590 }
6c50ab1c
JB
4591
4592 /* We can free this command tree now. */
4593 do_cleanups (this_cmd_tree_chain);
4594
c906108c 4595 if (breakpoint_proceeded)
32c1e744
VP
4596 {
4597 if (target_can_async_p ())
347bddb7
PA
4598 /* If we are in async mode, then the target might be still
4599 running, not stopped at any breakpoint, so nothing for
4600 us to do here -- just return to the event loop. */
4601 ;
32c1e744
VP
4602 else
4603 /* In sync mode, when execute_control_command returns
4604 we're already standing on the next breakpoint.
347bddb7
PA
4605 Breakpoint commands for that stop were not run, since
4606 execute_command does not run breakpoint commands --
4607 only command_line_handler does, but that one is not
4608 involved in execution of breakpoint commands. So, we
4609 can now execute breakpoint commands. It should be
4610 noted that making execute_command do bpstat actions is
4611 not an option -- in this case we'll have recursive
4612 invocation of bpstat for each breakpoint with a
4613 command, and can easily blow up GDB stack. Instead, we
4614 return true, which will trigger the caller to recall us
4615 with the new stop_bpstat. */
4616 again = 1;
4617 break;
32c1e744 4618 }
c906108c 4619 }
c2b8ed2c 4620 do_cleanups (old_chain);
347bddb7
PA
4621 return again;
4622}
4623
4624void
4625bpstat_do_actions (void)
4626{
353d1d73
JK
4627 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4628
347bddb7
PA
4629 /* Do any commands attached to breakpoint we are stopped at. */
4630 while (!ptid_equal (inferior_ptid, null_ptid)
4631 && target_has_execution
4632 && !is_exited (inferior_ptid)
4633 && !is_executing (inferior_ptid))
4634 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4635 and only return when it is stopped at the next breakpoint, we
4636 keep doing breakpoint actions until it returns false to
4637 indicate the inferior was not resumed. */
16c381f0 4638 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4639 break;
353d1d73
JK
4640
4641 discard_cleanups (cleanup_if_error);
c906108c
SS
4642}
4643
fa4727a6
DJ
4644/* Print out the (old or new) value associated with a watchpoint. */
4645
4646static void
4647watchpoint_value_print (struct value *val, struct ui_file *stream)
4648{
4649 if (val == NULL)
4650 fprintf_unfiltered (stream, _("<unreadable>"));
4651 else
79a45b7d
TT
4652 {
4653 struct value_print_options opts;
4654 get_user_print_options (&opts);
4655 value_print (val, stream, &opts);
4656 }
fa4727a6
DJ
4657}
4658
e514a9d6 4659/* Generic routine for printing messages indicating why we
4a64f543 4660 stopped. The behavior of this function depends on the value
e514a9d6
JM
4661 'print_it' in the bpstat structure. Under some circumstances we
4662 may decide not to print anything here and delegate the task to
4a64f543 4663 normal_stop(). */
e514a9d6
JM
4664
4665static enum print_stop_action
4666print_bp_stop_message (bpstat bs)
4667{
4668 switch (bs->print_it)
4669 {
4670 case print_it_noop:
4a64f543 4671 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4672 return PRINT_UNKNOWN;
4673 break;
4674
4675 case print_it_done:
4676 /* We still want to print the frame, but we already printed the
4a64f543 4677 relevant messages. */
e514a9d6
JM
4678 return PRINT_SRC_AND_LOC;
4679 break;
4680
4681 case print_it_normal:
4f8d1dc6 4682 {
f431efe5
PA
4683 struct breakpoint *b = bs->breakpoint_at;
4684
1a6a67de
TJB
4685 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4686 which has since been deleted. */
4687 if (b == NULL)
4688 return PRINT_UNKNOWN;
4689
348d480f
PA
4690 /* Normal case. Call the breakpoint's print_it method. */
4691 return b->ops->print_it (bs);
4f8d1dc6 4692 }
348d480f 4693 break;
3086aeae 4694
e514a9d6 4695 default:
8e65ff28 4696 internal_error (__FILE__, __LINE__,
e2e0b3e5 4697 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4698 break;
c906108c 4699 }
c906108c
SS
4700}
4701
edcc5120
TT
4702/* A helper function that prints a shared library stopped event. */
4703
4704static void
4705print_solib_event (int is_catchpoint)
4706{
4707 int any_deleted
4708 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4709 int any_added
4710 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4711
4712 if (!is_catchpoint)
4713 {
4714 if (any_added || any_deleted)
4715 ui_out_text (current_uiout,
4716 _("Stopped due to shared library event:\n"));
4717 else
4718 ui_out_text (current_uiout,
4719 _("Stopped due to shared library event (no "
4720 "libraries added or removed)\n"));
4721 }
4722
4723 if (ui_out_is_mi_like_p (current_uiout))
4724 ui_out_field_string (current_uiout, "reason",
4725 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4726
4727 if (any_deleted)
4728 {
4729 struct cleanup *cleanup;
4730 char *name;
4731 int ix;
4732
4733 ui_out_text (current_uiout, _(" Inferior unloaded "));
4734 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4735 "removed");
4736 for (ix = 0;
4737 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4738 ix, name);
4739 ++ix)
4740 {
4741 if (ix > 0)
4742 ui_out_text (current_uiout, " ");
4743 ui_out_field_string (current_uiout, "library", name);
4744 ui_out_text (current_uiout, "\n");
4745 }
4746
4747 do_cleanups (cleanup);
4748 }
4749
4750 if (any_added)
4751 {
4752 struct so_list *iter;
4753 int ix;
4754 struct cleanup *cleanup;
4755
4756 ui_out_text (current_uiout, _(" Inferior loaded "));
4757 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4758 "added");
4759 for (ix = 0;
4760 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4761 ix, iter);
4762 ++ix)
4763 {
4764 if (ix > 0)
4765 ui_out_text (current_uiout, " ");
4766 ui_out_field_string (current_uiout, "library", iter->so_name);
4767 ui_out_text (current_uiout, "\n");
4768 }
4769
4770 do_cleanups (cleanup);
4771 }
4772}
4773
e514a9d6
JM
4774/* Print a message indicating what happened. This is called from
4775 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4776 list - a list of the eventpoints that caused this stop. KIND is
4777 the target_waitkind for the stopping event. This
e514a9d6
JM
4778 routine calls the generic print routine for printing a message
4779 about reasons for stopping. This will print (for example) the
4780 "Breakpoint n," part of the output. The return value of this
4781 routine is one of:
c906108c 4782
4a64f543 4783 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4784 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4785 code to print the location. An example is
c5aa993b
JM
4786 "Breakpoint 1, " which should be followed by
4787 the location.
917317f4 4788 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4789 to also print the location part of the message.
4790 An example is the catch/throw messages, which
4a64f543 4791 don't require a location appended to the end.
917317f4 4792 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4793 further info to be printed. */
c906108c 4794
917317f4 4795enum print_stop_action
36dfb11c 4796bpstat_print (bpstat bs, int kind)
c906108c
SS
4797{
4798 int val;
c5aa993b 4799
c906108c 4800 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4801 (Currently all watchpoints go on the bpstat whether hit or not.
4802 That probably could (should) be changed, provided care is taken
c906108c 4803 with respect to bpstat_explains_signal). */
e514a9d6
JM
4804 for (; bs; bs = bs->next)
4805 {
4806 val = print_bp_stop_message (bs);
4807 if (val == PRINT_SRC_ONLY
4808 || val == PRINT_SRC_AND_LOC
4809 || val == PRINT_NOTHING)
4810 return val;
4811 }
c906108c 4812
36dfb11c
TT
4813 /* If we had hit a shared library event breakpoint,
4814 print_bp_stop_message would print out this message. If we hit an
4815 OS-level shared library event, do the same thing. */
4816 if (kind == TARGET_WAITKIND_LOADED)
4817 {
edcc5120 4818 print_solib_event (0);
36dfb11c
TT
4819 return PRINT_NOTHING;
4820 }
4821
e514a9d6 4822 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4823 with and nothing was printed. */
917317f4 4824 return PRINT_UNKNOWN;
c906108c
SS
4825}
4826
c42bd95a
DE
4827/* Evaluate the expression EXP and return 1 if value is zero.
4828 This returns the inverse of the condition because it is called
4829 from catch_errors which returns 0 if an exception happened, and if an
4830 exception happens we want execution to stop.
4a64f543 4831 The argument is a "struct expression *" that has been cast to a
c42bd95a 4832 "void *" to make it pass through catch_errors. */
c906108c
SS
4833
4834static int
4efb68b1 4835breakpoint_cond_eval (void *exp)
c906108c 4836{
278cd55f 4837 struct value *mark = value_mark ();
c5aa993b 4838 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4839
c906108c
SS
4840 value_free_to_mark (mark);
4841 return i;
4842}
4843
5760d0ab 4844/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4845
4846static bpstat
5760d0ab 4847bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4848{
4849 bpstat bs;
4850
4851 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4852 bs->next = NULL;
4853 **bs_link_pointer = bs;
4854 *bs_link_pointer = &bs->next;
f431efe5
PA
4855 bs->breakpoint_at = bl->owner;
4856 bs->bp_location_at = bl;
4857 incref_bp_location (bl);
c906108c
SS
4858 /* If the condition is false, etc., don't do the commands. */
4859 bs->commands = NULL;
4860 bs->old_val = NULL;
4861 bs->print_it = print_it_normal;
4862 return bs;
4863}
4864\f
d983da9c
DJ
4865/* The target has stopped with waitstatus WS. Check if any hardware
4866 watchpoints have triggered, according to the target. */
4867
4868int
4869watchpoints_triggered (struct target_waitstatus *ws)
4870{
d92524f1 4871 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4872 CORE_ADDR addr;
4873 struct breakpoint *b;
4874
4875 if (!stopped_by_watchpoint)
4876 {
4877 /* We were not stopped by a watchpoint. Mark all watchpoints
4878 as not triggered. */
4879 ALL_BREAKPOINTS (b)
cc60f2e3 4880 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4881 {
4882 struct watchpoint *w = (struct watchpoint *) b;
4883
4884 w->watchpoint_triggered = watch_triggered_no;
4885 }
d983da9c
DJ
4886
4887 return 0;
4888 }
4889
4890 if (!target_stopped_data_address (&current_target, &addr))
4891 {
4892 /* We were stopped by a watchpoint, but we don't know where.
4893 Mark all watchpoints as unknown. */
4894 ALL_BREAKPOINTS (b)
cc60f2e3 4895 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4896 {
4897 struct watchpoint *w = (struct watchpoint *) b;
4898
4899 w->watchpoint_triggered = watch_triggered_unknown;
4900 }
d983da9c 4901
3c4797ba 4902 return 1;
d983da9c
DJ
4903 }
4904
4905 /* The target could report the data address. Mark watchpoints
4906 affected by this data address as triggered, and all others as not
4907 triggered. */
4908
4909 ALL_BREAKPOINTS (b)
cc60f2e3 4910 if (is_hardware_watchpoint (b))
d983da9c 4911 {
3a5c3e22 4912 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4913 struct bp_location *loc;
d983da9c 4914
3a5c3e22 4915 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4916 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4917 {
3a5c3e22 4918 if (is_masked_watchpoint (b))
9c06b0b4 4919 {
3a5c3e22
PA
4920 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4921 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4922
4923 if (newaddr == start)
4924 {
3a5c3e22 4925 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4926 break;
4927 }
4928 }
4929 /* Exact match not required. Within range is sufficient. */
4930 else if (target_watchpoint_addr_within_range (&current_target,
4931 addr, loc->address,
4932 loc->length))
4933 {
3a5c3e22 4934 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4935 break;
4936 }
4937 }
d983da9c
DJ
4938 }
4939
4940 return 1;
4941}
4942
c906108c
SS
4943/* Possible return values for watchpoint_check (this can't be an enum
4944 because of check_errors). */
4945/* The watchpoint has been deleted. */
4946#define WP_DELETED 1
4947/* The value has changed. */
4948#define WP_VALUE_CHANGED 2
4949/* The value has not changed. */
4950#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4951/* Ignore this watchpoint, no matter if the value changed or not. */
4952#define WP_IGNORE 4
c906108c
SS
4953
4954#define BP_TEMPFLAG 1
4955#define BP_HARDWAREFLAG 2
4956
4a64f543
MS
4957/* Evaluate watchpoint condition expression and check if its value
4958 changed.
553e4c11
JB
4959
4960 P should be a pointer to struct bpstat, but is defined as a void *
4961 in order for this function to be usable with catch_errors. */
c906108c
SS
4962
4963static int
4efb68b1 4964watchpoint_check (void *p)
c906108c
SS
4965{
4966 bpstat bs = (bpstat) p;
3a5c3e22 4967 struct watchpoint *b;
c906108c
SS
4968 struct frame_info *fr;
4969 int within_current_scope;
4970
f431efe5 4971 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4972 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4973 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4974
f6bc2008
PA
4975 /* If this is a local watchpoint, we only want to check if the
4976 watchpoint frame is in scope if the current thread is the thread
4977 that was used to create the watchpoint. */
4978 if (!watchpoint_in_thread_scope (b))
60e1c644 4979 return WP_IGNORE;
f6bc2008 4980
c906108c
SS
4981 if (b->exp_valid_block == NULL)
4982 within_current_scope = 1;
4983 else
4984 {
edb3359d
DJ
4985 struct frame_info *frame = get_current_frame ();
4986 struct gdbarch *frame_arch = get_frame_arch (frame);
4987 CORE_ADDR frame_pc = get_frame_pc (frame);
4988
4a64f543
MS
4989 /* in_function_epilogue_p() returns a non-zero value if we're
4990 still in the function but the stack frame has already been
4991 invalidated. Since we can't rely on the values of local
4992 variables after the stack has been destroyed, we are treating
4993 the watchpoint in that state as `not changed' without further
4994 checking. Don't mark watchpoints as changed if the current
4995 frame is in an epilogue - even if they are in some other
4996 frame, our view of the stack is likely to be wrong and
4997 frame_find_by_id could error out. */
a0f49112 4998 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 4999 return WP_IGNORE;
a0f49112 5000
101dcfbe 5001 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5002 within_current_scope = (fr != NULL);
69fbadd5
DJ
5003
5004 /* If we've gotten confused in the unwinder, we might have
5005 returned a frame that can't describe this variable. */
edb3359d
DJ
5006 if (within_current_scope)
5007 {
5008 struct symbol *function;
5009
5010 function = get_frame_function (fr);
5011 if (function == NULL
5012 || !contained_in (b->exp_valid_block,
5013 SYMBOL_BLOCK_VALUE (function)))
5014 within_current_scope = 0;
5015 }
69fbadd5 5016
edb3359d 5017 if (within_current_scope)
c906108c
SS
5018 /* If we end up stopping, the current frame will get selected
5019 in normal_stop. So this call to select_frame won't affect
5020 the user. */
0f7d239c 5021 select_frame (fr);
c906108c 5022 }
c5aa993b 5023
c906108c
SS
5024 if (within_current_scope)
5025 {
4a64f543
MS
5026 /* We use value_{,free_to_}mark because it could be a *long*
5027 time before we return to the command level and call
5028 free_all_values. We can't call free_all_values because we
5029 might be in the middle of evaluating a function call. */
c906108c 5030
0cf6dd15 5031 int pc = 0;
9c06b0b4 5032 struct value *mark;
fa4727a6
DJ
5033 struct value *new_val;
5034
3a5c3e22 5035 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5036 /* Since we don't know the exact trigger address (from
5037 stopped_data_address), just tell the user we've triggered
5038 a mask watchpoint. */
5039 return WP_VALUE_CHANGED;
5040
5041 mark = value_mark ();
3a1115a0 5042 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5043
4a64f543
MS
5044 /* We use value_equal_contents instead of value_equal because
5045 the latter coerces an array to a pointer, thus comparing just
5046 the address of the array instead of its contents. This is
5047 not what we want. */
fa4727a6 5048 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5049 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5050 {
fa4727a6
DJ
5051 if (new_val != NULL)
5052 {
5053 release_value (new_val);
5054 value_free_to_mark (mark);
5055 }
c906108c
SS
5056 bs->old_val = b->val;
5057 b->val = new_val;
fa4727a6 5058 b->val_valid = 1;
c906108c
SS
5059 return WP_VALUE_CHANGED;
5060 }
5061 else
5062 {
60e1c644 5063 /* Nothing changed. */
c906108c 5064 value_free_to_mark (mark);
c906108c
SS
5065 return WP_VALUE_NOT_CHANGED;
5066 }
5067 }
5068 else
5069 {
79a45e25
PA
5070 struct ui_out *uiout = current_uiout;
5071
c906108c 5072 /* This seems like the only logical thing to do because
c5aa993b
JM
5073 if we temporarily ignored the watchpoint, then when
5074 we reenter the block in which it is valid it contains
5075 garbage (in the case of a function, it may have two
5076 garbage values, one before and one after the prologue).
5077 So we can't even detect the first assignment to it and
5078 watch after that (since the garbage may or may not equal
5079 the first value assigned). */
348d480f
PA
5080 /* We print all the stop information in
5081 breakpoint_ops->print_it, but in this case, by the time we
5082 call breakpoint_ops->print_it this bp will be deleted
5083 already. So we have no choice but print the information
5084 here. */
9dc5e2a9 5085 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5086 ui_out_field_string
5087 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5088 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5089 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5090 ui_out_text (uiout,
5091 " deleted because the program has left the block in\n\
8b93c638 5092which its expression is valid.\n");
4ce44c66 5093
cdac0397 5094 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5095 decref_counted_command_line (&b->base.commands);
d0fb5eae 5096 watchpoint_del_at_next_stop (b);
c906108c
SS
5097
5098 return WP_DELETED;
5099 }
5100}
5101
18a18393 5102/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5103 breakpoint location BL. This function does not check if we should
5104 stop, only if BL explains the stop. */
5105
18a18393 5106static int
6c95b8df 5107bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5108 struct address_space *aspace, CORE_ADDR bp_addr,
5109 const struct target_waitstatus *ws)
18a18393
VP
5110{
5111 struct breakpoint *b = bl->owner;
5112
348d480f 5113 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5114 gdb_assert (b != NULL);
5115
09ac7c10 5116 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5117}
5118
3a5c3e22
PA
5119/* Determine if the watched values have actually changed, and we
5120 should stop. If not, set BS->stop to 0. */
5121
18a18393
VP
5122static void
5123bpstat_check_watchpoint (bpstat bs)
5124{
2bdf28a0 5125 const struct bp_location *bl;
3a5c3e22 5126 struct watchpoint *b;
2bdf28a0
JK
5127
5128 /* BS is built for existing struct breakpoint. */
f431efe5 5129 bl = bs->bp_location_at;
2bdf28a0 5130 gdb_assert (bl != NULL);
3a5c3e22 5131 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5132 gdb_assert (b != NULL);
18a18393 5133
18a18393 5134 {
18a18393
VP
5135 int must_check_value = 0;
5136
3a5c3e22 5137 if (b->base.type == bp_watchpoint)
18a18393
VP
5138 /* For a software watchpoint, we must always check the
5139 watched value. */
5140 must_check_value = 1;
5141 else if (b->watchpoint_triggered == watch_triggered_yes)
5142 /* We have a hardware watchpoint (read, write, or access)
5143 and the target earlier reported an address watched by
5144 this watchpoint. */
5145 must_check_value = 1;
5146 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5147 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5148 /* We were stopped by a hardware watchpoint, but the target could
5149 not report the data address. We must check the watchpoint's
5150 value. Access and read watchpoints are out of luck; without
5151 a data address, we can't figure it out. */
5152 must_check_value = 1;
3a5c3e22 5153
18a18393
VP
5154 if (must_check_value)
5155 {
3e43a32a
MS
5156 char *message
5157 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5158 b->base.number);
18a18393
VP
5159 struct cleanup *cleanups = make_cleanup (xfree, message);
5160 int e = catch_errors (watchpoint_check, bs, message,
5161 RETURN_MASK_ALL);
5162 do_cleanups (cleanups);
5163 switch (e)
5164 {
5165 case WP_DELETED:
5166 /* We've already printed what needs to be printed. */
5167 bs->print_it = print_it_done;
5168 /* Stop. */
5169 break;
60e1c644
PA
5170 case WP_IGNORE:
5171 bs->print_it = print_it_noop;
5172 bs->stop = 0;
5173 break;
18a18393 5174 case WP_VALUE_CHANGED:
3a5c3e22 5175 if (b->base.type == bp_read_watchpoint)
18a18393 5176 {
85d721b8
PA
5177 /* There are two cases to consider here:
5178
4a64f543 5179 1. We're watching the triggered memory for reads.
85d721b8
PA
5180 In that case, trust the target, and always report
5181 the watchpoint hit to the user. Even though
5182 reads don't cause value changes, the value may
5183 have changed since the last time it was read, and
5184 since we're not trapping writes, we will not see
5185 those, and as such we should ignore our notion of
5186 old value.
5187
4a64f543 5188 2. We're watching the triggered memory for both
85d721b8
PA
5189 reads and writes. There are two ways this may
5190 happen:
5191
4a64f543 5192 2.1. This is a target that can't break on data
85d721b8
PA
5193 reads only, but can break on accesses (reads or
5194 writes), such as e.g., x86. We detect this case
5195 at the time we try to insert read watchpoints.
5196
4a64f543 5197 2.2. Otherwise, the target supports read
85d721b8
PA
5198 watchpoints, but, the user set an access or write
5199 watchpoint watching the same memory as this read
5200 watchpoint.
5201
5202 If we're watching memory writes as well as reads,
5203 ignore watchpoint hits when we find that the
5204 value hasn't changed, as reads don't cause
5205 changes. This still gives false positives when
5206 the program writes the same value to memory as
5207 what there was already in memory (we will confuse
5208 it for a read), but it's much better than
5209 nothing. */
5210
5211 int other_write_watchpoint = 0;
5212
5213 if (bl->watchpoint_type == hw_read)
5214 {
5215 struct breakpoint *other_b;
5216
5217 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5218 if (other_b->type == bp_hardware_watchpoint
5219 || other_b->type == bp_access_watchpoint)
85d721b8 5220 {
3a5c3e22
PA
5221 struct watchpoint *other_w =
5222 (struct watchpoint *) other_b;
5223
5224 if (other_w->watchpoint_triggered
5225 == watch_triggered_yes)
5226 {
5227 other_write_watchpoint = 1;
5228 break;
5229 }
85d721b8
PA
5230 }
5231 }
5232
5233 if (other_write_watchpoint
5234 || bl->watchpoint_type == hw_access)
5235 {
5236 /* We're watching the same memory for writes,
5237 and the value changed since the last time we
5238 updated it, so this trap must be for a write.
5239 Ignore it. */
5240 bs->print_it = print_it_noop;
5241 bs->stop = 0;
5242 }
18a18393
VP
5243 }
5244 break;
5245 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5246 if (b->base.type == bp_hardware_watchpoint
5247 || b->base.type == bp_watchpoint)
18a18393
VP
5248 {
5249 /* Don't stop: write watchpoints shouldn't fire if
5250 the value hasn't changed. */
5251 bs->print_it = print_it_noop;
5252 bs->stop = 0;
5253 }
5254 /* Stop. */
5255 break;
5256 default:
5257 /* Can't happen. */
5258 case 0:
5259 /* Error from catch_errors. */
3a5c3e22 5260 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5261 watchpoint_del_at_next_stop (b);
18a18393
VP
5262 /* We've already printed what needs to be printed. */
5263 bs->print_it = print_it_done;
5264 break;
5265 }
5266 }
5267 else /* must_check_value == 0 */
5268 {
5269 /* This is a case where some watchpoint(s) triggered, but
5270 not at the address of this watchpoint, or else no
5271 watchpoint triggered after all. So don't print
5272 anything for this watchpoint. */
5273 bs->print_it = print_it_noop;
5274 bs->stop = 0;
5275 }
5276 }
5277}
5278
7d4df6a4
DE
5279/* For breakpoints that are currently marked as telling gdb to stop,
5280 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5281 of breakpoint referred to by BS. If we should not stop for this
5282 breakpoint, set BS->stop to 0. */
f431efe5 5283
18a18393
VP
5284static void
5285bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5286{
2bdf28a0
JK
5287 const struct bp_location *bl;
5288 struct breakpoint *b;
7d4df6a4
DE
5289 int value_is_zero = 0;
5290 struct expression *cond;
5291
5292 gdb_assert (bs->stop);
2bdf28a0
JK
5293
5294 /* BS is built for existing struct breakpoint. */
f431efe5 5295 bl = bs->bp_location_at;
2bdf28a0 5296 gdb_assert (bl != NULL);
f431efe5 5297 b = bs->breakpoint_at;
2bdf28a0 5298 gdb_assert (b != NULL);
18a18393 5299
b775012e
LM
5300 /* Even if the target evaluated the condition on its end and notified GDB, we
5301 need to do so again since GDB does not know if we stopped due to a
5302 breakpoint or a single step breakpoint. */
5303
18a18393 5304 if (frame_id_p (b->frame_id)
edb3359d 5305 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5306 {
7d4df6a4
DE
5307 bs->stop = 0;
5308 return;
5309 }
60e1c644 5310
12ab52e9
PA
5311 /* If this is a thread/task-specific breakpoint, don't waste cpu
5312 evaluating the condition if this isn't the specified
5313 thread/task. */
5314 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5315 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5316
6c1b0f7b
DE
5317 {
5318 bs->stop = 0;
5319 return;
5320 }
5321
6dddc817
DE
5322 /* Evaluate extension language breakpoints that have a "stop" method
5323 implemented. */
5324 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5325
7d4df6a4
DE
5326 if (is_watchpoint (b))
5327 {
5328 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5329
7d4df6a4
DE
5330 cond = w->cond_exp;
5331 }
5332 else
5333 cond = bl->cond;
60e1c644 5334
7d4df6a4
DE
5335 if (cond && b->disposition != disp_del_at_next_stop)
5336 {
5337 int within_current_scope = 1;
5338 struct watchpoint * w;
60e1c644 5339
7d4df6a4
DE
5340 /* We use value_mark and value_free_to_mark because it could
5341 be a long time before we return to the command level and
5342 call free_all_values. We can't call free_all_values
5343 because we might be in the middle of evaluating a
5344 function call. */
5345 struct value *mark = value_mark ();
5346
5347 if (is_watchpoint (b))
5348 w = (struct watchpoint *) b;
5349 else
5350 w = NULL;
5351
5352 /* Need to select the frame, with all that implies so that
5353 the conditions will have the right context. Because we
5354 use the frame, we will not see an inlined function's
5355 variables when we arrive at a breakpoint at the start
5356 of the inlined function; the current frame will be the
5357 call site. */
5358 if (w == NULL || w->cond_exp_valid_block == NULL)
5359 select_frame (get_current_frame ());
5360 else
18a18393 5361 {
7d4df6a4
DE
5362 struct frame_info *frame;
5363
5364 /* For local watchpoint expressions, which particular
5365 instance of a local is being watched matters, so we
5366 keep track of the frame to evaluate the expression
5367 in. To evaluate the condition however, it doesn't
5368 really matter which instantiation of the function
5369 where the condition makes sense triggers the
5370 watchpoint. This allows an expression like "watch
5371 global if q > 10" set in `func', catch writes to
5372 global on all threads that call `func', or catch
5373 writes on all recursive calls of `func' by a single
5374 thread. We simply always evaluate the condition in
5375 the innermost frame that's executing where it makes
5376 sense to evaluate the condition. It seems
5377 intuitive. */
5378 frame = block_innermost_frame (w->cond_exp_valid_block);
5379 if (frame != NULL)
5380 select_frame (frame);
5381 else
5382 within_current_scope = 0;
18a18393 5383 }
7d4df6a4
DE
5384 if (within_current_scope)
5385 value_is_zero
5386 = catch_errors (breakpoint_cond_eval, cond,
5387 "Error in testing breakpoint condition:\n",
5388 RETURN_MASK_ALL);
5389 else
18a18393 5390 {
7d4df6a4
DE
5391 warning (_("Watchpoint condition cannot be tested "
5392 "in the current scope"));
5393 /* If we failed to set the right context for this
5394 watchpoint, unconditionally report it. */
5395 value_is_zero = 0;
18a18393 5396 }
7d4df6a4
DE
5397 /* FIXME-someday, should give breakpoint #. */
5398 value_free_to_mark (mark);
18a18393 5399 }
7d4df6a4
DE
5400
5401 if (cond && value_is_zero)
5402 {
5403 bs->stop = 0;
5404 }
7d4df6a4
DE
5405 else if (b->ignore_count > 0)
5406 {
5407 b->ignore_count--;
5408 bs->stop = 0;
5409 /* Increase the hit count even though we don't stop. */
5410 ++(b->hit_count);
5411 observer_notify_breakpoint_modified (b);
5412 }
18a18393
VP
5413}
5414
5415
9709f61c 5416/* Get a bpstat associated with having just stopped at address
d983da9c 5417 BP_ADDR in thread PTID.
c906108c 5418
d983da9c 5419 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5420 don't understand this stop. Result is a chain of bpstat's such
5421 that:
c906108c 5422
c5aa993b 5423 if we don't understand the stop, the result is a null pointer.
c906108c 5424
c5aa993b 5425 if we understand why we stopped, the result is not null.
c906108c 5426
c5aa993b
JM
5427 Each element of the chain refers to a particular breakpoint or
5428 watchpoint at which we have stopped. (We may have stopped for
5429 several reasons concurrently.)
c906108c 5430
c5aa993b
JM
5431 Each element of the chain has valid next, breakpoint_at,
5432 commands, FIXME??? fields. */
c906108c
SS
5433
5434bpstat
6c95b8df 5435bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5436 CORE_ADDR bp_addr, ptid_t ptid,
5437 const struct target_waitstatus *ws)
c906108c 5438{
0d381245 5439 struct breakpoint *b = NULL;
afe38095 5440 struct bp_location *bl;
20874c92 5441 struct bp_location *loc;
5760d0ab
JK
5442 /* First item of allocated bpstat's. */
5443 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5444 /* Pointer to the last thing in the chain currently. */
5760d0ab 5445 bpstat bs;
20874c92 5446 int ix;
429374b8 5447 int need_remove_insert;
f431efe5 5448 int removed_any;
c906108c 5449
f431efe5
PA
5450 /* First, build the bpstat chain with locations that explain a
5451 target stop, while being careful to not set the target running,
5452 as that may invalidate locations (in particular watchpoint
5453 locations are recreated). Resuming will happen here with
5454 breakpoint conditions or watchpoint expressions that include
5455 inferior function calls. */
c5aa993b 5456
429374b8
JK
5457 ALL_BREAKPOINTS (b)
5458 {
5459 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5460 continue;
a5606eee 5461
429374b8
JK
5462 for (bl = b->loc; bl != NULL; bl = bl->next)
5463 {
4a64f543
MS
5464 /* For hardware watchpoints, we look only at the first
5465 location. The watchpoint_check function will work on the
5466 entire expression, not the individual locations. For
5467 read watchpoints, the watchpoints_triggered function has
5468 checked all locations already. */
429374b8
JK
5469 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5470 break;
18a18393 5471
f6592439 5472 if (!bl->enabled || bl->shlib_disabled)
429374b8 5473 continue;
c5aa993b 5474
09ac7c10 5475 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5476 continue;
c5aa993b 5477
4a64f543
MS
5478 /* Come here if it's a watchpoint, or if the break address
5479 matches. */
c5aa993b 5480
4a64f543
MS
5481 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5482 explain stop. */
c5aa993b 5483
f431efe5
PA
5484 /* Assume we stop. Should we find a watchpoint that is not
5485 actually triggered, or if the condition of the breakpoint
5486 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5487 bs->stop = 1;
5488 bs->print = 1;
d983da9c 5489
f431efe5
PA
5490 /* If this is a scope breakpoint, mark the associated
5491 watchpoint as triggered so that we will handle the
5492 out-of-scope event. We'll get to the watchpoint next
5493 iteration. */
d0fb5eae 5494 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5495 {
5496 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5497
5498 w->watchpoint_triggered = watch_triggered_yes;
5499 }
f431efe5
PA
5500 }
5501 }
5502
5503 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5504 {
f1310107 5505 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5506 {
5760d0ab 5507 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5508 /* For hits of moribund locations, we should just proceed. */
5509 bs->stop = 0;
5510 bs->print = 0;
5511 bs->print_it = print_it_noop;
5512 }
5513 }
5514
edcc5120
TT
5515 /* A bit of special processing for shlib breakpoints. We need to
5516 process solib loading here, so that the lists of loaded and
5517 unloaded libraries are correct before we handle "catch load" and
5518 "catch unload". */
5519 for (bs = bs_head; bs != NULL; bs = bs->next)
5520 {
5d268276 5521 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5522 {
5523 handle_solib_event ();
5524 break;
5525 }
5526 }
5527
f431efe5
PA
5528 /* Now go through the locations that caused the target to stop, and
5529 check whether we're interested in reporting this stop to higher
5530 layers, or whether we should resume the target transparently. */
5531
5532 removed_any = 0;
5533
5760d0ab 5534 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5535 {
5536 if (!bs->stop)
5537 continue;
5538
f431efe5 5539 b = bs->breakpoint_at;
348d480f
PA
5540 b->ops->check_status (bs);
5541 if (bs->stop)
28010a5d 5542 {
348d480f 5543 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5544
429374b8
JK
5545 if (bs->stop)
5546 {
5547 ++(b->hit_count);
8d3788bd 5548 observer_notify_breakpoint_modified (b);
c906108c 5549
4a64f543 5550 /* We will stop here. */
429374b8
JK
5551 if (b->disposition == disp_disable)
5552 {
816338b5
SS
5553 --(b->enable_count);
5554 if (b->enable_count <= 0
5555 && b->enable_state != bp_permanent)
429374b8 5556 b->enable_state = bp_disabled;
f431efe5 5557 removed_any = 1;
429374b8
JK
5558 }
5559 if (b->silent)
5560 bs->print = 0;
5561 bs->commands = b->commands;
9add0f1b 5562 incref_counted_command_line (bs->commands);
abf85f46
JK
5563 if (command_line_is_silent (bs->commands
5564 ? bs->commands->commands : NULL))
5565 bs->print = 0;
9d6e6e84
HZ
5566
5567 b->ops->after_condition_true (bs);
429374b8
JK
5568 }
5569
348d480f 5570 }
a9b3a50f
PA
5571
5572 /* Print nothing for this entry if we don't stop or don't
5573 print. */
5574 if (!bs->stop || !bs->print)
5575 bs->print_it = print_it_noop;
429374b8 5576 }
876fa593 5577
d983da9c
DJ
5578 /* If we aren't stopping, the value of some hardware watchpoint may
5579 not have changed, but the intermediate memory locations we are
5580 watching may have. Don't bother if we're stopping; this will get
5581 done later. */
d832cb68 5582 need_remove_insert = 0;
5760d0ab
JK
5583 if (! bpstat_causes_stop (bs_head))
5584 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5585 if (!bs->stop
f431efe5
PA
5586 && bs->breakpoint_at
5587 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5588 {
3a5c3e22
PA
5589 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5590
5591 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5592 need_remove_insert = 1;
d983da9c
DJ
5593 }
5594
d832cb68 5595 if (need_remove_insert)
2d134ed3 5596 update_global_location_list (1);
f431efe5
PA
5597 else if (removed_any)
5598 update_global_location_list (0);
d832cb68 5599
5760d0ab 5600 return bs_head;
c906108c 5601}
628fe4e4
JK
5602
5603static void
5604handle_jit_event (void)
5605{
5606 struct frame_info *frame;
5607 struct gdbarch *gdbarch;
5608
5609 /* Switch terminal for any messages produced by
5610 breakpoint_re_set. */
5611 target_terminal_ours_for_output ();
5612
5613 frame = get_current_frame ();
5614 gdbarch = get_frame_arch (frame);
5615
5616 jit_event_handler (gdbarch);
5617
5618 target_terminal_inferior ();
5619}
5620
5621/* Prepare WHAT final decision for infrun. */
5622
5623/* Decide what infrun needs to do with this bpstat. */
5624
c906108c 5625struct bpstat_what
0e30163f 5626bpstat_what (bpstat bs_head)
c906108c 5627{
c906108c 5628 struct bpstat_what retval;
628fe4e4 5629 int jit_event = 0;
0e30163f 5630 bpstat bs;
c906108c 5631
628fe4e4 5632 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5633 retval.call_dummy = STOP_NONE;
186c406b 5634 retval.is_longjmp = 0;
628fe4e4 5635
0e30163f 5636 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5637 {
628fe4e4
JK
5638 /* Extract this BS's action. After processing each BS, we check
5639 if its action overrides all we've seem so far. */
5640 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5641 enum bptype bptype;
5642
c906108c 5643 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5644 {
5645 /* I suspect this can happen if it was a momentary
5646 breakpoint which has since been deleted. */
5647 bptype = bp_none;
5648 }
20874c92 5649 else
f431efe5 5650 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5651
5652 switch (bptype)
c906108c
SS
5653 {
5654 case bp_none:
628fe4e4 5655 break;
c906108c
SS
5656 case bp_breakpoint:
5657 case bp_hardware_breakpoint:
5658 case bp_until:
5659 case bp_finish:
a9b3a50f 5660 case bp_shlib_event:
c906108c
SS
5661 if (bs->stop)
5662 {
5663 if (bs->print)
628fe4e4 5664 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5665 else
628fe4e4 5666 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5667 }
5668 else
628fe4e4 5669 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5670 break;
5671 case bp_watchpoint:
5672 case bp_hardware_watchpoint:
5673 case bp_read_watchpoint:
5674 case bp_access_watchpoint:
5675 if (bs->stop)
5676 {
5677 if (bs->print)
628fe4e4 5678 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5679 else
628fe4e4 5680 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5681 }
5682 else
628fe4e4
JK
5683 {
5684 /* There was a watchpoint, but we're not stopping.
5685 This requires no further action. */
5686 }
c906108c
SS
5687 break;
5688 case bp_longjmp:
e2e4d78b 5689 case bp_longjmp_call_dummy:
186c406b 5690 case bp_exception:
628fe4e4 5691 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5692 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5693 break;
5694 case bp_longjmp_resume:
186c406b 5695 case bp_exception_resume:
628fe4e4 5696 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5697 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5698 break;
5699 case bp_step_resume:
5700 if (bs->stop)
628fe4e4
JK
5701 this_action = BPSTAT_WHAT_STEP_RESUME;
5702 else
c906108c 5703 {
628fe4e4
JK
5704 /* It is for the wrong frame. */
5705 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5706 }
c906108c 5707 break;
2c03e5be
PA
5708 case bp_hp_step_resume:
5709 if (bs->stop)
5710 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5711 else
5712 {
5713 /* It is for the wrong frame. */
5714 this_action = BPSTAT_WHAT_SINGLE;
5715 }
5716 break;
c906108c 5717 case bp_watchpoint_scope:
c4093a6a 5718 case bp_thread_event:
1900040c 5719 case bp_overlay_event:
0fd8e87f 5720 case bp_longjmp_master:
aa7d318d 5721 case bp_std_terminate_master:
186c406b 5722 case bp_exception_master:
628fe4e4 5723 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5724 break;
ce78b96d 5725 case bp_catchpoint:
c5aa993b
JM
5726 if (bs->stop)
5727 {
5728 if (bs->print)
628fe4e4 5729 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5730 else
628fe4e4 5731 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5732 }
5733 else
628fe4e4
JK
5734 {
5735 /* There was a catchpoint, but we're not stopping.
5736 This requires no further action. */
5737 }
5738 break;
628fe4e4
JK
5739 case bp_jit_event:
5740 jit_event = 1;
5741 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5742 break;
c906108c 5743 case bp_call_dummy:
53a5351d
JM
5744 /* Make sure the action is stop (silent or noisy),
5745 so infrun.c pops the dummy frame. */
aa7d318d 5746 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5747 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5748 break;
5749 case bp_std_terminate:
5750 /* Make sure the action is stop (silent or noisy),
5751 so infrun.c pops the dummy frame. */
aa7d318d 5752 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5753 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5754 break;
1042e4c0 5755 case bp_tracepoint:
7a697b8d 5756 case bp_fast_tracepoint:
0fb4aa4b 5757 case bp_static_tracepoint:
1042e4c0
SS
5758 /* Tracepoint hits should not be reported back to GDB, and
5759 if one got through somehow, it should have been filtered
5760 out already. */
5761 internal_error (__FILE__, __LINE__,
7a697b8d 5762 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5763 break;
5764 case bp_gnu_ifunc_resolver:
5765 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5766 this_action = BPSTAT_WHAT_SINGLE;
5767 break;
5768 case bp_gnu_ifunc_resolver_return:
5769 /* The breakpoint will be removed, execution will restart from the
5770 PC of the former breakpoint. */
5771 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5772 break;
e7e0cddf
SS
5773
5774 case bp_dprintf:
a11cfd87
HZ
5775 if (bs->stop)
5776 this_action = BPSTAT_WHAT_STOP_SILENT;
5777 else
5778 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5779 break;
5780
628fe4e4
JK
5781 default:
5782 internal_error (__FILE__, __LINE__,
5783 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5784 }
628fe4e4
JK
5785
5786 retval.main_action = max (retval.main_action, this_action);
c906108c 5787 }
628fe4e4 5788
0e30163f
JK
5789 /* These operations may affect the bs->breakpoint_at state so they are
5790 delayed after MAIN_ACTION is decided above. */
5791
628fe4e4
JK
5792 if (jit_event)
5793 {
5794 if (debug_infrun)
5795 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5796
5797 handle_jit_event ();
5798 }
5799
0e30163f
JK
5800 for (bs = bs_head; bs != NULL; bs = bs->next)
5801 {
5802 struct breakpoint *b = bs->breakpoint_at;
5803
5804 if (b == NULL)
5805 continue;
5806 switch (b->type)
5807 {
5808 case bp_gnu_ifunc_resolver:
5809 gnu_ifunc_resolver_stop (b);
5810 break;
5811 case bp_gnu_ifunc_resolver_return:
5812 gnu_ifunc_resolver_return_stop (b);
5813 break;
5814 }
5815 }
5816
c906108c
SS
5817 return retval;
5818}
5819
5820/* Nonzero if we should step constantly (e.g. watchpoints on machines
5821 without hardware support). This isn't related to a specific bpstat,
5822 just to things like whether watchpoints are set. */
5823
c5aa993b 5824int
fba45db2 5825bpstat_should_step (void)
c906108c
SS
5826{
5827 struct breakpoint *b;
cc59ec59 5828
c906108c 5829 ALL_BREAKPOINTS (b)
717a8278 5830 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5831 return 1;
c906108c
SS
5832 return 0;
5833}
5834
67822962
PA
5835int
5836bpstat_causes_stop (bpstat bs)
5837{
5838 for (; bs != NULL; bs = bs->next)
5839 if (bs->stop)
5840 return 1;
5841
5842 return 0;
5843}
5844
c906108c 5845\f
c5aa993b 5846
170b53b2
UW
5847/* Compute a string of spaces suitable to indent the next line
5848 so it starts at the position corresponding to the table column
5849 named COL_NAME in the currently active table of UIOUT. */
5850
5851static char *
5852wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5853{
5854 static char wrap_indent[80];
5855 int i, total_width, width, align;
5856 char *text;
5857
5858 total_width = 0;
5859 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5860 {
5861 if (strcmp (text, col_name) == 0)
5862 {
5863 gdb_assert (total_width < sizeof wrap_indent);
5864 memset (wrap_indent, ' ', total_width);
5865 wrap_indent[total_width] = 0;
5866
5867 return wrap_indent;
5868 }
5869
5870 total_width += width + 1;
5871 }
5872
5873 return NULL;
5874}
5875
b775012e
LM
5876/* Determine if the locations of this breakpoint will have their conditions
5877 evaluated by the target, host or a mix of both. Returns the following:
5878
5879 "host": Host evals condition.
5880 "host or target": Host or Target evals condition.
5881 "target": Target evals condition.
5882*/
5883
5884static const char *
5885bp_condition_evaluator (struct breakpoint *b)
5886{
5887 struct bp_location *bl;
5888 char host_evals = 0;
5889 char target_evals = 0;
5890
5891 if (!b)
5892 return NULL;
5893
5894 if (!is_breakpoint (b))
5895 return NULL;
5896
5897 if (gdb_evaluates_breakpoint_condition_p ()
5898 || !target_supports_evaluation_of_breakpoint_conditions ())
5899 return condition_evaluation_host;
5900
5901 for (bl = b->loc; bl; bl = bl->next)
5902 {
5903 if (bl->cond_bytecode)
5904 target_evals++;
5905 else
5906 host_evals++;
5907 }
5908
5909 if (host_evals && target_evals)
5910 return condition_evaluation_both;
5911 else if (target_evals)
5912 return condition_evaluation_target;
5913 else
5914 return condition_evaluation_host;
5915}
5916
5917/* Determine the breakpoint location's condition evaluator. This is
5918 similar to bp_condition_evaluator, but for locations. */
5919
5920static const char *
5921bp_location_condition_evaluator (struct bp_location *bl)
5922{
5923 if (bl && !is_breakpoint (bl->owner))
5924 return NULL;
5925
5926 if (gdb_evaluates_breakpoint_condition_p ()
5927 || !target_supports_evaluation_of_breakpoint_conditions ())
5928 return condition_evaluation_host;
5929
5930 if (bl && bl->cond_bytecode)
5931 return condition_evaluation_target;
5932 else
5933 return condition_evaluation_host;
5934}
5935
859825b8
JK
5936/* Print the LOC location out of the list of B->LOC locations. */
5937
170b53b2
UW
5938static void
5939print_breakpoint_location (struct breakpoint *b,
5940 struct bp_location *loc)
0d381245 5941{
79a45e25 5942 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5943 struct cleanup *old_chain = save_current_program_space ();
5944
859825b8
JK
5945 if (loc != NULL && loc->shlib_disabled)
5946 loc = NULL;
5947
6c95b8df
PA
5948 if (loc != NULL)
5949 set_current_program_space (loc->pspace);
5950
56435ebe
TT
5951 if (b->display_canonical)
5952 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 5953 else if (loc && loc->symtab)
0d381245
VP
5954 {
5955 struct symbol *sym
5956 = find_pc_sect_function (loc->address, loc->section);
5957 if (sym)
5958 {
5959 ui_out_text (uiout, "in ");
5960 ui_out_field_string (uiout, "func",
5961 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
5962 ui_out_text (uiout, " ");
5963 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5964 ui_out_text (uiout, "at ");
0d381245 5965 }
05cba821
JK
5966 ui_out_field_string (uiout, "file",
5967 symtab_to_filename_for_display (loc->symtab));
0d381245 5968 ui_out_text (uiout, ":");
05cba821 5969
0d381245 5970 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
5971 ui_out_field_string (uiout, "fullname",
5972 symtab_to_fullname (loc->symtab));
0d381245 5973
f8eba3c6 5974 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 5975 }
859825b8 5976 else if (loc)
0d381245 5977 {
f99d8bf4
PA
5978 struct ui_file *stb = mem_fileopen ();
5979 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 5980
f99d8bf4 5981 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 5982 demangle, "");
0d381245 5983 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
5984
5985 do_cleanups (stb_chain);
0d381245 5986 }
859825b8
JK
5987 else
5988 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 5989
b775012e
LM
5990 if (loc && is_breakpoint (b)
5991 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5992 && bp_condition_evaluator (b) == condition_evaluation_both)
5993 {
5994 ui_out_text (uiout, " (");
5995 ui_out_field_string (uiout, "evaluated-by",
5996 bp_location_condition_evaluator (loc));
5997 ui_out_text (uiout, ")");
5998 }
5999
6c95b8df 6000 do_cleanups (old_chain);
0d381245
VP
6001}
6002
269b11a2
PA
6003static const char *
6004bptype_string (enum bptype type)
c906108c 6005{
c4093a6a
JM
6006 struct ep_type_description
6007 {
6008 enum bptype type;
6009 char *description;
6010 };
6011 static struct ep_type_description bptypes[] =
c906108c 6012 {
c5aa993b
JM
6013 {bp_none, "?deleted?"},
6014 {bp_breakpoint, "breakpoint"},
c906108c 6015 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
6016 {bp_until, "until"},
6017 {bp_finish, "finish"},
6018 {bp_watchpoint, "watchpoint"},
c906108c 6019 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6020 {bp_read_watchpoint, "read watchpoint"},
6021 {bp_access_watchpoint, "acc watchpoint"},
6022 {bp_longjmp, "longjmp"},
6023 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6024 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6025 {bp_exception, "exception"},
6026 {bp_exception_resume, "exception resume"},
c5aa993b 6027 {bp_step_resume, "step resume"},
2c03e5be 6028 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6029 {bp_watchpoint_scope, "watchpoint scope"},
6030 {bp_call_dummy, "call dummy"},
aa7d318d 6031 {bp_std_terminate, "std::terminate"},
c5aa993b 6032 {bp_shlib_event, "shlib events"},
c4093a6a 6033 {bp_thread_event, "thread events"},
1900040c 6034 {bp_overlay_event, "overlay events"},
0fd8e87f 6035 {bp_longjmp_master, "longjmp master"},
aa7d318d 6036 {bp_std_terminate_master, "std::terminate master"},
186c406b 6037 {bp_exception_master, "exception master"},
ce78b96d 6038 {bp_catchpoint, "catchpoint"},
1042e4c0 6039 {bp_tracepoint, "tracepoint"},
7a697b8d 6040 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6041 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6042 {bp_dprintf, "dprintf"},
4efc6507 6043 {bp_jit_event, "jit events"},
0e30163f
JK
6044 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6045 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6046 };
269b11a2
PA
6047
6048 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6049 || ((int) type != bptypes[(int) type].type))
6050 internal_error (__FILE__, __LINE__,
6051 _("bptypes table does not describe type #%d."),
6052 (int) type);
6053
6054 return bptypes[(int) type].description;
6055}
6056
998580f1
MK
6057/* For MI, output a field named 'thread-groups' with a list as the value.
6058 For CLI, prefix the list with the string 'inf'. */
6059
6060static void
6061output_thread_groups (struct ui_out *uiout,
6062 const char *field_name,
6063 VEC(int) *inf_num,
6064 int mi_only)
6065{
752eb8b4 6066 struct cleanup *back_to;
998580f1
MK
6067 int is_mi = ui_out_is_mi_like_p (uiout);
6068 int inf;
6069 int i;
6070
6071 /* For backward compatibility, don't display inferiors in CLI unless
6072 there are several. Always display them for MI. */
6073 if (!is_mi && mi_only)
6074 return;
6075
752eb8b4
TT
6076 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6077
998580f1
MK
6078 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6079 {
6080 if (is_mi)
6081 {
6082 char mi_group[10];
6083
6084 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6085 ui_out_field_string (uiout, NULL, mi_group);
6086 }
6087 else
6088 {
6089 if (i == 0)
6090 ui_out_text (uiout, " inf ");
6091 else
6092 ui_out_text (uiout, ", ");
6093
6094 ui_out_text (uiout, plongest (inf));
6095 }
6096 }
6097
6098 do_cleanups (back_to);
6099}
6100
269b11a2
PA
6101/* Print B to gdb_stdout. */
6102
6103static void
6104print_one_breakpoint_location (struct breakpoint *b,
6105 struct bp_location *loc,
6106 int loc_number,
6107 struct bp_location **last_loc,
269b11a2
PA
6108 int allflag)
6109{
6110 struct command_line *l;
c2c6d25f 6111 static char bpenables[] = "nynny";
c906108c 6112
79a45e25 6113 struct ui_out *uiout = current_uiout;
0d381245
VP
6114 int header_of_multiple = 0;
6115 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6116 struct value_print_options opts;
6117
6118 get_user_print_options (&opts);
0d381245
VP
6119
6120 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6121 /* See comment in print_one_breakpoint concerning treatment of
6122 breakpoints with single disabled location. */
0d381245
VP
6123 if (loc == NULL
6124 && (b->loc != NULL
6125 && (b->loc->next != NULL || !b->loc->enabled)))
6126 header_of_multiple = 1;
6127 if (loc == NULL)
6128 loc = b->loc;
6129
c4093a6a
JM
6130 annotate_record ();
6131
6132 /* 1 */
6133 annotate_field (0);
0d381245
VP
6134 if (part_of_multiple)
6135 {
6136 char *formatted;
0c6773c1 6137 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6138 ui_out_field_string (uiout, "number", formatted);
6139 xfree (formatted);
6140 }
6141 else
6142 {
6143 ui_out_field_int (uiout, "number", b->number);
6144 }
c4093a6a
JM
6145
6146 /* 2 */
6147 annotate_field (1);
0d381245
VP
6148 if (part_of_multiple)
6149 ui_out_field_skip (uiout, "type");
269b11a2
PA
6150 else
6151 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6152
6153 /* 3 */
6154 annotate_field (2);
0d381245
VP
6155 if (part_of_multiple)
6156 ui_out_field_skip (uiout, "disp");
6157 else
2cec12e5 6158 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6159
c4093a6a
JM
6160
6161 /* 4 */
6162 annotate_field (3);
0d381245 6163 if (part_of_multiple)
54e52265 6164 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6165 else
4a64f543
MS
6166 ui_out_field_fmt (uiout, "enabled", "%c",
6167 bpenables[(int) b->enable_state]);
54e52265 6168 ui_out_spaces (uiout, 2);
0d381245 6169
c4093a6a
JM
6170
6171 /* 5 and 6 */
3086aeae 6172 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6173 {
4a64f543
MS
6174 /* Although the print_one can possibly print all locations,
6175 calling it here is not likely to get any nice result. So,
6176 make sure there's just one location. */
0d381245 6177 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6178 b->ops->print_one (b, last_loc);
0d381245 6179 }
3086aeae
DJ
6180 else
6181 switch (b->type)
6182 {
6183 case bp_none:
6184 internal_error (__FILE__, __LINE__,
e2e0b3e5 6185 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6186 break;
c906108c 6187
3086aeae
DJ
6188 case bp_watchpoint:
6189 case bp_hardware_watchpoint:
6190 case bp_read_watchpoint:
6191 case bp_access_watchpoint:
3a5c3e22
PA
6192 {
6193 struct watchpoint *w = (struct watchpoint *) b;
6194
6195 /* Field 4, the address, is omitted (which makes the columns
6196 not line up too nicely with the headers, but the effect
6197 is relatively readable). */
6198 if (opts.addressprint)
6199 ui_out_field_skip (uiout, "addr");
6200 annotate_field (5);
6201 ui_out_field_string (uiout, "what", w->exp_string);
6202 }
3086aeae
DJ
6203 break;
6204
3086aeae
DJ
6205 case bp_breakpoint:
6206 case bp_hardware_breakpoint:
6207 case bp_until:
6208 case bp_finish:
6209 case bp_longjmp:
6210 case bp_longjmp_resume:
e2e4d78b 6211 case bp_longjmp_call_dummy:
186c406b
TT
6212 case bp_exception:
6213 case bp_exception_resume:
3086aeae 6214 case bp_step_resume:
2c03e5be 6215 case bp_hp_step_resume:
3086aeae
DJ
6216 case bp_watchpoint_scope:
6217 case bp_call_dummy:
aa7d318d 6218 case bp_std_terminate:
3086aeae
DJ
6219 case bp_shlib_event:
6220 case bp_thread_event:
6221 case bp_overlay_event:
0fd8e87f 6222 case bp_longjmp_master:
aa7d318d 6223 case bp_std_terminate_master:
186c406b 6224 case bp_exception_master:
1042e4c0 6225 case bp_tracepoint:
7a697b8d 6226 case bp_fast_tracepoint:
0fb4aa4b 6227 case bp_static_tracepoint:
e7e0cddf 6228 case bp_dprintf:
4efc6507 6229 case bp_jit_event:
0e30163f
JK
6230 case bp_gnu_ifunc_resolver:
6231 case bp_gnu_ifunc_resolver_return:
79a45b7d 6232 if (opts.addressprint)
3086aeae
DJ
6233 {
6234 annotate_field (4);
54e52265 6235 if (header_of_multiple)
0d381245 6236 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6237 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6238 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6239 else
5af949e3
UW
6240 ui_out_field_core_addr (uiout, "addr",
6241 loc->gdbarch, loc->address);
3086aeae
DJ
6242 }
6243 annotate_field (5);
0d381245 6244 if (!header_of_multiple)
170b53b2 6245 print_breakpoint_location (b, loc);
0d381245 6246 if (b->loc)
a6d9a66e 6247 *last_loc = b->loc;
3086aeae
DJ
6248 break;
6249 }
c906108c 6250
6c95b8df 6251
998580f1 6252 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6253 {
6254 struct inferior *inf;
998580f1
MK
6255 VEC(int) *inf_num = NULL;
6256 int mi_only = 1;
6c95b8df 6257
998580f1 6258 ALL_INFERIORS (inf)
6c95b8df
PA
6259 {
6260 if (inf->pspace == loc->pspace)
998580f1 6261 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6262 }
998580f1
MK
6263
6264 /* For backward compatibility, don't display inferiors in CLI unless
6265 there are several. Always display for MI. */
6266 if (allflag
6267 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6268 && (number_of_program_spaces () > 1
6269 || number_of_inferiors () > 1)
6270 /* LOC is for existing B, it cannot be in
6271 moribund_locations and thus having NULL OWNER. */
6272 && loc->owner->type != bp_catchpoint))
6273 mi_only = 0;
6274 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6275 VEC_free (int, inf_num);
6c95b8df
PA
6276 }
6277
4a306c9a 6278 if (!part_of_multiple)
c4093a6a 6279 {
4a306c9a
JB
6280 if (b->thread != -1)
6281 {
6282 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6283 "stop only in" line a little further down. */
4a306c9a
JB
6284 ui_out_text (uiout, " thread ");
6285 ui_out_field_int (uiout, "thread", b->thread);
6286 }
6287 else if (b->task != 0)
6288 {
6289 ui_out_text (uiout, " task ");
6290 ui_out_field_int (uiout, "task", b->task);
6291 }
c4093a6a 6292 }
f1310107 6293
8b93c638 6294 ui_out_text (uiout, "\n");
f1310107 6295
348d480f 6296 if (!part_of_multiple)
f1310107
TJB
6297 b->ops->print_one_detail (b, uiout);
6298
0d381245 6299 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6300 {
6301 annotate_field (6);
8b93c638 6302 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6303 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6304 the frame ID. */
5af949e3
UW
6305 ui_out_field_core_addr (uiout, "frame",
6306 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6307 ui_out_text (uiout, "\n");
c4093a6a
JM
6308 }
6309
28010a5d 6310 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6311 {
6312 annotate_field (7);
d77f58be 6313 if (is_tracepoint (b))
1042e4c0
SS
6314 ui_out_text (uiout, "\ttrace only if ");
6315 else
6316 ui_out_text (uiout, "\tstop only if ");
0101ce28 6317 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6318
6319 /* Print whether the target is doing the breakpoint's condition
6320 evaluation. If GDB is doing the evaluation, don't print anything. */
6321 if (is_breakpoint (b)
6322 && breakpoint_condition_evaluation_mode ()
6323 == condition_evaluation_target)
6324 {
6325 ui_out_text (uiout, " (");
6326 ui_out_field_string (uiout, "evaluated-by",
6327 bp_condition_evaluator (b));
6328 ui_out_text (uiout, " evals)");
6329 }
0101ce28
JJ
6330 ui_out_text (uiout, "\n");
6331 }
6332
0d381245 6333 if (!part_of_multiple && b->thread != -1)
c4093a6a 6334 {
4a64f543 6335 /* FIXME should make an annotation for this. */
8b93c638
JM
6336 ui_out_text (uiout, "\tstop only in thread ");
6337 ui_out_field_int (uiout, "thread", b->thread);
6338 ui_out_text (uiout, "\n");
c4093a6a
JM
6339 }
6340
556ec64d
YQ
6341 if (!part_of_multiple)
6342 {
6343 if (b->hit_count)
31f56a27
YQ
6344 {
6345 /* FIXME should make an annotation for this. */
6346 if (is_catchpoint (b))
6347 ui_out_text (uiout, "\tcatchpoint");
6348 else if (is_tracepoint (b))
6349 ui_out_text (uiout, "\ttracepoint");
6350 else
6351 ui_out_text (uiout, "\tbreakpoint");
6352 ui_out_text (uiout, " already hit ");
6353 ui_out_field_int (uiout, "times", b->hit_count);
6354 if (b->hit_count == 1)
6355 ui_out_text (uiout, " time\n");
6356 else
6357 ui_out_text (uiout, " times\n");
6358 }
556ec64d
YQ
6359 else
6360 {
31f56a27
YQ
6361 /* Output the count also if it is zero, but only if this is mi. */
6362 if (ui_out_is_mi_like_p (uiout))
6363 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6364 }
6365 }
8b93c638 6366
0d381245 6367 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6368 {
6369 annotate_field (8);
8b93c638
JM
6370 ui_out_text (uiout, "\tignore next ");
6371 ui_out_field_int (uiout, "ignore", b->ignore_count);
6372 ui_out_text (uiout, " hits\n");
c4093a6a 6373 }
059fb39f 6374
816338b5
SS
6375 /* Note that an enable count of 1 corresponds to "enable once"
6376 behavior, which is reported by the combination of enablement and
6377 disposition, so we don't need to mention it here. */
6378 if (!part_of_multiple && b->enable_count > 1)
6379 {
6380 annotate_field (8);
6381 ui_out_text (uiout, "\tdisable after ");
6382 /* Tweak the wording to clarify that ignore and enable counts
6383 are distinct, and have additive effect. */
6384 if (b->ignore_count)
6385 ui_out_text (uiout, "additional ");
6386 else
6387 ui_out_text (uiout, "next ");
6388 ui_out_field_int (uiout, "enable", b->enable_count);
6389 ui_out_text (uiout, " hits\n");
6390 }
6391
f196051f
SS
6392 if (!part_of_multiple && is_tracepoint (b))
6393 {
6394 struct tracepoint *tp = (struct tracepoint *) b;
6395
6396 if (tp->traceframe_usage)
6397 {
6398 ui_out_text (uiout, "\ttrace buffer usage ");
6399 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6400 ui_out_text (uiout, " bytes\n");
6401 }
6402 }
d3ce09f5 6403
9add0f1b 6404 l = b->commands ? b->commands->commands : NULL;
059fb39f 6405 if (!part_of_multiple && l)
c4093a6a 6406 {
3b31d625
EZ
6407 struct cleanup *script_chain;
6408
c4093a6a 6409 annotate_field (9);
3b31d625 6410 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6411 print_command_lines (uiout, l, 4);
3b31d625 6412 do_cleanups (script_chain);
c4093a6a 6413 }
d24317b4 6414
d9b3f62e 6415 if (is_tracepoint (b))
1042e4c0 6416 {
d9b3f62e
PA
6417 struct tracepoint *t = (struct tracepoint *) b;
6418
6419 if (!part_of_multiple && t->pass_count)
6420 {
6421 annotate_field (10);
6422 ui_out_text (uiout, "\tpass count ");
6423 ui_out_field_int (uiout, "pass", t->pass_count);
6424 ui_out_text (uiout, " \n");
6425 }
f2a8bc8a
YQ
6426
6427 /* Don't display it when tracepoint or tracepoint location is
6428 pending. */
6429 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6430 {
6431 annotate_field (11);
6432
6433 if (ui_out_is_mi_like_p (uiout))
6434 ui_out_field_string (uiout, "installed",
6435 loc->inserted ? "y" : "n");
6436 else
6437 {
6438 if (loc->inserted)
6439 ui_out_text (uiout, "\t");
6440 else
6441 ui_out_text (uiout, "\tnot ");
6442 ui_out_text (uiout, "installed on target\n");
6443 }
6444 }
1042e4c0
SS
6445 }
6446
d24317b4
VP
6447 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6448 {
3a5c3e22
PA
6449 if (is_watchpoint (b))
6450 {
6451 struct watchpoint *w = (struct watchpoint *) b;
6452
6453 ui_out_field_string (uiout, "original-location", w->exp_string);
6454 }
6455 else if (b->addr_string)
d24317b4 6456 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6457 }
c4093a6a 6458}
c5aa993b 6459
0d381245
VP
6460static void
6461print_one_breakpoint (struct breakpoint *b,
4a64f543 6462 struct bp_location **last_loc,
6c95b8df 6463 int allflag)
0d381245 6464{
8d3788bd 6465 struct cleanup *bkpt_chain;
79a45e25 6466 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6467
6468 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6469
12c5a436 6470 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6471 do_cleanups (bkpt_chain);
0d381245
VP
6472
6473 /* If this breakpoint has custom print function,
6474 it's already printed. Otherwise, print individual
6475 locations, if any. */
6476 if (b->ops == NULL || b->ops->print_one == NULL)
6477 {
4a64f543
MS
6478 /* If breakpoint has a single location that is disabled, we
6479 print it as if it had several locations, since otherwise it's
6480 hard to represent "breakpoint enabled, location disabled"
6481 situation.
6482
6483 Note that while hardware watchpoints have several locations
a3be7890 6484 internally, that's not a property exposed to user. */
0d381245 6485 if (b->loc
a5606eee 6486 && !is_hardware_watchpoint (b)
8d3788bd 6487 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6488 {
6489 struct bp_location *loc;
6490 int n = 1;
8d3788bd 6491
0d381245 6492 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6493 {
6494 struct cleanup *inner2 =
6495 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6496 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6497 do_cleanups (inner2);
6498 }
0d381245
VP
6499 }
6500 }
6501}
6502
a6d9a66e
UW
6503static int
6504breakpoint_address_bits (struct breakpoint *b)
6505{
6506 int print_address_bits = 0;
6507 struct bp_location *loc;
6508
6509 for (loc = b->loc; loc; loc = loc->next)
6510 {
c7437ca6
PA
6511 int addr_bit;
6512
6513 /* Software watchpoints that aren't watching memory don't have
6514 an address to print. */
6515 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6516 continue;
6517
6518 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6519 if (addr_bit > print_address_bits)
6520 print_address_bits = addr_bit;
6521 }
6522
6523 return print_address_bits;
6524}
0d381245 6525
c4093a6a
JM
6526struct captured_breakpoint_query_args
6527 {
6528 int bnum;
6529 };
c5aa993b 6530
c4093a6a 6531static int
2b65245e 6532do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6533{
6534 struct captured_breakpoint_query_args *args = data;
52f0bd74 6535 struct breakpoint *b;
a6d9a66e 6536 struct bp_location *dummy_loc = NULL;
cc59ec59 6537
c4093a6a
JM
6538 ALL_BREAKPOINTS (b)
6539 {
6540 if (args->bnum == b->number)
c5aa993b 6541 {
12c5a436 6542 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6543 return GDB_RC_OK;
c5aa993b 6544 }
c4093a6a
JM
6545 }
6546 return GDB_RC_NONE;
6547}
c5aa993b 6548
c4093a6a 6549enum gdb_rc
4a64f543
MS
6550gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6551 char **error_message)
c4093a6a
JM
6552{
6553 struct captured_breakpoint_query_args args;
cc59ec59 6554
c4093a6a
JM
6555 args.bnum = bnum;
6556 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6557 an error. */
b0b13bb4
DJ
6558 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6559 error_message, RETURN_MASK_ALL) < 0)
6560 return GDB_RC_FAIL;
6561 else
6562 return GDB_RC_OK;
c4093a6a 6563}
c5aa993b 6564
09d682a4
TT
6565/* Return true if this breakpoint was set by the user, false if it is
6566 internal or momentary. */
6567
6568int
6569user_breakpoint_p (struct breakpoint *b)
6570{
46c6471b 6571 return b->number > 0;
09d682a4
TT
6572}
6573
7f3b0473 6574/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6575 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6576 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6577 FILTER is non-NULL, call it on each breakpoint and only include the
6578 ones for which it returns non-zero. Return the total number of
6579 breakpoints listed. */
c906108c 6580
d77f58be 6581static int
e5a67952 6582breakpoint_1 (char *args, int allflag,
4a64f543 6583 int (*filter) (const struct breakpoint *))
c4093a6a 6584{
52f0bd74 6585 struct breakpoint *b;
a6d9a66e 6586 struct bp_location *last_loc = NULL;
7f3b0473 6587 int nr_printable_breakpoints;
3b31d625 6588 struct cleanup *bkpttbl_chain;
79a45b7d 6589 struct value_print_options opts;
a6d9a66e 6590 int print_address_bits = 0;
269b11a2 6591 int print_type_col_width = 14;
79a45e25 6592 struct ui_out *uiout = current_uiout;
269b11a2 6593
79a45b7d
TT
6594 get_user_print_options (&opts);
6595
4a64f543
MS
6596 /* Compute the number of rows in the table, as well as the size
6597 required for address fields. */
7f3b0473
AC
6598 nr_printable_breakpoints = 0;
6599 ALL_BREAKPOINTS (b)
e5a67952
MS
6600 {
6601 /* If we have a filter, only list the breakpoints it accepts. */
6602 if (filter && !filter (b))
6603 continue;
6604
6605 /* If we have an "args" string, it is a list of breakpoints to
6606 accept. Skip the others. */
6607 if (args != NULL && *args != '\0')
6608 {
6609 if (allflag && parse_and_eval_long (args) != b->number)
6610 continue;
6611 if (!allflag && !number_is_in_list (args, b->number))
6612 continue;
6613 }
269b11a2 6614
e5a67952
MS
6615 if (allflag || user_breakpoint_p (b))
6616 {
6617 int addr_bit, type_len;
a6d9a66e 6618
e5a67952
MS
6619 addr_bit = breakpoint_address_bits (b);
6620 if (addr_bit > print_address_bits)
6621 print_address_bits = addr_bit;
269b11a2 6622
e5a67952
MS
6623 type_len = strlen (bptype_string (b->type));
6624 if (type_len > print_type_col_width)
6625 print_type_col_width = type_len;
6626
6627 nr_printable_breakpoints++;
6628 }
6629 }
7f3b0473 6630
79a45b7d 6631 if (opts.addressprint)
3b31d625 6632 bkpttbl_chain
3e43a32a
MS
6633 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6634 nr_printable_breakpoints,
3b31d625 6635 "BreakpointTable");
8b93c638 6636 else
3b31d625 6637 bkpttbl_chain
3e43a32a
MS
6638 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6639 nr_printable_breakpoints,
3b31d625 6640 "BreakpointTable");
8b93c638 6641
7f3b0473 6642 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6643 annotate_breakpoints_headers ();
6644 if (nr_printable_breakpoints > 0)
6645 annotate_field (0);
4a64f543 6646 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6647 if (nr_printable_breakpoints > 0)
6648 annotate_field (1);
269b11a2 6649 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6650 "type", "Type"); /* 2 */
d7faa9e7
AC
6651 if (nr_printable_breakpoints > 0)
6652 annotate_field (2);
4a64f543 6653 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6654 if (nr_printable_breakpoints > 0)
6655 annotate_field (3);
54e52265 6656 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6657 if (opts.addressprint)
e5a67952
MS
6658 {
6659 if (nr_printable_breakpoints > 0)
6660 annotate_field (4);
6661 if (print_address_bits <= 32)
6662 ui_out_table_header (uiout, 10, ui_left,
6663 "addr", "Address"); /* 5 */
6664 else
6665 ui_out_table_header (uiout, 18, ui_left,
6666 "addr", "Address"); /* 5 */
6667 }
d7faa9e7
AC
6668 if (nr_printable_breakpoints > 0)
6669 annotate_field (5);
6670 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6671 ui_out_table_body (uiout);
6672 if (nr_printable_breakpoints > 0)
6673 annotate_breakpoints_table ();
7f3b0473 6674
c4093a6a 6675 ALL_BREAKPOINTS (b)
e5a67952
MS
6676 {
6677 QUIT;
6678 /* If we have a filter, only list the breakpoints it accepts. */
6679 if (filter && !filter (b))
6680 continue;
6681
6682 /* If we have an "args" string, it is a list of breakpoints to
6683 accept. Skip the others. */
6684
6685 if (args != NULL && *args != '\0')
6686 {
6687 if (allflag) /* maintenance info breakpoint */
6688 {
6689 if (parse_and_eval_long (args) != b->number)
6690 continue;
6691 }
6692 else /* all others */
6693 {
6694 if (!number_is_in_list (args, b->number))
6695 continue;
6696 }
6697 }
6698 /* We only print out user settable breakpoints unless the
6699 allflag is set. */
6700 if (allflag || user_breakpoint_p (b))
12c5a436 6701 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6702 }
6703
3b31d625 6704 do_cleanups (bkpttbl_chain);
698384cd 6705
7f3b0473 6706 if (nr_printable_breakpoints == 0)
c906108c 6707 {
4a64f543
MS
6708 /* If there's a filter, let the caller decide how to report
6709 empty list. */
d77f58be
SS
6710 if (!filter)
6711 {
e5a67952 6712 if (args == NULL || *args == '\0')
d77f58be
SS
6713 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6714 else
4a64f543 6715 ui_out_message (uiout, 0,
e5a67952
MS
6716 "No breakpoint or watchpoint matching '%s'.\n",
6717 args);
d77f58be 6718 }
c906108c
SS
6719 }
6720 else
c4093a6a 6721 {
a6d9a66e
UW
6722 if (last_loc && !server_command)
6723 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6724 }
c906108c 6725
4a64f543 6726 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6727 there have been breakpoints? */
c906108c 6728 annotate_breakpoints_table_end ();
d77f58be
SS
6729
6730 return nr_printable_breakpoints;
c906108c
SS
6731}
6732
ad443146
SS
6733/* Display the value of default-collect in a way that is generally
6734 compatible with the breakpoint list. */
6735
6736static void
6737default_collect_info (void)
6738{
79a45e25
PA
6739 struct ui_out *uiout = current_uiout;
6740
ad443146
SS
6741 /* If it has no value (which is frequently the case), say nothing; a
6742 message like "No default-collect." gets in user's face when it's
6743 not wanted. */
6744 if (!*default_collect)
6745 return;
6746
6747 /* The following phrase lines up nicely with per-tracepoint collect
6748 actions. */
6749 ui_out_text (uiout, "default collect ");
6750 ui_out_field_string (uiout, "default-collect", default_collect);
6751 ui_out_text (uiout, " \n");
6752}
6753
c906108c 6754static void
e5a67952 6755breakpoints_info (char *args, int from_tty)
c906108c 6756{
e5a67952 6757 breakpoint_1 (args, 0, NULL);
ad443146
SS
6758
6759 default_collect_info ();
d77f58be
SS
6760}
6761
6762static void
e5a67952 6763watchpoints_info (char *args, int from_tty)
d77f58be 6764{
e5a67952 6765 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6766 struct ui_out *uiout = current_uiout;
d77f58be
SS
6767
6768 if (num_printed == 0)
6769 {
e5a67952 6770 if (args == NULL || *args == '\0')
d77f58be
SS
6771 ui_out_message (uiout, 0, "No watchpoints.\n");
6772 else
e5a67952 6773 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6774 }
c906108c
SS
6775}
6776
7a292a7a 6777static void
e5a67952 6778maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6779{
e5a67952 6780 breakpoint_1 (args, 1, NULL);
ad443146
SS
6781
6782 default_collect_info ();
c906108c
SS
6783}
6784
0d381245 6785static int
714835d5 6786breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6787 struct program_space *pspace,
714835d5 6788 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6789{
6790 struct bp_location *bl = b->loc;
cc59ec59 6791
0d381245
VP
6792 for (; bl; bl = bl->next)
6793 {
6c95b8df
PA
6794 if (bl->pspace == pspace
6795 && bl->address == pc
0d381245
VP
6796 && (!overlay_debugging || bl->section == section))
6797 return 1;
6798 }
6799 return 0;
6800}
6801
672f9b60 6802/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6803 concerns with logical breakpoints, so we match program spaces, not
6804 address spaces. */
c906108c
SS
6805
6806static void
6c95b8df
PA
6807describe_other_breakpoints (struct gdbarch *gdbarch,
6808 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6809 struct obj_section *section, int thread)
c906108c 6810{
52f0bd74
AC
6811 int others = 0;
6812 struct breakpoint *b;
c906108c
SS
6813
6814 ALL_BREAKPOINTS (b)
672f9b60
KP
6815 others += (user_breakpoint_p (b)
6816 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6817 if (others > 0)
6818 {
a3f17187
AC
6819 if (others == 1)
6820 printf_filtered (_("Note: breakpoint "));
6821 else /* if (others == ???) */
6822 printf_filtered (_("Note: breakpoints "));
c906108c 6823 ALL_BREAKPOINTS (b)
672f9b60 6824 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6825 {
6826 others--;
6827 printf_filtered ("%d", b->number);
6828 if (b->thread == -1 && thread != -1)
6829 printf_filtered (" (all threads)");
6830 else if (b->thread != -1)
6831 printf_filtered (" (thread %d)", b->thread);
6832 printf_filtered ("%s%s ",
059fb39f 6833 ((b->enable_state == bp_disabled
f8eba3c6 6834 || b->enable_state == bp_call_disabled)
0d381245
VP
6835 ? " (disabled)"
6836 : b->enable_state == bp_permanent
6837 ? " (permanent)"
6838 : ""),
6839 (others > 1) ? ","
6840 : ((others == 1) ? " and" : ""));
6841 }
a3f17187 6842 printf_filtered (_("also set at pc "));
5af949e3 6843 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6844 printf_filtered (".\n");
6845 }
6846}
6847\f
c906108c 6848
e4f237da
KB
6849/* Return true iff it is meaningful to use the address member of
6850 BPT. For some breakpoint types, the address member is irrelevant
6851 and it makes no sense to attempt to compare it to other addresses
6852 (or use it for any other purpose either).
6853
4a64f543
MS
6854 More specifically, each of the following breakpoint types will
6855 always have a zero valued address and we don't want to mark
6856 breakpoints of any of these types to be a duplicate of an actual
6857 breakpoint at address zero:
e4f237da
KB
6858
6859 bp_watchpoint
2d134ed3
PA
6860 bp_catchpoint
6861
6862*/
e4f237da
KB
6863
6864static int
6865breakpoint_address_is_meaningful (struct breakpoint *bpt)
6866{
6867 enum bptype type = bpt->type;
6868
2d134ed3
PA
6869 return (type != bp_watchpoint && type != bp_catchpoint);
6870}
6871
6872/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6873 true if LOC1 and LOC2 represent the same watchpoint location. */
6874
6875static int
4a64f543
MS
6876watchpoint_locations_match (struct bp_location *loc1,
6877 struct bp_location *loc2)
2d134ed3 6878{
3a5c3e22
PA
6879 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6880 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6881
6882 /* Both of them must exist. */
6883 gdb_assert (w1 != NULL);
6884 gdb_assert (w2 != NULL);
2bdf28a0 6885
4a64f543
MS
6886 /* If the target can evaluate the condition expression in hardware,
6887 then we we need to insert both watchpoints even if they are at
6888 the same place. Otherwise the watchpoint will only trigger when
6889 the condition of whichever watchpoint was inserted evaluates to
6890 true, not giving a chance for GDB to check the condition of the
6891 other watchpoint. */
3a5c3e22 6892 if ((w1->cond_exp
4a64f543
MS
6893 && target_can_accel_watchpoint_condition (loc1->address,
6894 loc1->length,
0cf6dd15 6895 loc1->watchpoint_type,
3a5c3e22
PA
6896 w1->cond_exp))
6897 || (w2->cond_exp
4a64f543
MS
6898 && target_can_accel_watchpoint_condition (loc2->address,
6899 loc2->length,
0cf6dd15 6900 loc2->watchpoint_type,
3a5c3e22 6901 w2->cond_exp)))
0cf6dd15
TJB
6902 return 0;
6903
85d721b8
PA
6904 /* Note that this checks the owner's type, not the location's. In
6905 case the target does not support read watchpoints, but does
6906 support access watchpoints, we'll have bp_read_watchpoint
6907 watchpoints with hw_access locations. Those should be considered
6908 duplicates of hw_read locations. The hw_read locations will
6909 become hw_access locations later. */
2d134ed3
PA
6910 return (loc1->owner->type == loc2->owner->type
6911 && loc1->pspace->aspace == loc2->pspace->aspace
6912 && loc1->address == loc2->address
6913 && loc1->length == loc2->length);
e4f237da
KB
6914}
6915
31e77af2 6916/* See breakpoint.h. */
6c95b8df 6917
31e77af2 6918int
6c95b8df
PA
6919breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6920 struct address_space *aspace2, CORE_ADDR addr2)
6921{
f5656ead 6922 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6923 || aspace1 == aspace2)
6924 && addr1 == addr2);
6925}
6926
f1310107
TJB
6927/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6928 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6929 matches ASPACE2. On targets that have global breakpoints, the address
6930 space doesn't really matter. */
6931
6932static int
6933breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6934 int len1, struct address_space *aspace2,
6935 CORE_ADDR addr2)
6936{
f5656ead 6937 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6938 || aspace1 == aspace2)
6939 && addr2 >= addr1 && addr2 < addr1 + len1);
6940}
6941
6942/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6943 a ranged breakpoint. In most targets, a match happens only if ASPACE
6944 matches the breakpoint's address space. On targets that have global
6945 breakpoints, the address space doesn't really matter. */
6946
6947static int
6948breakpoint_location_address_match (struct bp_location *bl,
6949 struct address_space *aspace,
6950 CORE_ADDR addr)
6951{
6952 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6953 aspace, addr)
6954 || (bl->length
6955 && breakpoint_address_match_range (bl->pspace->aspace,
6956 bl->address, bl->length,
6957 aspace, addr)));
6958}
6959
1e4d1764
YQ
6960/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6961 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6962 true, otherwise returns false. */
6963
6964static int
6965tracepoint_locations_match (struct bp_location *loc1,
6966 struct bp_location *loc2)
6967{
6968 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6969 /* Since tracepoint locations are never duplicated with others', tracepoint
6970 locations at the same address of different tracepoints are regarded as
6971 different locations. */
6972 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6973 else
6974 return 0;
6975}
6976
2d134ed3
PA
6977/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6978 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6979 represent the same location. */
6980
6981static int
4a64f543
MS
6982breakpoint_locations_match (struct bp_location *loc1,
6983 struct bp_location *loc2)
2d134ed3 6984{
2bdf28a0
JK
6985 int hw_point1, hw_point2;
6986
6987 /* Both of them must not be in moribund_locations. */
6988 gdb_assert (loc1->owner != NULL);
6989 gdb_assert (loc2->owner != NULL);
6990
6991 hw_point1 = is_hardware_watchpoint (loc1->owner);
6992 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6993
6994 if (hw_point1 != hw_point2)
6995 return 0;
6996 else if (hw_point1)
6997 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6998 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6999 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7000 else
f1310107
TJB
7001 /* We compare bp_location.length in order to cover ranged breakpoints. */
7002 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7003 loc2->pspace->aspace, loc2->address)
7004 && loc1->length == loc2->length);
2d134ed3
PA
7005}
7006
76897487
KB
7007static void
7008breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7009 int bnum, int have_bnum)
7010{
f63fbe86
MS
7011 /* The longest string possibly returned by hex_string_custom
7012 is 50 chars. These must be at least that big for safety. */
7013 char astr1[64];
7014 char astr2[64];
76897487 7015
bb599908
PH
7016 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7017 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7018 if (have_bnum)
8a3fe4f8 7019 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7020 bnum, astr1, astr2);
7021 else
8a3fe4f8 7022 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7023}
7024
4a64f543
MS
7025/* Adjust a breakpoint's address to account for architectural
7026 constraints on breakpoint placement. Return the adjusted address.
7027 Note: Very few targets require this kind of adjustment. For most
7028 targets, this function is simply the identity function. */
76897487
KB
7029
7030static CORE_ADDR
a6d9a66e
UW
7031adjust_breakpoint_address (struct gdbarch *gdbarch,
7032 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7033{
a6d9a66e 7034 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7035 {
7036 /* Very few targets need any kind of breakpoint adjustment. */
7037 return bpaddr;
7038 }
88f7da05
KB
7039 else if (bptype == bp_watchpoint
7040 || bptype == bp_hardware_watchpoint
7041 || bptype == bp_read_watchpoint
7042 || bptype == bp_access_watchpoint
fe798b75 7043 || bptype == bp_catchpoint)
88f7da05
KB
7044 {
7045 /* Watchpoints and the various bp_catch_* eventpoints should not
7046 have their addresses modified. */
7047 return bpaddr;
7048 }
76897487
KB
7049 else
7050 {
7051 CORE_ADDR adjusted_bpaddr;
7052
7053 /* Some targets have architectural constraints on the placement
7054 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7055 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7056
7057 /* An adjusted breakpoint address can significantly alter
7058 a user's expectations. Print a warning if an adjustment
7059 is required. */
7060 if (adjusted_bpaddr != bpaddr)
7061 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7062
7063 return adjusted_bpaddr;
7064 }
7065}
7066
28010a5d
PA
7067void
7068init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7069 struct breakpoint *owner)
7cc221ef 7070{
7cc221ef
DJ
7071 memset (loc, 0, sizeof (*loc));
7072
348d480f
PA
7073 gdb_assert (ops != NULL);
7074
28010a5d
PA
7075 loc->ops = ops;
7076 loc->owner = owner;
511a6cd4 7077 loc->cond = NULL;
b775012e 7078 loc->cond_bytecode = NULL;
0d381245
VP
7079 loc->shlib_disabled = 0;
7080 loc->enabled = 1;
e049a4b5 7081
28010a5d 7082 switch (owner->type)
e049a4b5
DJ
7083 {
7084 case bp_breakpoint:
7085 case bp_until:
7086 case bp_finish:
7087 case bp_longjmp:
7088 case bp_longjmp_resume:
e2e4d78b 7089 case bp_longjmp_call_dummy:
186c406b
TT
7090 case bp_exception:
7091 case bp_exception_resume:
e049a4b5 7092 case bp_step_resume:
2c03e5be 7093 case bp_hp_step_resume:
e049a4b5
DJ
7094 case bp_watchpoint_scope:
7095 case bp_call_dummy:
aa7d318d 7096 case bp_std_terminate:
e049a4b5
DJ
7097 case bp_shlib_event:
7098 case bp_thread_event:
7099 case bp_overlay_event:
4efc6507 7100 case bp_jit_event:
0fd8e87f 7101 case bp_longjmp_master:
aa7d318d 7102 case bp_std_terminate_master:
186c406b 7103 case bp_exception_master:
0e30163f
JK
7104 case bp_gnu_ifunc_resolver:
7105 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7106 case bp_dprintf:
e049a4b5 7107 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7108 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7109 break;
7110 case bp_hardware_breakpoint:
7111 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7112 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7113 break;
7114 case bp_hardware_watchpoint:
7115 case bp_read_watchpoint:
7116 case bp_access_watchpoint:
7117 loc->loc_type = bp_loc_hardware_watchpoint;
7118 break;
7119 case bp_watchpoint:
ce78b96d 7120 case bp_catchpoint:
15c3d785
PA
7121 case bp_tracepoint:
7122 case bp_fast_tracepoint:
0fb4aa4b 7123 case bp_static_tracepoint:
e049a4b5
DJ
7124 loc->loc_type = bp_loc_other;
7125 break;
7126 default:
e2e0b3e5 7127 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7128 }
7129
f431efe5 7130 loc->refc = 1;
28010a5d
PA
7131}
7132
7133/* Allocate a struct bp_location. */
7134
7135static struct bp_location *
7136allocate_bp_location (struct breakpoint *bpt)
7137{
348d480f
PA
7138 return bpt->ops->allocate_location (bpt);
7139}
7cc221ef 7140
f431efe5
PA
7141static void
7142free_bp_location (struct bp_location *loc)
fe3f5fa8 7143{
348d480f 7144 loc->ops->dtor (loc);
fe3f5fa8
VP
7145 xfree (loc);
7146}
7147
f431efe5
PA
7148/* Increment reference count. */
7149
7150static void
7151incref_bp_location (struct bp_location *bl)
7152{
7153 ++bl->refc;
7154}
7155
7156/* Decrement reference count. If the reference count reaches 0,
7157 destroy the bp_location. Sets *BLP to NULL. */
7158
7159static void
7160decref_bp_location (struct bp_location **blp)
7161{
0807b50c
PA
7162 gdb_assert ((*blp)->refc > 0);
7163
f431efe5
PA
7164 if (--(*blp)->refc == 0)
7165 free_bp_location (*blp);
7166 *blp = NULL;
7167}
7168
346774a9 7169/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7170
346774a9
PA
7171static void
7172add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7173{
346774a9 7174 struct breakpoint *b1;
c906108c 7175
346774a9
PA
7176 /* Add this breakpoint to the end of the chain so that a list of
7177 breakpoints will come out in order of increasing numbers. */
7178
7179 b1 = breakpoint_chain;
7180 if (b1 == 0)
7181 breakpoint_chain = b;
7182 else
7183 {
7184 while (b1->next)
7185 b1 = b1->next;
7186 b1->next = b;
7187 }
7188}
7189
7190/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7191
7192static void
7193init_raw_breakpoint_without_location (struct breakpoint *b,
7194 struct gdbarch *gdbarch,
28010a5d 7195 enum bptype bptype,
c0a91b2b 7196 const struct breakpoint_ops *ops)
346774a9 7197{
c906108c 7198 memset (b, 0, sizeof (*b));
2219d63c 7199
348d480f
PA
7200 gdb_assert (ops != NULL);
7201
28010a5d 7202 b->ops = ops;
4d28f7a8 7203 b->type = bptype;
a6d9a66e 7204 b->gdbarch = gdbarch;
c906108c
SS
7205 b->language = current_language->la_language;
7206 b->input_radix = input_radix;
7207 b->thread = -1;
b5de0fa7 7208 b->enable_state = bp_enabled;
c906108c
SS
7209 b->next = 0;
7210 b->silent = 0;
7211 b->ignore_count = 0;
7212 b->commands = NULL;
818dd999 7213 b->frame_id = null_frame_id;
0d381245 7214 b->condition_not_parsed = 0;
84f4c1fe 7215 b->py_bp_object = NULL;
d0fb5eae 7216 b->related_breakpoint = b;
346774a9
PA
7217}
7218
7219/* Helper to set_raw_breakpoint below. Creates a breakpoint
7220 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7221
7222static struct breakpoint *
7223set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7224 enum bptype bptype,
c0a91b2b 7225 const struct breakpoint_ops *ops)
346774a9
PA
7226{
7227 struct breakpoint *b = XNEW (struct breakpoint);
7228
348d480f 7229 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7230 add_to_breakpoint_chain (b);
0d381245
VP
7231 return b;
7232}
7233
0e30163f
JK
7234/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7235 resolutions should be made as the user specified the location explicitly
7236 enough. */
7237
0d381245 7238static void
0e30163f 7239set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7240{
2bdf28a0
JK
7241 gdb_assert (loc->owner != NULL);
7242
0d381245 7243 if (loc->owner->type == bp_breakpoint
1042e4c0 7244 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7245 || is_tracepoint (loc->owner))
0d381245 7246 {
0e30163f 7247 int is_gnu_ifunc;
2c02bd72 7248 const char *function_name;
6a3a010b 7249 CORE_ADDR func_addr;
0e30163f 7250
2c02bd72 7251 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7252 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7253
7254 if (is_gnu_ifunc && !explicit_loc)
7255 {
7256 struct breakpoint *b = loc->owner;
7257
7258 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7259 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7260 &loc->requested_address))
7261 {
7262 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7263 loc->address = adjust_breakpoint_address (loc->gdbarch,
7264 loc->requested_address,
7265 b->type);
7266 }
7267 else if (b->type == bp_breakpoint && b->loc == loc
7268 && loc->next == NULL && b->related_breakpoint == b)
7269 {
7270 /* Create only the whole new breakpoint of this type but do not
7271 mess more complicated breakpoints with multiple locations. */
7272 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7273 /* Remember the resolver's address for use by the return
7274 breakpoint. */
7275 loc->related_address = func_addr;
0e30163f
JK
7276 }
7277 }
7278
2c02bd72
DE
7279 if (function_name)
7280 loc->function_name = xstrdup (function_name);
0d381245
VP
7281 }
7282}
7283
a6d9a66e 7284/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7285struct gdbarch *
a6d9a66e
UW
7286get_sal_arch (struct symtab_and_line sal)
7287{
7288 if (sal.section)
7289 return get_objfile_arch (sal.section->objfile);
7290 if (sal.symtab)
7291 return get_objfile_arch (sal.symtab->objfile);
7292
7293 return NULL;
7294}
7295
346774a9
PA
7296/* Low level routine for partially initializing a breakpoint of type
7297 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7298 file name, and line number are provided by SAL.
0d381245
VP
7299
7300 It is expected that the caller will complete the initialization of
7301 the newly created breakpoint struct as well as output any status
c56053d2 7302 information regarding the creation of a new breakpoint. */
0d381245 7303
346774a9
PA
7304static void
7305init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7306 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7307 const struct breakpoint_ops *ops)
0d381245 7308{
28010a5d 7309 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7310
3742cc8b 7311 add_location_to_breakpoint (b, &sal);
0d381245 7312
6c95b8df
PA
7313 if (bptype != bp_catchpoint)
7314 gdb_assert (sal.pspace != NULL);
7315
f8eba3c6
TT
7316 /* Store the program space that was used to set the breakpoint,
7317 except for ordinary breakpoints, which are independent of the
7318 program space. */
7319 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7320 b->pspace = sal.pspace;
346774a9 7321}
c906108c 7322
346774a9
PA
7323/* set_raw_breakpoint is a low level routine for allocating and
7324 partially initializing a breakpoint of type BPTYPE. The newly
7325 created breakpoint's address, section, source file name, and line
7326 number are provided by SAL. The newly created and partially
7327 initialized breakpoint is added to the breakpoint chain and
7328 is also returned as the value of this function.
7329
7330 It is expected that the caller will complete the initialization of
7331 the newly created breakpoint struct as well as output any status
7332 information regarding the creation of a new breakpoint. In
7333 particular, set_raw_breakpoint does NOT set the breakpoint
7334 number! Care should be taken to not allow an error to occur
7335 prior to completing the initialization of the breakpoint. If this
7336 should happen, a bogus breakpoint will be left on the chain. */
7337
7338struct breakpoint *
7339set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7340 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7341 const struct breakpoint_ops *ops)
346774a9
PA
7342{
7343 struct breakpoint *b = XNEW (struct breakpoint);
7344
348d480f 7345 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7346 add_to_breakpoint_chain (b);
c906108c
SS
7347 return b;
7348}
7349
c2c6d25f
JM
7350
7351/* Note that the breakpoint object B describes a permanent breakpoint
7352 instruction, hard-wired into the inferior's code. */
7353void
7354make_breakpoint_permanent (struct breakpoint *b)
7355{
0d381245 7356 struct bp_location *bl;
cc59ec59 7357
b5de0fa7 7358 b->enable_state = bp_permanent;
c2c6d25f 7359
4a64f543
MS
7360 /* By definition, permanent breakpoints are already present in the
7361 code. Mark all locations as inserted. For now,
7362 make_breakpoint_permanent is called in just one place, so it's
7363 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7364 multiple locations or not, but it's easy to implement. */
0d381245
VP
7365 for (bl = b->loc; bl; bl = bl->next)
7366 bl->inserted = 1;
c2c6d25f
JM
7367}
7368
53a5351d 7369/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7370 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7371 initiated the operation. */
c906108c
SS
7372
7373void
186c406b 7374set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7375{
35df4500 7376 struct breakpoint *b, *b_tmp;
186c406b 7377 int thread = tp->num;
0fd8e87f
UW
7378
7379 /* To avoid having to rescan all objfile symbols at every step,
7380 we maintain a list of continually-inserted but always disabled
7381 longjmp "master" breakpoints. Here, we simply create momentary
7382 clones of those and enable them for the requested thread. */
35df4500 7383 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7384 if (b->pspace == current_program_space
186c406b
TT
7385 && (b->type == bp_longjmp_master
7386 || b->type == bp_exception_master))
0fd8e87f 7387 {
06edf0c0
PA
7388 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7389 struct breakpoint *clone;
cc59ec59 7390
e2e4d78b
JK
7391 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7392 after their removal. */
06edf0c0 7393 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7394 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7395 clone->thread = thread;
7396 }
186c406b
TT
7397
7398 tp->initiating_frame = frame;
c906108c
SS
7399}
7400
611c83ae 7401/* Delete all longjmp breakpoints from THREAD. */
c906108c 7402void
611c83ae 7403delete_longjmp_breakpoint (int thread)
c906108c 7404{
35df4500 7405 struct breakpoint *b, *b_tmp;
c906108c 7406
35df4500 7407 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7408 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7409 {
7410 if (b->thread == thread)
7411 delete_breakpoint (b);
7412 }
c906108c
SS
7413}
7414
f59f708a
PA
7415void
7416delete_longjmp_breakpoint_at_next_stop (int thread)
7417{
7418 struct breakpoint *b, *b_tmp;
7419
7420 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7421 if (b->type == bp_longjmp || b->type == bp_exception)
7422 {
7423 if (b->thread == thread)
7424 b->disposition = disp_del_at_next_stop;
7425 }
7426}
7427
e2e4d78b
JK
7428/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7429 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7430 pointer to any of them. Return NULL if this system cannot place longjmp
7431 breakpoints. */
7432
7433struct breakpoint *
7434set_longjmp_breakpoint_for_call_dummy (void)
7435{
7436 struct breakpoint *b, *retval = NULL;
7437
7438 ALL_BREAKPOINTS (b)
7439 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7440 {
7441 struct breakpoint *new_b;
7442
7443 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7444 &momentary_breakpoint_ops,
7445 1);
e2e4d78b
JK
7446 new_b->thread = pid_to_thread_id (inferior_ptid);
7447
7448 /* Link NEW_B into the chain of RETVAL breakpoints. */
7449
7450 gdb_assert (new_b->related_breakpoint == new_b);
7451 if (retval == NULL)
7452 retval = new_b;
7453 new_b->related_breakpoint = retval;
7454 while (retval->related_breakpoint != new_b->related_breakpoint)
7455 retval = retval->related_breakpoint;
7456 retval->related_breakpoint = new_b;
7457 }
7458
7459 return retval;
7460}
7461
7462/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7463 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7464 stack.
7465
7466 You should call this function only at places where it is safe to currently
7467 unwind the whole stack. Failed stack unwind would discard live dummy
7468 frames. */
7469
7470void
b67a2c6f 7471check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7472{
7473 struct breakpoint *b, *b_tmp;
7474
7475 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7476 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7477 {
7478 struct breakpoint *dummy_b = b->related_breakpoint;
7479
7480 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7481 dummy_b = dummy_b->related_breakpoint;
7482 if (dummy_b->type != bp_call_dummy
7483 || frame_find_by_id (dummy_b->frame_id) != NULL)
7484 continue;
7485
b67a2c6f 7486 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7487
7488 while (b->related_breakpoint != b)
7489 {
7490 if (b_tmp == b->related_breakpoint)
7491 b_tmp = b->related_breakpoint->next;
7492 delete_breakpoint (b->related_breakpoint);
7493 }
7494 delete_breakpoint (b);
7495 }
7496}
7497
1900040c
MS
7498void
7499enable_overlay_breakpoints (void)
7500{
52f0bd74 7501 struct breakpoint *b;
1900040c
MS
7502
7503 ALL_BREAKPOINTS (b)
7504 if (b->type == bp_overlay_event)
7505 {
7506 b->enable_state = bp_enabled;
b60e7edf 7507 update_global_location_list (1);
c02f5703 7508 overlay_events_enabled = 1;
1900040c
MS
7509 }
7510}
7511
7512void
7513disable_overlay_breakpoints (void)
7514{
52f0bd74 7515 struct breakpoint *b;
1900040c
MS
7516
7517 ALL_BREAKPOINTS (b)
7518 if (b->type == bp_overlay_event)
7519 {
7520 b->enable_state = bp_disabled;
b60e7edf 7521 update_global_location_list (0);
c02f5703 7522 overlay_events_enabled = 0;
1900040c
MS
7523 }
7524}
7525
aa7d318d
TT
7526/* Set an active std::terminate breakpoint for each std::terminate
7527 master breakpoint. */
7528void
7529set_std_terminate_breakpoint (void)
7530{
35df4500 7531 struct breakpoint *b, *b_tmp;
aa7d318d 7532
35df4500 7533 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7534 if (b->pspace == current_program_space
7535 && b->type == bp_std_terminate_master)
7536 {
06edf0c0 7537 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7538 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7539 }
7540}
7541
7542/* Delete all the std::terminate breakpoints. */
7543void
7544delete_std_terminate_breakpoint (void)
7545{
35df4500 7546 struct breakpoint *b, *b_tmp;
aa7d318d 7547
35df4500 7548 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7549 if (b->type == bp_std_terminate)
7550 delete_breakpoint (b);
7551}
7552
c4093a6a 7553struct breakpoint *
a6d9a66e 7554create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7555{
7556 struct breakpoint *b;
c4093a6a 7557
06edf0c0
PA
7558 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7559 &internal_breakpoint_ops);
7560
b5de0fa7 7561 b->enable_state = bp_enabled;
c4093a6a 7562 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7563 b->addr_string
7564 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7565
b60e7edf 7566 update_global_location_list_nothrow (1);
74960c60 7567
c4093a6a
JM
7568 return b;
7569}
7570
7571void
7572remove_thread_event_breakpoints (void)
7573{
35df4500 7574 struct breakpoint *b, *b_tmp;
c4093a6a 7575
35df4500 7576 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7577 if (b->type == bp_thread_event
7578 && b->loc->pspace == current_program_space)
c4093a6a
JM
7579 delete_breakpoint (b);
7580}
7581
0101ce28
JJ
7582struct lang_and_radix
7583 {
7584 enum language lang;
7585 int radix;
7586 };
7587
4efc6507
DE
7588/* Create a breakpoint for JIT code registration and unregistration. */
7589
7590struct breakpoint *
7591create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7592{
7593 struct breakpoint *b;
7594
06edf0c0
PA
7595 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7596 &internal_breakpoint_ops);
4efc6507
DE
7597 update_global_location_list_nothrow (1);
7598 return b;
7599}
0101ce28 7600
03673fc7
PP
7601/* Remove JIT code registration and unregistration breakpoint(s). */
7602
7603void
7604remove_jit_event_breakpoints (void)
7605{
7606 struct breakpoint *b, *b_tmp;
7607
7608 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7609 if (b->type == bp_jit_event
7610 && b->loc->pspace == current_program_space)
7611 delete_breakpoint (b);
7612}
7613
cae688ec
JJ
7614void
7615remove_solib_event_breakpoints (void)
7616{
35df4500 7617 struct breakpoint *b, *b_tmp;
cae688ec 7618
35df4500 7619 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7620 if (b->type == bp_shlib_event
7621 && b->loc->pspace == current_program_space)
cae688ec
JJ
7622 delete_breakpoint (b);
7623}
7624
7625struct breakpoint *
a6d9a66e 7626create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
7627{
7628 struct breakpoint *b;
7629
06edf0c0
PA
7630 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7631 &internal_breakpoint_ops);
b60e7edf 7632 update_global_location_list_nothrow (1);
cae688ec
JJ
7633 return b;
7634}
7635
7636/* Disable any breakpoints that are on code in shared libraries. Only
7637 apply to enabled breakpoints, disabled ones can just stay disabled. */
7638
7639void
cb851954 7640disable_breakpoints_in_shlibs (void)
cae688ec 7641{
876fa593 7642 struct bp_location *loc, **locp_tmp;
cae688ec 7643
876fa593 7644 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7645 {
2bdf28a0 7646 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7647 struct breakpoint *b = loc->owner;
2bdf28a0 7648
4a64f543
MS
7649 /* We apply the check to all breakpoints, including disabled for
7650 those with loc->duplicate set. This is so that when breakpoint
7651 becomes enabled, or the duplicate is removed, gdb will try to
7652 insert all breakpoints. If we don't set shlib_disabled here,
7653 we'll try to insert those breakpoints and fail. */
1042e4c0 7654 if (((b->type == bp_breakpoint)
508ccb1f 7655 || (b->type == bp_jit_event)
1042e4c0 7656 || (b->type == bp_hardware_breakpoint)
d77f58be 7657 || (is_tracepoint (b)))
6c95b8df 7658 && loc->pspace == current_program_space
0d381245 7659 && !loc->shlib_disabled
6c95b8df 7660 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7661 )
0d381245
VP
7662 {
7663 loc->shlib_disabled = 1;
7664 }
cae688ec
JJ
7665 }
7666}
7667
63644780
NB
7668/* Disable any breakpoints and tracepoints that are in SOLIB upon
7669 notification of unloaded_shlib. Only apply to enabled breakpoints,
7670 disabled ones can just stay disabled. */
84acb35a 7671
75149521 7672static void
84acb35a
JJ
7673disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7674{
876fa593 7675 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7676 int disabled_shlib_breaks = 0;
7677
c86cf029
VP
7678 /* SunOS a.out shared libraries are always mapped, so do not
7679 disable breakpoints; they will only be reported as unloaded
7680 through clear_solib when GDB discards its shared library
7681 list. See clear_solib for more information. */
7682 if (exec_bfd != NULL
7683 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7684 return;
7685
876fa593 7686 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7687 {
2bdf28a0 7688 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7689 struct breakpoint *b = loc->owner;
cc59ec59 7690
1e4d1764 7691 if (solib->pspace == loc->pspace
e2dd7057 7692 && !loc->shlib_disabled
1e4d1764
YQ
7693 && (((b->type == bp_breakpoint
7694 || b->type == bp_jit_event
7695 || b->type == bp_hardware_breakpoint)
7696 && (loc->loc_type == bp_loc_hardware_breakpoint
7697 || loc->loc_type == bp_loc_software_breakpoint))
7698 || is_tracepoint (b))
e2dd7057 7699 && solib_contains_address_p (solib, loc->address))
84acb35a 7700 {
e2dd7057
PP
7701 loc->shlib_disabled = 1;
7702 /* At this point, we cannot rely on remove_breakpoint
7703 succeeding so we must mark the breakpoint as not inserted
7704 to prevent future errors occurring in remove_breakpoints. */
7705 loc->inserted = 0;
8d3788bd
VP
7706
7707 /* This may cause duplicate notifications for the same breakpoint. */
7708 observer_notify_breakpoint_modified (b);
7709
e2dd7057
PP
7710 if (!disabled_shlib_breaks)
7711 {
7712 target_terminal_ours_for_output ();
3e43a32a
MS
7713 warning (_("Temporarily disabling breakpoints "
7714 "for unloaded shared library \"%s\""),
e2dd7057 7715 solib->so_name);
84acb35a 7716 }
e2dd7057 7717 disabled_shlib_breaks = 1;
84acb35a
JJ
7718 }
7719 }
84acb35a
JJ
7720}
7721
63644780
NB
7722/* Disable any breakpoints and tracepoints in OBJFILE upon
7723 notification of free_objfile. Only apply to enabled breakpoints,
7724 disabled ones can just stay disabled. */
7725
7726static void
7727disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7728{
7729 struct breakpoint *b;
7730
7731 if (objfile == NULL)
7732 return;
7733
d03de421
PA
7734 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7735 managed by the user with add-symbol-file/remove-symbol-file.
7736 Similarly to how breakpoints in shared libraries are handled in
7737 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7738 shlib_disabled so they end up uninserted on the next global
7739 location list update. Shared libraries not loaded by the user
7740 aren't handled here -- they're already handled in
7741 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7742 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7743 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7744 main objfile). */
7745 if ((objfile->flags & OBJF_SHARED) == 0
7746 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7747 return;
7748
7749 ALL_BREAKPOINTS (b)
7750 {
7751 struct bp_location *loc;
7752 int bp_modified = 0;
7753
7754 if (!is_breakpoint (b) && !is_tracepoint (b))
7755 continue;
7756
7757 for (loc = b->loc; loc != NULL; loc = loc->next)
7758 {
7759 CORE_ADDR loc_addr = loc->address;
7760
7761 if (loc->loc_type != bp_loc_hardware_breakpoint
7762 && loc->loc_type != bp_loc_software_breakpoint)
7763 continue;
7764
7765 if (loc->shlib_disabled != 0)
7766 continue;
7767
7768 if (objfile->pspace != loc->pspace)
7769 continue;
7770
7771 if (loc->loc_type != bp_loc_hardware_breakpoint
7772 && loc->loc_type != bp_loc_software_breakpoint)
7773 continue;
7774
7775 if (is_addr_in_objfile (loc_addr, objfile))
7776 {
7777 loc->shlib_disabled = 1;
08351840
PA
7778 /* At this point, we don't know whether the object was
7779 unmapped from the inferior or not, so leave the
7780 inserted flag alone. We'll handle failure to
7781 uninsert quietly, in case the object was indeed
7782 unmapped. */
63644780
NB
7783
7784 mark_breakpoint_location_modified (loc);
7785
7786 bp_modified = 1;
7787 }
7788 }
7789
7790 if (bp_modified)
7791 observer_notify_breakpoint_modified (b);
7792 }
7793}
7794
ce78b96d
JB
7795/* FORK & VFORK catchpoints. */
7796
e29a4733
PA
7797/* An instance of this type is used to represent a fork or vfork
7798 catchpoint. It includes a "struct breakpoint" as a kind of base
7799 class; users downcast to "struct breakpoint *" when needed. A
7800 breakpoint is really of this type iff its ops pointer points to
7801 CATCH_FORK_BREAKPOINT_OPS. */
7802
7803struct fork_catchpoint
7804{
7805 /* The base class. */
7806 struct breakpoint base;
7807
7808 /* Process id of a child process whose forking triggered this
7809 catchpoint. This field is only valid immediately after this
7810 catchpoint has triggered. */
7811 ptid_t forked_inferior_pid;
7812};
7813
4a64f543
MS
7814/* Implement the "insert" breakpoint_ops method for fork
7815 catchpoints. */
ce78b96d 7816
77b06cd7
TJB
7817static int
7818insert_catch_fork (struct bp_location *bl)
ce78b96d 7819{
dfd4cc63 7820 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7821}
7822
4a64f543
MS
7823/* Implement the "remove" breakpoint_ops method for fork
7824 catchpoints. */
ce78b96d
JB
7825
7826static int
77b06cd7 7827remove_catch_fork (struct bp_location *bl)
ce78b96d 7828{
dfd4cc63 7829 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7830}
7831
7832/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7833 catchpoints. */
7834
7835static int
f1310107 7836breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7837 struct address_space *aspace, CORE_ADDR bp_addr,
7838 const struct target_waitstatus *ws)
ce78b96d 7839{
e29a4733
PA
7840 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7841
f90263c1
TT
7842 if (ws->kind != TARGET_WAITKIND_FORKED)
7843 return 0;
7844
7845 c->forked_inferior_pid = ws->value.related_pid;
7846 return 1;
ce78b96d
JB
7847}
7848
4a64f543
MS
7849/* Implement the "print_it" breakpoint_ops method for fork
7850 catchpoints. */
ce78b96d
JB
7851
7852static enum print_stop_action
348d480f 7853print_it_catch_fork (bpstat bs)
ce78b96d 7854{
36dfb11c 7855 struct ui_out *uiout = current_uiout;
348d480f
PA
7856 struct breakpoint *b = bs->breakpoint_at;
7857 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7858
ce78b96d 7859 annotate_catchpoint (b->number);
36dfb11c
TT
7860 if (b->disposition == disp_del)
7861 ui_out_text (uiout, "\nTemporary catchpoint ");
7862 else
7863 ui_out_text (uiout, "\nCatchpoint ");
7864 if (ui_out_is_mi_like_p (uiout))
7865 {
7866 ui_out_field_string (uiout, "reason",
7867 async_reason_lookup (EXEC_ASYNC_FORK));
7868 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7869 }
7870 ui_out_field_int (uiout, "bkptno", b->number);
7871 ui_out_text (uiout, " (forked process ");
7872 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7873 ui_out_text (uiout, "), ");
ce78b96d
JB
7874 return PRINT_SRC_AND_LOC;
7875}
7876
4a64f543
MS
7877/* Implement the "print_one" breakpoint_ops method for fork
7878 catchpoints. */
ce78b96d
JB
7879
7880static void
a6d9a66e 7881print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7882{
e29a4733 7883 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7884 struct value_print_options opts;
79a45e25 7885 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7886
7887 get_user_print_options (&opts);
7888
4a64f543
MS
7889 /* Field 4, the address, is omitted (which makes the columns not
7890 line up too nicely with the headers, but the effect is relatively
7891 readable). */
79a45b7d 7892 if (opts.addressprint)
ce78b96d
JB
7893 ui_out_field_skip (uiout, "addr");
7894 annotate_field (5);
7895 ui_out_text (uiout, "fork");
e29a4733 7896 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7897 {
7898 ui_out_text (uiout, ", process ");
7899 ui_out_field_int (uiout, "what",
e29a4733 7900 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7901 ui_out_spaces (uiout, 1);
7902 }
8ac3646f
TT
7903
7904 if (ui_out_is_mi_like_p (uiout))
7905 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
7906}
7907
7908/* Implement the "print_mention" breakpoint_ops method for fork
7909 catchpoints. */
7910
7911static void
7912print_mention_catch_fork (struct breakpoint *b)
7913{
7914 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7915}
7916
6149aea9
PA
7917/* Implement the "print_recreate" breakpoint_ops method for fork
7918 catchpoints. */
7919
7920static void
7921print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7922{
7923 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7924 print_recreate_thread (b, fp);
6149aea9
PA
7925}
7926
ce78b96d
JB
7927/* The breakpoint_ops structure to be used in fork catchpoints. */
7928
2060206e 7929static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7930
4a64f543
MS
7931/* Implement the "insert" breakpoint_ops method for vfork
7932 catchpoints. */
ce78b96d 7933
77b06cd7
TJB
7934static int
7935insert_catch_vfork (struct bp_location *bl)
ce78b96d 7936{
dfd4cc63 7937 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7938}
7939
4a64f543
MS
7940/* Implement the "remove" breakpoint_ops method for vfork
7941 catchpoints. */
ce78b96d
JB
7942
7943static int
77b06cd7 7944remove_catch_vfork (struct bp_location *bl)
ce78b96d 7945{
dfd4cc63 7946 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7947}
7948
7949/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7950 catchpoints. */
7951
7952static int
f1310107 7953breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7954 struct address_space *aspace, CORE_ADDR bp_addr,
7955 const struct target_waitstatus *ws)
ce78b96d 7956{
e29a4733
PA
7957 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7958
f90263c1
TT
7959 if (ws->kind != TARGET_WAITKIND_VFORKED)
7960 return 0;
7961
7962 c->forked_inferior_pid = ws->value.related_pid;
7963 return 1;
ce78b96d
JB
7964}
7965
4a64f543
MS
7966/* Implement the "print_it" breakpoint_ops method for vfork
7967 catchpoints. */
ce78b96d
JB
7968
7969static enum print_stop_action
348d480f 7970print_it_catch_vfork (bpstat bs)
ce78b96d 7971{
36dfb11c 7972 struct ui_out *uiout = current_uiout;
348d480f 7973 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7974 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7975
ce78b96d 7976 annotate_catchpoint (b->number);
36dfb11c
TT
7977 if (b->disposition == disp_del)
7978 ui_out_text (uiout, "\nTemporary catchpoint ");
7979 else
7980 ui_out_text (uiout, "\nCatchpoint ");
7981 if (ui_out_is_mi_like_p (uiout))
7982 {
7983 ui_out_field_string (uiout, "reason",
7984 async_reason_lookup (EXEC_ASYNC_VFORK));
7985 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7986 }
7987 ui_out_field_int (uiout, "bkptno", b->number);
7988 ui_out_text (uiout, " (vforked process ");
7989 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7990 ui_out_text (uiout, "), ");
ce78b96d
JB
7991 return PRINT_SRC_AND_LOC;
7992}
7993
4a64f543
MS
7994/* Implement the "print_one" breakpoint_ops method for vfork
7995 catchpoints. */
ce78b96d
JB
7996
7997static void
a6d9a66e 7998print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7999{
e29a4733 8000 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8001 struct value_print_options opts;
79a45e25 8002 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8003
8004 get_user_print_options (&opts);
4a64f543
MS
8005 /* Field 4, the address, is omitted (which makes the columns not
8006 line up too nicely with the headers, but the effect is relatively
8007 readable). */
79a45b7d 8008 if (opts.addressprint)
ce78b96d
JB
8009 ui_out_field_skip (uiout, "addr");
8010 annotate_field (5);
8011 ui_out_text (uiout, "vfork");
e29a4733 8012 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8013 {
8014 ui_out_text (uiout, ", process ");
8015 ui_out_field_int (uiout, "what",
e29a4733 8016 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8017 ui_out_spaces (uiout, 1);
8018 }
8ac3646f
TT
8019
8020 if (ui_out_is_mi_like_p (uiout))
8021 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8022}
8023
8024/* Implement the "print_mention" breakpoint_ops method for vfork
8025 catchpoints. */
8026
8027static void
8028print_mention_catch_vfork (struct breakpoint *b)
8029{
8030 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8031}
8032
6149aea9
PA
8033/* Implement the "print_recreate" breakpoint_ops method for vfork
8034 catchpoints. */
8035
8036static void
8037print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8038{
8039 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8040 print_recreate_thread (b, fp);
6149aea9
PA
8041}
8042
ce78b96d
JB
8043/* The breakpoint_ops structure to be used in vfork catchpoints. */
8044
2060206e 8045static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8046
edcc5120
TT
8047/* An instance of this type is used to represent an solib catchpoint.
8048 It includes a "struct breakpoint" as a kind of base class; users
8049 downcast to "struct breakpoint *" when needed. A breakpoint is
8050 really of this type iff its ops pointer points to
8051 CATCH_SOLIB_BREAKPOINT_OPS. */
8052
8053struct solib_catchpoint
8054{
8055 /* The base class. */
8056 struct breakpoint base;
8057
8058 /* True for "catch load", false for "catch unload". */
8059 unsigned char is_load;
8060
8061 /* Regular expression to match, if any. COMPILED is only valid when
8062 REGEX is non-NULL. */
8063 char *regex;
8064 regex_t compiled;
8065};
8066
8067static void
8068dtor_catch_solib (struct breakpoint *b)
8069{
8070 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8071
8072 if (self->regex)
8073 regfree (&self->compiled);
8074 xfree (self->regex);
8075
8076 base_breakpoint_ops.dtor (b);
8077}
8078
8079static int
8080insert_catch_solib (struct bp_location *ignore)
8081{
8082 return 0;
8083}
8084
8085static int
8086remove_catch_solib (struct bp_location *ignore)
8087{
8088 return 0;
8089}
8090
8091static int
8092breakpoint_hit_catch_solib (const struct bp_location *bl,
8093 struct address_space *aspace,
8094 CORE_ADDR bp_addr,
8095 const struct target_waitstatus *ws)
8096{
8097 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8098 struct breakpoint *other;
8099
8100 if (ws->kind == TARGET_WAITKIND_LOADED)
8101 return 1;
8102
8103 ALL_BREAKPOINTS (other)
8104 {
8105 struct bp_location *other_bl;
8106
8107 if (other == bl->owner)
8108 continue;
8109
8110 if (other->type != bp_shlib_event)
8111 continue;
8112
8113 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8114 continue;
8115
8116 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8117 {
8118 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8119 return 1;
8120 }
8121 }
8122
8123 return 0;
8124}
8125
8126static void
8127check_status_catch_solib (struct bpstats *bs)
8128{
8129 struct solib_catchpoint *self
8130 = (struct solib_catchpoint *) bs->breakpoint_at;
8131 int ix;
8132
8133 if (self->is_load)
8134 {
8135 struct so_list *iter;
8136
8137 for (ix = 0;
8138 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8139 ix, iter);
8140 ++ix)
8141 {
8142 if (!self->regex
8143 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8144 return;
8145 }
8146 }
8147 else
8148 {
8149 char *iter;
8150
8151 for (ix = 0;
8152 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8153 ix, iter);
8154 ++ix)
8155 {
8156 if (!self->regex
8157 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8158 return;
8159 }
8160 }
8161
8162 bs->stop = 0;
8163 bs->print_it = print_it_noop;
8164}
8165
8166static enum print_stop_action
8167print_it_catch_solib (bpstat bs)
8168{
8169 struct breakpoint *b = bs->breakpoint_at;
8170 struct ui_out *uiout = current_uiout;
8171
8172 annotate_catchpoint (b->number);
8173 if (b->disposition == disp_del)
8174 ui_out_text (uiout, "\nTemporary catchpoint ");
8175 else
8176 ui_out_text (uiout, "\nCatchpoint ");
8177 ui_out_field_int (uiout, "bkptno", b->number);
8178 ui_out_text (uiout, "\n");
8179 if (ui_out_is_mi_like_p (uiout))
8180 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8181 print_solib_event (1);
8182 return PRINT_SRC_AND_LOC;
8183}
8184
8185static void
8186print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8187{
8188 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8189 struct value_print_options opts;
8190 struct ui_out *uiout = current_uiout;
8191 char *msg;
8192
8193 get_user_print_options (&opts);
8194 /* Field 4, the address, is omitted (which makes the columns not
8195 line up too nicely with the headers, but the effect is relatively
8196 readable). */
8197 if (opts.addressprint)
8198 {
8199 annotate_field (4);
8200 ui_out_field_skip (uiout, "addr");
8201 }
8202
8203 annotate_field (5);
8204 if (self->is_load)
8205 {
8206 if (self->regex)
8207 msg = xstrprintf (_("load of library matching %s"), self->regex);
8208 else
8209 msg = xstrdup (_("load of library"));
8210 }
8211 else
8212 {
8213 if (self->regex)
8214 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8215 else
8216 msg = xstrdup (_("unload of library"));
8217 }
8218 ui_out_field_string (uiout, "what", msg);
8219 xfree (msg);
8ac3646f
TT
8220
8221 if (ui_out_is_mi_like_p (uiout))
8222 ui_out_field_string (uiout, "catch-type",
8223 self->is_load ? "load" : "unload");
edcc5120
TT
8224}
8225
8226static void
8227print_mention_catch_solib (struct breakpoint *b)
8228{
8229 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8230
8231 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8232 self->is_load ? "load" : "unload");
8233}
8234
8235static void
8236print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8237{
8238 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8239
8240 fprintf_unfiltered (fp, "%s %s",
8241 b->disposition == disp_del ? "tcatch" : "catch",
8242 self->is_load ? "load" : "unload");
8243 if (self->regex)
8244 fprintf_unfiltered (fp, " %s", self->regex);
8245 fprintf_unfiltered (fp, "\n");
8246}
8247
8248static struct breakpoint_ops catch_solib_breakpoint_ops;
8249
91985142
MG
8250/* Shared helper function (MI and CLI) for creating and installing
8251 a shared object event catchpoint. If IS_LOAD is non-zero then
8252 the events to be caught are load events, otherwise they are
8253 unload events. If IS_TEMP is non-zero the catchpoint is a
8254 temporary one. If ENABLED is non-zero the catchpoint is
8255 created in an enabled state. */
edcc5120 8256
91985142
MG
8257void
8258add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8259{
8260 struct solib_catchpoint *c;
8261 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8262 struct cleanup *cleanup;
8263
edcc5120
TT
8264 if (!arg)
8265 arg = "";
8266 arg = skip_spaces (arg);
8267
8268 c = XCNEW (struct solib_catchpoint);
8269 cleanup = make_cleanup (xfree, c);
8270
8271 if (*arg != '\0')
8272 {
8273 int errcode;
8274
8275 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8276 if (errcode != 0)
8277 {
8278 char *err = get_regcomp_error (errcode, &c->compiled);
8279
8280 make_cleanup (xfree, err);
8281 error (_("Invalid regexp (%s): %s"), err, arg);
8282 }
8283 c->regex = xstrdup (arg);
8284 }
8285
8286 c->is_load = is_load;
91985142 8287 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8288 &catch_solib_breakpoint_ops);
8289
91985142
MG
8290 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8291
edcc5120
TT
8292 discard_cleanups (cleanup);
8293 install_breakpoint (0, &c->base, 1);
8294}
8295
91985142
MG
8296/* A helper function that does all the work for "catch load" and
8297 "catch unload". */
8298
8299static void
8300catch_load_or_unload (char *arg, int from_tty, int is_load,
8301 struct cmd_list_element *command)
8302{
8303 int tempflag;
8304 const int enabled = 1;
8305
8306 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8307
8308 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8309}
8310
edcc5120
TT
8311static void
8312catch_load_command_1 (char *arg, int from_tty,
8313 struct cmd_list_element *command)
8314{
8315 catch_load_or_unload (arg, from_tty, 1, command);
8316}
8317
8318static void
8319catch_unload_command_1 (char *arg, int from_tty,
8320 struct cmd_list_element *command)
8321{
8322 catch_load_or_unload (arg, from_tty, 0, command);
8323}
8324
be5c67c1
PA
8325/* An instance of this type is used to represent a syscall catchpoint.
8326 It includes a "struct breakpoint" as a kind of base class; users
8327 downcast to "struct breakpoint *" when needed. A breakpoint is
8328 really of this type iff its ops pointer points to
8329 CATCH_SYSCALL_BREAKPOINT_OPS. */
8330
8331struct syscall_catchpoint
8332{
8333 /* The base class. */
8334 struct breakpoint base;
8335
8336 /* Syscall numbers used for the 'catch syscall' feature. If no
8337 syscall has been specified for filtering, its value is NULL.
8338 Otherwise, it holds a list of all syscalls to be caught. The
8339 list elements are allocated with xmalloc. */
8340 VEC(int) *syscalls_to_be_caught;
8341};
8342
8343/* Implement the "dtor" breakpoint_ops method for syscall
8344 catchpoints. */
8345
8346static void
8347dtor_catch_syscall (struct breakpoint *b)
8348{
8349 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8350
8351 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8352
2060206e 8353 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8354}
8355
fa3064dd
YQ
8356static const struct inferior_data *catch_syscall_inferior_data = NULL;
8357
8358struct catch_syscall_inferior_data
8359{
8360 /* We keep a count of the number of times the user has requested a
8361 particular syscall to be tracked, and pass this information to the
8362 target. This lets capable targets implement filtering directly. */
8363
8364 /* Number of times that "any" syscall is requested. */
8365 int any_syscall_count;
8366
8367 /* Count of each system call. */
8368 VEC(int) *syscalls_counts;
8369
8370 /* This counts all syscall catch requests, so we can readily determine
8371 if any catching is necessary. */
8372 int total_syscalls_count;
8373};
8374
8375static struct catch_syscall_inferior_data*
8376get_catch_syscall_inferior_data (struct inferior *inf)
8377{
8378 struct catch_syscall_inferior_data *inf_data;
8379
8380 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8381 if (inf_data == NULL)
8382 {
41bf6aca 8383 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8384 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8385 }
8386
8387 return inf_data;
8388}
8389
8390static void
8391catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8392{
8393 xfree (arg);
8394}
8395
8396
a96d9b2e
SDJ
8397/* Implement the "insert" breakpoint_ops method for syscall
8398 catchpoints. */
8399
77b06cd7
TJB
8400static int
8401insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8402{
be5c67c1 8403 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8404 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8405 struct catch_syscall_inferior_data *inf_data
8406 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8407
fa3064dd 8408 ++inf_data->total_syscalls_count;
be5c67c1 8409 if (!c->syscalls_to_be_caught)
fa3064dd 8410 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8411 else
8412 {
8413 int i, iter;
cc59ec59 8414
a96d9b2e 8415 for (i = 0;
be5c67c1 8416 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8417 i++)
8418 {
8419 int elem;
cc59ec59 8420
fa3064dd 8421 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8422 {
fa3064dd 8423 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8424 uintptr_t vec_addr_offset
8425 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8426 uintptr_t vec_addr;
fa3064dd
YQ
8427 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8428 vec_addr = ((uintptr_t) VEC_address (int,
8429 inf_data->syscalls_counts)
8430 + vec_addr_offset);
a96d9b2e
SDJ
8431 memset ((void *) vec_addr, 0,
8432 (iter + 1 - old_size) * sizeof (int));
8433 }
fa3064dd
YQ
8434 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8435 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8436 }
8437 }
8438
dfd4cc63 8439 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8440 inf_data->total_syscalls_count != 0,
8441 inf_data->any_syscall_count,
8442 VEC_length (int,
8443 inf_data->syscalls_counts),
8444 VEC_address (int,
8445 inf_data->syscalls_counts));
a96d9b2e
SDJ
8446}
8447
8448/* Implement the "remove" breakpoint_ops method for syscall
8449 catchpoints. */
8450
8451static int
77b06cd7 8452remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8453{
be5c67c1 8454 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8455 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8456 struct catch_syscall_inferior_data *inf_data
8457 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8458
fa3064dd 8459 --inf_data->total_syscalls_count;
be5c67c1 8460 if (!c->syscalls_to_be_caught)
fa3064dd 8461 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8462 else
8463 {
8464 int i, iter;
cc59ec59 8465
a96d9b2e 8466 for (i = 0;
be5c67c1 8467 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8468 i++)
8469 {
8470 int elem;
fa3064dd 8471 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8472 /* Shouldn't happen. */
8473 continue;
fa3064dd
YQ
8474 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8475 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8476 }
8477 }
8478
dfd4cc63 8479 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8480 inf_data->total_syscalls_count != 0,
8481 inf_data->any_syscall_count,
8482 VEC_length (int,
8483 inf_data->syscalls_counts),
3e43a32a 8484 VEC_address (int,
fa3064dd 8485 inf_data->syscalls_counts));
a96d9b2e
SDJ
8486}
8487
8488/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8489 catchpoints. */
8490
8491static int
f1310107 8492breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8493 struct address_space *aspace, CORE_ADDR bp_addr,
8494 const struct target_waitstatus *ws)
a96d9b2e 8495{
4a64f543
MS
8496 /* We must check if we are catching specific syscalls in this
8497 breakpoint. If we are, then we must guarantee that the called
8498 syscall is the same syscall we are catching. */
a96d9b2e 8499 int syscall_number = 0;
be5c67c1
PA
8500 const struct syscall_catchpoint *c
8501 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8502
f90263c1
TT
8503 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8504 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8505 return 0;
8506
f90263c1
TT
8507 syscall_number = ws->value.syscall_number;
8508
a96d9b2e 8509 /* Now, checking if the syscall is the same. */
be5c67c1 8510 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8511 {
8512 int i, iter;
cc59ec59 8513
a96d9b2e 8514 for (i = 0;
be5c67c1 8515 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8516 i++)
8517 if (syscall_number == iter)
4924df79
GKB
8518 return 1;
8519
8520 return 0;
a96d9b2e
SDJ
8521 }
8522
8523 return 1;
8524}
8525
8526/* Implement the "print_it" breakpoint_ops method for syscall
8527 catchpoints. */
8528
8529static enum print_stop_action
348d480f 8530print_it_catch_syscall (bpstat bs)
a96d9b2e 8531{
36dfb11c 8532 struct ui_out *uiout = current_uiout;
348d480f 8533 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8534 /* These are needed because we want to know in which state a
8535 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8536 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8537 must print "called syscall" or "returned from syscall". */
8538 ptid_t ptid;
8539 struct target_waitstatus last;
8540 struct syscall s;
a96d9b2e
SDJ
8541
8542 get_last_target_status (&ptid, &last);
8543
8544 get_syscall_by_number (last.value.syscall_number, &s);
8545
8546 annotate_catchpoint (b->number);
8547
36dfb11c
TT
8548 if (b->disposition == disp_del)
8549 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8550 else
36dfb11c
TT
8551 ui_out_text (uiout, "\nCatchpoint ");
8552 if (ui_out_is_mi_like_p (uiout))
8553 {
8554 ui_out_field_string (uiout, "reason",
8555 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8556 ? EXEC_ASYNC_SYSCALL_ENTRY
8557 : EXEC_ASYNC_SYSCALL_RETURN));
8558 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8559 }
8560 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8561
8562 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8563 ui_out_text (uiout, " (call to syscall ");
8564 else
8565 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8566
36dfb11c
TT
8567 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8568 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8569 if (s.name != NULL)
8570 ui_out_field_string (uiout, "syscall-name", s.name);
8571
8572 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8573
8574 return PRINT_SRC_AND_LOC;
8575}
8576
8577/* Implement the "print_one" breakpoint_ops method for syscall
8578 catchpoints. */
8579
8580static void
8581print_one_catch_syscall (struct breakpoint *b,
f1310107 8582 struct bp_location **last_loc)
a96d9b2e 8583{
be5c67c1 8584 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8585 struct value_print_options opts;
79a45e25 8586 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8587
8588 get_user_print_options (&opts);
4a64f543
MS
8589 /* Field 4, the address, is omitted (which makes the columns not
8590 line up too nicely with the headers, but the effect is relatively
8591 readable). */
a96d9b2e
SDJ
8592 if (opts.addressprint)
8593 ui_out_field_skip (uiout, "addr");
8594 annotate_field (5);
8595
be5c67c1
PA
8596 if (c->syscalls_to_be_caught
8597 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8598 ui_out_text (uiout, "syscalls \"");
8599 else
8600 ui_out_text (uiout, "syscall \"");
8601
be5c67c1 8602 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8603 {
8604 int i, iter;
8605 char *text = xstrprintf ("%s", "");
cc59ec59 8606
a96d9b2e 8607 for (i = 0;
be5c67c1 8608 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8609 i++)
8610 {
8611 char *x = text;
8612 struct syscall s;
8613 get_syscall_by_number (iter, &s);
8614
8615 if (s.name != NULL)
8616 text = xstrprintf ("%s%s, ", text, s.name);
8617 else
8618 text = xstrprintf ("%s%d, ", text, iter);
8619
8620 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8621 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8622 on every call. */
8623 xfree (x);
8624 }
8625 /* Remove the last comma. */
8626 text[strlen (text) - 2] = '\0';
8627 ui_out_field_string (uiout, "what", text);
8628 }
8629 else
8630 ui_out_field_string (uiout, "what", "<any syscall>");
8631 ui_out_text (uiout, "\" ");
8ac3646f
TT
8632
8633 if (ui_out_is_mi_like_p (uiout))
8634 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8635}
8636
8637/* Implement the "print_mention" breakpoint_ops method for syscall
8638 catchpoints. */
8639
8640static void
8641print_mention_catch_syscall (struct breakpoint *b)
8642{
be5c67c1
PA
8643 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8644
8645 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8646 {
8647 int i, iter;
8648
be5c67c1 8649 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8650 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8651 else
8652 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8653
8654 for (i = 0;
be5c67c1 8655 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8656 i++)
8657 {
8658 struct syscall s;
8659 get_syscall_by_number (iter, &s);
8660
8661 if (s.name)
8662 printf_filtered (" '%s' [%d]", s.name, s.number);
8663 else
8664 printf_filtered (" %d", s.number);
8665 }
8666 printf_filtered (")");
8667 }
8668 else
8669 printf_filtered (_("Catchpoint %d (any syscall)"),
8670 b->number);
8671}
8672
6149aea9
PA
8673/* Implement the "print_recreate" breakpoint_ops method for syscall
8674 catchpoints. */
8675
8676static void
8677print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8678{
be5c67c1
PA
8679 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8680
6149aea9
PA
8681 fprintf_unfiltered (fp, "catch syscall");
8682
be5c67c1 8683 if (c->syscalls_to_be_caught)
6149aea9
PA
8684 {
8685 int i, iter;
8686
8687 for (i = 0;
be5c67c1 8688 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8689 i++)
8690 {
8691 struct syscall s;
8692
8693 get_syscall_by_number (iter, &s);
8694 if (s.name)
8695 fprintf_unfiltered (fp, " %s", s.name);
8696 else
8697 fprintf_unfiltered (fp, " %d", s.number);
8698 }
8699 }
d9b3f62e 8700 print_recreate_thread (b, fp);
6149aea9
PA
8701}
8702
a96d9b2e
SDJ
8703/* The breakpoint_ops structure to be used in syscall catchpoints. */
8704
2060206e 8705static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8706
8707/* Returns non-zero if 'b' is a syscall catchpoint. */
8708
8709static int
8710syscall_catchpoint_p (struct breakpoint *b)
8711{
8712 return (b->ops == &catch_syscall_breakpoint_ops);
8713}
8714
346774a9
PA
8715/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8716 is non-zero, then make the breakpoint temporary. If COND_STRING is
8717 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8718 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8719
ab04a2af 8720void
346774a9
PA
8721init_catchpoint (struct breakpoint *b,
8722 struct gdbarch *gdbarch, int tempflag,
8723 char *cond_string,
c0a91b2b 8724 const struct breakpoint_ops *ops)
c906108c 8725{
c5aa993b 8726 struct symtab_and_line sal;
346774a9 8727
fe39c653 8728 init_sal (&sal);
6c95b8df 8729 sal.pspace = current_program_space;
c5aa993b 8730
28010a5d 8731 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8732
1b36a34b 8733 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8734 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8735}
8736
28010a5d 8737void
3ea46bff 8738install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8739{
8740 add_to_breakpoint_chain (b);
3a5c3e22 8741 set_breakpoint_number (internal, b);
558a9d82
YQ
8742 if (is_tracepoint (b))
8743 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8744 if (!internal)
8745 mention (b);
c56053d2 8746 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8747
8748 if (update_gll)
8749 update_global_location_list (1);
c56053d2
PA
8750}
8751
9b70b993 8752static void
a6d9a66e
UW
8753create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8754 int tempflag, char *cond_string,
c0a91b2b 8755 const struct breakpoint_ops *ops)
c906108c 8756{
e29a4733 8757 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8758
e29a4733
PA
8759 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8760
8761 c->forked_inferior_pid = null_ptid;
8762
3ea46bff 8763 install_breakpoint (0, &c->base, 1);
c906108c
SS
8764}
8765
fe798b75
JB
8766/* Exec catchpoints. */
8767
b4d90040
PA
8768/* An instance of this type is used to represent an exec catchpoint.
8769 It includes a "struct breakpoint" as a kind of base class; users
8770 downcast to "struct breakpoint *" when needed. A breakpoint is
8771 really of this type iff its ops pointer points to
8772 CATCH_EXEC_BREAKPOINT_OPS. */
8773
8774struct exec_catchpoint
8775{
8776 /* The base class. */
8777 struct breakpoint base;
8778
8779 /* Filename of a program whose exec triggered this catchpoint.
8780 This field is only valid immediately after this catchpoint has
8781 triggered. */
8782 char *exec_pathname;
8783};
8784
8785/* Implement the "dtor" breakpoint_ops method for exec
8786 catchpoints. */
8787
8788static void
8789dtor_catch_exec (struct breakpoint *b)
8790{
8791 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8792
8793 xfree (c->exec_pathname);
348d480f 8794
2060206e 8795 base_breakpoint_ops.dtor (b);
b4d90040
PA
8796}
8797
77b06cd7
TJB
8798static int
8799insert_catch_exec (struct bp_location *bl)
c906108c 8800{
dfd4cc63 8801 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8802}
c906108c 8803
fe798b75 8804static int
77b06cd7 8805remove_catch_exec (struct bp_location *bl)
fe798b75 8806{
dfd4cc63 8807 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8808}
c906108c 8809
fe798b75 8810static int
f1310107 8811breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8812 struct address_space *aspace, CORE_ADDR bp_addr,
8813 const struct target_waitstatus *ws)
fe798b75 8814{
b4d90040
PA
8815 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8816
f90263c1
TT
8817 if (ws->kind != TARGET_WAITKIND_EXECD)
8818 return 0;
8819
8820 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8821 return 1;
fe798b75 8822}
c906108c 8823
fe798b75 8824static enum print_stop_action
348d480f 8825print_it_catch_exec (bpstat bs)
fe798b75 8826{
36dfb11c 8827 struct ui_out *uiout = current_uiout;
348d480f 8828 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8829 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8830
fe798b75 8831 annotate_catchpoint (b->number);
36dfb11c
TT
8832 if (b->disposition == disp_del)
8833 ui_out_text (uiout, "\nTemporary catchpoint ");
8834 else
8835 ui_out_text (uiout, "\nCatchpoint ");
8836 if (ui_out_is_mi_like_p (uiout))
8837 {
8838 ui_out_field_string (uiout, "reason",
8839 async_reason_lookup (EXEC_ASYNC_EXEC));
8840 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8841 }
8842 ui_out_field_int (uiout, "bkptno", b->number);
8843 ui_out_text (uiout, " (exec'd ");
8844 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8845 ui_out_text (uiout, "), ");
8846
fe798b75 8847 return PRINT_SRC_AND_LOC;
c906108c
SS
8848}
8849
fe798b75 8850static void
a6d9a66e 8851print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8852{
b4d90040 8853 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8854 struct value_print_options opts;
79a45e25 8855 struct ui_out *uiout = current_uiout;
fe798b75
JB
8856
8857 get_user_print_options (&opts);
8858
8859 /* Field 4, the address, is omitted (which makes the columns
8860 not line up too nicely with the headers, but the effect
8861 is relatively readable). */
8862 if (opts.addressprint)
8863 ui_out_field_skip (uiout, "addr");
8864 annotate_field (5);
8865 ui_out_text (uiout, "exec");
b4d90040 8866 if (c->exec_pathname != NULL)
fe798b75
JB
8867 {
8868 ui_out_text (uiout, ", program \"");
b4d90040 8869 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8870 ui_out_text (uiout, "\" ");
8871 }
8ac3646f
TT
8872
8873 if (ui_out_is_mi_like_p (uiout))
8874 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8875}
8876
8877static void
8878print_mention_catch_exec (struct breakpoint *b)
8879{
8880 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8881}
8882
6149aea9
PA
8883/* Implement the "print_recreate" breakpoint_ops method for exec
8884 catchpoints. */
8885
8886static void
8887print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8888{
8889 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8890 print_recreate_thread (b, fp);
6149aea9
PA
8891}
8892
2060206e 8893static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8894
a96d9b2e
SDJ
8895static void
8896create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8897 const struct breakpoint_ops *ops)
a96d9b2e 8898{
be5c67c1 8899 struct syscall_catchpoint *c;
a96d9b2e 8900 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 8901
be5c67c1
PA
8902 c = XNEW (struct syscall_catchpoint);
8903 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8904 c->syscalls_to_be_caught = filter;
a96d9b2e 8905
3ea46bff 8906 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
8907}
8908
c906108c 8909static int
fba45db2 8910hw_breakpoint_used_count (void)
c906108c 8911{
c906108c 8912 int i = 0;
f1310107
TJB
8913 struct breakpoint *b;
8914 struct bp_location *bl;
c906108c
SS
8915
8916 ALL_BREAKPOINTS (b)
c5aa993b 8917 {
d6b74ac4 8918 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8919 for (bl = b->loc; bl; bl = bl->next)
8920 {
8921 /* Special types of hardware breakpoints may use more than
8922 one register. */
348d480f 8923 i += b->ops->resources_needed (bl);
f1310107 8924 }
c5aa993b 8925 }
c906108c
SS
8926
8927 return i;
8928}
8929
a1398e0c
PA
8930/* Returns the resources B would use if it were a hardware
8931 watchpoint. */
8932
c906108c 8933static int
a1398e0c 8934hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8935{
c906108c 8936 int i = 0;
e09342b5 8937 struct bp_location *bl;
c906108c 8938
a1398e0c
PA
8939 if (!breakpoint_enabled (b))
8940 return 0;
8941
8942 for (bl = b->loc; bl; bl = bl->next)
8943 {
8944 /* Special types of hardware watchpoints may use more than
8945 one register. */
8946 i += b->ops->resources_needed (bl);
8947 }
8948
8949 return i;
8950}
8951
8952/* Returns the sum the used resources of all hardware watchpoints of
8953 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8954 the sum of the used resources of all hardware watchpoints of other
8955 types _not_ TYPE. */
8956
8957static int
8958hw_watchpoint_used_count_others (struct breakpoint *except,
8959 enum bptype type, int *other_type_used)
8960{
8961 int i = 0;
8962 struct breakpoint *b;
8963
c906108c
SS
8964 *other_type_used = 0;
8965 ALL_BREAKPOINTS (b)
e09342b5 8966 {
a1398e0c
PA
8967 if (b == except)
8968 continue;
e09342b5
TJB
8969 if (!breakpoint_enabled (b))
8970 continue;
8971
a1398e0c
PA
8972 if (b->type == type)
8973 i += hw_watchpoint_use_count (b);
8974 else if (is_hardware_watchpoint (b))
8975 *other_type_used = 1;
e09342b5
TJB
8976 }
8977
c906108c
SS
8978 return i;
8979}
8980
c906108c 8981void
fba45db2 8982disable_watchpoints_before_interactive_call_start (void)
c906108c 8983{
c5aa993b 8984 struct breakpoint *b;
c906108c
SS
8985
8986 ALL_BREAKPOINTS (b)
c5aa993b 8987 {
cc60f2e3 8988 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8989 {
b5de0fa7 8990 b->enable_state = bp_call_disabled;
b60e7edf 8991 update_global_location_list (0);
c5aa993b
JM
8992 }
8993 }
c906108c
SS
8994}
8995
8996void
fba45db2 8997enable_watchpoints_after_interactive_call_stop (void)
c906108c 8998{
c5aa993b 8999 struct breakpoint *b;
c906108c
SS
9000
9001 ALL_BREAKPOINTS (b)
c5aa993b 9002 {
cc60f2e3 9003 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 9004 {
b5de0fa7 9005 b->enable_state = bp_enabled;
b60e7edf 9006 update_global_location_list (1);
c5aa993b
JM
9007 }
9008 }
c906108c
SS
9009}
9010
8bea4e01
UW
9011void
9012disable_breakpoints_before_startup (void)
9013{
6c95b8df 9014 current_program_space->executing_startup = 1;
f8eba3c6 9015 update_global_location_list (0);
8bea4e01
UW
9016}
9017
9018void
9019enable_breakpoints_after_startup (void)
9020{
6c95b8df 9021 current_program_space->executing_startup = 0;
f8eba3c6 9022 breakpoint_re_set ();
8bea4e01
UW
9023}
9024
c906108c
SS
9025
9026/* Set a breakpoint that will evaporate an end of command
9027 at address specified by SAL.
9028 Restrict it to frame FRAME if FRAME is nonzero. */
9029
9030struct breakpoint *
a6d9a66e
UW
9031set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9032 struct frame_id frame_id, enum bptype type)
c906108c 9033{
52f0bd74 9034 struct breakpoint *b;
edb3359d 9035
193facb3
JK
9036 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9037 tail-called one. */
9038 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9039
06edf0c0 9040 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9041 b->enable_state = bp_enabled;
9042 b->disposition = disp_donttouch;
818dd999 9043 b->frame_id = frame_id;
c906108c 9044
4a64f543
MS
9045 /* If we're debugging a multi-threaded program, then we want
9046 momentary breakpoints to be active in only a single thread of
9047 control. */
39f77062
KB
9048 if (in_thread_list (inferior_ptid))
9049 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9050
b60e7edf 9051 update_global_location_list_nothrow (1);
74960c60 9052
c906108c
SS
9053 return b;
9054}
611c83ae 9055
06edf0c0 9056/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
9057 The new breakpoint will have type TYPE, use OPS as its
9058 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 9059
06edf0c0
PA
9060static struct breakpoint *
9061momentary_breakpoint_from_master (struct breakpoint *orig,
9062 enum bptype type,
a1aa2221
LM
9063 const struct breakpoint_ops *ops,
9064 int loc_enabled)
e58b0e63
PA
9065{
9066 struct breakpoint *copy;
9067
06edf0c0 9068 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9069 copy->loc = allocate_bp_location (copy);
0e30163f 9070 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9071
a6d9a66e 9072 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9073 copy->loc->requested_address = orig->loc->requested_address;
9074 copy->loc->address = orig->loc->address;
9075 copy->loc->section = orig->loc->section;
6c95b8df 9076 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9077 copy->loc->probe = orig->loc->probe;
f8eba3c6 9078 copy->loc->line_number = orig->loc->line_number;
2f202fde 9079 copy->loc->symtab = orig->loc->symtab;
a1aa2221 9080 copy->loc->enabled = loc_enabled;
e58b0e63
PA
9081 copy->frame_id = orig->frame_id;
9082 copy->thread = orig->thread;
6c95b8df 9083 copy->pspace = orig->pspace;
e58b0e63
PA
9084
9085 copy->enable_state = bp_enabled;
9086 copy->disposition = disp_donttouch;
9087 copy->number = internal_breakpoint_number--;
9088
9089 update_global_location_list_nothrow (0);
9090 return copy;
9091}
9092
06edf0c0
PA
9093/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9094 ORIG is NULL. */
9095
9096struct breakpoint *
9097clone_momentary_breakpoint (struct breakpoint *orig)
9098{
9099 /* If there's nothing to clone, then return nothing. */
9100 if (orig == NULL)
9101 return NULL;
9102
a1aa2221 9103 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9104}
9105
611c83ae 9106struct breakpoint *
a6d9a66e
UW
9107set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9108 enum bptype type)
611c83ae
PA
9109{
9110 struct symtab_and_line sal;
9111
9112 sal = find_pc_line (pc, 0);
9113 sal.pc = pc;
9114 sal.section = find_pc_overlay (pc);
9115 sal.explicit_pc = 1;
9116
a6d9a66e 9117 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9118}
c906108c 9119\f
c5aa993b 9120
c906108c
SS
9121/* Tell the user we have just set a breakpoint B. */
9122
9123static void
fba45db2 9124mention (struct breakpoint *b)
c906108c 9125{
348d480f 9126 b->ops->print_mention (b);
79a45e25 9127 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9128 return;
c906108c
SS
9129 printf_filtered ("\n");
9130}
c906108c 9131\f
c5aa993b 9132
0d381245 9133static struct bp_location *
39d61571 9134add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9135 const struct symtab_and_line *sal)
9136{
9137 struct bp_location *loc, **tmp;
3742cc8b
YQ
9138 CORE_ADDR adjusted_address;
9139 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9140
9141 if (loc_gdbarch == NULL)
9142 loc_gdbarch = b->gdbarch;
9143
9144 /* Adjust the breakpoint's address prior to allocating a location.
9145 Once we call allocate_bp_location(), that mostly uninitialized
9146 location will be placed on the location chain. Adjustment of the
9147 breakpoint may cause target_read_memory() to be called and we do
9148 not want its scan of the location chain to find a breakpoint and
9149 location that's only been partially initialized. */
9150 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9151 sal->pc, b->type);
0d381245 9152
d30113d4 9153 /* Sort the locations by their ADDRESS. */
39d61571 9154 loc = allocate_bp_location (b);
d30113d4
JK
9155 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9156 tmp = &((*tmp)->next))
0d381245 9157 ;
d30113d4 9158 loc->next = *tmp;
0d381245 9159 *tmp = loc;
3742cc8b 9160
0d381245 9161 loc->requested_address = sal->pc;
3742cc8b 9162 loc->address = adjusted_address;
6c95b8df 9163 loc->pspace = sal->pspace;
729662a5
TT
9164 loc->probe.probe = sal->probe;
9165 loc->probe.objfile = sal->objfile;
6c95b8df 9166 gdb_assert (loc->pspace != NULL);
0d381245 9167 loc->section = sal->section;
3742cc8b 9168 loc->gdbarch = loc_gdbarch;
f8eba3c6 9169 loc->line_number = sal->line;
2f202fde 9170 loc->symtab = sal->symtab;
f8eba3c6 9171
0e30163f
JK
9172 set_breakpoint_location_function (loc,
9173 sal->explicit_pc || sal->explicit_line);
0d381245
VP
9174 return loc;
9175}
514f746b
AR
9176\f
9177
9178/* Return 1 if LOC is pointing to a permanent breakpoint,
9179 return 0 otherwise. */
9180
9181static int
9182bp_loc_is_permanent (struct bp_location *loc)
9183{
9184 int len;
9185 CORE_ADDR addr;
1afeeb75 9186 const gdb_byte *bpoint;
514f746b 9187 gdb_byte *target_mem;
939c61fa
JK
9188 struct cleanup *cleanup;
9189 int retval = 0;
514f746b
AR
9190
9191 gdb_assert (loc != NULL);
9192
9193 addr = loc->address;
1afeeb75 9194 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9195
939c61fa 9196 /* Software breakpoints unsupported? */
1afeeb75 9197 if (bpoint == NULL)
939c61fa
JK
9198 return 0;
9199
514f746b
AR
9200 target_mem = alloca (len);
9201
939c61fa
JK
9202 /* Enable the automatic memory restoration from breakpoints while
9203 we read the memory. Otherwise we could say about our temporary
9204 breakpoints they are permanent. */
6c95b8df
PA
9205 cleanup = save_current_space_and_thread ();
9206
9207 switch_to_program_space_and_thread (loc->pspace);
9208 make_show_memory_breakpoints_cleanup (0);
939c61fa 9209
514f746b 9210 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9211 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9212 retval = 1;
514f746b 9213
939c61fa
JK
9214 do_cleanups (cleanup);
9215
9216 return retval;
514f746b
AR
9217}
9218
e7e0cddf
SS
9219/* Build a command list for the dprintf corresponding to the current
9220 settings of the dprintf style options. */
9221
9222static void
9223update_dprintf_command_list (struct breakpoint *b)
9224{
9225 char *dprintf_args = b->extra_string;
9226 char *printf_line = NULL;
9227
9228 if (!dprintf_args)
9229 return;
9230
9231 dprintf_args = skip_spaces (dprintf_args);
9232
9233 /* Allow a comma, as it may have terminated a location, but don't
9234 insist on it. */
9235 if (*dprintf_args == ',')
9236 ++dprintf_args;
9237 dprintf_args = skip_spaces (dprintf_args);
9238
9239 if (*dprintf_args != '"')
9240 error (_("Bad format string, missing '\"'."));
9241
d3ce09f5 9242 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9243 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9244 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9245 {
9246 if (!dprintf_function)
9247 error (_("No function supplied for dprintf call"));
9248
9249 if (dprintf_channel && strlen (dprintf_channel) > 0)
9250 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9251 dprintf_function,
9252 dprintf_channel,
9253 dprintf_args);
9254 else
9255 printf_line = xstrprintf ("call (void) %s (%s)",
9256 dprintf_function,
9257 dprintf_args);
9258 }
d3ce09f5
SS
9259 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9260 {
9261 if (target_can_run_breakpoint_commands ())
9262 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9263 else
9264 {
9265 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9266 printf_line = xstrprintf ("printf %s", dprintf_args);
9267 }
9268 }
e7e0cddf
SS
9269 else
9270 internal_error (__FILE__, __LINE__,
9271 _("Invalid dprintf style."));
9272
f28045c2 9273 gdb_assert (printf_line != NULL);
9d6e6e84 9274 /* Manufacture a printf sequence. */
f28045c2 9275 {
9d6e6e84
HZ
9276 struct command_line *printf_cmd_line
9277 = xmalloc (sizeof (struct command_line));
e7e0cddf 9278
f28045c2
YQ
9279 printf_cmd_line = xmalloc (sizeof (struct command_line));
9280 printf_cmd_line->control_type = simple_control;
9281 printf_cmd_line->body_count = 0;
9282 printf_cmd_line->body_list = NULL;
9d6e6e84 9283 printf_cmd_line->next = NULL;
f28045c2 9284 printf_cmd_line->line = printf_line;
e7e0cddf 9285
f28045c2
YQ
9286 breakpoint_set_commands (b, printf_cmd_line);
9287 }
e7e0cddf
SS
9288}
9289
9290/* Update all dprintf commands, making their command lists reflect
9291 current style settings. */
9292
9293static void
9294update_dprintf_commands (char *args, int from_tty,
9295 struct cmd_list_element *c)
9296{
9297 struct breakpoint *b;
9298
9299 ALL_BREAKPOINTS (b)
9300 {
9301 if (b->type == bp_dprintf)
9302 update_dprintf_command_list (b);
9303 }
9304}
c3f6f71d 9305
018d34a4
VP
9306/* Create a breakpoint with SAL as location. Use ADDR_STRING
9307 as textual description of the location, and COND_STRING
db107f19 9308 as condition expression. */
018d34a4
VP
9309
9310static void
d9b3f62e
PA
9311init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9312 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9313 char *filter, char *cond_string,
e7e0cddf 9314 char *extra_string,
d9b3f62e
PA
9315 enum bptype type, enum bpdisp disposition,
9316 int thread, int task, int ignore_count,
c0a91b2b 9317 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9318 int enabled, int internal, unsigned flags,
9319 int display_canonical)
018d34a4 9320{
0d381245 9321 int i;
018d34a4
VP
9322
9323 if (type == bp_hardware_breakpoint)
9324 {
fbbd034e
AS
9325 int target_resources_ok;
9326
9327 i = hw_breakpoint_used_count ();
9328 target_resources_ok =
9329 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9330 i + 1, 0);
9331 if (target_resources_ok == 0)
9332 error (_("No hardware breakpoint support in the target."));
9333 else if (target_resources_ok < 0)
9334 error (_("Hardware breakpoints used exceeds limit."));
9335 }
9336
6c95b8df
PA
9337 gdb_assert (sals.nelts > 0);
9338
0d381245
VP
9339 for (i = 0; i < sals.nelts; ++i)
9340 {
9341 struct symtab_and_line sal = sals.sals[i];
9342 struct bp_location *loc;
9343
9344 if (from_tty)
5af949e3
UW
9345 {
9346 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9347 if (!loc_gdbarch)
9348 loc_gdbarch = gdbarch;
9349
9350 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9351 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9352 }
0d381245
VP
9353
9354 if (i == 0)
9355 {
d9b3f62e 9356 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9357 b->thread = thread;
4a306c9a 9358 b->task = task;
855a6e68 9359
0d381245 9360 b->cond_string = cond_string;
e7e0cddf 9361 b->extra_string = extra_string;
0d381245 9362 b->ignore_count = ignore_count;
41447f92 9363 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9364 b->disposition = disposition;
6c95b8df 9365
44f238bb
PA
9366 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9367 b->loc->inserted = 1;
9368
0fb4aa4b
PA
9369 if (type == bp_static_tracepoint)
9370 {
d9b3f62e 9371 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9372 struct static_tracepoint_marker marker;
9373
983af33b 9374 if (strace_marker_p (b))
0fb4aa4b
PA
9375 {
9376 /* We already know the marker exists, otherwise, we
9377 wouldn't see a sal for it. */
9378 char *p = &addr_string[3];
9379 char *endp;
9380 char *marker_str;
0fb4aa4b 9381
e9cafbcc 9382 p = skip_spaces (p);
0fb4aa4b 9383
e9cafbcc 9384 endp = skip_to_space (p);
0fb4aa4b
PA
9385
9386 marker_str = savestring (p, endp - p);
d9b3f62e 9387 t->static_trace_marker_id = marker_str;
0fb4aa4b 9388
3e43a32a
MS
9389 printf_filtered (_("Probed static tracepoint "
9390 "marker \"%s\"\n"),
d9b3f62e 9391 t->static_trace_marker_id);
0fb4aa4b
PA
9392 }
9393 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9394 {
d9b3f62e 9395 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9396 release_static_tracepoint_marker (&marker);
9397
3e43a32a
MS
9398 printf_filtered (_("Probed static tracepoint "
9399 "marker \"%s\"\n"),
d9b3f62e 9400 t->static_trace_marker_id);
0fb4aa4b
PA
9401 }
9402 else
3e43a32a
MS
9403 warning (_("Couldn't determine the static "
9404 "tracepoint marker to probe"));
0fb4aa4b
PA
9405 }
9406
0d381245
VP
9407 loc = b->loc;
9408 }
9409 else
018d34a4 9410 {
39d61571 9411 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9412 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9413 loc->inserted = 1;
0d381245
VP
9414 }
9415
514f746b
AR
9416 if (bp_loc_is_permanent (loc))
9417 make_breakpoint_permanent (b);
9418
0d381245
VP
9419 if (b->cond_string)
9420 {
bbc13ae3
KS
9421 const char *arg = b->cond_string;
9422
1bb9788d
TT
9423 loc->cond = parse_exp_1 (&arg, loc->address,
9424 block_for_pc (loc->address), 0);
0d381245 9425 if (*arg)
588ae58c 9426 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9427 }
e7e0cddf
SS
9428
9429 /* Dynamic printf requires and uses additional arguments on the
9430 command line, otherwise it's an error. */
9431 if (type == bp_dprintf)
9432 {
9433 if (b->extra_string)
9434 update_dprintf_command_list (b);
9435 else
9436 error (_("Format string required"));
9437 }
9438 else if (b->extra_string)
588ae58c 9439 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9440 }
018d34a4 9441
56435ebe 9442 b->display_canonical = display_canonical;
018d34a4
VP
9443 if (addr_string)
9444 b->addr_string = addr_string;
9445 else
9446 /* addr_string has to be used or breakpoint_re_set will delete
9447 me. */
5af949e3
UW
9448 b->addr_string
9449 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9450 b->filter = filter;
d9b3f62e 9451}
018d34a4 9452
d9b3f62e
PA
9453static void
9454create_breakpoint_sal (struct gdbarch *gdbarch,
9455 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9456 char *filter, char *cond_string,
e7e0cddf 9457 char *extra_string,
d9b3f62e
PA
9458 enum bptype type, enum bpdisp disposition,
9459 int thread, int task, int ignore_count,
c0a91b2b 9460 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9461 int enabled, int internal, unsigned flags,
9462 int display_canonical)
d9b3f62e
PA
9463{
9464 struct breakpoint *b;
9465 struct cleanup *old_chain;
9466
9467 if (is_tracepoint_type (type))
9468 {
9469 struct tracepoint *t;
9470
9471 t = XCNEW (struct tracepoint);
9472 b = &t->base;
9473 }
9474 else
9475 b = XNEW (struct breakpoint);
9476
9477 old_chain = make_cleanup (xfree, b);
9478
9479 init_breakpoint_sal (b, gdbarch,
9480 sals, addr_string,
e7e0cddf 9481 filter, cond_string, extra_string,
d9b3f62e
PA
9482 type, disposition,
9483 thread, task, ignore_count,
9484 ops, from_tty,
44f238bb
PA
9485 enabled, internal, flags,
9486 display_canonical);
d9b3f62e
PA
9487 discard_cleanups (old_chain);
9488
3ea46bff 9489 install_breakpoint (internal, b, 0);
018d34a4
VP
9490}
9491
9492/* Add SALS.nelts breakpoints to the breakpoint table. For each
9493 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9494 value. COND_STRING, if not NULL, specified the condition to be
9495 used for all breakpoints. Essentially the only case where
9496 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9497 function. In that case, it's still not possible to specify
9498 separate conditions for different overloaded functions, so
9499 we take just a single condition string.
9500
c3f6f71d 9501 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9502 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9503 array contents). If the function fails (error() is called), the
9504 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9505 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9506
9507static void
8cdf0e15 9508create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9509 struct linespec_result *canonical,
e7e0cddf 9510 char *cond_string, char *extra_string,
8cdf0e15
VP
9511 enum bptype type, enum bpdisp disposition,
9512 int thread, int task, int ignore_count,
c0a91b2b 9513 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9514 int enabled, int internal, unsigned flags)
c906108c 9515{
018d34a4 9516 int i;
f8eba3c6 9517 struct linespec_sals *lsal;
cc59ec59 9518
f8eba3c6
TT
9519 if (canonical->pre_expanded)
9520 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9521
9522 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9523 {
f8eba3c6
TT
9524 /* Note that 'addr_string' can be NULL in the case of a plain
9525 'break', without arguments. */
9526 char *addr_string = (canonical->addr_string
9527 ? xstrdup (canonical->addr_string)
9528 : NULL);
9529 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9530 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9531
f8eba3c6
TT
9532 make_cleanup (xfree, filter_string);
9533 create_breakpoint_sal (gdbarch, lsal->sals,
9534 addr_string,
9535 filter_string,
e7e0cddf
SS
9536 cond_string, extra_string,
9537 type, disposition,
84f4c1fe 9538 thread, task, ignore_count, ops,
44f238bb 9539 from_tty, enabled, internal, flags,
56435ebe 9540 canonical->special_display);
f8eba3c6 9541 discard_cleanups (inner);
c3f6f71d 9542 }
c3f6f71d 9543}
c906108c 9544
9998af43 9545/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9546 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9547 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9548 address strings. ADDRESS points to the end of the SAL.
9549
9550 The array and the line spec strings are allocated on the heap, it is
9551 the caller's responsibility to free them. */
c906108c 9552
b9362cc7 9553static void
c3f6f71d 9554parse_breakpoint_sals (char **address,
58438ac1 9555 struct linespec_result *canonical)
c3f6f71d 9556{
c3f6f71d 9557 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9558 breakpoint. */
c3f6f71d
JM
9559 if ((*address) == NULL
9560 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9561 {
1bfeeb0f
JL
9562 /* The last displayed codepoint, if it's valid, is our default breakpoint
9563 address. */
9564 if (last_displayed_sal_is_valid ())
c906108c 9565 {
f8eba3c6 9566 struct linespec_sals lsal;
c3f6f71d 9567 struct symtab_and_line sal;
1c8cdcb1 9568 CORE_ADDR pc;
cc59ec59 9569
4a64f543 9570 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9571 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9572 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9573
9574 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9575 corresponding to the last call to print_frame_info.
9576 Be sure to reinitialize LINE with NOTCURRENT == 0
9577 as the breakpoint line number is inappropriate otherwise.
9578 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9579 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9580 pc = sal.pc;
9581 sal = find_pc_line (pc, 0);
00903456 9582
4a64f543 9583 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9584 where PC is the last displayed codepoint's address. So
9585 make sure to set sal.explicit_pc to prevent GDB from
9586 trying to expand the list of sals to include all other
9587 instances with the same symtab and line. */
1c8cdcb1 9588 sal.pc = pc;
00903456
JK
9589 sal.explicit_pc = 1;
9590
f8eba3c6
TT
9591 lsal.sals.sals[0] = sal;
9592 lsal.sals.nelts = 1;
9593 lsal.canonical = NULL;
9594
9595 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9596 }
9597 else
8a3fe4f8 9598 error (_("No default breakpoint address now."));
c906108c
SS
9599 }
9600 else
9601 {
cc80f267
JK
9602 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9603
c906108c 9604 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9605 current_source_symtab (which is decode_line_1's default).
9606 This should produce the results we want almost all of the
cc80f267
JK
9607 time while leaving default_breakpoint_* alone.
9608
9609 ObjC: However, don't match an Objective-C method name which
9610 may have a '+' or '-' succeeded by a '['. */
9611 if (last_displayed_sal_is_valid ()
9612 && (!cursal.symtab
9613 || ((strchr ("+-", (*address)[0]) != NULL)
9614 && ((*address)[1] != '['))))
f8eba3c6
TT
9615 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9616 get_last_displayed_symtab (),
9617 get_last_displayed_line (),
9618 canonical, NULL, NULL);
c906108c 9619 else
f8eba3c6 9620 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9621 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9622 }
c3f6f71d 9623}
c906108c 9624
c906108c 9625
c3f6f71d 9626/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9627 inserted as a breakpoint. If it can't throw an error. */
c906108c 9628
b9362cc7 9629static void
23e7acfb 9630breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9631{
9632 int i;
cc59ec59 9633
c3f6f71d 9634 for (i = 0; i < sals->nelts; i++)
ee53e872 9635 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9636}
9637
7a697b8d
SS
9638/* Fast tracepoints may have restrictions on valid locations. For
9639 instance, a fast tracepoint using a jump instead of a trap will
9640 likely have to overwrite more bytes than a trap would, and so can
9641 only be placed where the instruction is longer than the jump, or a
9642 multi-instruction sequence does not have a jump into the middle of
9643 it, etc. */
9644
9645static void
9646check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9647 struct symtabs_and_lines *sals)
9648{
9649 int i, rslt;
9650 struct symtab_and_line *sal;
9651 char *msg;
9652 struct cleanup *old_chain;
9653
9654 for (i = 0; i < sals->nelts; i++)
9655 {
f8eba3c6
TT
9656 struct gdbarch *sarch;
9657
7a697b8d
SS
9658 sal = &sals->sals[i];
9659
f8eba3c6
TT
9660 sarch = get_sal_arch (*sal);
9661 /* We fall back to GDBARCH if there is no architecture
9662 associated with SAL. */
9663 if (sarch == NULL)
9664 sarch = gdbarch;
9665 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9666 NULL, &msg);
9667 old_chain = make_cleanup (xfree, msg);
9668
9669 if (!rslt)
9670 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9671 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9672
9673 do_cleanups (old_chain);
9674 }
9675}
9676
af4908ba
KS
9677/* Issue an invalid thread ID error. */
9678
9679static void ATTRIBUTE_NORETURN
9680invalid_thread_id_error (int id)
9681{
9682 error (_("Unknown thread %d."), id);
9683}
9684
018d34a4
VP
9685/* Given TOK, a string specification of condition and thread, as
9686 accepted by the 'break' command, extract the condition
9687 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9688 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9689 If no condition is found, *COND_STRING is set to NULL.
9690 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9691
9692static void
bbc13ae3 9693find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9694 char **cond_string, int *thread, int *task,
9695 char **rest)
018d34a4
VP
9696{
9697 *cond_string = NULL;
9698 *thread = -1;
ed1d1739
KS
9699 *task = 0;
9700 *rest = NULL;
9701
018d34a4
VP
9702 while (tok && *tok)
9703 {
bbc13ae3 9704 const char *end_tok;
018d34a4 9705 int toklen;
bbc13ae3
KS
9706 const char *cond_start = NULL;
9707 const char *cond_end = NULL;
cc59ec59 9708
bbc13ae3 9709 tok = skip_spaces_const (tok);
e7e0cddf
SS
9710
9711 if ((*tok == '"' || *tok == ',') && rest)
9712 {
9713 *rest = savestring (tok, strlen (tok));
9714 return;
9715 }
9716
bbc13ae3 9717 end_tok = skip_to_space_const (tok);
d634f2de 9718
018d34a4 9719 toklen = end_tok - tok;
d634f2de 9720
018d34a4
VP
9721 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9722 {
f7545552
TT
9723 struct expression *expr;
9724
018d34a4 9725 tok = cond_start = end_tok + 1;
1bb9788d 9726 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9727 xfree (expr);
018d34a4 9728 cond_end = tok;
d634f2de 9729 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9730 }
9731 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9732 {
9733 char *tmptok;
d634f2de 9734
018d34a4 9735 tok = end_tok + 1;
bbc13ae3 9736 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9737 if (tok == tmptok)
9738 error (_("Junk after thread keyword."));
9739 if (!valid_thread_id (*thread))
af4908ba 9740 invalid_thread_id_error (*thread);
bbc13ae3 9741 tok = tmptok;
018d34a4 9742 }
4a306c9a
JB
9743 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9744 {
9745 char *tmptok;
9746
9747 tok = end_tok + 1;
bbc13ae3 9748 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9749 if (tok == tmptok)
9750 error (_("Junk after task keyword."));
9751 if (!valid_task_id (*task))
b6199126 9752 error (_("Unknown task %d."), *task);
bbc13ae3 9753 tok = tmptok;
4a306c9a 9754 }
e7e0cddf
SS
9755 else if (rest)
9756 {
9757 *rest = savestring (tok, strlen (tok));
ccab2054 9758 return;
e7e0cddf 9759 }
018d34a4
VP
9760 else
9761 error (_("Junk at end of arguments."));
9762 }
9763}
9764
0fb4aa4b
PA
9765/* Decode a static tracepoint marker spec. */
9766
9767static struct symtabs_and_lines
9768decode_static_tracepoint_spec (char **arg_p)
9769{
9770 VEC(static_tracepoint_marker_p) *markers = NULL;
9771 struct symtabs_and_lines sals;
0fb4aa4b
PA
9772 struct cleanup *old_chain;
9773 char *p = &(*arg_p)[3];
9774 char *endp;
9775 char *marker_str;
9776 int i;
9777
e9cafbcc 9778 p = skip_spaces (p);
0fb4aa4b 9779
e9cafbcc 9780 endp = skip_to_space (p);
0fb4aa4b
PA
9781
9782 marker_str = savestring (p, endp - p);
9783 old_chain = make_cleanup (xfree, marker_str);
9784
9785 markers = target_static_tracepoint_markers_by_strid (marker_str);
9786 if (VEC_empty(static_tracepoint_marker_p, markers))
9787 error (_("No known static tracepoint marker named %s"), marker_str);
9788
9789 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9790 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9791
9792 for (i = 0; i < sals.nelts; i++)
9793 {
9794 struct static_tracepoint_marker *marker;
9795
9796 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9797
9798 init_sal (&sals.sals[i]);
9799
9800 sals.sals[i] = find_pc_line (marker->address, 0);
9801 sals.sals[i].pc = marker->address;
9802
9803 release_static_tracepoint_marker (marker);
9804 }
9805
9806 do_cleanups (old_chain);
9807
9808 *arg_p = endp;
9809 return sals;
9810}
9811
fd9b8c24
PA
9812/* Set a breakpoint. This function is shared between CLI and MI
9813 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9814 modes of operations, selected by the PARSE_ARG parameter. If
9815 non-zero, the function will parse ARG, extracting location,
9816 condition, thread and extra string. Otherwise, ARG is just the
9817 breakpoint's location, with condition, thread, and extra string
9818 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9819 If INTERNAL is non-zero, the breakpoint number will be allocated
9820 from the internal breakpoint count. Returns true if any breakpoint
9821 was created; false otherwise. */
0101ce28 9822
8cdf0e15
VP
9823int
9824create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9825 char *arg, char *cond_string,
9826 int thread, char *extra_string,
f6de8ec2 9827 int parse_arg,
0fb4aa4b 9828 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9829 int ignore_count,
9830 enum auto_boolean pending_break_support,
c0a91b2b 9831 const struct breakpoint_ops *ops,
44f238bb
PA
9832 int from_tty, int enabled, int internal,
9833 unsigned flags)
c3f6f71d 9834{
b78a6381 9835 volatile struct gdb_exception e;
f8eba3c6 9836 char *copy_arg = NULL;
c3f6f71d 9837 char *addr_start = arg;
7efd8fc2 9838 struct linespec_result canonical;
c3f6f71d 9839 struct cleanup *old_chain;
80c99de1 9840 struct cleanup *bkpt_chain = NULL;
0101ce28 9841 int pending = 0;
4a306c9a 9842 int task = 0;
86b17b60 9843 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9844
348d480f
PA
9845 gdb_assert (ops != NULL);
9846
7efd8fc2 9847 init_linespec_result (&canonical);
c3f6f71d 9848
b78a6381
TT
9849 TRY_CATCH (e, RETURN_MASK_ALL)
9850 {
983af33b
SDJ
9851 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9852 addr_start, &copy_arg);
b78a6381 9853 }
0101ce28
JJ
9854
9855 /* If caller is interested in rc value from parse, set value. */
05ff989b 9856 switch (e.reason)
0101ce28 9857 {
983af33b
SDJ
9858 case GDB_NO_ERROR:
9859 if (VEC_empty (linespec_sals, canonical.sals))
9860 return 0;
9861 break;
05ff989b
AC
9862 case RETURN_ERROR:
9863 switch (e.error)
0101ce28 9864 {
05ff989b 9865 case NOT_FOUND_ERROR:
0101ce28 9866
05ff989b
AC
9867 /* If pending breakpoint support is turned off, throw
9868 error. */
fa8d40ab
JJ
9869
9870 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9871 throw_exception (e);
9872
9873 exception_print (gdb_stderr, e);
fa8d40ab 9874
05ff989b
AC
9875 /* If pending breakpoint support is auto query and the user
9876 selects no, then simply return the error code. */
059fb39f 9877 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9878 && !nquery (_("Make %s pending on future shared library load? "),
9879 bptype_string (type_wanted)))
fd9b8c24 9880 return 0;
fa8d40ab 9881
05ff989b
AC
9882 /* At this point, either the user was queried about setting
9883 a pending breakpoint and selected yes, or pending
9884 breakpoint behavior is on and thus a pending breakpoint
9885 is defaulted on behalf of the user. */
f8eba3c6
TT
9886 {
9887 struct linespec_sals lsal;
9888
9889 copy_arg = xstrdup (addr_start);
9890 lsal.canonical = xstrdup (copy_arg);
9891 lsal.sals.nelts = 1;
9892 lsal.sals.sals = XNEW (struct symtab_and_line);
9893 init_sal (&lsal.sals.sals[0]);
9894 pending = 1;
9895 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9896 }
05ff989b
AC
9897 break;
9898 default:
98deb0da 9899 throw_exception (e);
0101ce28 9900 }
2abae994 9901 break;
05ff989b 9902 default:
983af33b 9903 throw_exception (e);
0101ce28 9904 }
c3f6f71d 9905
4a64f543 9906 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9907 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9908
c3f6f71d
JM
9909 /* ----------------------------- SNIP -----------------------------
9910 Anything added to the cleanup chain beyond this point is assumed
9911 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9912 then the memory is not reclaimed. */
9913 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9914
c3f6f71d
JM
9915 /* Resolve all line numbers to PC's and verify that the addresses
9916 are ok for the target. */
0101ce28 9917 if (!pending)
f8eba3c6
TT
9918 {
9919 int ix;
9920 struct linespec_sals *iter;
9921
9922 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9923 breakpoint_sals_to_pc (&iter->sals);
9924 }
c3f6f71d 9925
7a697b8d 9926 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9927 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9928 {
9929 int ix;
9930 struct linespec_sals *iter;
9931
9932 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9933 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9934 }
7a697b8d 9935
c3f6f71d
JM
9936 /* Verify that condition can be parsed, before setting any
9937 breakpoints. Allocate a separate condition expression for each
4a64f543 9938 breakpoint. */
0101ce28 9939 if (!pending)
c3f6f71d 9940 {
f6de8ec2 9941 if (parse_arg)
72b2ff0e 9942 {
0878d0fa 9943 char *rest;
52d361e1
YQ
9944 struct linespec_sals *lsal;
9945
9946 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9947
0878d0fa
YQ
9948 /* Here we only parse 'arg' to separate condition
9949 from thread number, so parsing in context of first
9950 sal is OK. When setting the breakpoint we'll
9951 re-parse it in context of each sal. */
9952
9953 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9954 &thread, &task, &rest);
9955 if (cond_string)
9956 make_cleanup (xfree, cond_string);
9957 if (rest)
9958 make_cleanup (xfree, rest);
9959 if (rest)
9960 extra_string = rest;
72b2ff0e 9961 }
2f069f6f 9962 else
72b2ff0e 9963 {
0878d0fa
YQ
9964 if (*arg != '\0')
9965 error (_("Garbage '%s' at end of location"), arg);
9966
9967 /* Create a private copy of condition string. */
9968 if (cond_string)
9969 {
9970 cond_string = xstrdup (cond_string);
9971 make_cleanup (xfree, cond_string);
9972 }
9973 /* Create a private copy of any extra string. */
9974 if (extra_string)
9975 {
9976 extra_string = xstrdup (extra_string);
9977 make_cleanup (xfree, extra_string);
9978 }
72b2ff0e 9979 }
0fb4aa4b 9980
52d361e1 9981 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9982 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9983 tempflag ? disp_del : disp_donttouch,
9984 thread, task, ignore_count, ops,
44f238bb 9985 from_tty, enabled, internal, flags);
c906108c 9986 }
0101ce28
JJ
9987 else
9988 {
0101ce28
JJ
9989 struct breakpoint *b;
9990
0101ce28
JJ
9991 make_cleanup (xfree, copy_arg);
9992
bfccc43c
YQ
9993 if (is_tracepoint_type (type_wanted))
9994 {
9995 struct tracepoint *t;
9996
9997 t = XCNEW (struct tracepoint);
9998 b = &t->base;
9999 }
10000 else
10001 b = XNEW (struct breakpoint);
10002
10003 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10004
f8eba3c6 10005 b->addr_string = copy_arg;
f6de8ec2 10006 if (parse_arg)
e12c7713
MK
10007 b->cond_string = NULL;
10008 else
10009 {
10010 /* Create a private copy of condition string. */
10011 if (cond_string)
10012 {
10013 cond_string = xstrdup (cond_string);
10014 make_cleanup (xfree, cond_string);
10015 }
10016 b->cond_string = cond_string;
10017 }
e7e0cddf 10018 b->extra_string = NULL;
0101ce28 10019 b->ignore_count = ignore_count;
0101ce28 10020 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 10021 b->condition_not_parsed = 1;
41447f92 10022 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
10023 if ((type_wanted != bp_breakpoint
10024 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 10025 b->pspace = current_program_space;
8bea4e01 10026
bfccc43c 10027 install_breakpoint (internal, b, 0);
0101ce28
JJ
10028 }
10029
f8eba3c6 10030 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 10031 {
3e43a32a
MS
10032 warning (_("Multiple breakpoints were set.\nUse the "
10033 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10034 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10035 }
10036
80c99de1
PA
10037 /* That's it. Discard the cleanups for data inserted into the
10038 breakpoint. */
10039 discard_cleanups (bkpt_chain);
10040 /* But cleanup everything else. */
c3f6f71d 10041 do_cleanups (old_chain);
217dc9e2 10042
80c99de1 10043 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 10044 update_global_location_list (1);
fd9b8c24
PA
10045
10046 return 1;
c3f6f71d 10047}
c906108c 10048
348d480f 10049/* Set a breakpoint.
72b2ff0e
VP
10050 ARG is a string describing breakpoint address,
10051 condition, and thread.
10052 FLAG specifies if a breakpoint is hardware on,
10053 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10054 and BP_TEMPFLAG. */
348d480f 10055
98deb0da 10056static void
72b2ff0e 10057break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10058{
72b2ff0e 10059 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10060 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10061 ? bp_hardware_breakpoint
10062 : bp_breakpoint);
55aa24fb
SDJ
10063 struct breakpoint_ops *ops;
10064 const char *arg_cp = arg;
10065
10066 /* Matching breakpoints on probes. */
10067 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10068 ops = &bkpt_probe_breakpoint_ops;
10069 else
10070 ops = &bkpt_breakpoint_ops;
c3f6f71d 10071
8cdf0e15
VP
10072 create_breakpoint (get_current_arch (),
10073 arg,
e7e0cddf 10074 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10075 tempflag, type_wanted,
8cdf0e15
VP
10076 0 /* Ignore count */,
10077 pending_break_support,
55aa24fb 10078 ops,
8cdf0e15 10079 from_tty,
84f4c1fe 10080 1 /* enabled */,
44f238bb
PA
10081 0 /* internal */,
10082 0);
c906108c
SS
10083}
10084
c906108c
SS
10085/* Helper function for break_command_1 and disassemble_command. */
10086
10087void
fba45db2 10088resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10089{
10090 CORE_ADDR pc;
10091
10092 if (sal->pc == 0 && sal->symtab != NULL)
10093 {
10094 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10095 error (_("No line %d in file \"%s\"."),
05cba821 10096 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10097 sal->pc = pc;
6a048695 10098
4a64f543
MS
10099 /* If this SAL corresponds to a breakpoint inserted using a line
10100 number, then skip the function prologue if necessary. */
6a048695 10101 if (sal->explicit_line)
059acae7 10102 skip_prologue_sal (sal);
c906108c
SS
10103 }
10104
10105 if (sal->section == 0 && sal->symtab != NULL)
10106 {
346d1dfe 10107 const struct blockvector *bv;
3977b71f 10108 const struct block *b;
c5aa993b 10109 struct symbol *sym;
c906108c 10110
801e3a5b 10111 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
10112 if (bv != NULL)
10113 {
7f0df278 10114 sym = block_linkage_function (b);
c906108c
SS
10115 if (sym != NULL)
10116 {
10117 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 10118 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
10119 }
10120 else
10121 {
4a64f543
MS
10122 /* It really is worthwhile to have the section, so we'll
10123 just have to look harder. This case can be executed
10124 if we have line numbers but no functions (as can
10125 happen in assembly source). */
c906108c 10126
7cbd4a93 10127 struct bound_minimal_symbol msym;
6c95b8df
PA
10128 struct cleanup *old_chain = save_current_space_and_thread ();
10129
10130 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10131
10132 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10133 if (msym.minsym)
efd66ac6 10134 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10135
10136 do_cleanups (old_chain);
c906108c
SS
10137 }
10138 }
10139 }
10140}
10141
10142void
fba45db2 10143break_command (char *arg, int from_tty)
c906108c 10144{
db107f19 10145 break_command_1 (arg, 0, from_tty);
c906108c
SS
10146}
10147
c906108c 10148void
fba45db2 10149tbreak_command (char *arg, int from_tty)
c906108c 10150{
db107f19 10151 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10152}
10153
c906108c 10154static void
fba45db2 10155hbreak_command (char *arg, int from_tty)
c906108c 10156{
db107f19 10157 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10158}
10159
10160static void
fba45db2 10161thbreak_command (char *arg, int from_tty)
c906108c 10162{
db107f19 10163 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10164}
10165
10166static void
fba45db2 10167stop_command (char *arg, int from_tty)
c906108c 10168{
a3f17187 10169 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10170Usage: stop in <function | address>\n\
a3f17187 10171 stop at <line>\n"));
c906108c
SS
10172}
10173
10174static void
fba45db2 10175stopin_command (char *arg, int from_tty)
c906108c
SS
10176{
10177 int badInput = 0;
10178
c5aa993b 10179 if (arg == (char *) NULL)
c906108c
SS
10180 badInput = 1;
10181 else if (*arg != '*')
10182 {
10183 char *argptr = arg;
10184 int hasColon = 0;
10185
4a64f543 10186 /* Look for a ':'. If this is a line number specification, then
53a5351d 10187 say it is bad, otherwise, it should be an address or
4a64f543 10188 function/method name. */
c906108c 10189 while (*argptr && !hasColon)
c5aa993b
JM
10190 {
10191 hasColon = (*argptr == ':');
10192 argptr++;
10193 }
c906108c
SS
10194
10195 if (hasColon)
c5aa993b 10196 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10197 else
c5aa993b 10198 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10199 }
10200
10201 if (badInput)
a3f17187 10202 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10203 else
db107f19 10204 break_command_1 (arg, 0, from_tty);
c906108c
SS
10205}
10206
10207static void
fba45db2 10208stopat_command (char *arg, int from_tty)
c906108c
SS
10209{
10210 int badInput = 0;
10211
c5aa993b 10212 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10213 badInput = 1;
10214 else
10215 {
10216 char *argptr = arg;
10217 int hasColon = 0;
10218
4a64f543
MS
10219 /* Look for a ':'. If there is a '::' then get out, otherwise
10220 it is probably a line number. */
c906108c 10221 while (*argptr && !hasColon)
c5aa993b
JM
10222 {
10223 hasColon = (*argptr == ':');
10224 argptr++;
10225 }
c906108c
SS
10226
10227 if (hasColon)
c5aa993b 10228 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10229 else
c5aa993b 10230 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10231 }
10232
10233 if (badInput)
a3f17187 10234 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10235 else
db107f19 10236 break_command_1 (arg, 0, from_tty);
c906108c
SS
10237}
10238
e7e0cddf
SS
10239/* The dynamic printf command is mostly like a regular breakpoint, but
10240 with a prewired command list consisting of a single output command,
10241 built from extra arguments supplied on the dprintf command
10242 line. */
10243
da821c7b 10244static void
e7e0cddf
SS
10245dprintf_command (char *arg, int from_tty)
10246{
10247 create_breakpoint (get_current_arch (),
10248 arg,
10249 NULL, 0, NULL, 1 /* parse arg */,
10250 0, bp_dprintf,
10251 0 /* Ignore count */,
10252 pending_break_support,
10253 &dprintf_breakpoint_ops,
10254 from_tty,
10255 1 /* enabled */,
10256 0 /* internal */,
10257 0);
10258}
10259
d3ce09f5
SS
10260static void
10261agent_printf_command (char *arg, int from_tty)
10262{
10263 error (_("May only run agent-printf on the target"));
10264}
10265
f1310107
TJB
10266/* Implement the "breakpoint_hit" breakpoint_ops method for
10267 ranged breakpoints. */
10268
10269static int
10270breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10271 struct address_space *aspace,
09ac7c10
TT
10272 CORE_ADDR bp_addr,
10273 const struct target_waitstatus *ws)
f1310107 10274{
09ac7c10 10275 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10276 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10277 return 0;
10278
f1310107
TJB
10279 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10280 bl->length, aspace, bp_addr);
10281}
10282
10283/* Implement the "resources_needed" breakpoint_ops method for
10284 ranged breakpoints. */
10285
10286static int
10287resources_needed_ranged_breakpoint (const struct bp_location *bl)
10288{
10289 return target_ranged_break_num_registers ();
10290}
10291
10292/* Implement the "print_it" breakpoint_ops method for
10293 ranged breakpoints. */
10294
10295static enum print_stop_action
348d480f 10296print_it_ranged_breakpoint (bpstat bs)
f1310107 10297{
348d480f 10298 struct breakpoint *b = bs->breakpoint_at;
f1310107 10299 struct bp_location *bl = b->loc;
79a45e25 10300 struct ui_out *uiout = current_uiout;
f1310107
TJB
10301
10302 gdb_assert (b->type == bp_hardware_breakpoint);
10303
10304 /* Ranged breakpoints have only one location. */
10305 gdb_assert (bl && bl->next == NULL);
10306
10307 annotate_breakpoint (b->number);
10308 if (b->disposition == disp_del)
10309 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10310 else
10311 ui_out_text (uiout, "\nRanged breakpoint ");
10312 if (ui_out_is_mi_like_p (uiout))
10313 {
10314 ui_out_field_string (uiout, "reason",
10315 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10316 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10317 }
10318 ui_out_field_int (uiout, "bkptno", b->number);
10319 ui_out_text (uiout, ", ");
10320
10321 return PRINT_SRC_AND_LOC;
10322}
10323
10324/* Implement the "print_one" breakpoint_ops method for
10325 ranged breakpoints. */
10326
10327static void
10328print_one_ranged_breakpoint (struct breakpoint *b,
10329 struct bp_location **last_loc)
10330{
10331 struct bp_location *bl = b->loc;
10332 struct value_print_options opts;
79a45e25 10333 struct ui_out *uiout = current_uiout;
f1310107
TJB
10334
10335 /* Ranged breakpoints have only one location. */
10336 gdb_assert (bl && bl->next == NULL);
10337
10338 get_user_print_options (&opts);
10339
10340 if (opts.addressprint)
10341 /* We don't print the address range here, it will be printed later
10342 by print_one_detail_ranged_breakpoint. */
10343 ui_out_field_skip (uiout, "addr");
10344 annotate_field (5);
10345 print_breakpoint_location (b, bl);
10346 *last_loc = bl;
10347}
10348
10349/* Implement the "print_one_detail" breakpoint_ops method for
10350 ranged breakpoints. */
10351
10352static void
10353print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10354 struct ui_out *uiout)
10355{
10356 CORE_ADDR address_start, address_end;
10357 struct bp_location *bl = b->loc;
f99d8bf4
PA
10358 struct ui_file *stb = mem_fileopen ();
10359 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10360
10361 gdb_assert (bl);
10362
10363 address_start = bl->address;
10364 address_end = address_start + bl->length - 1;
10365
10366 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10367 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10368 print_core_address (bl->gdbarch, address_start),
10369 print_core_address (bl->gdbarch, address_end));
10370 ui_out_field_stream (uiout, "addr", stb);
10371 ui_out_text (uiout, "\n");
10372
10373 do_cleanups (cleanup);
10374}
10375
10376/* Implement the "print_mention" breakpoint_ops method for
10377 ranged breakpoints. */
10378
10379static void
10380print_mention_ranged_breakpoint (struct breakpoint *b)
10381{
10382 struct bp_location *bl = b->loc;
79a45e25 10383 struct ui_out *uiout = current_uiout;
f1310107
TJB
10384
10385 gdb_assert (bl);
10386 gdb_assert (b->type == bp_hardware_breakpoint);
10387
10388 if (ui_out_is_mi_like_p (uiout))
10389 return;
10390
10391 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10392 b->number, paddress (bl->gdbarch, bl->address),
10393 paddress (bl->gdbarch, bl->address + bl->length - 1));
10394}
10395
10396/* Implement the "print_recreate" breakpoint_ops method for
10397 ranged breakpoints. */
10398
10399static void
10400print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10401{
10402 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10403 b->addr_string_range_end);
d9b3f62e 10404 print_recreate_thread (b, fp);
f1310107
TJB
10405}
10406
10407/* The breakpoint_ops structure to be used in ranged breakpoints. */
10408
2060206e 10409static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10410
10411/* Find the address where the end of the breakpoint range should be
10412 placed, given the SAL of the end of the range. This is so that if
10413 the user provides a line number, the end of the range is set to the
10414 last instruction of the given line. */
10415
10416static CORE_ADDR
10417find_breakpoint_range_end (struct symtab_and_line sal)
10418{
10419 CORE_ADDR end;
10420
10421 /* If the user provided a PC value, use it. Otherwise,
10422 find the address of the end of the given location. */
10423 if (sal.explicit_pc)
10424 end = sal.pc;
10425 else
10426 {
10427 int ret;
10428 CORE_ADDR start;
10429
10430 ret = find_line_pc_range (sal, &start, &end);
10431 if (!ret)
10432 error (_("Could not find location of the end of the range."));
10433
10434 /* find_line_pc_range returns the start of the next line. */
10435 end--;
10436 }
10437
10438 return end;
10439}
10440
10441/* Implement the "break-range" CLI command. */
10442
10443static void
10444break_range_command (char *arg, int from_tty)
10445{
10446 char *arg_start, *addr_string_start, *addr_string_end;
10447 struct linespec_result canonical_start, canonical_end;
10448 int bp_count, can_use_bp, length;
10449 CORE_ADDR end;
10450 struct breakpoint *b;
10451 struct symtab_and_line sal_start, sal_end;
f1310107 10452 struct cleanup *cleanup_bkpt;
f8eba3c6 10453 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10454
10455 /* We don't support software ranged breakpoints. */
10456 if (target_ranged_break_num_registers () < 0)
10457 error (_("This target does not support hardware ranged breakpoints."));
10458
10459 bp_count = hw_breakpoint_used_count ();
10460 bp_count += target_ranged_break_num_registers ();
10461 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10462 bp_count, 0);
10463 if (can_use_bp < 0)
10464 error (_("Hardware breakpoints used exceeds limit."));
10465
f8eba3c6 10466 arg = skip_spaces (arg);
f1310107
TJB
10467 if (arg == NULL || arg[0] == '\0')
10468 error(_("No address range specified."));
10469
f1310107
TJB
10470 init_linespec_result (&canonical_start);
10471
f8eba3c6
TT
10472 arg_start = arg;
10473 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10474
f8eba3c6 10475 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10476
10477 if (arg[0] != ',')
10478 error (_("Too few arguments."));
f8eba3c6 10479 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10480 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10481
10482 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10483
10484 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10485 || lsal_start->sals.nelts != 1)
f1310107
TJB
10486 error (_("Cannot create a ranged breakpoint with multiple locations."));
10487
f8eba3c6
TT
10488 sal_start = lsal_start->sals.sals[0];
10489 addr_string_start = savestring (arg_start, arg - arg_start);
10490 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10491
10492 arg++; /* Skip the comma. */
f8eba3c6 10493 arg = skip_spaces (arg);
f1310107
TJB
10494
10495 /* Parse the end location. */
10496
f1310107
TJB
10497 init_linespec_result (&canonical_end);
10498 arg_start = arg;
10499
f8eba3c6 10500 /* We call decode_line_full directly here instead of using
f1310107
TJB
10501 parse_breakpoint_sals because we need to specify the start location's
10502 symtab and line as the default symtab and line for the end of the
10503 range. This makes it possible to have ranges like "foo.c:27, +14",
10504 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10505 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10506 sal_start.symtab, sal_start.line,
10507 &canonical_end, NULL, NULL);
10508
10509 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10510
f8eba3c6 10511 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10512 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10513
10514 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10515 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10516 || lsal_end->sals.nelts != 1)
f1310107
TJB
10517 error (_("Cannot create a ranged breakpoint with multiple locations."));
10518
f8eba3c6
TT
10519 sal_end = lsal_end->sals.sals[0];
10520 addr_string_end = savestring (arg_start, arg - arg_start);
10521 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10522
10523 end = find_breakpoint_range_end (sal_end);
10524 if (sal_start.pc > end)
177b42fe 10525 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10526
10527 length = end - sal_start.pc + 1;
10528 if (length < 0)
10529 /* Length overflowed. */
10530 error (_("Address range too large."));
10531 else if (length == 1)
10532 {
10533 /* This range is simple enough to be handled by
10534 the `hbreak' command. */
10535 hbreak_command (addr_string_start, 1);
10536
10537 do_cleanups (cleanup_bkpt);
10538
10539 return;
10540 }
10541
10542 /* Now set up the breakpoint. */
10543 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10544 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10545 set_breakpoint_count (breakpoint_count + 1);
10546 b->number = breakpoint_count;
10547 b->disposition = disp_donttouch;
f8eba3c6
TT
10548 b->addr_string = xstrdup (addr_string_start);
10549 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10550 b->loc->length = length;
10551
f8eba3c6 10552 do_cleanups (cleanup_bkpt);
f1310107
TJB
10553
10554 mention (b);
8d3788bd 10555 observer_notify_breakpoint_created (b);
f1310107
TJB
10556 update_global_location_list (1);
10557}
10558
4a64f543
MS
10559/* Return non-zero if EXP is verified as constant. Returned zero
10560 means EXP is variable. Also the constant detection may fail for
10561 some constant expressions and in such case still falsely return
10562 zero. */
2e6e3d9c 10563
65d79d4b
SDJ
10564static int
10565watchpoint_exp_is_const (const struct expression *exp)
10566{
10567 int i = exp->nelts;
10568
10569 while (i > 0)
10570 {
10571 int oplenp, argsp;
10572
10573 /* We are only interested in the descriptor of each element. */
10574 operator_length (exp, i, &oplenp, &argsp);
10575 i -= oplenp;
10576
10577 switch (exp->elts[i].opcode)
10578 {
10579 case BINOP_ADD:
10580 case BINOP_SUB:
10581 case BINOP_MUL:
10582 case BINOP_DIV:
10583 case BINOP_REM:
10584 case BINOP_MOD:
10585 case BINOP_LSH:
10586 case BINOP_RSH:
10587 case BINOP_LOGICAL_AND:
10588 case BINOP_LOGICAL_OR:
10589 case BINOP_BITWISE_AND:
10590 case BINOP_BITWISE_IOR:
10591 case BINOP_BITWISE_XOR:
10592 case BINOP_EQUAL:
10593 case BINOP_NOTEQUAL:
10594 case BINOP_LESS:
10595 case BINOP_GTR:
10596 case BINOP_LEQ:
10597 case BINOP_GEQ:
10598 case BINOP_REPEAT:
10599 case BINOP_COMMA:
10600 case BINOP_EXP:
10601 case BINOP_MIN:
10602 case BINOP_MAX:
10603 case BINOP_INTDIV:
10604 case BINOP_CONCAT:
10605 case BINOP_IN:
10606 case BINOP_RANGE:
10607 case TERNOP_COND:
10608 case TERNOP_SLICE:
65d79d4b
SDJ
10609
10610 case OP_LONG:
10611 case OP_DOUBLE:
10612 case OP_DECFLOAT:
10613 case OP_LAST:
10614 case OP_COMPLEX:
10615 case OP_STRING:
65d79d4b
SDJ
10616 case OP_ARRAY:
10617 case OP_TYPE:
608b4967
TT
10618 case OP_TYPEOF:
10619 case OP_DECLTYPE:
6e72ca20 10620 case OP_TYPEID:
65d79d4b
SDJ
10621 case OP_NAME:
10622 case OP_OBJC_NSSTRING:
10623
10624 case UNOP_NEG:
10625 case UNOP_LOGICAL_NOT:
10626 case UNOP_COMPLEMENT:
10627 case UNOP_ADDR:
10628 case UNOP_HIGH:
aeaa2474 10629 case UNOP_CAST:
9eaf6705
TT
10630
10631 case UNOP_CAST_TYPE:
10632 case UNOP_REINTERPRET_CAST:
10633 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10634 /* Unary, binary and ternary operators: We have to check
10635 their operands. If they are constant, then so is the
10636 result of that operation. For instance, if A and B are
10637 determined to be constants, then so is "A + B".
10638
10639 UNOP_IND is one exception to the rule above, because the
10640 value of *ADDR is not necessarily a constant, even when
10641 ADDR is. */
65d79d4b
SDJ
10642 break;
10643
10644 case OP_VAR_VALUE:
10645 /* Check whether the associated symbol is a constant.
4a64f543 10646
65d79d4b 10647 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10648 possible that a buggy compiler could mark a variable as
10649 constant even when it is not, and TYPE_CONST would return
10650 true in this case, while SYMBOL_CLASS wouldn't.
10651
10652 We also have to check for function symbols because they
10653 are always constant. */
65d79d4b
SDJ
10654 {
10655 struct symbol *s = exp->elts[i + 2].symbol;
10656
10657 if (SYMBOL_CLASS (s) != LOC_BLOCK
10658 && SYMBOL_CLASS (s) != LOC_CONST
10659 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10660 return 0;
10661 break;
10662 }
10663
10664 /* The default action is to return 0 because we are using
10665 the optimistic approach here: If we don't know something,
10666 then it is not a constant. */
10667 default:
10668 return 0;
10669 }
10670 }
10671
10672 return 1;
10673}
10674
3a5c3e22
PA
10675/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10676
10677static void
10678dtor_watchpoint (struct breakpoint *self)
10679{
10680 struct watchpoint *w = (struct watchpoint *) self;
10681
10682 xfree (w->cond_exp);
10683 xfree (w->exp);
10684 xfree (w->exp_string);
10685 xfree (w->exp_string_reparse);
10686 value_free (w->val);
10687
10688 base_breakpoint_ops.dtor (self);
10689}
10690
348d480f
PA
10691/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10692
10693static void
10694re_set_watchpoint (struct breakpoint *b)
10695{
3a5c3e22
PA
10696 struct watchpoint *w = (struct watchpoint *) b;
10697
348d480f
PA
10698 /* Watchpoint can be either on expression using entirely global
10699 variables, or it can be on local variables.
10700
10701 Watchpoints of the first kind are never auto-deleted, and even
10702 persist across program restarts. Since they can use variables
10703 from shared libraries, we need to reparse expression as libraries
10704 are loaded and unloaded.
10705
10706 Watchpoints on local variables can also change meaning as result
10707 of solib event. For example, if a watchpoint uses both a local
10708 and a global variables in expression, it's a local watchpoint,
10709 but unloading of a shared library will make the expression
10710 invalid. This is not a very common use case, but we still
10711 re-evaluate expression, to avoid surprises to the user.
10712
10713 Note that for local watchpoints, we re-evaluate it only if
10714 watchpoints frame id is still valid. If it's not, it means the
10715 watchpoint is out of scope and will be deleted soon. In fact,
10716 I'm not sure we'll ever be called in this case.
10717
10718 If a local watchpoint's frame id is still valid, then
3a5c3e22 10719 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10720
3a5c3e22
PA
10721 Don't do anything about disabled watchpoints, since they will be
10722 reevaluated again when enabled. */
10723 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10724}
10725
77b06cd7
TJB
10726/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10727
10728static int
10729insert_watchpoint (struct bp_location *bl)
10730{
3a5c3e22
PA
10731 struct watchpoint *w = (struct watchpoint *) bl->owner;
10732 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10733
10734 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10735 w->cond_exp);
77b06cd7
TJB
10736}
10737
10738/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10739
10740static int
10741remove_watchpoint (struct bp_location *bl)
10742{
3a5c3e22
PA
10743 struct watchpoint *w = (struct watchpoint *) bl->owner;
10744 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10745
10746 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10747 w->cond_exp);
e09342b5
TJB
10748}
10749
e09342b5 10750static int
348d480f 10751breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10752 struct address_space *aspace, CORE_ADDR bp_addr,
10753 const struct target_waitstatus *ws)
e09342b5 10754{
348d480f 10755 struct breakpoint *b = bl->owner;
3a5c3e22 10756 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10757
348d480f
PA
10758 /* Continuable hardware watchpoints are treated as non-existent if the
10759 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10760 some data address). Otherwise gdb won't stop on a break instruction
10761 in the code (not from a breakpoint) when a hardware watchpoint has
10762 been defined. Also skip watchpoints which we know did not trigger
10763 (did not match the data address). */
10764 if (is_hardware_watchpoint (b)
3a5c3e22 10765 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10766 return 0;
9c06b0b4 10767
348d480f 10768 return 1;
9c06b0b4
TJB
10769}
10770
348d480f
PA
10771static void
10772check_status_watchpoint (bpstat bs)
9c06b0b4 10773{
348d480f 10774 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10775
348d480f 10776 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10777}
10778
10779/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10780 hardware watchpoints. */
9c06b0b4
TJB
10781
10782static int
348d480f 10783resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10784{
3a5c3e22
PA
10785 struct watchpoint *w = (struct watchpoint *) bl->owner;
10786 int length = w->exact? 1 : bl->length;
348d480f
PA
10787
10788 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10789}
10790
10791/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10792 hardware watchpoints. */
9c06b0b4
TJB
10793
10794static int
348d480f 10795works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10796{
efa80663
PA
10797 /* Read and access watchpoints only work with hardware support. */
10798 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10799}
10800
9c06b0b4 10801static enum print_stop_action
348d480f 10802print_it_watchpoint (bpstat bs)
9c06b0b4 10803{
348d480f
PA
10804 struct cleanup *old_chain;
10805 struct breakpoint *b;
f99d8bf4 10806 struct ui_file *stb;
348d480f 10807 enum print_stop_action result;
3a5c3e22 10808 struct watchpoint *w;
79a45e25 10809 struct ui_out *uiout = current_uiout;
348d480f
PA
10810
10811 gdb_assert (bs->bp_location_at != NULL);
10812
348d480f 10813 b = bs->breakpoint_at;
3a5c3e22 10814 w = (struct watchpoint *) b;
348d480f 10815
f99d8bf4
PA
10816 stb = mem_fileopen ();
10817 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10818
10819 switch (b->type)
10820 {
348d480f 10821 case bp_watchpoint:
9c06b0b4
TJB
10822 case bp_hardware_watchpoint:
10823 annotate_watchpoint (b->number);
10824 if (ui_out_is_mi_like_p (uiout))
10825 ui_out_field_string
10826 (uiout, "reason",
10827 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10828 mention (b);
10829 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10830 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10831 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10832 ui_out_field_stream (uiout, "old", stb);
10833 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10834 watchpoint_value_print (w->val, stb);
348d480f
PA
10835 ui_out_field_stream (uiout, "new", stb);
10836 ui_out_text (uiout, "\n");
10837 /* More than one watchpoint may have been triggered. */
10838 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10839 break;
10840
10841 case bp_read_watchpoint:
10842 if (ui_out_is_mi_like_p (uiout))
10843 ui_out_field_string
10844 (uiout, "reason",
10845 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10846 mention (b);
10847 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10848 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10849 watchpoint_value_print (w->val, stb);
348d480f
PA
10850 ui_out_field_stream (uiout, "value", stb);
10851 ui_out_text (uiout, "\n");
10852 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10853 break;
10854
10855 case bp_access_watchpoint:
348d480f
PA
10856 if (bs->old_val != NULL)
10857 {
10858 annotate_watchpoint (b->number);
10859 if (ui_out_is_mi_like_p (uiout))
10860 ui_out_field_string
10861 (uiout, "reason",
10862 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10863 mention (b);
10864 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10865 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10866 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10867 ui_out_field_stream (uiout, "old", stb);
10868 ui_out_text (uiout, "\nNew value = ");
10869 }
10870 else
10871 {
10872 mention (b);
10873 if (ui_out_is_mi_like_p (uiout))
10874 ui_out_field_string
10875 (uiout, "reason",
10876 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10877 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10878 ui_out_text (uiout, "\nValue = ");
10879 }
f99d8bf4 10880 watchpoint_value_print (w->val, stb);
348d480f
PA
10881 ui_out_field_stream (uiout, "new", stb);
10882 ui_out_text (uiout, "\n");
10883 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10884 break;
10885 default:
348d480f 10886 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10887 }
10888
348d480f
PA
10889 do_cleanups (old_chain);
10890 return result;
10891}
10892
10893/* Implement the "print_mention" breakpoint_ops method for hardware
10894 watchpoints. */
10895
10896static void
10897print_mention_watchpoint (struct breakpoint *b)
10898{
10899 struct cleanup *ui_out_chain;
3a5c3e22 10900 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10901 struct ui_out *uiout = current_uiout;
348d480f
PA
10902
10903 switch (b->type)
10904 {
10905 case bp_watchpoint:
10906 ui_out_text (uiout, "Watchpoint ");
10907 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10908 break;
10909 case bp_hardware_watchpoint:
10910 ui_out_text (uiout, "Hardware watchpoint ");
10911 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10912 break;
10913 case bp_read_watchpoint:
10914 ui_out_text (uiout, "Hardware read watchpoint ");
10915 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10916 break;
10917 case bp_access_watchpoint:
10918 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10919 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10920 break;
10921 default:
10922 internal_error (__FILE__, __LINE__,
10923 _("Invalid hardware watchpoint type."));
10924 }
10925
10926 ui_out_field_int (uiout, "number", b->number);
10927 ui_out_text (uiout, ": ");
3a5c3e22 10928 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10929 do_cleanups (ui_out_chain);
10930}
10931
10932/* Implement the "print_recreate" breakpoint_ops method for
10933 watchpoints. */
10934
10935static void
10936print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10937{
3a5c3e22
PA
10938 struct watchpoint *w = (struct watchpoint *) b;
10939
348d480f
PA
10940 switch (b->type)
10941 {
10942 case bp_watchpoint:
10943 case bp_hardware_watchpoint:
10944 fprintf_unfiltered (fp, "watch");
10945 break;
10946 case bp_read_watchpoint:
10947 fprintf_unfiltered (fp, "rwatch");
10948 break;
10949 case bp_access_watchpoint:
10950 fprintf_unfiltered (fp, "awatch");
10951 break;
10952 default:
10953 internal_error (__FILE__, __LINE__,
10954 _("Invalid watchpoint type."));
10955 }
10956
3a5c3e22 10957 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10958 print_recreate_thread (b, fp);
348d480f
PA
10959}
10960
427cd150
TT
10961/* Implement the "explains_signal" breakpoint_ops method for
10962 watchpoints. */
10963
47591c29 10964static int
427cd150
TT
10965explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10966{
10967 /* A software watchpoint cannot cause a signal other than
10968 GDB_SIGNAL_TRAP. */
10969 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10970 return 0;
427cd150 10971
47591c29 10972 return 1;
427cd150
TT
10973}
10974
348d480f
PA
10975/* The breakpoint_ops structure to be used in hardware watchpoints. */
10976
2060206e 10977static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10978
10979/* Implement the "insert" breakpoint_ops method for
10980 masked hardware watchpoints. */
10981
10982static int
10983insert_masked_watchpoint (struct bp_location *bl)
10984{
3a5c3e22
PA
10985 struct watchpoint *w = (struct watchpoint *) bl->owner;
10986
10987 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10988 bl->watchpoint_type);
10989}
10990
10991/* Implement the "remove" breakpoint_ops method for
10992 masked hardware watchpoints. */
10993
10994static int
10995remove_masked_watchpoint (struct bp_location *bl)
10996{
3a5c3e22
PA
10997 struct watchpoint *w = (struct watchpoint *) bl->owner;
10998
10999 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11000 bl->watchpoint_type);
11001}
11002
11003/* Implement the "resources_needed" breakpoint_ops method for
11004 masked hardware watchpoints. */
11005
11006static int
11007resources_needed_masked_watchpoint (const struct bp_location *bl)
11008{
3a5c3e22
PA
11009 struct watchpoint *w = (struct watchpoint *) bl->owner;
11010
11011 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
11012}
11013
11014/* Implement the "works_in_software_mode" breakpoint_ops method for
11015 masked hardware watchpoints. */
11016
11017static int
11018works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11019{
11020 return 0;
11021}
11022
11023/* Implement the "print_it" breakpoint_ops method for
11024 masked hardware watchpoints. */
11025
11026static enum print_stop_action
11027print_it_masked_watchpoint (bpstat bs)
11028{
11029 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11030 struct ui_out *uiout = current_uiout;
348d480f
PA
11031
11032 /* Masked watchpoints have only one location. */
11033 gdb_assert (b->loc && b->loc->next == NULL);
11034
11035 switch (b->type)
11036 {
11037 case bp_hardware_watchpoint:
11038 annotate_watchpoint (b->number);
11039 if (ui_out_is_mi_like_p (uiout))
11040 ui_out_field_string
11041 (uiout, "reason",
11042 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11043 break;
11044
11045 case bp_read_watchpoint:
11046 if (ui_out_is_mi_like_p (uiout))
11047 ui_out_field_string
11048 (uiout, "reason",
11049 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11050 break;
11051
11052 case bp_access_watchpoint:
11053 if (ui_out_is_mi_like_p (uiout))
11054 ui_out_field_string
11055 (uiout, "reason",
11056 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11057 break;
11058 default:
11059 internal_error (__FILE__, __LINE__,
11060 _("Invalid hardware watchpoint type."));
11061 }
11062
11063 mention (b);
9c06b0b4
TJB
11064 ui_out_text (uiout, _("\n\
11065Check the underlying instruction at PC for the memory\n\
11066address and value which triggered this watchpoint.\n"));
11067 ui_out_text (uiout, "\n");
11068
11069 /* More than one watchpoint may have been triggered. */
11070 return PRINT_UNKNOWN;
11071}
11072
11073/* Implement the "print_one_detail" breakpoint_ops method for
11074 masked hardware watchpoints. */
11075
11076static void
11077print_one_detail_masked_watchpoint (const struct breakpoint *b,
11078 struct ui_out *uiout)
11079{
3a5c3e22
PA
11080 struct watchpoint *w = (struct watchpoint *) b;
11081
9c06b0b4
TJB
11082 /* Masked watchpoints have only one location. */
11083 gdb_assert (b->loc && b->loc->next == NULL);
11084
11085 ui_out_text (uiout, "\tmask ");
3a5c3e22 11086 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11087 ui_out_text (uiout, "\n");
11088}
11089
11090/* Implement the "print_mention" breakpoint_ops method for
11091 masked hardware watchpoints. */
11092
11093static void
11094print_mention_masked_watchpoint (struct breakpoint *b)
11095{
3a5c3e22 11096 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11097 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11098 struct cleanup *ui_out_chain;
11099
11100 switch (b->type)
11101 {
11102 case bp_hardware_watchpoint:
11103 ui_out_text (uiout, "Masked hardware watchpoint ");
11104 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11105 break;
11106 case bp_read_watchpoint:
11107 ui_out_text (uiout, "Masked hardware read watchpoint ");
11108 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11109 break;
11110 case bp_access_watchpoint:
11111 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11112 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11113 break;
11114 default:
11115 internal_error (__FILE__, __LINE__,
11116 _("Invalid hardware watchpoint type."));
11117 }
11118
11119 ui_out_field_int (uiout, "number", b->number);
11120 ui_out_text (uiout, ": ");
3a5c3e22 11121 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11122 do_cleanups (ui_out_chain);
11123}
11124
11125/* Implement the "print_recreate" breakpoint_ops method for
11126 masked hardware watchpoints. */
11127
11128static void
11129print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11130{
3a5c3e22 11131 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11132 char tmp[40];
11133
11134 switch (b->type)
11135 {
11136 case bp_hardware_watchpoint:
11137 fprintf_unfiltered (fp, "watch");
11138 break;
11139 case bp_read_watchpoint:
11140 fprintf_unfiltered (fp, "rwatch");
11141 break;
11142 case bp_access_watchpoint:
11143 fprintf_unfiltered (fp, "awatch");
11144 break;
11145 default:
11146 internal_error (__FILE__, __LINE__,
11147 _("Invalid hardware watchpoint type."));
11148 }
11149
3a5c3e22
PA
11150 sprintf_vma (tmp, w->hw_wp_mask);
11151 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11152 print_recreate_thread (b, fp);
9c06b0b4
TJB
11153}
11154
11155/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11156
2060206e 11157static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11158
11159/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11160
11161static int
11162is_masked_watchpoint (const struct breakpoint *b)
11163{
11164 return b->ops == &masked_watchpoint_breakpoint_ops;
11165}
11166
53a5351d
JM
11167/* accessflag: hw_write: watch write,
11168 hw_read: watch read,
11169 hw_access: watch access (read or write) */
c906108c 11170static void
bbc13ae3 11171watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11172 int just_location, int internal)
c906108c 11173{
a9634178 11174 volatile struct gdb_exception e;
d983da9c 11175 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11176 struct expression *exp;
270140bd 11177 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11178 struct value *val, *mark, *result;
c906108c 11179 struct frame_info *frame;
bbc13ae3
KS
11180 const char *exp_start = NULL;
11181 const char *exp_end = NULL;
11182 const char *tok, *end_tok;
9c06b0b4 11183 int toklen = -1;
bbc13ae3
KS
11184 const char *cond_start = NULL;
11185 const char *cond_end = NULL;
c906108c 11186 enum bptype bp_type;
37e4754d 11187 int thread = -1;
0cf6dd15 11188 int pc = 0;
9c06b0b4
TJB
11189 /* Flag to indicate whether we are going to use masks for
11190 the hardware watchpoint. */
11191 int use_mask = 0;
11192 CORE_ADDR mask = 0;
3a5c3e22 11193 struct watchpoint *w;
bbc13ae3
KS
11194 char *expression;
11195 struct cleanup *back_to;
c906108c 11196
37e4754d
LM
11197 /* Make sure that we actually have parameters to parse. */
11198 if (arg != NULL && arg[0] != '\0')
11199 {
bbc13ae3
KS
11200 const char *value_start;
11201
11202 exp_end = arg + strlen (arg);
37e4754d 11203
9c06b0b4
TJB
11204 /* Look for "parameter value" pairs at the end
11205 of the arguments string. */
bbc13ae3 11206 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11207 {
11208 /* Skip whitespace at the end of the argument list. */
11209 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11210 tok--;
11211
11212 /* Find the beginning of the last token.
11213 This is the value of the parameter. */
11214 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11215 tok--;
11216 value_start = tok + 1;
11217
11218 /* Skip whitespace. */
11219 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11220 tok--;
11221
11222 end_tok = tok;
11223
11224 /* Find the beginning of the second to last token.
11225 This is the parameter itself. */
11226 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11227 tok--;
11228 tok++;
11229 toklen = end_tok - tok + 1;
11230
11231 if (toklen == 6 && !strncmp (tok, "thread", 6))
11232 {
11233 /* At this point we've found a "thread" token, which means
11234 the user is trying to set a watchpoint that triggers
11235 only in a specific thread. */
11236 char *endp;
37e4754d 11237
9c06b0b4
TJB
11238 if (thread != -1)
11239 error(_("You can specify only one thread."));
37e4754d 11240
9c06b0b4
TJB
11241 /* Extract the thread ID from the next token. */
11242 thread = strtol (value_start, &endp, 0);
37e4754d 11243
9c06b0b4
TJB
11244 /* Check if the user provided a valid numeric value for the
11245 thread ID. */
11246 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11247 error (_("Invalid thread ID specification %s."), value_start);
11248
11249 /* Check if the thread actually exists. */
11250 if (!valid_thread_id (thread))
af4908ba 11251 invalid_thread_id_error (thread);
9c06b0b4
TJB
11252 }
11253 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11254 {
11255 /* We've found a "mask" token, which means the user wants to
11256 create a hardware watchpoint that is going to have the mask
11257 facility. */
11258 struct value *mask_value, *mark;
37e4754d 11259
9c06b0b4
TJB
11260 if (use_mask)
11261 error(_("You can specify only one mask."));
37e4754d 11262
9c06b0b4 11263 use_mask = just_location = 1;
37e4754d 11264
9c06b0b4
TJB
11265 mark = value_mark ();
11266 mask_value = parse_to_comma_and_eval (&value_start);
11267 mask = value_as_address (mask_value);
11268 value_free_to_mark (mark);
11269 }
11270 else
11271 /* We didn't recognize what we found. We should stop here. */
11272 break;
37e4754d 11273
9c06b0b4
TJB
11274 /* Truncate the string and get rid of the "parameter value" pair before
11275 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11276 exp_end = tok;
9c06b0b4 11277 }
37e4754d 11278 }
bbc13ae3
KS
11279 else
11280 exp_end = arg;
37e4754d 11281
bbc13ae3
KS
11282 /* Parse the rest of the arguments. From here on out, everything
11283 is in terms of a newly allocated string instead of the original
11284 ARG. */
c906108c 11285 innermost_block = NULL;
bbc13ae3
KS
11286 expression = savestring (arg, exp_end - arg);
11287 back_to = make_cleanup (xfree, expression);
11288 exp_start = arg = expression;
1bb9788d 11289 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11290 exp_end = arg;
fa8a61dc
TT
11291 /* Remove trailing whitespace from the expression before saving it.
11292 This makes the eventual display of the expression string a bit
11293 prettier. */
11294 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11295 --exp_end;
11296
65d79d4b
SDJ
11297 /* Checking if the expression is not constant. */
11298 if (watchpoint_exp_is_const (exp))
11299 {
11300 int len;
11301
11302 len = exp_end - exp_start;
11303 while (len > 0 && isspace (exp_start[len - 1]))
11304 len--;
11305 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11306 }
11307
c906108c
SS
11308 exp_valid_block = innermost_block;
11309 mark = value_mark ();
3a1115a0 11310 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b
TT
11311
11312 if (just_location)
11313 {
9c06b0b4
TJB
11314 int ret;
11315
06a64a0b 11316 exp_valid_block = NULL;
a1442452 11317 val = value_addr (result);
06a64a0b
TT
11318 release_value (val);
11319 value_free_to_mark (mark);
9c06b0b4
TJB
11320
11321 if (use_mask)
11322 {
11323 ret = target_masked_watch_num_registers (value_as_address (val),
11324 mask);
11325 if (ret == -1)
11326 error (_("This target does not support masked watchpoints."));
11327 else if (ret == -2)
11328 error (_("Invalid mask or memory region."));
11329 }
06a64a0b
TT
11330 }
11331 else if (val != NULL)
fa4727a6 11332 release_value (val);
c906108c 11333
bbc13ae3
KS
11334 tok = skip_spaces_const (arg);
11335 end_tok = skip_to_space_const (tok);
c906108c
SS
11336
11337 toklen = end_tok - tok;
11338 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11339 {
2d134ed3
PA
11340 struct expression *cond;
11341
60e1c644 11342 innermost_block = NULL;
c906108c 11343 tok = cond_start = end_tok + 1;
1bb9788d 11344 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11345
11346 /* The watchpoint expression may not be local, but the condition
11347 may still be. E.g.: `watch global if local > 0'. */
11348 cond_exp_valid_block = innermost_block;
11349
2d134ed3 11350 xfree (cond);
c906108c
SS
11351 cond_end = tok;
11352 }
11353 if (*tok)
8a3fe4f8 11354 error (_("Junk at end of command."));
c906108c 11355
d983da9c 11356 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11357
11358 /* If the expression is "local", then set up a "watchpoint scope"
11359 breakpoint at the point where we've left the scope of the watchpoint
11360 expression. Create the scope breakpoint before the watchpoint, so
11361 that we will encounter it first in bpstat_stop_status. */
60e1c644 11362 if (exp_valid_block && frame)
d983da9c 11363 {
edb3359d
DJ
11364 if (frame_id_p (frame_unwind_caller_id (frame)))
11365 {
11366 scope_breakpoint
a6d9a66e
UW
11367 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11368 frame_unwind_caller_pc (frame),
06edf0c0
PA
11369 bp_watchpoint_scope,
11370 &momentary_breakpoint_ops);
d983da9c 11371
edb3359d 11372 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11373
edb3359d
DJ
11374 /* Automatically delete the breakpoint when it hits. */
11375 scope_breakpoint->disposition = disp_del;
d983da9c 11376
edb3359d
DJ
11377 /* Only break in the proper frame (help with recursion). */
11378 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11379
edb3359d 11380 /* Set the address at which we will stop. */
a6d9a66e
UW
11381 scope_breakpoint->loc->gdbarch
11382 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11383 scope_breakpoint->loc->requested_address
11384 = frame_unwind_caller_pc (frame);
11385 scope_breakpoint->loc->address
a6d9a66e
UW
11386 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11387 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11388 scope_breakpoint->type);
11389 }
d983da9c
DJ
11390 }
11391
e8369a73
AB
11392 /* Now set up the breakpoint. We create all watchpoints as hardware
11393 watchpoints here even if hardware watchpoints are turned off, a call
11394 to update_watchpoint later in this function will cause the type to
11395 drop back to bp_watchpoint (software watchpoint) if required. */
11396
11397 if (accessflag == hw_read)
11398 bp_type = bp_read_watchpoint;
11399 else if (accessflag == hw_access)
11400 bp_type = bp_access_watchpoint;
11401 else
11402 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11403
11404 w = XCNEW (struct watchpoint);
11405 b = &w->base;
348d480f 11406 if (use_mask)
3a5c3e22
PA
11407 init_raw_breakpoint_without_location (b, NULL, bp_type,
11408 &masked_watchpoint_breakpoint_ops);
348d480f 11409 else
3a5c3e22
PA
11410 init_raw_breakpoint_without_location (b, NULL, bp_type,
11411 &watchpoint_breakpoint_ops);
37e4754d 11412 b->thread = thread;
b5de0fa7 11413 b->disposition = disp_donttouch;
348d480f 11414 b->pspace = current_program_space;
3a5c3e22
PA
11415 w->exp = exp;
11416 w->exp_valid_block = exp_valid_block;
11417 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11418 if (just_location)
11419 {
11420 struct type *t = value_type (val);
11421 CORE_ADDR addr = value_as_address (val);
11422 char *name;
11423
11424 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11425 name = type_to_string (t);
11426
3a5c3e22 11427 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11428 core_addr_to_string (addr));
06a64a0b
TT
11429 xfree (name);
11430
3a5c3e22 11431 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11432 (int) (exp_end - exp_start), exp_start);
11433
06a64a0b
TT
11434 /* The above expression is in C. */
11435 b->language = language_c;
11436 }
11437 else
3a5c3e22 11438 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11439
11440 if (use_mask)
11441 {
3a5c3e22 11442 w->hw_wp_mask = mask;
9c06b0b4
TJB
11443 }
11444 else
11445 {
3a5c3e22
PA
11446 w->val = val;
11447 w->val_valid = 1;
9c06b0b4 11448 }
77b06cd7 11449
c906108c
SS
11450 if (cond_start)
11451 b->cond_string = savestring (cond_start, cond_end - cond_start);
11452 else
11453 b->cond_string = 0;
c5aa993b 11454
c906108c 11455 if (frame)
f6bc2008 11456 {
3a5c3e22
PA
11457 w->watchpoint_frame = get_frame_id (frame);
11458 w->watchpoint_thread = inferior_ptid;
f6bc2008 11459 }
c906108c 11460 else
f6bc2008 11461 {
3a5c3e22
PA
11462 w->watchpoint_frame = null_frame_id;
11463 w->watchpoint_thread = null_ptid;
f6bc2008 11464 }
c906108c 11465
d983da9c 11466 if (scope_breakpoint != NULL)
c906108c 11467 {
d983da9c
DJ
11468 /* The scope breakpoint is related to the watchpoint. We will
11469 need to act on them together. */
11470 b->related_breakpoint = scope_breakpoint;
11471 scope_breakpoint->related_breakpoint = b;
c906108c 11472 }
d983da9c 11473
06a64a0b
TT
11474 if (!just_location)
11475 value_free_to_mark (mark);
2d134ed3 11476
a9634178
TJB
11477 TRY_CATCH (e, RETURN_MASK_ALL)
11478 {
11479 /* Finally update the new watchpoint. This creates the locations
11480 that should be inserted. */
3a5c3e22 11481 update_watchpoint (w, 1);
a9634178
TJB
11482 }
11483 if (e.reason < 0)
11484 {
11485 delete_breakpoint (b);
11486 throw_exception (e);
11487 }
11488
3ea46bff 11489 install_breakpoint (internal, b, 1);
bbc13ae3 11490 do_cleanups (back_to);
c906108c
SS
11491}
11492
e09342b5 11493/* Return count of debug registers needed to watch the given expression.
e09342b5 11494 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11495
c906108c 11496static int
a9634178 11497can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11498{
11499 int found_memory_cnt = 0;
2e70b7b9 11500 struct value *head = v;
c906108c
SS
11501
11502 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11503 if (!can_use_hw_watchpoints)
c906108c 11504 return 0;
c5aa993b 11505
5c44784c
JM
11506 /* Make sure that the value of the expression depends only upon
11507 memory contents, and values computed from them within GDB. If we
11508 find any register references or function calls, we can't use a
11509 hardware watchpoint.
11510
11511 The idea here is that evaluating an expression generates a series
11512 of values, one holding the value of every subexpression. (The
11513 expression a*b+c has five subexpressions: a, b, a*b, c, and
11514 a*b+c.) GDB's values hold almost enough information to establish
11515 the criteria given above --- they identify memory lvalues,
11516 register lvalues, computed values, etcetera. So we can evaluate
11517 the expression, and then scan the chain of values that leaves
11518 behind to decide whether we can detect any possible change to the
11519 expression's final value using only hardware watchpoints.
11520
11521 However, I don't think that the values returned by inferior
11522 function calls are special in any way. So this function may not
11523 notice that an expression involving an inferior function call
11524 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11525 for (; v; v = value_next (v))
c906108c 11526 {
5c44784c 11527 if (VALUE_LVAL (v) == lval_memory)
c906108c 11528 {
8464be76
DJ
11529 if (v != head && value_lazy (v))
11530 /* A lazy memory lvalue in the chain is one that GDB never
11531 needed to fetch; we either just used its address (e.g.,
11532 `a' in `a.b') or we never needed it at all (e.g., `a'
11533 in `a,b'). This doesn't apply to HEAD; if that is
11534 lazy then it was not readable, but watch it anyway. */
5c44784c 11535 ;
53a5351d 11536 else
5c44784c
JM
11537 {
11538 /* Ahh, memory we actually used! Check if we can cover
11539 it with hardware watchpoints. */
df407dfe 11540 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11541
11542 /* We only watch structs and arrays if user asked for it
11543 explicitly, never if they just happen to appear in a
11544 middle of some value chain. */
11545 if (v == head
11546 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11547 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11548 {
42ae5230 11549 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11550 int len;
11551 int num_regs;
11552
a9634178 11553 len = (target_exact_watchpoints
e09342b5
TJB
11554 && is_scalar_type_recursive (vtype))?
11555 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11556
e09342b5
TJB
11557 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11558 if (!num_regs)
2e70b7b9
MS
11559 return 0;
11560 else
e09342b5 11561 found_memory_cnt += num_regs;
2e70b7b9 11562 }
5c44784c 11563 }
c5aa993b 11564 }
5086187c
AC
11565 else if (VALUE_LVAL (v) != not_lval
11566 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11567 return 0; /* These are values from the history (e.g., $1). */
5086187c 11568 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11569 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11570 }
11571
11572 /* The expression itself looks suitable for using a hardware
11573 watchpoint, but give the target machine a chance to reject it. */
11574 return found_memory_cnt;
11575}
11576
8b93c638 11577void
84f4c1fe 11578watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11579{
84f4c1fe 11580 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11581}
11582
06a64a0b
TT
11583/* A helper function that looks for the "-location" argument and then
11584 calls watch_command_1. */
11585
11586static void
11587watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11588{
11589 int just_location = 0;
11590
11591 if (arg
11592 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11593 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11594 {
e9cafbcc 11595 arg = skip_spaces (arg);
06a64a0b
TT
11596 just_location = 1;
11597 }
11598
84f4c1fe 11599 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11600}
8926118c 11601
c5aa993b 11602static void
fba45db2 11603watch_command (char *arg, int from_tty)
c906108c 11604{
06a64a0b 11605 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11606}
11607
8b93c638 11608void
84f4c1fe 11609rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11610{
84f4c1fe 11611 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11612}
8926118c 11613
c5aa993b 11614static void
fba45db2 11615rwatch_command (char *arg, int from_tty)
c906108c 11616{
06a64a0b 11617 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11618}
11619
8b93c638 11620void
84f4c1fe 11621awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11622{
84f4c1fe 11623 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11624}
8926118c 11625
c5aa993b 11626static void
fba45db2 11627awatch_command (char *arg, int from_tty)
c906108c 11628{
06a64a0b 11629 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11630}
c906108c 11631\f
c5aa993b 11632
43ff13b4 11633/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11634 because it uses the mechanisms of breakpoints. */
11635
bfec99b2
PA
11636struct until_break_command_continuation_args
11637{
11638 struct breakpoint *breakpoint;
11639 struct breakpoint *breakpoint2;
186c406b 11640 int thread_num;
bfec99b2
PA
11641};
11642
43ff13b4 11643/* This function is called by fetch_inferior_event via the
4a64f543 11644 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11645 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11646 command. */
c2c6d25f 11647static void
fa4cd53f 11648until_break_command_continuation (void *arg, int err)
43ff13b4 11649{
bfec99b2
PA
11650 struct until_break_command_continuation_args *a = arg;
11651
11652 delete_breakpoint (a->breakpoint);
11653 if (a->breakpoint2)
11654 delete_breakpoint (a->breakpoint2);
186c406b 11655 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11656}
11657
c906108c 11658void
ae66c1fc 11659until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11660{
11661 struct symtabs_and_lines sals;
11662 struct symtab_and_line sal;
8556afb4
PA
11663 struct frame_info *frame;
11664 struct gdbarch *frame_gdbarch;
11665 struct frame_id stack_frame_id;
11666 struct frame_id caller_frame_id;
c906108c 11667 struct breakpoint *breakpoint;
f107f563 11668 struct breakpoint *breakpoint2 = NULL;
c906108c 11669 struct cleanup *old_chain;
186c406b
TT
11670 int thread;
11671 struct thread_info *tp;
c906108c
SS
11672
11673 clear_proceed_status ();
11674
11675 /* Set a breakpoint where the user wants it and at return from
4a64f543 11676 this function. */
c5aa993b 11677
1bfeeb0f 11678 if (last_displayed_sal_is_valid ())
f8eba3c6 11679 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11680 get_last_displayed_symtab (),
f8eba3c6 11681 get_last_displayed_line ());
c906108c 11682 else
f8eba3c6
TT
11683 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11684 (struct symtab *) NULL, 0);
c5aa993b 11685
c906108c 11686 if (sals.nelts != 1)
8a3fe4f8 11687 error (_("Couldn't get information on specified line."));
c5aa993b 11688
c906108c 11689 sal = sals.sals[0];
4a64f543 11690 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11691
c906108c 11692 if (*arg)
8a3fe4f8 11693 error (_("Junk at end of arguments."));
c5aa993b 11694
c906108c 11695 resolve_sal_pc (&sal);
c5aa993b 11696
186c406b
TT
11697 tp = inferior_thread ();
11698 thread = tp->num;
11699
883bc8d1
PA
11700 old_chain = make_cleanup (null_cleanup, NULL);
11701
8556afb4
PA
11702 /* Note linespec handling above invalidates the frame chain.
11703 Installing a breakpoint also invalidates the frame chain (as it
11704 may need to switch threads), so do any frame handling before
11705 that. */
11706
11707 frame = get_selected_frame (NULL);
11708 frame_gdbarch = get_frame_arch (frame);
11709 stack_frame_id = get_stack_frame_id (frame);
11710 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11711
ae66c1fc
EZ
11712 /* Keep within the current frame, or in frames called by the current
11713 one. */
edb3359d 11714
883bc8d1 11715 if (frame_id_p (caller_frame_id))
c906108c 11716 {
883bc8d1
PA
11717 struct symtab_and_line sal2;
11718
11719 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11720 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11721 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11722 sal2,
11723 caller_frame_id,
f107f563
VP
11724 bp_until);
11725 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11726
883bc8d1 11727 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11728 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11729 }
c5aa993b 11730
c70a6932
JK
11731 /* set_momentary_breakpoint could invalidate FRAME. */
11732 frame = NULL;
11733
883bc8d1
PA
11734 if (anywhere)
11735 /* If the user told us to continue until a specified location,
11736 we don't specify a frame at which we need to stop. */
11737 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11738 null_frame_id, bp_until);
11739 else
11740 /* Otherwise, specify the selected frame, because we want to stop
11741 only at the very same frame. */
11742 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11743 stack_frame_id, bp_until);
11744 make_cleanup_delete_breakpoint (breakpoint);
11745
a493e3e2 11746 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11747
4a64f543
MS
11748 /* If we are running asynchronously, and proceed call above has
11749 actually managed to start the target, arrange for breakpoints to
11750 be deleted when the target stops. Otherwise, we're already
11751 stopped and delete breakpoints via cleanup chain. */
f107f563 11752
8ea051c5 11753 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11754 {
bfec99b2
PA
11755 struct until_break_command_continuation_args *args;
11756 args = xmalloc (sizeof (*args));
f107f563 11757
bfec99b2
PA
11758 args->breakpoint = breakpoint;
11759 args->breakpoint2 = breakpoint2;
186c406b 11760 args->thread_num = thread;
f107f563
VP
11761
11762 discard_cleanups (old_chain);
95e54da7
PA
11763 add_continuation (inferior_thread (),
11764 until_break_command_continuation, args,
604ead4a 11765 xfree);
f107f563
VP
11766 }
11767 else
c5aa993b 11768 do_cleanups (old_chain);
c906108c 11769}
ae66c1fc 11770
c906108c
SS
11771/* This function attempts to parse an optional "if <cond>" clause
11772 from the arg string. If one is not found, it returns NULL.
c5aa993b 11773
c906108c
SS
11774 Else, it returns a pointer to the condition string. (It does not
11775 attempt to evaluate the string against a particular block.) And,
11776 it updates arg to point to the first character following the parsed
4a64f543 11777 if clause in the arg string. */
53a5351d 11778
916703c0 11779char *
fba45db2 11780ep_parse_optional_if_clause (char **arg)
c906108c 11781{
c5aa993b
JM
11782 char *cond_string;
11783
11784 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11785 return NULL;
c5aa993b 11786
4a64f543 11787 /* Skip the "if" keyword. */
c906108c 11788 (*arg) += 2;
c5aa993b 11789
c906108c 11790 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11791 condition string. */
e9cafbcc 11792 *arg = skip_spaces (*arg);
c906108c 11793 cond_string = *arg;
c5aa993b 11794
4a64f543
MS
11795 /* Assume that the condition occupies the remainder of the arg
11796 string. */
c906108c 11797 (*arg) += strlen (cond_string);
c5aa993b 11798
c906108c
SS
11799 return cond_string;
11800}
c5aa993b 11801
c906108c
SS
11802/* Commands to deal with catching events, such as signals, exceptions,
11803 process start/exit, etc. */
c5aa993b
JM
11804
11805typedef enum
11806{
44feb3ce
TT
11807 catch_fork_temporary, catch_vfork_temporary,
11808 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11809}
11810catch_fork_kind;
11811
c906108c 11812static void
cc59ec59
MS
11813catch_fork_command_1 (char *arg, int from_tty,
11814 struct cmd_list_element *command)
c906108c 11815{
a6d9a66e 11816 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11817 char *cond_string = NULL;
44feb3ce
TT
11818 catch_fork_kind fork_kind;
11819 int tempflag;
11820
11821 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11822 tempflag = (fork_kind == catch_fork_temporary
11823 || fork_kind == catch_vfork_temporary);
c5aa993b 11824
44feb3ce
TT
11825 if (!arg)
11826 arg = "";
e9cafbcc 11827 arg = skip_spaces (arg);
c5aa993b 11828
c906108c 11829 /* The allowed syntax is:
c5aa993b
JM
11830 catch [v]fork
11831 catch [v]fork if <cond>
11832
4a64f543 11833 First, check if there's an if clause. */
c906108c 11834 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11835
c906108c 11836 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11837 error (_("Junk at end of arguments."));
c5aa993b 11838
c906108c 11839 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11840 and enable reporting of such events. */
c5aa993b
JM
11841 switch (fork_kind)
11842 {
44feb3ce
TT
11843 case catch_fork_temporary:
11844 case catch_fork_permanent:
a6d9a66e 11845 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11846 &catch_fork_breakpoint_ops);
c906108c 11847 break;
44feb3ce
TT
11848 case catch_vfork_temporary:
11849 case catch_vfork_permanent:
a6d9a66e 11850 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11851 &catch_vfork_breakpoint_ops);
c906108c 11852 break;
c5aa993b 11853 default:
8a3fe4f8 11854 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11855 break;
c5aa993b 11856 }
c906108c
SS
11857}
11858
11859static void
cc59ec59
MS
11860catch_exec_command_1 (char *arg, int from_tty,
11861 struct cmd_list_element *command)
c906108c 11862{
b4d90040 11863 struct exec_catchpoint *c;
a6d9a66e 11864 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11865 int tempflag;
c5aa993b 11866 char *cond_string = NULL;
c906108c 11867
44feb3ce
TT
11868 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11869
11870 if (!arg)
11871 arg = "";
e9cafbcc 11872 arg = skip_spaces (arg);
c906108c
SS
11873
11874 /* The allowed syntax is:
c5aa993b
JM
11875 catch exec
11876 catch exec if <cond>
c906108c 11877
4a64f543 11878 First, check if there's an if clause. */
c906108c
SS
11879 cond_string = ep_parse_optional_if_clause (&arg);
11880
11881 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11882 error (_("Junk at end of arguments."));
c906108c 11883
b4d90040
PA
11884 c = XNEW (struct exec_catchpoint);
11885 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11886 &catch_exec_breakpoint_ops);
11887 c->exec_pathname = NULL;
11888
3ea46bff 11889 install_breakpoint (0, &c->base, 1);
c906108c 11890}
c5aa993b 11891
9ac4176b 11892void
28010a5d
PA
11893init_ada_exception_breakpoint (struct breakpoint *b,
11894 struct gdbarch *gdbarch,
11895 struct symtab_and_line sal,
11896 char *addr_string,
c0a91b2b 11897 const struct breakpoint_ops *ops,
28010a5d 11898 int tempflag,
349774ef 11899 int enabled,
28010a5d 11900 int from_tty)
f7f9143b 11901{
f7f9143b
JB
11902 if (from_tty)
11903 {
5af949e3
UW
11904 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11905 if (!loc_gdbarch)
11906 loc_gdbarch = gdbarch;
11907
6c95b8df
PA
11908 describe_other_breakpoints (loc_gdbarch,
11909 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11910 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11911 version for exception catchpoints, because two catchpoints
11912 used for different exception names will use the same address.
11913 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11914 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11915 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11916 the user what type of catchpoint it is. The above is good
11917 enough for now, though. */
11918 }
11919
28010a5d 11920 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11921
349774ef 11922 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11923 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11924 b->addr_string = addr_string;
11925 b->language = language_ada;
f7f9143b
JB
11926}
11927
a96d9b2e
SDJ
11928/* Splits the argument using space as delimiter. Returns an xmalloc'd
11929 filter list, or NULL if no filtering is required. */
11930static VEC(int) *
11931catch_syscall_split_args (char *arg)
11932{
11933 VEC(int) *result = NULL;
29d0bb3d 11934 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
11935
11936 while (*arg != '\0')
11937 {
11938 int i, syscall_number;
11939 char *endptr;
11940 char cur_name[128];
11941 struct syscall s;
11942
11943 /* Skip whitespace. */
529480d0 11944 arg = skip_spaces (arg);
a96d9b2e
SDJ
11945
11946 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11947 cur_name[i] = arg[i];
11948 cur_name[i] = '\0';
11949 arg += i;
11950
11951 /* Check if the user provided a syscall name or a number. */
11952 syscall_number = (int) strtol (cur_name, &endptr, 0);
11953 if (*endptr == '\0')
bccd0dd2 11954 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
11955 else
11956 {
11957 /* We have a name. Let's check if it's valid and convert it
11958 to a number. */
11959 get_syscall_by_name (cur_name, &s);
11960
11961 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
11962 /* Here we have to issue an error instead of a warning,
11963 because GDB cannot do anything useful if there's no
11964 syscall number to be caught. */
a96d9b2e
SDJ
11965 error (_("Unknown syscall name '%s'."), cur_name);
11966 }
11967
11968 /* Ok, it's valid. */
11969 VEC_safe_push (int, result, s.number);
11970 }
11971
11972 discard_cleanups (cleanup);
11973 return result;
11974}
11975
11976/* Implement the "catch syscall" command. */
11977
11978static void
cc59ec59
MS
11979catch_syscall_command_1 (char *arg, int from_tty,
11980 struct cmd_list_element *command)
a96d9b2e
SDJ
11981{
11982 int tempflag;
11983 VEC(int) *filter;
11984 struct syscall s;
11985 struct gdbarch *gdbarch = get_current_arch ();
11986
11987 /* Checking if the feature if supported. */
11988 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11989 error (_("The feature 'catch syscall' is not supported on \
ea666128 11990this architecture yet."));
a96d9b2e
SDJ
11991
11992 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11993
e9cafbcc 11994 arg = skip_spaces (arg);
a96d9b2e
SDJ
11995
11996 /* We need to do this first "dummy" translation in order
11997 to get the syscall XML file loaded or, most important,
11998 to display a warning to the user if there's no XML file
11999 for his/her architecture. */
12000 get_syscall_by_number (0, &s);
12001
12002 /* The allowed syntax is:
12003 catch syscall
12004 catch syscall <name | number> [<name | number> ... <name | number>]
12005
12006 Let's check if there's a syscall name. */
12007
12008 if (arg != NULL)
12009 filter = catch_syscall_split_args (arg);
12010 else
12011 filter = NULL;
12012
12013 create_syscall_event_catchpoint (tempflag, filter,
12014 &catch_syscall_breakpoint_ops);
12015}
12016
c906108c 12017static void
fba45db2 12018catch_command (char *arg, int from_tty)
c906108c 12019{
44feb3ce 12020 error (_("Catch requires an event name."));
c906108c
SS
12021}
12022\f
12023
12024static void
fba45db2 12025tcatch_command (char *arg, int from_tty)
c906108c 12026{
44feb3ce 12027 error (_("Catch requires an event name."));
c906108c
SS
12028}
12029
8a2c437b
TT
12030/* A qsort comparison function that sorts breakpoints in order. */
12031
12032static int
12033compare_breakpoints (const void *a, const void *b)
12034{
12035 const breakpoint_p *ba = a;
12036 uintptr_t ua = (uintptr_t) *ba;
12037 const breakpoint_p *bb = b;
12038 uintptr_t ub = (uintptr_t) *bb;
12039
12040 if ((*ba)->number < (*bb)->number)
12041 return -1;
12042 else if ((*ba)->number > (*bb)->number)
12043 return 1;
12044
12045 /* Now sort by address, in case we see, e..g, two breakpoints with
12046 the number 0. */
12047 if (ua < ub)
12048 return -1;
94b0e70d 12049 return ua > ub ? 1 : 0;
8a2c437b
TT
12050}
12051
80f8a6eb 12052/* Delete breakpoints by address or line. */
c906108c
SS
12053
12054static void
fba45db2 12055clear_command (char *arg, int from_tty)
c906108c 12056{
8a2c437b 12057 struct breakpoint *b, *prev;
d6e956e5
VP
12058 VEC(breakpoint_p) *found = 0;
12059 int ix;
c906108c
SS
12060 int default_match;
12061 struct symtabs_and_lines sals;
12062 struct symtab_and_line sal;
c906108c 12063 int i;
8a2c437b 12064 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12065
12066 if (arg)
12067 {
39cf75f7
DE
12068 sals = decode_line_with_current_source (arg,
12069 (DECODE_LINE_FUNFIRSTLINE
12070 | DECODE_LINE_LIST_MODE));
cf4ded82 12071 make_cleanup (xfree, sals.sals);
c906108c
SS
12072 default_match = 0;
12073 }
12074 else
12075 {
c5aa993b 12076 sals.sals = (struct symtab_and_line *)
c906108c 12077 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12078 make_cleanup (xfree, sals.sals);
4a64f543 12079 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12080
12081 /* Set sal's line, symtab, pc, and pspace to the values
12082 corresponding to the last call to print_frame_info. If the
12083 codepoint is not valid, this will set all the fields to 0. */
12084 get_last_displayed_sal (&sal);
c906108c 12085 if (sal.symtab == 0)
8a3fe4f8 12086 error (_("No source file specified."));
c906108c
SS
12087
12088 sals.sals[0] = sal;
12089 sals.nelts = 1;
12090
12091 default_match = 1;
12092 }
12093
4a64f543
MS
12094 /* We don't call resolve_sal_pc here. That's not as bad as it
12095 seems, because all existing breakpoints typically have both
12096 file/line and pc set. So, if clear is given file/line, we can
12097 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12098
12099 We only support clearing given the address explicitly
12100 present in breakpoint table. Say, we've set breakpoint
4a64f543 12101 at file:line. There were several PC values for that file:line,
ed0616c6 12102 due to optimization, all in one block.
4a64f543
MS
12103
12104 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12105 PC corresponding to the same file:line, the breakpoint won't
12106 be cleared. We probably can still clear the breakpoint, but
12107 since the other PC value is never presented to user, user
12108 can only find it by guessing, and it does not seem important
12109 to support that. */
12110
4a64f543
MS
12111 /* For each line spec given, delete bps which correspond to it. Do
12112 it in two passes, solely to preserve the current behavior that
12113 from_tty is forced true if we delete more than one
12114 breakpoint. */
c906108c 12115
80f8a6eb 12116 found = NULL;
8a2c437b 12117 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12118 for (i = 0; i < sals.nelts; i++)
12119 {
05cba821
JK
12120 const char *sal_fullname;
12121
c906108c 12122 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12123 If line given (pc == 0), clear all bpts on specified line.
12124 If defaulting, clear all bpts on default line
c906108c 12125 or at default pc.
c5aa993b
JM
12126
12127 defaulting sal.pc != 0 tests to do
12128
12129 0 1 pc
12130 1 1 pc _and_ line
12131 0 0 line
12132 1 0 <can't happen> */
c906108c
SS
12133
12134 sal = sals.sals[i];
05cba821
JK
12135 sal_fullname = (sal.symtab == NULL
12136 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12137
4a64f543 12138 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12139 ALL_BREAKPOINTS (b)
c5aa993b 12140 {
0d381245 12141 int match = 0;
4a64f543 12142 /* Are we going to delete b? */
cc60f2e3 12143 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12144 {
12145 struct bp_location *loc = b->loc;
12146 for (; loc; loc = loc->next)
12147 {
f8eba3c6
TT
12148 /* If the user specified file:line, don't allow a PC
12149 match. This matches historical gdb behavior. */
12150 int pc_match = (!sal.explicit_line
12151 && sal.pc
12152 && (loc->pspace == sal.pspace)
12153 && (loc->address == sal.pc)
12154 && (!section_is_overlay (loc->section)
12155 || loc->section == sal.section));
4aac40c8
TT
12156 int line_match = 0;
12157
12158 if ((default_match || sal.explicit_line)
2f202fde 12159 && loc->symtab != NULL
05cba821 12160 && sal_fullname != NULL
4aac40c8 12161 && sal.pspace == loc->pspace
05cba821
JK
12162 && loc->line_number == sal.line
12163 && filename_cmp (symtab_to_fullname (loc->symtab),
12164 sal_fullname) == 0)
12165 line_match = 1;
4aac40c8 12166
0d381245
VP
12167 if (pc_match || line_match)
12168 {
12169 match = 1;
12170 break;
12171 }
12172 }
12173 }
12174
12175 if (match)
d6e956e5 12176 VEC_safe_push(breakpoint_p, found, b);
c906108c 12177 }
80f8a6eb 12178 }
8a2c437b 12179
80f8a6eb 12180 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12181 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12182 {
12183 if (arg)
8a3fe4f8 12184 error (_("No breakpoint at %s."), arg);
80f8a6eb 12185 else
8a3fe4f8 12186 error (_("No breakpoint at this line."));
80f8a6eb 12187 }
c906108c 12188
8a2c437b
TT
12189 /* Remove duplicates from the vec. */
12190 qsort (VEC_address (breakpoint_p, found),
12191 VEC_length (breakpoint_p, found),
12192 sizeof (breakpoint_p),
12193 compare_breakpoints);
12194 prev = VEC_index (breakpoint_p, found, 0);
12195 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12196 {
12197 if (b == prev)
12198 {
12199 VEC_ordered_remove (breakpoint_p, found, ix);
12200 --ix;
12201 }
12202 }
12203
d6e956e5 12204 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12205 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12206 if (from_tty)
a3f17187 12207 {
d6e956e5 12208 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12209 printf_unfiltered (_("Deleted breakpoint "));
12210 else
12211 printf_unfiltered (_("Deleted breakpoints "));
12212 }
d6e956e5
VP
12213
12214 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12215 {
c5aa993b 12216 if (from_tty)
d6e956e5
VP
12217 printf_unfiltered ("%d ", b->number);
12218 delete_breakpoint (b);
c906108c 12219 }
80f8a6eb
MS
12220 if (from_tty)
12221 putchar_unfiltered ('\n');
8a2c437b
TT
12222
12223 do_cleanups (cleanups);
c906108c
SS
12224}
12225\f
12226/* Delete breakpoint in BS if they are `delete' breakpoints and
12227 all breakpoints that are marked for deletion, whether hit or not.
12228 This is called after any breakpoint is hit, or after errors. */
12229
12230void
fba45db2 12231breakpoint_auto_delete (bpstat bs)
c906108c 12232{
35df4500 12233 struct breakpoint *b, *b_tmp;
c906108c
SS
12234
12235 for (; bs; bs = bs->next)
f431efe5
PA
12236 if (bs->breakpoint_at
12237 && bs->breakpoint_at->disposition == disp_del
c906108c 12238 && bs->stop)
f431efe5 12239 delete_breakpoint (bs->breakpoint_at);
c906108c 12240
35df4500 12241 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12242 {
b5de0fa7 12243 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12244 delete_breakpoint (b);
12245 }
c906108c
SS
12246}
12247
4a64f543
MS
12248/* A comparison function for bp_location AP and BP being interfaced to
12249 qsort. Sort elements primarily by their ADDRESS (no matter what
12250 does breakpoint_address_is_meaningful say for its OWNER),
12251 secondarily by ordering first bp_permanent OWNERed elements and
12252 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12253 qsort being an unstable algorithm. */
876fa593
JK
12254
12255static int
494cfb0f 12256bp_location_compare (const void *ap, const void *bp)
876fa593 12257{
494cfb0f
JK
12258 struct bp_location *a = *(void **) ap;
12259 struct bp_location *b = *(void **) bp;
2bdf28a0 12260 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
12261 int a_perm = a->owner->enable_state == bp_permanent;
12262 int b_perm = b->owner->enable_state == bp_permanent;
12263
12264 if (a->address != b->address)
12265 return (a->address > b->address) - (a->address < b->address);
12266
dea2aa5f
LM
12267 /* Sort locations at the same address by their pspace number, keeping
12268 locations of the same inferior (in a multi-inferior environment)
12269 grouped. */
12270
12271 if (a->pspace->num != b->pspace->num)
12272 return ((a->pspace->num > b->pspace->num)
12273 - (a->pspace->num < b->pspace->num));
12274
876fa593
JK
12275 /* Sort permanent breakpoints first. */
12276 if (a_perm != b_perm)
12277 return (a_perm < b_perm) - (a_perm > b_perm);
12278
c56a97f9
JK
12279 /* Make the internal GDB representation stable across GDB runs
12280 where A and B memory inside GDB can differ. Breakpoint locations of
12281 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12282
12283 if (a->owner->number != b->owner->number)
c56a97f9
JK
12284 return ((a->owner->number > b->owner->number)
12285 - (a->owner->number < b->owner->number));
876fa593
JK
12286
12287 return (a > b) - (a < b);
12288}
12289
876fa593 12290/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12291 bp_location_shadow_len_after_address_max according to the current
12292 content of the bp_location array. */
f7545552
TT
12293
12294static void
876fa593 12295bp_location_target_extensions_update (void)
f7545552 12296{
876fa593
JK
12297 struct bp_location *bl, **blp_tmp;
12298
12299 bp_location_placed_address_before_address_max = 0;
12300 bp_location_shadow_len_after_address_max = 0;
12301
12302 ALL_BP_LOCATIONS (bl, blp_tmp)
12303 {
12304 CORE_ADDR start, end, addr;
12305
12306 if (!bp_location_has_shadow (bl))
12307 continue;
12308
12309 start = bl->target_info.placed_address;
12310 end = start + bl->target_info.shadow_len;
12311
12312 gdb_assert (bl->address >= start);
12313 addr = bl->address - start;
12314 if (addr > bp_location_placed_address_before_address_max)
12315 bp_location_placed_address_before_address_max = addr;
12316
12317 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12318
12319 gdb_assert (bl->address < end);
12320 addr = end - bl->address;
12321 if (addr > bp_location_shadow_len_after_address_max)
12322 bp_location_shadow_len_after_address_max = addr;
12323 }
f7545552
TT
12324}
12325
1e4d1764
YQ
12326/* Download tracepoint locations if they haven't been. */
12327
12328static void
12329download_tracepoint_locations (void)
12330{
7ed2c994 12331 struct breakpoint *b;
1e4d1764
YQ
12332 struct cleanup *old_chain;
12333
12334 if (!target_can_download_tracepoint ())
12335 return;
12336
12337 old_chain = save_current_space_and_thread ();
12338
7ed2c994 12339 ALL_TRACEPOINTS (b)
1e4d1764 12340 {
7ed2c994 12341 struct bp_location *bl;
1e4d1764 12342 struct tracepoint *t;
f2a8bc8a 12343 int bp_location_downloaded = 0;
1e4d1764 12344
7ed2c994 12345 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12346 ? !may_insert_fast_tracepoints
12347 : !may_insert_tracepoints))
12348 continue;
12349
7ed2c994
YQ
12350 for (bl = b->loc; bl; bl = bl->next)
12351 {
12352 /* In tracepoint, locations are _never_ duplicated, so
12353 should_be_inserted is equivalent to
12354 unduplicated_should_be_inserted. */
12355 if (!should_be_inserted (bl) || bl->inserted)
12356 continue;
1e4d1764 12357
7ed2c994 12358 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12359
7ed2c994 12360 target_download_tracepoint (bl);
1e4d1764 12361
7ed2c994 12362 bl->inserted = 1;
f2a8bc8a 12363 bp_location_downloaded = 1;
7ed2c994
YQ
12364 }
12365 t = (struct tracepoint *) b;
12366 t->number_on_target = b->number;
f2a8bc8a
YQ
12367 if (bp_location_downloaded)
12368 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12369 }
12370
12371 do_cleanups (old_chain);
12372}
12373
934709f0
PW
12374/* Swap the insertion/duplication state between two locations. */
12375
12376static void
12377swap_insertion (struct bp_location *left, struct bp_location *right)
12378{
12379 const int left_inserted = left->inserted;
12380 const int left_duplicate = left->duplicate;
b775012e 12381 const int left_needs_update = left->needs_update;
934709f0
PW
12382 const struct bp_target_info left_target_info = left->target_info;
12383
1e4d1764
YQ
12384 /* Locations of tracepoints can never be duplicated. */
12385 if (is_tracepoint (left->owner))
12386 gdb_assert (!left->duplicate);
12387 if (is_tracepoint (right->owner))
12388 gdb_assert (!right->duplicate);
12389
934709f0
PW
12390 left->inserted = right->inserted;
12391 left->duplicate = right->duplicate;
b775012e 12392 left->needs_update = right->needs_update;
934709f0
PW
12393 left->target_info = right->target_info;
12394 right->inserted = left_inserted;
12395 right->duplicate = left_duplicate;
b775012e 12396 right->needs_update = left_needs_update;
934709f0
PW
12397 right->target_info = left_target_info;
12398}
12399
b775012e
LM
12400/* Force the re-insertion of the locations at ADDRESS. This is called
12401 once a new/deleted/modified duplicate location is found and we are evaluating
12402 conditions on the target's side. Such conditions need to be updated on
12403 the target. */
12404
12405static void
12406force_breakpoint_reinsertion (struct bp_location *bl)
12407{
12408 struct bp_location **locp = NULL, **loc2p;
12409 struct bp_location *loc;
12410 CORE_ADDR address = 0;
12411 int pspace_num;
12412
12413 address = bl->address;
12414 pspace_num = bl->pspace->num;
12415
12416 /* This is only meaningful if the target is
12417 evaluating conditions and if the user has
12418 opted for condition evaluation on the target's
12419 side. */
12420 if (gdb_evaluates_breakpoint_condition_p ()
12421 || !target_supports_evaluation_of_breakpoint_conditions ())
12422 return;
12423
12424 /* Flag all breakpoint locations with this address and
12425 the same program space as the location
12426 as "its condition has changed". We need to
12427 update the conditions on the target's side. */
12428 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12429 {
12430 loc = *loc2p;
12431
12432 if (!is_breakpoint (loc->owner)
12433 || pspace_num != loc->pspace->num)
12434 continue;
12435
12436 /* Flag the location appropriately. We use a different state to
12437 let everyone know that we already updated the set of locations
12438 with addr bl->address and program space bl->pspace. This is so
12439 we don't have to keep calling these functions just to mark locations
12440 that have already been marked. */
12441 loc->condition_changed = condition_updated;
12442
12443 /* Free the agent expression bytecode as well. We will compute
12444 it later on. */
12445 if (loc->cond_bytecode)
12446 {
12447 free_agent_expr (loc->cond_bytecode);
12448 loc->cond_bytecode = NULL;
12449 }
12450 }
12451}
12452
4cd9bd08 12453/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
12454 into the inferior, only remove already-inserted locations that no
12455 longer should be inserted. Functions that delete a breakpoint or
12456 breakpoints should pass false, so that deleting a breakpoint
12457 doesn't have the side effect of inserting the locations of other
12458 breakpoints that are marked not-inserted, but should_be_inserted
12459 returns true on them.
12460
12461 This behaviour is useful is situations close to tear-down -- e.g.,
12462 after an exec, while the target still has execution, but breakpoint
12463 shadows of the previous executable image should *NOT* be restored
12464 to the new image; or before detaching, where the target still has
12465 execution and wants to delete breakpoints from GDB's lists, and all
12466 breakpoints had already been removed from the inferior. */
12467
0d381245 12468static void
b60e7edf 12469update_global_location_list (int should_insert)
0d381245 12470{
74960c60 12471 struct breakpoint *b;
876fa593 12472 struct bp_location **locp, *loc;
f7545552 12473 struct cleanup *cleanups;
b775012e
LM
12474 /* Last breakpoint location address that was marked for update. */
12475 CORE_ADDR last_addr = 0;
12476 /* Last breakpoint location program space that was marked for update. */
12477 int last_pspace_num = -1;
f7545552 12478
2d134ed3
PA
12479 /* Used in the duplicates detection below. When iterating over all
12480 bp_locations, points to the first bp_location of a given address.
12481 Breakpoints and watchpoints of different types are never
12482 duplicates of each other. Keep one pointer for each type of
12483 breakpoint/watchpoint, so we only need to loop over all locations
12484 once. */
12485 struct bp_location *bp_loc_first; /* breakpoint */
12486 struct bp_location *wp_loc_first; /* hardware watchpoint */
12487 struct bp_location *awp_loc_first; /* access watchpoint */
12488 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12489
4a64f543
MS
12490 /* Saved former bp_location array which we compare against the newly
12491 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12492 struct bp_location **old_location, **old_locp;
12493 unsigned old_location_count;
12494
12495 old_location = bp_location;
12496 old_location_count = bp_location_count;
12497 bp_location = NULL;
12498 bp_location_count = 0;
12499 cleanups = make_cleanup (xfree, old_location);
0d381245 12500
74960c60 12501 ALL_BREAKPOINTS (b)
876fa593
JK
12502 for (loc = b->loc; loc; loc = loc->next)
12503 bp_location_count++;
12504
12505 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12506 locp = bp_location;
12507 ALL_BREAKPOINTS (b)
12508 for (loc = b->loc; loc; loc = loc->next)
12509 *locp++ = loc;
12510 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12511 bp_location_compare);
876fa593
JK
12512
12513 bp_location_target_extensions_update ();
74960c60 12514
4a64f543
MS
12515 /* Identify bp_location instances that are no longer present in the
12516 new list, and therefore should be freed. Note that it's not
12517 necessary that those locations should be removed from inferior --
12518 if there's another location at the same address (previously
12519 marked as duplicate), we don't need to remove/insert the
12520 location.
876fa593 12521
4a64f543
MS
12522 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12523 and former bp_location array state respectively. */
876fa593
JK
12524
12525 locp = bp_location;
12526 for (old_locp = old_location; old_locp < old_location + old_location_count;
12527 old_locp++)
74960c60 12528 {
876fa593 12529 struct bp_location *old_loc = *old_locp;
c7d46a38 12530 struct bp_location **loc2p;
876fa593 12531
e5dd4106 12532 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12533 not, we have to free it. */
c7d46a38 12534 int found_object = 0;
20874c92
VP
12535 /* Tells if the location should remain inserted in the target. */
12536 int keep_in_target = 0;
12537 int removed = 0;
876fa593 12538
4a64f543
MS
12539 /* Skip LOCP entries which will definitely never be needed.
12540 Stop either at or being the one matching OLD_LOC. */
876fa593 12541 while (locp < bp_location + bp_location_count
c7d46a38 12542 && (*locp)->address < old_loc->address)
876fa593 12543 locp++;
c7d46a38
PA
12544
12545 for (loc2p = locp;
12546 (loc2p < bp_location + bp_location_count
12547 && (*loc2p)->address == old_loc->address);
12548 loc2p++)
12549 {
b775012e
LM
12550 /* Check if this is a new/duplicated location or a duplicated
12551 location that had its condition modified. If so, we want to send
12552 its condition to the target if evaluation of conditions is taking
12553 place there. */
12554 if ((*loc2p)->condition_changed == condition_modified
12555 && (last_addr != old_loc->address
12556 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12557 {
b775012e
LM
12558 force_breakpoint_reinsertion (*loc2p);
12559 last_pspace_num = old_loc->pspace->num;
c7d46a38 12560 }
b775012e
LM
12561
12562 if (*loc2p == old_loc)
12563 found_object = 1;
c7d46a38 12564 }
74960c60 12565
b775012e
LM
12566 /* We have already handled this address, update it so that we don't
12567 have to go through updates again. */
12568 last_addr = old_loc->address;
12569
12570 /* Target-side condition evaluation: Handle deleted locations. */
12571 if (!found_object)
12572 force_breakpoint_reinsertion (old_loc);
12573
4a64f543
MS
12574 /* If this location is no longer present, and inserted, look if
12575 there's maybe a new location at the same address. If so,
12576 mark that one inserted, and don't remove this one. This is
12577 needed so that we don't have a time window where a breakpoint
12578 at certain location is not inserted. */
74960c60 12579
876fa593 12580 if (old_loc->inserted)
0d381245 12581 {
4a64f543
MS
12582 /* If the location is inserted now, we might have to remove
12583 it. */
74960c60 12584
876fa593 12585 if (found_object && should_be_inserted (old_loc))
74960c60 12586 {
4a64f543
MS
12587 /* The location is still present in the location list,
12588 and still should be inserted. Don't do anything. */
20874c92 12589 keep_in_target = 1;
74960c60
VP
12590 }
12591 else
12592 {
b775012e
LM
12593 /* This location still exists, but it won't be kept in the
12594 target since it may have been disabled. We proceed to
12595 remove its target-side condition. */
12596
4a64f543
MS
12597 /* The location is either no longer present, or got
12598 disabled. See if there's another location at the
12599 same address, in which case we don't need to remove
12600 this one from the target. */
876fa593 12601
2bdf28a0 12602 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12603 if (breakpoint_address_is_meaningful (old_loc->owner))
12604 {
876fa593 12605 for (loc2p = locp;
c7d46a38
PA
12606 (loc2p < bp_location + bp_location_count
12607 && (*loc2p)->address == old_loc->address);
876fa593
JK
12608 loc2p++)
12609 {
12610 struct bp_location *loc2 = *loc2p;
12611
2d134ed3 12612 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12613 {
85d721b8
PA
12614 /* Read watchpoint locations are switched to
12615 access watchpoints, if the former are not
12616 supported, but the latter are. */
12617 if (is_hardware_watchpoint (old_loc->owner))
12618 {
12619 gdb_assert (is_hardware_watchpoint (loc2->owner));
12620 loc2->watchpoint_type = old_loc->watchpoint_type;
12621 }
12622
934709f0
PW
12623 /* loc2 is a duplicated location. We need to check
12624 if it should be inserted in case it will be
12625 unduplicated. */
12626 if (loc2 != old_loc
12627 && unduplicated_should_be_inserted (loc2))
c7d46a38 12628 {
934709f0 12629 swap_insertion (old_loc, loc2);
c7d46a38
PA
12630 keep_in_target = 1;
12631 break;
12632 }
876fa593
JK
12633 }
12634 }
12635 }
74960c60
VP
12636 }
12637
20874c92
VP
12638 if (!keep_in_target)
12639 {
876fa593 12640 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12641 {
4a64f543
MS
12642 /* This is just about all we can do. We could keep
12643 this location on the global list, and try to
12644 remove it next time, but there's no particular
12645 reason why we will succeed next time.
20874c92 12646
4a64f543
MS
12647 Note that at this point, old_loc->owner is still
12648 valid, as delete_breakpoint frees the breakpoint
12649 only after calling us. */
3e43a32a
MS
12650 printf_filtered (_("warning: Error removing "
12651 "breakpoint %d\n"),
876fa593 12652 old_loc->owner->number);
20874c92
VP
12653 }
12654 removed = 1;
12655 }
0d381245 12656 }
74960c60
VP
12657
12658 if (!found_object)
1c5cfe86 12659 {
db82e815
PA
12660 if (removed && non_stop
12661 && breakpoint_address_is_meaningful (old_loc->owner)
12662 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12663 {
db82e815
PA
12664 /* This location was removed from the target. In
12665 non-stop mode, a race condition is possible where
12666 we've removed a breakpoint, but stop events for that
12667 breakpoint are already queued and will arrive later.
12668 We apply an heuristic to be able to distinguish such
12669 SIGTRAPs from other random SIGTRAPs: we keep this
12670 breakpoint location for a bit, and will retire it
12671 after we see some number of events. The theory here
12672 is that reporting of events should, "on the average",
12673 be fair, so after a while we'll see events from all
12674 threads that have anything of interest, and no longer
12675 need to keep this breakpoint location around. We
12676 don't hold locations forever so to reduce chances of
12677 mistaking a non-breakpoint SIGTRAP for a breakpoint
12678 SIGTRAP.
12679
12680 The heuristic failing can be disastrous on
12681 decr_pc_after_break targets.
12682
12683 On decr_pc_after_break targets, like e.g., x86-linux,
12684 if we fail to recognize a late breakpoint SIGTRAP,
12685 because events_till_retirement has reached 0 too
12686 soon, we'll fail to do the PC adjustment, and report
12687 a random SIGTRAP to the user. When the user resumes
12688 the inferior, it will most likely immediately crash
2dec564e 12689 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12690 corrupted, because of being resumed e.g., in the
12691 middle of a multi-byte instruction, or skipped a
12692 one-byte instruction. This was actually seen happen
12693 on native x86-linux, and should be less rare on
12694 targets that do not support new thread events, like
12695 remote, due to the heuristic depending on
12696 thread_count.
12697
12698 Mistaking a random SIGTRAP for a breakpoint trap
12699 causes similar symptoms (PC adjustment applied when
12700 it shouldn't), but then again, playing with SIGTRAPs
12701 behind the debugger's back is asking for trouble.
12702
12703 Since hardware watchpoint traps are always
12704 distinguishable from other traps, so we don't need to
12705 apply keep hardware watchpoint moribund locations
12706 around. We simply always ignore hardware watchpoint
12707 traps we can no longer explain. */
12708
876fa593
JK
12709 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12710 old_loc->owner = NULL;
20874c92 12711
876fa593 12712 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12713 }
12714 else
f431efe5
PA
12715 {
12716 old_loc->owner = NULL;
12717 decref_bp_location (&old_loc);
12718 }
20874c92 12719 }
74960c60 12720 }
1c5cfe86 12721
348d480f
PA
12722 /* Rescan breakpoints at the same address and section, marking the
12723 first one as "first" and any others as "duplicates". This is so
12724 that the bpt instruction is only inserted once. If we have a
12725 permanent breakpoint at the same place as BPT, make that one the
12726 official one, and the rest as duplicates. Permanent breakpoints
12727 are sorted first for the same address.
12728
12729 Do the same for hardware watchpoints, but also considering the
12730 watchpoint's type (regular/access/read) and length. */
12731
12732 bp_loc_first = NULL;
12733 wp_loc_first = NULL;
12734 awp_loc_first = NULL;
12735 rwp_loc_first = NULL;
12736 ALL_BP_LOCATIONS (loc, locp)
12737 {
12738 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12739 non-NULL. */
348d480f 12740 struct bp_location **loc_first_p;
d3fbdd86 12741 b = loc->owner;
348d480f 12742
6f380991 12743 if (!unduplicated_should_be_inserted (loc)
348d480f 12744 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12745 /* Don't detect duplicate for tracepoint locations because they are
12746 never duplicated. See the comments in field `duplicate' of
12747 `struct bp_location'. */
348d480f 12748 || is_tracepoint (b))
b775012e
LM
12749 {
12750 /* Clear the condition modification flag. */
12751 loc->condition_changed = condition_unchanged;
12752 continue;
12753 }
348d480f
PA
12754
12755 /* Permanent breakpoint should always be inserted. */
12756 if (b->enable_state == bp_permanent && ! loc->inserted)
12757 internal_error (__FILE__, __LINE__,
12758 _("allegedly permanent breakpoint is not "
12759 "actually inserted"));
12760
12761 if (b->type == bp_hardware_watchpoint)
12762 loc_first_p = &wp_loc_first;
12763 else if (b->type == bp_read_watchpoint)
12764 loc_first_p = &rwp_loc_first;
12765 else if (b->type == bp_access_watchpoint)
12766 loc_first_p = &awp_loc_first;
12767 else
12768 loc_first_p = &bp_loc_first;
12769
12770 if (*loc_first_p == NULL
12771 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12772 || !breakpoint_locations_match (loc, *loc_first_p))
12773 {
12774 *loc_first_p = loc;
12775 loc->duplicate = 0;
b775012e
LM
12776
12777 if (is_breakpoint (loc->owner) && loc->condition_changed)
12778 {
12779 loc->needs_update = 1;
12780 /* Clear the condition modification flag. */
12781 loc->condition_changed = condition_unchanged;
12782 }
348d480f
PA
12783 continue;
12784 }
12785
934709f0
PW
12786
12787 /* This and the above ensure the invariant that the first location
12788 is not duplicated, and is the inserted one.
12789 All following are marked as duplicated, and are not inserted. */
12790 if (loc->inserted)
12791 swap_insertion (loc, *loc_first_p);
348d480f
PA
12792 loc->duplicate = 1;
12793
b775012e
LM
12794 /* Clear the condition modification flag. */
12795 loc->condition_changed = condition_unchanged;
12796
348d480f
PA
12797 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12798 && b->enable_state != bp_permanent)
12799 internal_error (__FILE__, __LINE__,
12800 _("another breakpoint was inserted on top of "
12801 "a permanent breakpoint"));
12802 }
12803
b775012e 12804 if (breakpoints_always_inserted_mode ()
348d480f 12805 && (have_live_inferiors ()
f5656ead 12806 || (gdbarch_has_global_breakpoints (target_gdbarch ()))))
b775012e
LM
12807 {
12808 if (should_insert)
12809 insert_breakpoint_locations ();
12810 else
12811 {
12812 /* Though should_insert is false, we may need to update conditions
12813 on the target's side if it is evaluating such conditions. We
12814 only update conditions for locations that are marked
12815 "needs_update". */
12816 update_inserted_breakpoint_locations ();
12817 }
12818 }
348d480f 12819
1e4d1764
YQ
12820 if (should_insert)
12821 download_tracepoint_locations ();
12822
348d480f
PA
12823 do_cleanups (cleanups);
12824}
12825
12826void
12827breakpoint_retire_moribund (void)
12828{
12829 struct bp_location *loc;
12830 int ix;
12831
12832 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12833 if (--(loc->events_till_retirement) == 0)
12834 {
12835 decref_bp_location (&loc);
12836 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12837 --ix;
12838 }
12839}
12840
12841static void
12842update_global_location_list_nothrow (int inserting)
12843{
bfd189b1 12844 volatile struct gdb_exception e;
348d480f
PA
12845
12846 TRY_CATCH (e, RETURN_MASK_ERROR)
12847 update_global_location_list (inserting);
12848}
12849
12850/* Clear BKP from a BPS. */
12851
12852static void
12853bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12854{
12855 bpstat bs;
12856
12857 for (bs = bps; bs; bs = bs->next)
12858 if (bs->breakpoint_at == bpt)
12859 {
12860 bs->breakpoint_at = NULL;
12861 bs->old_val = NULL;
12862 /* bs->commands will be freed later. */
12863 }
12864}
12865
12866/* Callback for iterate_over_threads. */
12867static int
12868bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12869{
12870 struct breakpoint *bpt = data;
12871
12872 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12873 return 0;
12874}
12875
12876/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12877 callbacks. */
12878
12879static void
12880say_where (struct breakpoint *b)
12881{
12882 struct value_print_options opts;
12883
12884 get_user_print_options (&opts);
12885
12886 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12887 single string. */
12888 if (b->loc == NULL)
12889 {
12890 printf_filtered (_(" (%s) pending."), b->addr_string);
12891 }
12892 else
12893 {
2f202fde 12894 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12895 {
12896 printf_filtered (" at ");
12897 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12898 gdb_stdout);
12899 }
2f202fde 12900 if (b->loc->symtab != NULL)
f8eba3c6
TT
12901 {
12902 /* If there is a single location, we can print the location
12903 more nicely. */
12904 if (b->loc->next == NULL)
12905 printf_filtered (": file %s, line %d.",
05cba821
JK
12906 symtab_to_filename_for_display (b->loc->symtab),
12907 b->loc->line_number);
f8eba3c6
TT
12908 else
12909 /* This is not ideal, but each location may have a
12910 different file name, and this at least reflects the
12911 real situation somewhat. */
12912 printf_filtered (": %s.", b->addr_string);
12913 }
348d480f
PA
12914
12915 if (b->loc->next)
12916 {
12917 struct bp_location *loc = b->loc;
12918 int n = 0;
12919 for (; loc; loc = loc->next)
12920 ++n;
12921 printf_filtered (" (%d locations)", n);
12922 }
12923 }
12924}
12925
348d480f
PA
12926/* Default bp_location_ops methods. */
12927
12928static void
12929bp_location_dtor (struct bp_location *self)
12930{
12931 xfree (self->cond);
b775012e
LM
12932 if (self->cond_bytecode)
12933 free_agent_expr (self->cond_bytecode);
348d480f 12934 xfree (self->function_name);
8b4f3082
PA
12935
12936 VEC_free (agent_expr_p, self->target_info.conditions);
12937 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12938}
12939
12940static const struct bp_location_ops bp_location_ops =
12941{
12942 bp_location_dtor
12943};
12944
2060206e
PA
12945/* Default breakpoint_ops methods all breakpoint_ops ultimately
12946 inherit from. */
348d480f 12947
2060206e
PA
12948static void
12949base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12950{
12951 decref_counted_command_line (&self->commands);
12952 xfree (self->cond_string);
fb81d016 12953 xfree (self->extra_string);
348d480f 12954 xfree (self->addr_string);
f8eba3c6 12955 xfree (self->filter);
348d480f 12956 xfree (self->addr_string_range_end);
348d480f
PA
12957}
12958
2060206e
PA
12959static struct bp_location *
12960base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12961{
12962 struct bp_location *loc;
12963
12964 loc = XNEW (struct bp_location);
12965 init_bp_location (loc, &bp_location_ops, self);
12966 return loc;
12967}
12968
2060206e
PA
12969static void
12970base_breakpoint_re_set (struct breakpoint *b)
12971{
12972 /* Nothing to re-set. */
12973}
12974
12975#define internal_error_pure_virtual_called() \
12976 gdb_assert_not_reached ("pure virtual function called")
12977
12978static int
12979base_breakpoint_insert_location (struct bp_location *bl)
12980{
12981 internal_error_pure_virtual_called ();
12982}
12983
12984static int
12985base_breakpoint_remove_location (struct bp_location *bl)
12986{
12987 internal_error_pure_virtual_called ();
12988}
12989
12990static int
12991base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12992 struct address_space *aspace,
09ac7c10
TT
12993 CORE_ADDR bp_addr,
12994 const struct target_waitstatus *ws)
2060206e
PA
12995{
12996 internal_error_pure_virtual_called ();
12997}
12998
12999static void
13000base_breakpoint_check_status (bpstat bs)
13001{
13002 /* Always stop. */
13003}
13004
13005/* A "works_in_software_mode" breakpoint_ops method that just internal
13006 errors. */
13007
13008static int
13009base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13010{
13011 internal_error_pure_virtual_called ();
13012}
13013
13014/* A "resources_needed" breakpoint_ops method that just internal
13015 errors. */
13016
13017static int
13018base_breakpoint_resources_needed (const struct bp_location *bl)
13019{
13020 internal_error_pure_virtual_called ();
13021}
13022
13023static enum print_stop_action
13024base_breakpoint_print_it (bpstat bs)
13025{
13026 internal_error_pure_virtual_called ();
13027}
13028
13029static void
13030base_breakpoint_print_one_detail (const struct breakpoint *self,
13031 struct ui_out *uiout)
13032{
13033 /* nothing */
13034}
13035
13036static void
13037base_breakpoint_print_mention (struct breakpoint *b)
13038{
13039 internal_error_pure_virtual_called ();
13040}
13041
13042static void
13043base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13044{
13045 internal_error_pure_virtual_called ();
13046}
13047
983af33b
SDJ
13048static void
13049base_breakpoint_create_sals_from_address (char **arg,
13050 struct linespec_result *canonical,
13051 enum bptype type_wanted,
13052 char *addr_start,
13053 char **copy_arg)
13054{
13055 internal_error_pure_virtual_called ();
13056}
13057
13058static void
13059base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13060 struct linespec_result *c,
983af33b 13061 char *cond_string,
e7e0cddf 13062 char *extra_string,
983af33b
SDJ
13063 enum bptype type_wanted,
13064 enum bpdisp disposition,
13065 int thread,
13066 int task, int ignore_count,
13067 const struct breakpoint_ops *o,
13068 int from_tty, int enabled,
44f238bb 13069 int internal, unsigned flags)
983af33b
SDJ
13070{
13071 internal_error_pure_virtual_called ();
13072}
13073
13074static void
13075base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13076 struct symtabs_and_lines *sals)
13077{
13078 internal_error_pure_virtual_called ();
13079}
13080
ab04a2af
TT
13081/* The default 'explains_signal' method. */
13082
47591c29 13083static int
427cd150 13084base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13085{
47591c29 13086 return 1;
ab04a2af
TT
13087}
13088
9d6e6e84
HZ
13089/* The default "after_condition_true" method. */
13090
13091static void
13092base_breakpoint_after_condition_true (struct bpstats *bs)
13093{
13094 /* Nothing to do. */
13095}
13096
ab04a2af 13097struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13098{
13099 base_breakpoint_dtor,
13100 base_breakpoint_allocate_location,
13101 base_breakpoint_re_set,
13102 base_breakpoint_insert_location,
13103 base_breakpoint_remove_location,
13104 base_breakpoint_breakpoint_hit,
13105 base_breakpoint_check_status,
13106 base_breakpoint_resources_needed,
13107 base_breakpoint_works_in_software_mode,
13108 base_breakpoint_print_it,
13109 NULL,
13110 base_breakpoint_print_one_detail,
13111 base_breakpoint_print_mention,
983af33b
SDJ
13112 base_breakpoint_print_recreate,
13113 base_breakpoint_create_sals_from_address,
13114 base_breakpoint_create_breakpoints_sal,
13115 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13116 base_breakpoint_explains_signal,
13117 base_breakpoint_after_condition_true,
2060206e
PA
13118};
13119
13120/* Default breakpoint_ops methods. */
13121
13122static void
348d480f
PA
13123bkpt_re_set (struct breakpoint *b)
13124{
06edf0c0
PA
13125 /* FIXME: is this still reachable? */
13126 if (b->addr_string == NULL)
13127 {
13128 /* Anything without a string can't be re-set. */
348d480f 13129 delete_breakpoint (b);
06edf0c0 13130 return;
348d480f 13131 }
06edf0c0
PA
13132
13133 breakpoint_re_set_default (b);
348d480f
PA
13134}
13135
ef370185
JB
13136/* Copy SRC's shadow buffer and whatever else we'd set if we actually
13137 inserted DEST, so we can remove it later, in case SRC is removed
13138 first. */
13139
13140static void
13141bp_target_info_copy_insertion_state (struct bp_target_info *dest,
13142 const struct bp_target_info *src)
13143{
13144 dest->shadow_len = src->shadow_len;
13145 memcpy (dest->shadow_contents, src->shadow_contents, src->shadow_len);
13146 dest->placed_size = src->placed_size;
13147}
13148
2060206e 13149static int
348d480f
PA
13150bkpt_insert_location (struct bp_location *bl)
13151{
13152 if (bl->loc_type == bp_loc_hardware_breakpoint)
13153 return target_insert_hw_breakpoint (bl->gdbarch,
13154 &bl->target_info);
13155 else
ef370185
JB
13156 {
13157 struct bp_target_info *bp_tgt = &bl->target_info;
13158 int ret;
13159 int sss_slot;
13160
13161 /* There is no need to insert a breakpoint if an unconditional
13162 raw/sss breakpoint is already inserted at that location. */
13163 sss_slot = find_single_step_breakpoint (bp_tgt->placed_address_space,
13164 bp_tgt->placed_address);
13165 if (sss_slot >= 0)
13166 {
13167 struct bp_target_info *sss_bp_tgt = single_step_breakpoints[sss_slot];
13168
13169 bp_target_info_copy_insertion_state (bp_tgt, sss_bp_tgt);
13170 return 0;
13171 }
13172
13173 return target_insert_breakpoint (bl->gdbarch, bp_tgt);
13174 }
348d480f
PA
13175}
13176
2060206e 13177static int
348d480f
PA
13178bkpt_remove_location (struct bp_location *bl)
13179{
13180 if (bl->loc_type == bp_loc_hardware_breakpoint)
13181 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13182 else
ef370185
JB
13183 {
13184 struct bp_target_info *bp_tgt = &bl->target_info;
13185 struct address_space *aspace = bp_tgt->placed_address_space;
13186 CORE_ADDR address = bp_tgt->placed_address;
13187
13188 /* Only remove the breakpoint if there is no raw/sss breakpoint
13189 still inserted at this location. Otherwise, we would be
13190 effectively disabling the raw/sss breakpoint. */
13191 if (single_step_breakpoint_inserted_here_p (aspace, address))
13192 return 0;
13193
13194 return target_remove_breakpoint (bl->gdbarch, bp_tgt);
13195 }
348d480f
PA
13196}
13197
2060206e 13198static int
348d480f 13199bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13200 struct address_space *aspace, CORE_ADDR bp_addr,
13201 const struct target_waitstatus *ws)
348d480f 13202{
09ac7c10 13203 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13204 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13205 return 0;
13206
348d480f
PA
13207 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13208 aspace, bp_addr))
13209 return 0;
13210
13211 if (overlay_debugging /* unmapped overlay section */
13212 && section_is_overlay (bl->section)
13213 && !section_is_mapped (bl->section))
13214 return 0;
13215
13216 return 1;
13217}
13218
cd1608cc
PA
13219static int
13220dprintf_breakpoint_hit (const struct bp_location *bl,
13221 struct address_space *aspace, CORE_ADDR bp_addr,
13222 const struct target_waitstatus *ws)
13223{
13224 if (dprintf_style == dprintf_style_agent
13225 && target_can_run_breakpoint_commands ())
13226 {
13227 /* An agent-style dprintf never causes a stop. If we see a trap
13228 for this address it must be for a breakpoint that happens to
13229 be set at the same address. */
13230 return 0;
13231 }
13232
13233 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13234}
13235
2060206e 13236static int
348d480f
PA
13237bkpt_resources_needed (const struct bp_location *bl)
13238{
13239 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13240
13241 return 1;
13242}
13243
2060206e 13244static enum print_stop_action
348d480f
PA
13245bkpt_print_it (bpstat bs)
13246{
348d480f
PA
13247 struct breakpoint *b;
13248 const struct bp_location *bl;
001c8c33 13249 int bp_temp;
79a45e25 13250 struct ui_out *uiout = current_uiout;
348d480f
PA
13251
13252 gdb_assert (bs->bp_location_at != NULL);
13253
13254 bl = bs->bp_location_at;
13255 b = bs->breakpoint_at;
13256
001c8c33
PA
13257 bp_temp = b->disposition == disp_del;
13258 if (bl->address != bl->requested_address)
13259 breakpoint_adjustment_warning (bl->requested_address,
13260 bl->address,
13261 b->number, 1);
13262 annotate_breakpoint (b->number);
13263 if (bp_temp)
13264 ui_out_text (uiout, "\nTemporary breakpoint ");
13265 else
13266 ui_out_text (uiout, "\nBreakpoint ");
13267 if (ui_out_is_mi_like_p (uiout))
348d480f 13268 {
001c8c33
PA
13269 ui_out_field_string (uiout, "reason",
13270 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13271 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13272 }
001c8c33
PA
13273 ui_out_field_int (uiout, "bkptno", b->number);
13274 ui_out_text (uiout, ", ");
06edf0c0 13275
001c8c33 13276 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13277}
13278
2060206e 13279static void
06edf0c0
PA
13280bkpt_print_mention (struct breakpoint *b)
13281{
79a45e25 13282 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13283 return;
13284
13285 switch (b->type)
13286 {
13287 case bp_breakpoint:
13288 case bp_gnu_ifunc_resolver:
13289 if (b->disposition == disp_del)
13290 printf_filtered (_("Temporary breakpoint"));
13291 else
13292 printf_filtered (_("Breakpoint"));
13293 printf_filtered (_(" %d"), b->number);
13294 if (b->type == bp_gnu_ifunc_resolver)
13295 printf_filtered (_(" at gnu-indirect-function resolver"));
13296 break;
13297 case bp_hardware_breakpoint:
13298 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13299 break;
e7e0cddf
SS
13300 case bp_dprintf:
13301 printf_filtered (_("Dprintf %d"), b->number);
13302 break;
06edf0c0
PA
13303 }
13304
13305 say_where (b);
13306}
13307
2060206e 13308static void
06edf0c0
PA
13309bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13310{
13311 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13312 fprintf_unfiltered (fp, "tbreak");
13313 else if (tp->type == bp_breakpoint)
13314 fprintf_unfiltered (fp, "break");
13315 else if (tp->type == bp_hardware_breakpoint
13316 && tp->disposition == disp_del)
13317 fprintf_unfiltered (fp, "thbreak");
13318 else if (tp->type == bp_hardware_breakpoint)
13319 fprintf_unfiltered (fp, "hbreak");
13320 else
13321 internal_error (__FILE__, __LINE__,
13322 _("unhandled breakpoint type %d"), (int) tp->type);
13323
2060206e 13324 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13325 print_recreate_thread (tp, fp);
06edf0c0
PA
13326}
13327
983af33b
SDJ
13328static void
13329bkpt_create_sals_from_address (char **arg,
13330 struct linespec_result *canonical,
13331 enum bptype type_wanted,
13332 char *addr_start, char **copy_arg)
13333{
13334 create_sals_from_address_default (arg, canonical, type_wanted,
13335 addr_start, copy_arg);
13336}
13337
13338static void
13339bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13340 struct linespec_result *canonical,
983af33b 13341 char *cond_string,
e7e0cddf 13342 char *extra_string,
983af33b
SDJ
13343 enum bptype type_wanted,
13344 enum bpdisp disposition,
13345 int thread,
13346 int task, int ignore_count,
13347 const struct breakpoint_ops *ops,
13348 int from_tty, int enabled,
44f238bb 13349 int internal, unsigned flags)
983af33b 13350{
023fa29b 13351 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13352 cond_string, extra_string,
13353 type_wanted,
983af33b
SDJ
13354 disposition, thread, task,
13355 ignore_count, ops, from_tty,
44f238bb 13356 enabled, internal, flags);
983af33b
SDJ
13357}
13358
13359static void
13360bkpt_decode_linespec (struct breakpoint *b, char **s,
13361 struct symtabs_and_lines *sals)
13362{
13363 decode_linespec_default (b, s, sals);
13364}
13365
06edf0c0
PA
13366/* Virtual table for internal breakpoints. */
13367
13368static void
13369internal_bkpt_re_set (struct breakpoint *b)
13370{
13371 switch (b->type)
13372 {
13373 /* Delete overlay event and longjmp master breakpoints; they
13374 will be reset later by breakpoint_re_set. */
13375 case bp_overlay_event:
13376 case bp_longjmp_master:
13377 case bp_std_terminate_master:
13378 case bp_exception_master:
13379 delete_breakpoint (b);
13380 break;
13381
13382 /* This breakpoint is special, it's set up when the inferior
13383 starts and we really don't want to touch it. */
13384 case bp_shlib_event:
13385
13386 /* Like bp_shlib_event, this breakpoint type is special. Once
13387 it is set up, we do not want to touch it. */
13388 case bp_thread_event:
13389 break;
13390 }
13391}
13392
13393static void
13394internal_bkpt_check_status (bpstat bs)
13395{
a9b3a50f
PA
13396 if (bs->breakpoint_at->type == bp_shlib_event)
13397 {
13398 /* If requested, stop when the dynamic linker notifies GDB of
13399 events. This allows the user to get control and place
13400 breakpoints in initializer routines for dynamically loaded
13401 objects (among other things). */
13402 bs->stop = stop_on_solib_events;
13403 bs->print = stop_on_solib_events;
13404 }
13405 else
13406 bs->stop = 0;
06edf0c0
PA
13407}
13408
13409static enum print_stop_action
13410internal_bkpt_print_it (bpstat bs)
13411{
06edf0c0 13412 struct breakpoint *b;
06edf0c0 13413
06edf0c0
PA
13414 b = bs->breakpoint_at;
13415
06edf0c0
PA
13416 switch (b->type)
13417 {
348d480f
PA
13418 case bp_shlib_event:
13419 /* Did we stop because the user set the stop_on_solib_events
13420 variable? (If so, we report this as a generic, "Stopped due
13421 to shlib event" message.) */
edcc5120 13422 print_solib_event (0);
348d480f
PA
13423 break;
13424
13425 case bp_thread_event:
13426 /* Not sure how we will get here.
13427 GDB should not stop for these breakpoints. */
13428 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13429 break;
13430
13431 case bp_overlay_event:
13432 /* By analogy with the thread event, GDB should not stop for these. */
13433 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13434 break;
13435
13436 case bp_longjmp_master:
13437 /* These should never be enabled. */
13438 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13439 break;
13440
13441 case bp_std_terminate_master:
13442 /* These should never be enabled. */
13443 printf_filtered (_("std::terminate Master Breakpoint: "
13444 "gdb should not stop!\n"));
348d480f
PA
13445 break;
13446
13447 case bp_exception_master:
13448 /* These should never be enabled. */
13449 printf_filtered (_("Exception Master Breakpoint: "
13450 "gdb should not stop!\n"));
06edf0c0
PA
13451 break;
13452 }
13453
001c8c33 13454 return PRINT_NOTHING;
06edf0c0
PA
13455}
13456
13457static void
13458internal_bkpt_print_mention (struct breakpoint *b)
13459{
13460 /* Nothing to mention. These breakpoints are internal. */
13461}
13462
06edf0c0
PA
13463/* Virtual table for momentary breakpoints */
13464
13465static void
13466momentary_bkpt_re_set (struct breakpoint *b)
13467{
13468 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13469 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13470 Otherwise these should have been blown away via the cleanup chain
13471 or by breakpoint_init_inferior when we rerun the executable. */
13472}
13473
13474static void
13475momentary_bkpt_check_status (bpstat bs)
13476{
13477 /* Nothing. The point of these breakpoints is causing a stop. */
13478}
13479
13480static enum print_stop_action
13481momentary_bkpt_print_it (bpstat bs)
13482{
79a45e25
PA
13483 struct ui_out *uiout = current_uiout;
13484
001c8c33 13485 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13486 {
001c8c33 13487 struct breakpoint *b = bs->breakpoint_at;
348d480f 13488
001c8c33
PA
13489 switch (b->type)
13490 {
13491 case bp_finish:
13492 ui_out_field_string
13493 (uiout, "reason",
13494 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13495 break;
348d480f 13496
001c8c33
PA
13497 case bp_until:
13498 ui_out_field_string
13499 (uiout, "reason",
13500 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13501 break;
13502 }
348d480f
PA
13503 }
13504
001c8c33 13505 return PRINT_UNKNOWN;
348d480f
PA
13506}
13507
06edf0c0
PA
13508static void
13509momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13510{
06edf0c0 13511 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13512}
13513
e2e4d78b
JK
13514/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13515
13516 It gets cleared already on the removal of the first one of such placed
13517 breakpoints. This is OK as they get all removed altogether. */
13518
13519static void
13520longjmp_bkpt_dtor (struct breakpoint *self)
13521{
13522 struct thread_info *tp = find_thread_id (self->thread);
13523
13524 if (tp)
13525 tp->initiating_frame = null_frame_id;
13526
13527 momentary_breakpoint_ops.dtor (self);
13528}
13529
55aa24fb
SDJ
13530/* Specific methods for probe breakpoints. */
13531
13532static int
13533bkpt_probe_insert_location (struct bp_location *bl)
13534{
13535 int v = bkpt_insert_location (bl);
13536
13537 if (v == 0)
13538 {
13539 /* The insertion was successful, now let's set the probe's semaphore
13540 if needed. */
729662a5
TT
13541 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13542 bl->probe.objfile,
13543 bl->gdbarch);
55aa24fb
SDJ
13544 }
13545
13546 return v;
13547}
13548
13549static int
13550bkpt_probe_remove_location (struct bp_location *bl)
13551{
13552 /* Let's clear the semaphore before removing the location. */
729662a5
TT
13553 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13554 bl->probe.objfile,
13555 bl->gdbarch);
55aa24fb
SDJ
13556
13557 return bkpt_remove_location (bl);
13558}
13559
13560static void
13561bkpt_probe_create_sals_from_address (char **arg,
13562 struct linespec_result *canonical,
13563 enum bptype type_wanted,
13564 char *addr_start, char **copy_arg)
13565{
13566 struct linespec_sals lsal;
13567
13568 lsal.sals = parse_probes (arg, canonical);
13569
13570 *copy_arg = xstrdup (canonical->addr_string);
13571 lsal.canonical = xstrdup (*copy_arg);
13572
13573 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13574}
13575
13576static void
13577bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13578 struct symtabs_and_lines *sals)
13579{
13580 *sals = parse_probes (s, NULL);
13581 if (!sals->sals)
13582 error (_("probe not found"));
13583}
13584
348d480f 13585/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13586
348d480f
PA
13587static void
13588tracepoint_re_set (struct breakpoint *b)
13589{
13590 breakpoint_re_set_default (b);
13591}
876fa593 13592
348d480f
PA
13593static int
13594tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13595 struct address_space *aspace, CORE_ADDR bp_addr,
13596 const struct target_waitstatus *ws)
348d480f
PA
13597{
13598 /* By definition, the inferior does not report stops at
13599 tracepoints. */
13600 return 0;
74960c60
VP
13601}
13602
13603static void
348d480f
PA
13604tracepoint_print_one_detail (const struct breakpoint *self,
13605 struct ui_out *uiout)
74960c60 13606{
d9b3f62e
PA
13607 struct tracepoint *tp = (struct tracepoint *) self;
13608 if (tp->static_trace_marker_id)
348d480f
PA
13609 {
13610 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13611
348d480f
PA
13612 ui_out_text (uiout, "\tmarker id is ");
13613 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13614 tp->static_trace_marker_id);
348d480f
PA
13615 ui_out_text (uiout, "\n");
13616 }
0d381245
VP
13617}
13618
a474d7c2 13619static void
348d480f 13620tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13621{
79a45e25 13622 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13623 return;
cc59ec59 13624
348d480f
PA
13625 switch (b->type)
13626 {
13627 case bp_tracepoint:
13628 printf_filtered (_("Tracepoint"));
13629 printf_filtered (_(" %d"), b->number);
13630 break;
13631 case bp_fast_tracepoint:
13632 printf_filtered (_("Fast tracepoint"));
13633 printf_filtered (_(" %d"), b->number);
13634 break;
13635 case bp_static_tracepoint:
13636 printf_filtered (_("Static tracepoint"));
13637 printf_filtered (_(" %d"), b->number);
13638 break;
13639 default:
13640 internal_error (__FILE__, __LINE__,
13641 _("unhandled tracepoint type %d"), (int) b->type);
13642 }
13643
13644 say_where (b);
a474d7c2
PA
13645}
13646
348d480f 13647static void
d9b3f62e 13648tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13649{
d9b3f62e
PA
13650 struct tracepoint *tp = (struct tracepoint *) self;
13651
13652 if (self->type == bp_fast_tracepoint)
348d480f 13653 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13654 if (self->type == bp_static_tracepoint)
348d480f 13655 fprintf_unfiltered (fp, "strace");
d9b3f62e 13656 else if (self->type == bp_tracepoint)
348d480f
PA
13657 fprintf_unfiltered (fp, "trace");
13658 else
13659 internal_error (__FILE__, __LINE__,
d9b3f62e 13660 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13661
d9b3f62e
PA
13662 fprintf_unfiltered (fp, " %s", self->addr_string);
13663 print_recreate_thread (self, fp);
13664
13665 if (tp->pass_count)
13666 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13667}
13668
983af33b
SDJ
13669static void
13670tracepoint_create_sals_from_address (char **arg,
13671 struct linespec_result *canonical,
13672 enum bptype type_wanted,
13673 char *addr_start, char **copy_arg)
13674{
13675 create_sals_from_address_default (arg, canonical, type_wanted,
13676 addr_start, copy_arg);
13677}
13678
13679static void
13680tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13681 struct linespec_result *canonical,
983af33b 13682 char *cond_string,
e7e0cddf 13683 char *extra_string,
983af33b
SDJ
13684 enum bptype type_wanted,
13685 enum bpdisp disposition,
13686 int thread,
13687 int task, int ignore_count,
13688 const struct breakpoint_ops *ops,
13689 int from_tty, int enabled,
44f238bb 13690 int internal, unsigned flags)
983af33b 13691{
023fa29b 13692 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13693 cond_string, extra_string,
13694 type_wanted,
983af33b
SDJ
13695 disposition, thread, task,
13696 ignore_count, ops, from_tty,
44f238bb 13697 enabled, internal, flags);
983af33b
SDJ
13698}
13699
13700static void
13701tracepoint_decode_linespec (struct breakpoint *b, char **s,
13702 struct symtabs_and_lines *sals)
13703{
13704 decode_linespec_default (b, s, sals);
13705}
13706
2060206e 13707struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13708
55aa24fb
SDJ
13709/* The breakpoint_ops structure to be use on tracepoints placed in a
13710 static probe. */
13711
13712static void
13713tracepoint_probe_create_sals_from_address (char **arg,
13714 struct linespec_result *canonical,
13715 enum bptype type_wanted,
13716 char *addr_start, char **copy_arg)
13717{
13718 /* We use the same method for breakpoint on probes. */
13719 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13720 addr_start, copy_arg);
13721}
13722
13723static void
13724tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13725 struct symtabs_and_lines *sals)
13726{
13727 /* We use the same method for breakpoint on probes. */
13728 bkpt_probe_decode_linespec (b, s, sals);
13729}
13730
13731static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13732
5c2b4418
HZ
13733/* Dprintf breakpoint_ops methods. */
13734
13735static void
13736dprintf_re_set (struct breakpoint *b)
13737{
13738 breakpoint_re_set_default (b);
13739
13740 /* This breakpoint could have been pending, and be resolved now, and
13741 if so, we should now have the extra string. If we don't, the
13742 dprintf was malformed when created, but we couldn't tell because
13743 we can't extract the extra string until the location is
13744 resolved. */
13745 if (b->loc != NULL && b->extra_string == NULL)
13746 error (_("Format string required"));
13747
13748 /* 1 - connect to target 1, that can run breakpoint commands.
13749 2 - create a dprintf, which resolves fine.
13750 3 - disconnect from target 1
13751 4 - connect to target 2, that can NOT run breakpoint commands.
13752
13753 After steps #3/#4, you'll want the dprintf command list to
13754 be updated, because target 1 and 2 may well return different
13755 answers for target_can_run_breakpoint_commands().
13756 Given absence of finer grained resetting, we get to do
13757 it all the time. */
13758 if (b->extra_string != NULL)
13759 update_dprintf_command_list (b);
13760}
13761
2d9442cc
HZ
13762/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13763
13764static void
13765dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13766{
13767 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13768 tp->extra_string);
13769 print_recreate_thread (tp, fp);
13770}
13771
9d6e6e84
HZ
13772/* Implement the "after_condition_true" breakpoint_ops method for
13773 dprintf.
13774
13775 dprintf's are implemented with regular commands in their command
13776 list, but we run the commands here instead of before presenting the
13777 stop to the user, as dprintf's don't actually cause a stop. This
13778 also makes it so that the commands of multiple dprintfs at the same
13779 address are all handled. */
13780
13781static void
13782dprintf_after_condition_true (struct bpstats *bs)
13783{
13784 struct cleanup *old_chain;
13785 struct bpstats tmp_bs = { NULL };
13786 struct bpstats *tmp_bs_p = &tmp_bs;
13787
13788 /* dprintf's never cause a stop. This wasn't set in the
13789 check_status hook instead because that would make the dprintf's
13790 condition not be evaluated. */
13791 bs->stop = 0;
13792
13793 /* Run the command list here. Take ownership of it instead of
13794 copying. We never want these commands to run later in
13795 bpstat_do_actions, if a breakpoint that causes a stop happens to
13796 be set at same address as this dprintf, or even if running the
13797 commands here throws. */
13798 tmp_bs.commands = bs->commands;
13799 bs->commands = NULL;
13800 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13801
13802 bpstat_do_actions_1 (&tmp_bs_p);
13803
13804 /* 'tmp_bs.commands' will usually be NULL by now, but
13805 bpstat_do_actions_1 may return early without processing the whole
13806 list. */
13807 do_cleanups (old_chain);
13808}
13809
983af33b
SDJ
13810/* The breakpoint_ops structure to be used on static tracepoints with
13811 markers (`-m'). */
13812
13813static void
13814strace_marker_create_sals_from_address (char **arg,
13815 struct linespec_result *canonical,
13816 enum bptype type_wanted,
13817 char *addr_start, char **copy_arg)
13818{
13819 struct linespec_sals lsal;
13820
13821 lsal.sals = decode_static_tracepoint_spec (arg);
13822
13823 *copy_arg = savestring (addr_start, *arg - addr_start);
13824
13825 canonical->addr_string = xstrdup (*copy_arg);
13826 lsal.canonical = xstrdup (*copy_arg);
13827 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13828}
13829
13830static void
13831strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13832 struct linespec_result *canonical,
983af33b 13833 char *cond_string,
e7e0cddf 13834 char *extra_string,
983af33b
SDJ
13835 enum bptype type_wanted,
13836 enum bpdisp disposition,
13837 int thread,
13838 int task, int ignore_count,
13839 const struct breakpoint_ops *ops,
13840 int from_tty, int enabled,
44f238bb 13841 int internal, unsigned flags)
983af33b
SDJ
13842{
13843 int i;
52d361e1
YQ
13844 struct linespec_sals *lsal = VEC_index (linespec_sals,
13845 canonical->sals, 0);
983af33b
SDJ
13846
13847 /* If the user is creating a static tracepoint by marker id
13848 (strace -m MARKER_ID), then store the sals index, so that
13849 breakpoint_re_set can try to match up which of the newly
13850 found markers corresponds to this one, and, don't try to
13851 expand multiple locations for each sal, given than SALS
13852 already should contain all sals for MARKER_ID. */
13853
13854 for (i = 0; i < lsal->sals.nelts; ++i)
13855 {
13856 struct symtabs_and_lines expanded;
13857 struct tracepoint *tp;
13858 struct cleanup *old_chain;
13859 char *addr_string;
13860
13861 expanded.nelts = 1;
13862 expanded.sals = &lsal->sals.sals[i];
13863
13864 addr_string = xstrdup (canonical->addr_string);
13865 old_chain = make_cleanup (xfree, addr_string);
13866
13867 tp = XCNEW (struct tracepoint);
13868 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13869 addr_string, NULL,
e7e0cddf
SS
13870 cond_string, extra_string,
13871 type_wanted, disposition,
983af33b 13872 thread, task, ignore_count, ops,
44f238bb 13873 from_tty, enabled, internal, flags,
983af33b
SDJ
13874 canonical->special_display);
13875 /* Given that its possible to have multiple markers with
13876 the same string id, if the user is creating a static
13877 tracepoint by marker id ("strace -m MARKER_ID"), then
13878 store the sals index, so that breakpoint_re_set can
13879 try to match up which of the newly found markers
13880 corresponds to this one */
13881 tp->static_trace_marker_id_idx = i;
13882
13883 install_breakpoint (internal, &tp->base, 0);
13884
13885 discard_cleanups (old_chain);
13886 }
13887}
13888
13889static void
13890strace_marker_decode_linespec (struct breakpoint *b, char **s,
13891 struct symtabs_and_lines *sals)
13892{
13893 struct tracepoint *tp = (struct tracepoint *) b;
13894
13895 *sals = decode_static_tracepoint_spec (s);
13896 if (sals->nelts > tp->static_trace_marker_id_idx)
13897 {
13898 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13899 sals->nelts = 1;
13900 }
13901 else
13902 error (_("marker %s not found"), tp->static_trace_marker_id);
13903}
13904
13905static struct breakpoint_ops strace_marker_breakpoint_ops;
13906
13907static int
13908strace_marker_p (struct breakpoint *b)
13909{
13910 return b->ops == &strace_marker_breakpoint_ops;
13911}
13912
53a5351d 13913/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13914 structures. */
c906108c
SS
13915
13916void
fba45db2 13917delete_breakpoint (struct breakpoint *bpt)
c906108c 13918{
52f0bd74 13919 struct breakpoint *b;
c906108c 13920
8a3fe4f8 13921 gdb_assert (bpt != NULL);
c906108c 13922
4a64f543
MS
13923 /* Has this bp already been deleted? This can happen because
13924 multiple lists can hold pointers to bp's. bpstat lists are
13925 especial culprits.
13926
13927 One example of this happening is a watchpoint's scope bp. When
13928 the scope bp triggers, we notice that the watchpoint is out of
13929 scope, and delete it. We also delete its scope bp. But the
13930 scope bp is marked "auto-deleting", and is already on a bpstat.
13931 That bpstat is then checked for auto-deleting bp's, which are
13932 deleted.
13933
13934 A real solution to this problem might involve reference counts in
13935 bp's, and/or giving them pointers back to their referencing
13936 bpstat's, and teaching delete_breakpoint to only free a bp's
13937 storage when no more references were extent. A cheaper bandaid
13938 was chosen. */
c906108c
SS
13939 if (bpt->type == bp_none)
13940 return;
13941
4a64f543
MS
13942 /* At least avoid this stale reference until the reference counting
13943 of breakpoints gets resolved. */
d0fb5eae 13944 if (bpt->related_breakpoint != bpt)
e5a0a904 13945 {
d0fb5eae 13946 struct breakpoint *related;
3a5c3e22 13947 struct watchpoint *w;
d0fb5eae
JK
13948
13949 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13950 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13951 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13952 w = (struct watchpoint *) bpt;
13953 else
13954 w = NULL;
13955 if (w != NULL)
13956 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13957
13958 /* Unlink bpt from the bpt->related_breakpoint ring. */
13959 for (related = bpt; related->related_breakpoint != bpt;
13960 related = related->related_breakpoint);
13961 related->related_breakpoint = bpt->related_breakpoint;
13962 bpt->related_breakpoint = bpt;
e5a0a904
JK
13963 }
13964
a9634178
TJB
13965 /* watch_command_1 creates a watchpoint but only sets its number if
13966 update_watchpoint succeeds in creating its bp_locations. If there's
13967 a problem in that process, we'll be asked to delete the half-created
13968 watchpoint. In that case, don't announce the deletion. */
13969 if (bpt->number)
13970 observer_notify_breakpoint_deleted (bpt);
c906108c 13971
c906108c
SS
13972 if (breakpoint_chain == bpt)
13973 breakpoint_chain = bpt->next;
13974
c906108c
SS
13975 ALL_BREAKPOINTS (b)
13976 if (b->next == bpt)
c5aa993b
JM
13977 {
13978 b->next = bpt->next;
13979 break;
13980 }
c906108c 13981
f431efe5
PA
13982 /* Be sure no bpstat's are pointing at the breakpoint after it's
13983 been freed. */
13984 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13985 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13986 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13987 commands are associated with the bpstat; if we remove it here,
13988 then the later call to bpstat_do_actions (&stop_bpstat); in
13989 event-top.c won't do anything, and temporary breakpoints with
13990 commands won't work. */
13991
13992 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13993
4a64f543
MS
13994 /* Now that breakpoint is removed from breakpoint list, update the
13995 global location list. This will remove locations that used to
13996 belong to this breakpoint. Do this before freeing the breakpoint
13997 itself, since remove_breakpoint looks at location's owner. It
13998 might be better design to have location completely
13999 self-contained, but it's not the case now. */
b60e7edf 14000 update_global_location_list (0);
74960c60 14001
348d480f 14002 bpt->ops->dtor (bpt);
4a64f543
MS
14003 /* On the chance that someone will soon try again to delete this
14004 same bp, we mark it as deleted before freeing its storage. */
c906108c 14005 bpt->type = bp_none;
b8c9b27d 14006 xfree (bpt);
c906108c
SS
14007}
14008
4d6140d9
AC
14009static void
14010do_delete_breakpoint_cleanup (void *b)
14011{
14012 delete_breakpoint (b);
14013}
14014
14015struct cleanup *
14016make_cleanup_delete_breakpoint (struct breakpoint *b)
14017{
14018 return make_cleanup (do_delete_breakpoint_cleanup, b);
14019}
14020
51be5b68
PA
14021/* Iterator function to call a user-provided callback function once
14022 for each of B and its related breakpoints. */
14023
14024static void
14025iterate_over_related_breakpoints (struct breakpoint *b,
14026 void (*function) (struct breakpoint *,
14027 void *),
14028 void *data)
14029{
14030 struct breakpoint *related;
14031
14032 related = b;
14033 do
14034 {
14035 struct breakpoint *next;
14036
14037 /* FUNCTION may delete RELATED. */
14038 next = related->related_breakpoint;
14039
14040 if (next == related)
14041 {
14042 /* RELATED is the last ring entry. */
14043 function (related, data);
14044
14045 /* FUNCTION may have deleted it, so we'd never reach back to
14046 B. There's nothing left to do anyway, so just break
14047 out. */
14048 break;
14049 }
14050 else
14051 function (related, data);
14052
14053 related = next;
14054 }
14055 while (related != b);
14056}
95a42b64
TT
14057
14058static void
14059do_delete_breakpoint (struct breakpoint *b, void *ignore)
14060{
14061 delete_breakpoint (b);
14062}
14063
51be5b68
PA
14064/* A callback for map_breakpoint_numbers that calls
14065 delete_breakpoint. */
14066
14067static void
14068do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14069{
14070 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14071}
14072
c906108c 14073void
fba45db2 14074delete_command (char *arg, int from_tty)
c906108c 14075{
35df4500 14076 struct breakpoint *b, *b_tmp;
c906108c 14077
ea9365bb
TT
14078 dont_repeat ();
14079
c906108c
SS
14080 if (arg == 0)
14081 {
14082 int breaks_to_delete = 0;
14083
46c6471b
PA
14084 /* Delete all breakpoints if no argument. Do not delete
14085 internal breakpoints, these have to be deleted with an
14086 explicit breakpoint number argument. */
c5aa993b 14087 ALL_BREAKPOINTS (b)
46c6471b 14088 if (user_breakpoint_p (b))
973d738b
DJ
14089 {
14090 breaks_to_delete = 1;
14091 break;
14092 }
c906108c
SS
14093
14094 /* Ask user only if there are some breakpoints to delete. */
14095 if (!from_tty
e2e0b3e5 14096 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14097 {
35df4500 14098 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14099 if (user_breakpoint_p (b))
c5aa993b 14100 delete_breakpoint (b);
c906108c
SS
14101 }
14102 }
14103 else
51be5b68 14104 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14105}
14106
0d381245
VP
14107static int
14108all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14109{
0d381245 14110 for (; loc; loc = loc->next)
8645ff69
UW
14111 if (!loc->shlib_disabled
14112 && !loc->pspace->executing_startup)
0d381245
VP
14113 return 0;
14114 return 1;
fe3f5fa8
VP
14115}
14116
776592bf
DE
14117/* Subroutine of update_breakpoint_locations to simplify it.
14118 Return non-zero if multiple fns in list LOC have the same name.
14119 Null names are ignored. */
14120
14121static int
14122ambiguous_names_p (struct bp_location *loc)
14123{
14124 struct bp_location *l;
14125 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14126 (int (*) (const void *,
14127 const void *)) streq,
776592bf
DE
14128 NULL, xcalloc, xfree);
14129
14130 for (l = loc; l != NULL; l = l->next)
14131 {
14132 const char **slot;
14133 const char *name = l->function_name;
14134
14135 /* Allow for some names to be NULL, ignore them. */
14136 if (name == NULL)
14137 continue;
14138
14139 slot = (const char **) htab_find_slot (htab, (const void *) name,
14140 INSERT);
4a64f543
MS
14141 /* NOTE: We can assume slot != NULL here because xcalloc never
14142 returns NULL. */
776592bf
DE
14143 if (*slot != NULL)
14144 {
14145 htab_delete (htab);
14146 return 1;
14147 }
14148 *slot = name;
14149 }
14150
14151 htab_delete (htab);
14152 return 0;
14153}
14154
0fb4aa4b
PA
14155/* When symbols change, it probably means the sources changed as well,
14156 and it might mean the static tracepoint markers are no longer at
14157 the same address or line numbers they used to be at last we
14158 checked. Losing your static tracepoints whenever you rebuild is
14159 undesirable. This function tries to resync/rematch gdb static
14160 tracepoints with the markers on the target, for static tracepoints
14161 that have not been set by marker id. Static tracepoint that have
14162 been set by marker id are reset by marker id in breakpoint_re_set.
14163 The heuristic is:
14164
14165 1) For a tracepoint set at a specific address, look for a marker at
14166 the old PC. If one is found there, assume to be the same marker.
14167 If the name / string id of the marker found is different from the
14168 previous known name, assume that means the user renamed the marker
14169 in the sources, and output a warning.
14170
14171 2) For a tracepoint set at a given line number, look for a marker
14172 at the new address of the old line number. If one is found there,
14173 assume to be the same marker. If the name / string id of the
14174 marker found is different from the previous known name, assume that
14175 means the user renamed the marker in the sources, and output a
14176 warning.
14177
14178 3) If a marker is no longer found at the same address or line, it
14179 may mean the marker no longer exists. But it may also just mean
14180 the code changed a bit. Maybe the user added a few lines of code
14181 that made the marker move up or down (in line number terms). Ask
14182 the target for info about the marker with the string id as we knew
14183 it. If found, update line number and address in the matching
14184 static tracepoint. This will get confused if there's more than one
14185 marker with the same ID (possible in UST, although unadvised
14186 precisely because it confuses tools). */
14187
14188static struct symtab_and_line
14189update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14190{
d9b3f62e 14191 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14192 struct static_tracepoint_marker marker;
14193 CORE_ADDR pc;
0fb4aa4b
PA
14194
14195 pc = sal.pc;
14196 if (sal.line)
14197 find_line_pc (sal.symtab, sal.line, &pc);
14198
14199 if (target_static_tracepoint_marker_at (pc, &marker))
14200 {
d9b3f62e 14201 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14202 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14203 b->number,
d9b3f62e 14204 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14205
d9b3f62e
PA
14206 xfree (tp->static_trace_marker_id);
14207 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14208 release_static_tracepoint_marker (&marker);
14209
14210 return sal;
14211 }
14212
14213 /* Old marker wasn't found on target at lineno. Try looking it up
14214 by string ID. */
14215 if (!sal.explicit_pc
14216 && sal.line != 0
14217 && sal.symtab != NULL
d9b3f62e 14218 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14219 {
14220 VEC(static_tracepoint_marker_p) *markers;
14221
14222 markers
d9b3f62e 14223 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14224
14225 if (!VEC_empty(static_tracepoint_marker_p, markers))
14226 {
80e1d417 14227 struct symtab_and_line sal2;
0fb4aa4b 14228 struct symbol *sym;
80e1d417 14229 struct static_tracepoint_marker *tpmarker;
79a45e25 14230 struct ui_out *uiout = current_uiout;
0fb4aa4b 14231
80e1d417 14232 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14233
d9b3f62e 14234 xfree (tp->static_trace_marker_id);
80e1d417 14235 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14236
14237 warning (_("marker for static tracepoint %d (%s) not "
14238 "found at previous line number"),
d9b3f62e 14239 b->number, tp->static_trace_marker_id);
0fb4aa4b 14240
80e1d417 14241 init_sal (&sal2);
0fb4aa4b 14242
80e1d417 14243 sal2.pc = tpmarker->address;
0fb4aa4b 14244
80e1d417
AS
14245 sal2 = find_pc_line (tpmarker->address, 0);
14246 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14247 ui_out_text (uiout, "Now in ");
14248 if (sym)
14249 {
14250 ui_out_field_string (uiout, "func",
14251 SYMBOL_PRINT_NAME (sym));
14252 ui_out_text (uiout, " at ");
14253 }
05cba821
JK
14254 ui_out_field_string (uiout, "file",
14255 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14256 ui_out_text (uiout, ":");
14257
14258 if (ui_out_is_mi_like_p (uiout))
14259 {
0b0865da 14260 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14261
f35a17b5 14262 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14263 }
14264
80e1d417 14265 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14266 ui_out_text (uiout, "\n");
14267
80e1d417 14268 b->loc->line_number = sal2.line;
2f202fde 14269 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14270
14271 xfree (b->addr_string);
14272 b->addr_string = xstrprintf ("%s:%d",
05cba821 14273 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14274 b->loc->line_number);
0fb4aa4b
PA
14275
14276 /* Might be nice to check if function changed, and warn if
14277 so. */
14278
80e1d417 14279 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14280 }
14281 }
14282 return sal;
14283}
14284
8d3788bd
VP
14285/* Returns 1 iff locations A and B are sufficiently same that
14286 we don't need to report breakpoint as changed. */
14287
14288static int
14289locations_are_equal (struct bp_location *a, struct bp_location *b)
14290{
14291 while (a && b)
14292 {
14293 if (a->address != b->address)
14294 return 0;
14295
14296 if (a->shlib_disabled != b->shlib_disabled)
14297 return 0;
14298
14299 if (a->enabled != b->enabled)
14300 return 0;
14301
14302 a = a->next;
14303 b = b->next;
14304 }
14305
14306 if ((a == NULL) != (b == NULL))
14307 return 0;
14308
14309 return 1;
14310}
14311
f1310107
TJB
14312/* Create new breakpoint locations for B (a hardware or software breakpoint)
14313 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14314 a ranged breakpoint. */
14315
0e30163f 14316void
0d381245 14317update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14318 struct symtabs_and_lines sals,
14319 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14320{
14321 int i;
0d381245
VP
14322 struct bp_location *existing_locations = b->loc;
14323
f8eba3c6
TT
14324 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14325 {
14326 /* Ranged breakpoints have only one start location and one end
14327 location. */
14328 b->enable_state = bp_disabled;
14329 update_global_location_list (1);
14330 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14331 "multiple locations found\n"),
14332 b->number);
14333 return;
14334 }
f1310107 14335
4a64f543
MS
14336 /* If there's no new locations, and all existing locations are
14337 pending, don't do anything. This optimizes the common case where
14338 all locations are in the same shared library, that was unloaded.
14339 We'd like to retain the location, so that when the library is
14340 loaded again, we don't loose the enabled/disabled status of the
14341 individual locations. */
0d381245 14342 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14343 return;
14344
fe3f5fa8
VP
14345 b->loc = NULL;
14346
0d381245 14347 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14348 {
f8eba3c6
TT
14349 struct bp_location *new_loc;
14350
14351 switch_to_program_space_and_thread (sals.sals[i].pspace);
14352
14353 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14354
0d381245
VP
14355 /* Reparse conditions, they might contain references to the
14356 old symtab. */
14357 if (b->cond_string != NULL)
14358 {
bbc13ae3 14359 const char *s;
bfd189b1 14360 volatile struct gdb_exception e;
fe3f5fa8 14361
0d381245
VP
14362 s = b->cond_string;
14363 TRY_CATCH (e, RETURN_MASK_ERROR)
14364 {
1bb9788d
TT
14365 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14366 block_for_pc (sals.sals[i].pc),
0d381245
VP
14367 0);
14368 }
14369 if (e.reason < 0)
14370 {
3e43a32a
MS
14371 warning (_("failed to reevaluate condition "
14372 "for breakpoint %d: %s"),
0d381245
VP
14373 b->number, e.message);
14374 new_loc->enabled = 0;
14375 }
14376 }
fe3f5fa8 14377
f1310107
TJB
14378 if (sals_end.nelts)
14379 {
14380 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14381
14382 new_loc->length = end - sals.sals[0].pc + 1;
14383 }
0d381245 14384 }
fe3f5fa8 14385
514f746b
AR
14386 /* Update locations of permanent breakpoints. */
14387 if (b->enable_state == bp_permanent)
14388 make_breakpoint_permanent (b);
14389
4a64f543
MS
14390 /* If possible, carry over 'disable' status from existing
14391 breakpoints. */
0d381245
VP
14392 {
14393 struct bp_location *e = existing_locations;
776592bf
DE
14394 /* If there are multiple breakpoints with the same function name,
14395 e.g. for inline functions, comparing function names won't work.
14396 Instead compare pc addresses; this is just a heuristic as things
14397 may have moved, but in practice it gives the correct answer
14398 often enough until a better solution is found. */
14399 int have_ambiguous_names = ambiguous_names_p (b->loc);
14400
0d381245
VP
14401 for (; e; e = e->next)
14402 {
14403 if (!e->enabled && e->function_name)
14404 {
14405 struct bp_location *l = b->loc;
776592bf
DE
14406 if (have_ambiguous_names)
14407 {
14408 for (; l; l = l->next)
f1310107 14409 if (breakpoint_locations_match (e, l))
776592bf
DE
14410 {
14411 l->enabled = 0;
14412 break;
14413 }
14414 }
14415 else
14416 {
14417 for (; l; l = l->next)
14418 if (l->function_name
14419 && strcmp (e->function_name, l->function_name) == 0)
14420 {
14421 l->enabled = 0;
14422 break;
14423 }
14424 }
0d381245
VP
14425 }
14426 }
14427 }
fe3f5fa8 14428
8d3788bd
VP
14429 if (!locations_are_equal (existing_locations, b->loc))
14430 observer_notify_breakpoint_modified (b);
14431
b60e7edf 14432 update_global_location_list (1);
fe3f5fa8
VP
14433}
14434
ef23e705
TJB
14435/* Find the SaL locations corresponding to the given ADDR_STRING.
14436 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14437
14438static struct symtabs_and_lines
14439addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14440{
14441 char *s;
02d20e4a 14442 struct symtabs_and_lines sals = {0};
f8eba3c6 14443 volatile struct gdb_exception e;
ef23e705 14444
983af33b 14445 gdb_assert (b->ops != NULL);
ef23e705 14446 s = addr_string;
ef23e705
TJB
14447
14448 TRY_CATCH (e, RETURN_MASK_ERROR)
14449 {
983af33b 14450 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14451 }
14452 if (e.reason < 0)
14453 {
14454 int not_found_and_ok = 0;
14455 /* For pending breakpoints, it's expected that parsing will
14456 fail until the right shared library is loaded. User has
14457 already told to create pending breakpoints and don't need
14458 extra messages. If breakpoint is in bp_shlib_disabled
14459 state, then user already saw the message about that
14460 breakpoint being disabled, and don't want to see more
14461 errors. */
58438ac1 14462 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14463 && (b->condition_not_parsed
14464 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14465 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14466 || b->enable_state == bp_disabled))
14467 not_found_and_ok = 1;
14468
14469 if (!not_found_and_ok)
14470 {
14471 /* We surely don't want to warn about the same breakpoint
14472 10 times. One solution, implemented here, is disable
14473 the breakpoint on error. Another solution would be to
14474 have separate 'warning emitted' flag. Since this
14475 happens only when a binary has changed, I don't know
14476 which approach is better. */
14477 b->enable_state = bp_disabled;
14478 throw_exception (e);
14479 }
14480 }
14481
58438ac1 14482 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14483 {
f8eba3c6 14484 int i;
ef23e705 14485
f8eba3c6
TT
14486 for (i = 0; i < sals.nelts; ++i)
14487 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14488 if (b->condition_not_parsed && s && s[0])
14489 {
ed1d1739
KS
14490 char *cond_string, *extra_string;
14491 int thread, task;
ef23e705
TJB
14492
14493 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14494 &cond_string, &thread, &task,
14495 &extra_string);
ef23e705
TJB
14496 if (cond_string)
14497 b->cond_string = cond_string;
14498 b->thread = thread;
14499 b->task = task;
e7e0cddf
SS
14500 if (extra_string)
14501 b->extra_string = extra_string;
ef23e705
TJB
14502 b->condition_not_parsed = 0;
14503 }
14504
983af33b 14505 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14506 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14507
58438ac1
TT
14508 *found = 1;
14509 }
14510 else
14511 *found = 0;
ef23e705
TJB
14512
14513 return sals;
14514}
14515
348d480f
PA
14516/* The default re_set method, for typical hardware or software
14517 breakpoints. Reevaluate the breakpoint and recreate its
14518 locations. */
14519
14520static void
28010a5d 14521breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14522{
14523 int found;
f1310107 14524 struct symtabs_and_lines sals, sals_end;
ef23e705 14525 struct symtabs_and_lines expanded = {0};
f1310107 14526 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14527
14528 sals = addr_string_to_sals (b, b->addr_string, &found);
14529 if (found)
14530 {
14531 make_cleanup (xfree, sals.sals);
f8eba3c6 14532 expanded = sals;
ef23e705
TJB
14533 }
14534
f1310107
TJB
14535 if (b->addr_string_range_end)
14536 {
14537 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14538 if (found)
14539 {
14540 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14541 expanded_end = sals_end;
f1310107
TJB
14542 }
14543 }
14544
14545 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14546}
14547
983af33b
SDJ
14548/* Default method for creating SALs from an address string. It basically
14549 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14550
14551static void
14552create_sals_from_address_default (char **arg,
14553 struct linespec_result *canonical,
14554 enum bptype type_wanted,
14555 char *addr_start, char **copy_arg)
14556{
14557 parse_breakpoint_sals (arg, canonical);
14558}
14559
14560/* Call create_breakpoints_sal for the given arguments. This is the default
14561 function for the `create_breakpoints_sal' method of
14562 breakpoint_ops. */
14563
14564static void
14565create_breakpoints_sal_default (struct gdbarch *gdbarch,
14566 struct linespec_result *canonical,
983af33b 14567 char *cond_string,
e7e0cddf 14568 char *extra_string,
983af33b
SDJ
14569 enum bptype type_wanted,
14570 enum bpdisp disposition,
14571 int thread,
14572 int task, int ignore_count,
14573 const struct breakpoint_ops *ops,
14574 int from_tty, int enabled,
44f238bb 14575 int internal, unsigned flags)
983af33b
SDJ
14576{
14577 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14578 extra_string,
983af33b
SDJ
14579 type_wanted, disposition,
14580 thread, task, ignore_count, ops, from_tty,
44f238bb 14581 enabled, internal, flags);
983af33b
SDJ
14582}
14583
14584/* Decode the line represented by S by calling decode_line_full. This is the
14585 default function for the `decode_linespec' method of breakpoint_ops. */
14586
14587static void
14588decode_linespec_default (struct breakpoint *b, char **s,
14589 struct symtabs_and_lines *sals)
14590{
14591 struct linespec_result canonical;
14592
14593 init_linespec_result (&canonical);
14594 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14595 (struct symtab *) NULL, 0,
14596 &canonical, multiple_symbols_all,
14597 b->filter);
14598
14599 /* We should get 0 or 1 resulting SALs. */
14600 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14601
14602 if (VEC_length (linespec_sals, canonical.sals) > 0)
14603 {
14604 struct linespec_sals *lsal;
14605
14606 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14607 *sals = lsal->sals;
14608 /* Arrange it so the destructor does not free the
14609 contents. */
14610 lsal->sals.sals = NULL;
14611 }
14612
14613 destroy_linespec_result (&canonical);
14614}
14615
28010a5d
PA
14616/* Prepare the global context for a re-set of breakpoint B. */
14617
14618static struct cleanup *
14619prepare_re_set_context (struct breakpoint *b)
14620{
14621 struct cleanup *cleanups;
14622
14623 input_radix = b->input_radix;
14624 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14625 if (b->pspace != NULL)
14626 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14627 set_language (b->language);
14628
14629 return cleanups;
ef23e705
TJB
14630}
14631
c906108c
SS
14632/* Reset a breakpoint given it's struct breakpoint * BINT.
14633 The value we return ends up being the return value from catch_errors.
14634 Unused in this case. */
14635
14636static int
4efb68b1 14637breakpoint_re_set_one (void *bint)
c906108c 14638{
4a64f543 14639 /* Get past catch_errs. */
53a5351d 14640 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14641 struct cleanup *cleanups;
c906108c 14642
348d480f
PA
14643 cleanups = prepare_re_set_context (b);
14644 b->ops->re_set (b);
14645 do_cleanups (cleanups);
c906108c
SS
14646 return 0;
14647}
14648
69de3c6a 14649/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14650void
69de3c6a 14651breakpoint_re_set (void)
c906108c 14652{
35df4500 14653 struct breakpoint *b, *b_tmp;
c906108c
SS
14654 enum language save_language;
14655 int save_input_radix;
6c95b8df 14656 struct cleanup *old_chain;
c5aa993b 14657
c906108c
SS
14658 save_language = current_language->la_language;
14659 save_input_radix = input_radix;
6c95b8df
PA
14660 old_chain = save_current_program_space ();
14661
35df4500 14662 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14663 {
4a64f543 14664 /* Format possible error msg. */
fe3f5fa8 14665 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14666 b->number);
14667 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14668 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14669 do_cleanups (cleanups);
c5aa993b 14670 }
c906108c
SS
14671 set_language (save_language);
14672 input_radix = save_input_radix;
e62c965a 14673
0756c555 14674 jit_breakpoint_re_set ();
4efc6507 14675
6c95b8df
PA
14676 do_cleanups (old_chain);
14677
af02033e
PP
14678 create_overlay_event_breakpoint ();
14679 create_longjmp_master_breakpoint ();
14680 create_std_terminate_master_breakpoint ();
186c406b 14681 create_exception_master_breakpoint ();
c906108c
SS
14682}
14683\f
c906108c
SS
14684/* Reset the thread number of this breakpoint:
14685
14686 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14687 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14688void
fba45db2 14689breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14690{
14691 if (b->thread != -1)
14692 {
39f77062
KB
14693 if (in_thread_list (inferior_ptid))
14694 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14695
14696 /* We're being called after following a fork. The new fork is
14697 selected as current, and unless this was a vfork will have a
14698 different program space from the original thread. Reset that
14699 as well. */
14700 b->loc->pspace = current_program_space;
c906108c
SS
14701 }
14702}
14703
03ac34d5
MS
14704/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14705 If from_tty is nonzero, it prints a message to that effect,
14706 which ends with a period (no newline). */
14707
c906108c 14708void
fba45db2 14709set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14710{
52f0bd74 14711 struct breakpoint *b;
c906108c
SS
14712
14713 if (count < 0)
14714 count = 0;
14715
14716 ALL_BREAKPOINTS (b)
14717 if (b->number == bptnum)
c5aa993b 14718 {
d77f58be
SS
14719 if (is_tracepoint (b))
14720 {
14721 if (from_tty && count != 0)
14722 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14723 bptnum);
14724 return;
14725 }
14726
c5aa993b 14727 b->ignore_count = count;
221ea385
KS
14728 if (from_tty)
14729 {
14730 if (count == 0)
3e43a32a
MS
14731 printf_filtered (_("Will stop next time "
14732 "breakpoint %d is reached."),
221ea385
KS
14733 bptnum);
14734 else if (count == 1)
a3f17187 14735 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14736 bptnum);
14737 else
3e43a32a
MS
14738 printf_filtered (_("Will ignore next %d "
14739 "crossings of breakpoint %d."),
221ea385
KS
14740 count, bptnum);
14741 }
8d3788bd 14742 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14743 return;
14744 }
c906108c 14745
8a3fe4f8 14746 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14747}
14748
c906108c
SS
14749/* Command to set ignore-count of breakpoint N to COUNT. */
14750
14751static void
fba45db2 14752ignore_command (char *args, int from_tty)
c906108c
SS
14753{
14754 char *p = args;
52f0bd74 14755 int num;
c906108c
SS
14756
14757 if (p == 0)
e2e0b3e5 14758 error_no_arg (_("a breakpoint number"));
c5aa993b 14759
c906108c 14760 num = get_number (&p);
5c44784c 14761 if (num == 0)
8a3fe4f8 14762 error (_("bad breakpoint number: '%s'"), args);
c906108c 14763 if (*p == 0)
8a3fe4f8 14764 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14765
14766 set_ignore_count (num,
14767 longest_to_int (value_as_long (parse_and_eval (p))),
14768 from_tty);
221ea385
KS
14769 if (from_tty)
14770 printf_filtered ("\n");
c906108c
SS
14771}
14772\f
14773/* Call FUNCTION on each of the breakpoints
14774 whose numbers are given in ARGS. */
14775
14776static void
95a42b64
TT
14777map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14778 void *),
14779 void *data)
c906108c 14780{
52f0bd74
AC
14781 int num;
14782 struct breakpoint *b, *tmp;
11cf8741 14783 int match;
197f0a60 14784 struct get_number_or_range_state state;
c906108c 14785
197f0a60 14786 if (args == 0)
e2e0b3e5 14787 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14788
197f0a60
TT
14789 init_number_or_range (&state, args);
14790
14791 while (!state.finished)
c906108c 14792 {
197f0a60
TT
14793 char *p = state.string;
14794
11cf8741 14795 match = 0;
c5aa993b 14796
197f0a60 14797 num = get_number_or_range (&state);
5c44784c 14798 if (num == 0)
c5aa993b 14799 {
8a3fe4f8 14800 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14801 }
14802 else
14803 {
14804 ALL_BREAKPOINTS_SAFE (b, tmp)
14805 if (b->number == num)
14806 {
11cf8741 14807 match = 1;
cdac0397 14808 function (b, data);
11cf8741 14809 break;
5c44784c 14810 }
11cf8741 14811 if (match == 0)
a3f17187 14812 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14813 }
c906108c
SS
14814 }
14815}
14816
0d381245
VP
14817static struct bp_location *
14818find_location_by_number (char *number)
14819{
14820 char *dot = strchr (number, '.');
14821 char *p1;
14822 int bp_num;
14823 int loc_num;
14824 struct breakpoint *b;
14825 struct bp_location *loc;
14826
14827 *dot = '\0';
14828
14829 p1 = number;
197f0a60 14830 bp_num = get_number (&p1);
0d381245
VP
14831 if (bp_num == 0)
14832 error (_("Bad breakpoint number '%s'"), number);
14833
14834 ALL_BREAKPOINTS (b)
14835 if (b->number == bp_num)
14836 {
14837 break;
14838 }
14839
14840 if (!b || b->number != bp_num)
14841 error (_("Bad breakpoint number '%s'"), number);
14842
14843 p1 = dot+1;
197f0a60 14844 loc_num = get_number (&p1);
0d381245
VP
14845 if (loc_num == 0)
14846 error (_("Bad breakpoint location number '%s'"), number);
14847
14848 --loc_num;
14849 loc = b->loc;
14850 for (;loc_num && loc; --loc_num, loc = loc->next)
14851 ;
14852 if (!loc)
14853 error (_("Bad breakpoint location number '%s'"), dot+1);
14854
14855 return loc;
14856}
14857
14858
1900040c
MS
14859/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14860 If from_tty is nonzero, it prints a message to that effect,
14861 which ends with a period (no newline). */
14862
c906108c 14863void
fba45db2 14864disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14865{
14866 /* Never disable a watchpoint scope breakpoint; we want to
14867 hit them when we leave scope so we can delete both the
14868 watchpoint and its scope breakpoint at that time. */
14869 if (bpt->type == bp_watchpoint_scope)
14870 return;
14871
c2c6d25f 14872 /* You can't disable permanent breakpoints. */
b5de0fa7 14873 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14874 return;
14875
b5de0fa7 14876 bpt->enable_state = bp_disabled;
c906108c 14877
b775012e
LM
14878 /* Mark breakpoint locations modified. */
14879 mark_breakpoint_modified (bpt);
14880
d248b706
KY
14881 if (target_supports_enable_disable_tracepoint ()
14882 && current_trace_status ()->running && is_tracepoint (bpt))
14883 {
14884 struct bp_location *location;
14885
14886 for (location = bpt->loc; location; location = location->next)
14887 target_disable_tracepoint (location);
14888 }
14889
b60e7edf 14890 update_global_location_list (0);
c906108c 14891
8d3788bd 14892 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14893}
14894
51be5b68
PA
14895/* A callback for iterate_over_related_breakpoints. */
14896
14897static void
14898do_disable_breakpoint (struct breakpoint *b, void *ignore)
14899{
14900 disable_breakpoint (b);
14901}
14902
95a42b64
TT
14903/* A callback for map_breakpoint_numbers that calls
14904 disable_breakpoint. */
14905
14906static void
14907do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14908{
51be5b68 14909 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14910}
14911
c906108c 14912static void
fba45db2 14913disable_command (char *args, int from_tty)
c906108c 14914{
c906108c 14915 if (args == 0)
46c6471b
PA
14916 {
14917 struct breakpoint *bpt;
14918
14919 ALL_BREAKPOINTS (bpt)
14920 if (user_breakpoint_p (bpt))
14921 disable_breakpoint (bpt);
14922 }
9eaabc75 14923 else
0d381245 14924 {
9eaabc75
MW
14925 char *num = extract_arg (&args);
14926
14927 while (num)
d248b706 14928 {
9eaabc75 14929 if (strchr (num, '.'))
b775012e 14930 {
9eaabc75
MW
14931 struct bp_location *loc = find_location_by_number (num);
14932
14933 if (loc)
14934 {
14935 if (loc->enabled)
14936 {
14937 loc->enabled = 0;
14938 mark_breakpoint_location_modified (loc);
14939 }
14940 if (target_supports_enable_disable_tracepoint ()
14941 && current_trace_status ()->running && loc->owner
14942 && is_tracepoint (loc->owner))
14943 target_disable_tracepoint (loc);
14944 }
14945 update_global_location_list (0);
b775012e 14946 }
9eaabc75
MW
14947 else
14948 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14949 num = extract_arg (&args);
d248b706 14950 }
0d381245 14951 }
c906108c
SS
14952}
14953
14954static void
816338b5
SS
14955enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14956 int count)
c906108c 14957{
afe38095 14958 int target_resources_ok;
c906108c
SS
14959
14960 if (bpt->type == bp_hardware_breakpoint)
14961 {
14962 int i;
c5aa993b 14963 i = hw_breakpoint_used_count ();
53a5351d 14964 target_resources_ok =
d92524f1 14965 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14966 i + 1, 0);
c906108c 14967 if (target_resources_ok == 0)
8a3fe4f8 14968 error (_("No hardware breakpoint support in the target."));
c906108c 14969 else if (target_resources_ok < 0)
8a3fe4f8 14970 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14971 }
14972
cc60f2e3 14973 if (is_watchpoint (bpt))
c906108c 14974 {
d07205c2
JK
14975 /* Initialize it just to avoid a GCC false warning. */
14976 enum enable_state orig_enable_state = 0;
bfd189b1 14977 volatile struct gdb_exception e;
dde02812
ES
14978
14979 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 14980 {
3a5c3e22
PA
14981 struct watchpoint *w = (struct watchpoint *) bpt;
14982
1e718ff1
TJB
14983 orig_enable_state = bpt->enable_state;
14984 bpt->enable_state = bp_enabled;
3a5c3e22 14985 update_watchpoint (w, 1 /* reparse */);
c906108c 14986 }
dde02812 14987 if (e.reason < 0)
c5aa993b 14988 {
1e718ff1 14989 bpt->enable_state = orig_enable_state;
dde02812
ES
14990 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14991 bpt->number);
14992 return;
c5aa993b 14993 }
c906108c 14994 }
0101ce28 14995
b4c291bb
KH
14996 if (bpt->enable_state != bp_permanent)
14997 bpt->enable_state = bp_enabled;
d248b706 14998
b775012e
LM
14999 bpt->enable_state = bp_enabled;
15000
15001 /* Mark breakpoint locations modified. */
15002 mark_breakpoint_modified (bpt);
15003
d248b706
KY
15004 if (target_supports_enable_disable_tracepoint ()
15005 && current_trace_status ()->running && is_tracepoint (bpt))
15006 {
15007 struct bp_location *location;
15008
15009 for (location = bpt->loc; location; location = location->next)
15010 target_enable_tracepoint (location);
15011 }
15012
b4c291bb 15013 bpt->disposition = disposition;
816338b5 15014 bpt->enable_count = count;
b60e7edf 15015 update_global_location_list (1);
9c97429f 15016
8d3788bd 15017 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15018}
15019
fe3f5fa8 15020
c906108c 15021void
fba45db2 15022enable_breakpoint (struct breakpoint *bpt)
c906108c 15023{
816338b5 15024 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15025}
15026
15027static void
15028do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15029{
15030 enable_breakpoint (bpt);
c906108c
SS
15031}
15032
95a42b64
TT
15033/* A callback for map_breakpoint_numbers that calls
15034 enable_breakpoint. */
15035
15036static void
15037do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15038{
51be5b68 15039 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15040}
15041
c906108c
SS
15042/* The enable command enables the specified breakpoints (or all defined
15043 breakpoints) so they once again become (or continue to be) effective
1272ad14 15044 in stopping the inferior. */
c906108c 15045
c906108c 15046static void
fba45db2 15047enable_command (char *args, int from_tty)
c906108c 15048{
c906108c 15049 if (args == 0)
46c6471b
PA
15050 {
15051 struct breakpoint *bpt;
15052
15053 ALL_BREAKPOINTS (bpt)
15054 if (user_breakpoint_p (bpt))
15055 enable_breakpoint (bpt);
15056 }
9eaabc75 15057 else
0d381245 15058 {
9eaabc75
MW
15059 char *num = extract_arg (&args);
15060
15061 while (num)
d248b706 15062 {
9eaabc75 15063 if (strchr (num, '.'))
b775012e 15064 {
9eaabc75
MW
15065 struct bp_location *loc = find_location_by_number (num);
15066
15067 if (loc)
15068 {
15069 if (!loc->enabled)
15070 {
15071 loc->enabled = 1;
15072 mark_breakpoint_location_modified (loc);
15073 }
15074 if (target_supports_enable_disable_tracepoint ()
15075 && current_trace_status ()->running && loc->owner
15076 && is_tracepoint (loc->owner))
15077 target_enable_tracepoint (loc);
15078 }
15079 update_global_location_list (1);
b775012e 15080 }
9eaabc75
MW
15081 else
15082 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15083 num = extract_arg (&args);
d248b706 15084 }
0d381245 15085 }
c906108c
SS
15086}
15087
816338b5
SS
15088/* This struct packages up disposition data for application to multiple
15089 breakpoints. */
15090
15091struct disp_data
15092{
15093 enum bpdisp disp;
15094 int count;
15095};
15096
c906108c 15097static void
51be5b68
PA
15098do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15099{
816338b5 15100 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15101
816338b5 15102 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15103}
15104
15105static void
15106do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15107{
816338b5 15108 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15109
15110 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15111}
15112
c906108c 15113static void
fba45db2 15114enable_once_command (char *args, int from_tty)
c906108c 15115{
51be5b68 15116 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15117}
15118
816338b5
SS
15119static void
15120do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15121{
15122 struct disp_data disp = { disp_disable, *(int *) countptr };
15123
15124 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15125}
15126
15127static void
15128enable_count_command (char *args, int from_tty)
15129{
15130 int count = get_number (&args);
15131
15132 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15133}
15134
c906108c 15135static void
51be5b68 15136do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15137{
816338b5 15138 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15139
15140 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15141}
15142
c906108c 15143static void
fba45db2 15144enable_delete_command (char *args, int from_tty)
c906108c 15145{
51be5b68 15146 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15147}
15148\f
fa8d40ab
JJ
15149static void
15150set_breakpoint_cmd (char *args, int from_tty)
15151{
15152}
15153
15154static void
15155show_breakpoint_cmd (char *args, int from_tty)
15156{
15157}
15158
1f3b5d1b
PP
15159/* Invalidate last known value of any hardware watchpoint if
15160 the memory which that value represents has been written to by
15161 GDB itself. */
15162
15163static void
8de0566d
YQ
15164invalidate_bp_value_on_memory_change (struct inferior *inferior,
15165 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15166 const bfd_byte *data)
15167{
15168 struct breakpoint *bp;
15169
15170 ALL_BREAKPOINTS (bp)
15171 if (bp->enable_state == bp_enabled
3a5c3e22 15172 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15173 {
3a5c3e22 15174 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15175
3a5c3e22
PA
15176 if (wp->val_valid && wp->val)
15177 {
15178 struct bp_location *loc;
15179
15180 for (loc = bp->loc; loc != NULL; loc = loc->next)
15181 if (loc->loc_type == bp_loc_hardware_watchpoint
15182 && loc->address + loc->length > addr
15183 && addr + len > loc->address)
15184 {
15185 value_free (wp->val);
15186 wp->val = NULL;
15187 wp->val_valid = 0;
15188 }
15189 }
1f3b5d1b
PP
15190 }
15191}
15192
8181d85f
DJ
15193/* Create and insert a raw software breakpoint at PC. Return an
15194 identifier, which should be used to remove the breakpoint later.
15195 In general, places which call this should be using something on the
15196 breakpoint chain instead; this function should be eliminated
15197 someday. */
15198
15199void *
6c95b8df
PA
15200deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
15201 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
15202{
15203 struct bp_target_info *bp_tgt;
ef370185 15204 struct bp_location *bl;
8181d85f 15205
41bf6aca 15206 bp_tgt = XCNEW (struct bp_target_info);
8181d85f 15207
6c95b8df 15208 bp_tgt->placed_address_space = aspace;
8181d85f 15209 bp_tgt->placed_address = pc;
6c95b8df 15210
ef370185
JB
15211 /* If an unconditional non-raw breakpoint is already inserted at
15212 that location, there's no need to insert another. However, with
15213 target-side evaluation of breakpoint conditions, if the
15214 breakpoint that is currently inserted on the target is
15215 conditional, we need to make it unconditional. Note that a
15216 breakpoint with target-side commands is not reported even if
15217 unconditional, so we need to remove the commands from the target
15218 as well. */
15219 bl = find_non_raw_software_breakpoint_inserted_here (aspace, pc);
15220 if (bl != NULL
15221 && VEC_empty (agent_expr_p, bl->target_info.conditions)
15222 && VEC_empty (agent_expr_p, bl->target_info.tcommands))
15223 {
15224 bp_target_info_copy_insertion_state (bp_tgt, &bl->target_info);
15225 return bp_tgt;
15226 }
15227
a6d9a66e 15228 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
15229 {
15230 /* Could not insert the breakpoint. */
15231 xfree (bp_tgt);
15232 return NULL;
15233 }
15234
15235 return bp_tgt;
15236}
15237
4a64f543
MS
15238/* Remove a breakpoint BP inserted by
15239 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
15240
15241int
a6d9a66e 15242deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
15243{
15244 struct bp_target_info *bp_tgt = bp;
ef370185
JB
15245 struct address_space *aspace = bp_tgt->placed_address_space;
15246 CORE_ADDR address = bp_tgt->placed_address;
15247 struct bp_location *bl;
8181d85f
DJ
15248 int ret;
15249
ef370185
JB
15250 bl = find_non_raw_software_breakpoint_inserted_here (aspace, address);
15251
15252 /* Only remove the raw breakpoint if there are no other non-raw
15253 breakpoints still inserted at this location. Otherwise, we would
15254 be effectively disabling those breakpoints. */
15255 if (bl == NULL)
15256 ret = target_remove_breakpoint (gdbarch, bp_tgt);
15257 else if (!VEC_empty (agent_expr_p, bl->target_info.conditions)
15258 || !VEC_empty (agent_expr_p, bl->target_info.tcommands))
15259 {
15260 /* The target is evaluating conditions, and when we inserted the
15261 software single-step breakpoint, we had made the breakpoint
15262 unconditional and command-less on the target side. Reinsert
15263 to restore the conditions/commands. */
15264 ret = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
15265 }
15266 else
15267 ret = 0;
15268
8181d85f
DJ
15269 xfree (bp_tgt);
15270
15271 return ret;
15272}
15273
8181d85f
DJ
15274/* Create and insert a breakpoint for software single step. */
15275
15276void
6c95b8df 15277insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15278 struct address_space *aspace,
15279 CORE_ADDR next_pc)
8181d85f
DJ
15280{
15281 void **bpt_p;
15282
15283 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
15284 {
15285 bpt_p = &single_step_breakpoints[0];
15286 single_step_gdbarch[0] = gdbarch;
15287 }
8181d85f
DJ
15288 else
15289 {
15290 gdb_assert (single_step_breakpoints[1] == NULL);
15291 bpt_p = &single_step_breakpoints[1];
a6d9a66e 15292 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
15293 }
15294
4a64f543
MS
15295 /* NOTE drow/2006-04-11: A future improvement to this function would
15296 be to only create the breakpoints once, and actually put them on
15297 the breakpoint chain. That would let us use set_raw_breakpoint.
15298 We could adjust the addresses each time they were needed. Doing
15299 this requires corresponding changes elsewhere where single step
15300 breakpoints are handled, however. So, for now, we use this. */
8181d85f 15301
6c95b8df 15302 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 15303 if (*bpt_p == NULL)
5af949e3
UW
15304 error (_("Could not insert single-step breakpoint at %s"),
15305 paddress (gdbarch, next_pc));
8181d85f
DJ
15306}
15307
f02253f1
HZ
15308/* Check if the breakpoints used for software single stepping
15309 were inserted or not. */
15310
15311int
15312single_step_breakpoints_inserted (void)
15313{
15314 return (single_step_breakpoints[0] != NULL
15315 || single_step_breakpoints[1] != NULL);
15316}
15317
8181d85f
DJ
15318/* Remove and delete any breakpoints used for software single step. */
15319
15320void
15321remove_single_step_breakpoints (void)
15322{
15323 gdb_assert (single_step_breakpoints[0] != NULL);
15324
15325 /* See insert_single_step_breakpoint for more about this deprecated
15326 call. */
a6d9a66e
UW
15327 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15328 single_step_breakpoints[0]);
15329 single_step_gdbarch[0] = NULL;
8181d85f
DJ
15330 single_step_breakpoints[0] = NULL;
15331
15332 if (single_step_breakpoints[1] != NULL)
15333 {
a6d9a66e
UW
15334 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15335 single_step_breakpoints[1]);
15336 single_step_gdbarch[1] = NULL;
8181d85f
DJ
15337 single_step_breakpoints[1] = NULL;
15338 }
15339}
15340
d03285ec
UW
15341/* Delete software single step breakpoints without removing them from
15342 the inferior. This is intended to be used if the inferior's address
15343 space where they were inserted is already gone, e.g. after exit or
15344 exec. */
15345
15346void
15347cancel_single_step_breakpoints (void)
15348{
15349 int i;
15350
15351 for (i = 0; i < 2; i++)
15352 if (single_step_breakpoints[i])
15353 {
15354 xfree (single_step_breakpoints[i]);
15355 single_step_breakpoints[i] = NULL;
15356 single_step_gdbarch[i] = NULL;
15357 }
15358}
15359
15360/* Detach software single-step breakpoints from INFERIOR_PTID without
15361 removing them. */
15362
15363static void
15364detach_single_step_breakpoints (void)
15365{
15366 int i;
15367
15368 for (i = 0; i < 2; i++)
15369 if (single_step_breakpoints[i])
15370 target_remove_breakpoint (single_step_gdbarch[i],
15371 single_step_breakpoints[i]);
15372}
15373
ef370185
JB
15374/* Find the software single-step breakpoint that inserted at PC.
15375 Returns its slot if found, and -1 if not found. */
1aafd4da 15376
ef370185
JB
15377static int
15378find_single_step_breakpoint (struct address_space *aspace,
15379 CORE_ADDR pc)
1aafd4da
UW
15380{
15381 int i;
15382
15383 for (i = 0; i < 2; i++)
15384 {
15385 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
15386 if (bp_tgt
15387 && breakpoint_address_match (bp_tgt->placed_address_space,
15388 bp_tgt->placed_address,
15389 aspace, pc))
ef370185 15390 return i;
1aafd4da
UW
15391 }
15392
ef370185
JB
15393 return -1;
15394}
15395
15396/* Check whether a software single-step breakpoint is inserted at
15397 PC. */
15398
15399int
15400single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15401 CORE_ADDR pc)
15402{
15403 return find_single_step_breakpoint (aspace, pc) >= 0;
1aafd4da
UW
15404}
15405
a96d9b2e
SDJ
15406/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15407 non-zero otherwise. */
15408static int
15409is_syscall_catchpoint_enabled (struct breakpoint *bp)
15410{
15411 if (syscall_catchpoint_p (bp)
15412 && bp->enable_state != bp_disabled
15413 && bp->enable_state != bp_call_disabled)
15414 return 1;
15415 else
15416 return 0;
15417}
15418
15419int
15420catch_syscall_enabled (void)
15421{
fa3064dd
YQ
15422 struct catch_syscall_inferior_data *inf_data
15423 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15424
fa3064dd 15425 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15426}
15427
15428int
15429catching_syscall_number (int syscall_number)
15430{
15431 struct breakpoint *bp;
15432
15433 ALL_BREAKPOINTS (bp)
15434 if (is_syscall_catchpoint_enabled (bp))
15435 {
be5c67c1
PA
15436 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15437
15438 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15439 {
15440 int i, iter;
15441 for (i = 0;
be5c67c1 15442 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15443 i++)
15444 if (syscall_number == iter)
15445 return 1;
15446 }
15447 else
15448 return 1;
15449 }
15450
15451 return 0;
15452}
15453
15454/* Complete syscall names. Used by "catch syscall". */
49c4e619 15455static VEC (char_ptr) *
a96d9b2e 15456catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15457 const char *text, const char *word)
a96d9b2e
SDJ
15458{
15459 const char **list = get_syscall_names ();
49c4e619 15460 VEC (char_ptr) *retlist
b45627a0 15461 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15462
c38eea1a
MS
15463 xfree (list);
15464 return retlist;
a96d9b2e
SDJ
15465}
15466
1042e4c0
SS
15467/* Tracepoint-specific operations. */
15468
15469/* Set tracepoint count to NUM. */
15470static void
15471set_tracepoint_count (int num)
15472{
15473 tracepoint_count = num;
4fa62494 15474 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15475}
15476
70221824 15477static void
1042e4c0
SS
15478trace_command (char *arg, int from_tty)
15479{
55aa24fb
SDJ
15480 struct breakpoint_ops *ops;
15481 const char *arg_cp = arg;
15482
15483 if (arg && probe_linespec_to_ops (&arg_cp))
15484 ops = &tracepoint_probe_breakpoint_ops;
15485 else
15486 ops = &tracepoint_breakpoint_ops;
15487
558a9d82
YQ
15488 create_breakpoint (get_current_arch (),
15489 arg,
15490 NULL, 0, NULL, 1 /* parse arg */,
15491 0 /* tempflag */,
15492 bp_tracepoint /* type_wanted */,
15493 0 /* Ignore count */,
15494 pending_break_support,
15495 ops,
15496 from_tty,
15497 1 /* enabled */,
15498 0 /* internal */, 0);
1042e4c0
SS
15499}
15500
70221824 15501static void
7a697b8d
SS
15502ftrace_command (char *arg, int from_tty)
15503{
558a9d82
YQ
15504 create_breakpoint (get_current_arch (),
15505 arg,
15506 NULL, 0, NULL, 1 /* parse arg */,
15507 0 /* tempflag */,
15508 bp_fast_tracepoint /* type_wanted */,
15509 0 /* Ignore count */,
15510 pending_break_support,
15511 &tracepoint_breakpoint_ops,
15512 from_tty,
15513 1 /* enabled */,
15514 0 /* internal */, 0);
0fb4aa4b
PA
15515}
15516
15517/* strace command implementation. Creates a static tracepoint. */
15518
70221824 15519static void
0fb4aa4b
PA
15520strace_command (char *arg, int from_tty)
15521{
983af33b
SDJ
15522 struct breakpoint_ops *ops;
15523
15524 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15525 or with a normal static tracepoint. */
15526 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15527 ops = &strace_marker_breakpoint_ops;
15528 else
15529 ops = &tracepoint_breakpoint_ops;
15530
558a9d82
YQ
15531 create_breakpoint (get_current_arch (),
15532 arg,
15533 NULL, 0, NULL, 1 /* parse arg */,
15534 0 /* tempflag */,
15535 bp_static_tracepoint /* type_wanted */,
15536 0 /* Ignore count */,
15537 pending_break_support,
15538 ops,
15539 from_tty,
15540 1 /* enabled */,
15541 0 /* internal */, 0);
7a697b8d
SS
15542}
15543
409873ef
SS
15544/* Set up a fake reader function that gets command lines from a linked
15545 list that was acquired during tracepoint uploading. */
15546
15547static struct uploaded_tp *this_utp;
3149d8c1 15548static int next_cmd;
409873ef
SS
15549
15550static char *
15551read_uploaded_action (void)
15552{
15553 char *rslt;
15554
3149d8c1 15555 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15556
3149d8c1 15557 next_cmd++;
409873ef
SS
15558
15559 return rslt;
15560}
15561
00bf0b85
SS
15562/* Given information about a tracepoint as recorded on a target (which
15563 can be either a live system or a trace file), attempt to create an
15564 equivalent GDB tracepoint. This is not a reliable process, since
15565 the target does not necessarily have all the information used when
15566 the tracepoint was originally defined. */
15567
d9b3f62e 15568struct tracepoint *
00bf0b85 15569create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15570{
409873ef 15571 char *addr_str, small_buf[100];
d9b3f62e 15572 struct tracepoint *tp;
fd9b8c24 15573
409873ef
SS
15574 if (utp->at_string)
15575 addr_str = utp->at_string;
15576 else
15577 {
15578 /* In the absence of a source location, fall back to raw
15579 address. Since there is no way to confirm that the address
15580 means the same thing as when the trace was started, warn the
15581 user. */
3e43a32a
MS
15582 warning (_("Uploaded tracepoint %d has no "
15583 "source location, using raw address"),
409873ef 15584 utp->number);
8c042590 15585 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15586 addr_str = small_buf;
15587 }
15588
15589 /* There's not much we can do with a sequence of bytecodes. */
15590 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15591 warning (_("Uploaded tracepoint %d condition "
15592 "has no source form, ignoring it"),
409873ef 15593 utp->number);
d5551862 15594
8cdf0e15 15595 if (!create_breakpoint (get_current_arch (),
409873ef 15596 addr_str,
e7e0cddf
SS
15597 utp->cond_string, -1, NULL,
15598 0 /* parse cond/thread */,
8cdf0e15 15599 0 /* tempflag */,
0fb4aa4b 15600 utp->type /* type_wanted */,
8cdf0e15
VP
15601 0 /* Ignore count */,
15602 pending_break_support,
348d480f 15603 &tracepoint_breakpoint_ops,
8cdf0e15 15604 0 /* from_tty */,
84f4c1fe 15605 utp->enabled /* enabled */,
44f238bb
PA
15606 0 /* internal */,
15607 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15608 return NULL;
15609
409873ef 15610 /* Get the tracepoint we just created. */
fd9b8c24
PA
15611 tp = get_tracepoint (tracepoint_count);
15612 gdb_assert (tp != NULL);
d5551862 15613
00bf0b85
SS
15614 if (utp->pass > 0)
15615 {
8c042590
PM
15616 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15617 tp->base.number);
00bf0b85 15618
409873ef 15619 trace_pass_command (small_buf, 0);
00bf0b85
SS
15620 }
15621
409873ef
SS
15622 /* If we have uploaded versions of the original commands, set up a
15623 special-purpose "reader" function and call the usual command line
15624 reader, then pass the result to the breakpoint command-setting
15625 function. */
3149d8c1 15626 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15627 {
409873ef 15628 struct command_line *cmd_list;
00bf0b85 15629
409873ef 15630 this_utp = utp;
3149d8c1 15631 next_cmd = 0;
d5551862 15632
409873ef
SS
15633 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15634
d9b3f62e 15635 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15636 }
3149d8c1
SS
15637 else if (!VEC_empty (char_ptr, utp->actions)
15638 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15639 warning (_("Uploaded tracepoint %d actions "
15640 "have no source form, ignoring them"),
409873ef 15641 utp->number);
00bf0b85 15642
f196051f
SS
15643 /* Copy any status information that might be available. */
15644 tp->base.hit_count = utp->hit_count;
15645 tp->traceframe_usage = utp->traceframe_usage;
15646
00bf0b85 15647 return tp;
d9b3f62e 15648}
00bf0b85 15649
1042e4c0
SS
15650/* Print information on tracepoint number TPNUM_EXP, or all if
15651 omitted. */
15652
15653static void
e5a67952 15654tracepoints_info (char *args, int from_tty)
1042e4c0 15655{
79a45e25 15656 struct ui_out *uiout = current_uiout;
e5a67952 15657 int num_printed;
1042e4c0 15658
e5a67952 15659 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15660
15661 if (num_printed == 0)
1042e4c0 15662 {
e5a67952 15663 if (args == NULL || *args == '\0')
d77f58be
SS
15664 ui_out_message (uiout, 0, "No tracepoints.\n");
15665 else
e5a67952 15666 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15667 }
ad443146
SS
15668
15669 default_collect_info ();
1042e4c0
SS
15670}
15671
4a64f543 15672/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15673 Not supported by all targets. */
15674static void
15675enable_trace_command (char *args, int from_tty)
15676{
15677 enable_command (args, from_tty);
15678}
15679
4a64f543 15680/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15681 Not supported by all targets. */
15682static void
15683disable_trace_command (char *args, int from_tty)
15684{
15685 disable_command (args, from_tty);
15686}
15687
4a64f543 15688/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15689static void
15690delete_trace_command (char *arg, int from_tty)
15691{
35df4500 15692 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15693
15694 dont_repeat ();
15695
15696 if (arg == 0)
15697 {
15698 int breaks_to_delete = 0;
15699
15700 /* Delete all breakpoints if no argument.
15701 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15702 have to be deleted with an explicit breakpoint number
15703 argument. */
1042e4c0 15704 ALL_TRACEPOINTS (b)
46c6471b 15705 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15706 {
15707 breaks_to_delete = 1;
15708 break;
15709 }
1042e4c0
SS
15710
15711 /* Ask user only if there are some breakpoints to delete. */
15712 if (!from_tty
15713 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15714 {
35df4500 15715 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15716 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15717 delete_breakpoint (b);
1042e4c0
SS
15718 }
15719 }
15720 else
51be5b68 15721 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15722}
15723
197f0a60
TT
15724/* Helper function for trace_pass_command. */
15725
15726static void
d9b3f62e 15727trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15728{
d9b3f62e 15729 tp->pass_count = count;
6f6484cd 15730 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15731 if (from_tty)
15732 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15733 tp->base.number, count);
197f0a60
TT
15734}
15735
1042e4c0
SS
15736/* Set passcount for tracepoint.
15737
15738 First command argument is passcount, second is tracepoint number.
15739 If tracepoint number omitted, apply to most recently defined.
15740 Also accepts special argument "all". */
15741
15742static void
15743trace_pass_command (char *args, int from_tty)
15744{
d9b3f62e 15745 struct tracepoint *t1;
1042e4c0 15746 unsigned int count;
1042e4c0
SS
15747
15748 if (args == 0 || *args == 0)
3e43a32a
MS
15749 error (_("passcount command requires an "
15750 "argument (count + optional TP num)"));
1042e4c0 15751
4a64f543 15752 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15753
529480d0 15754 args = skip_spaces (args);
1042e4c0
SS
15755 if (*args && strncasecmp (args, "all", 3) == 0)
15756 {
d9b3f62e
PA
15757 struct breakpoint *b;
15758
1042e4c0 15759 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15760 if (*args)
15761 error (_("Junk at end of arguments."));
1042e4c0 15762
d9b3f62e 15763 ALL_TRACEPOINTS (b)
197f0a60 15764 {
d9b3f62e 15765 t1 = (struct tracepoint *) b;
197f0a60
TT
15766 trace_pass_set_count (t1, count, from_tty);
15767 }
15768 }
15769 else if (*args == '\0')
1042e4c0 15770 {
5fa1d40e 15771 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15772 if (t1)
197f0a60
TT
15773 trace_pass_set_count (t1, count, from_tty);
15774 }
15775 else
15776 {
15777 struct get_number_or_range_state state;
15778
15779 init_number_or_range (&state, args);
15780 while (!state.finished)
1042e4c0 15781 {
5fa1d40e 15782 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15783 if (t1)
15784 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15785 }
15786 }
1042e4c0
SS
15787}
15788
d9b3f62e 15789struct tracepoint *
1042e4c0
SS
15790get_tracepoint (int num)
15791{
15792 struct breakpoint *t;
15793
15794 ALL_TRACEPOINTS (t)
15795 if (t->number == num)
d9b3f62e 15796 return (struct tracepoint *) t;
1042e4c0
SS
15797
15798 return NULL;
15799}
15800
d5551862
SS
15801/* Find the tracepoint with the given target-side number (which may be
15802 different from the tracepoint number after disconnecting and
15803 reconnecting). */
15804
d9b3f62e 15805struct tracepoint *
d5551862
SS
15806get_tracepoint_by_number_on_target (int num)
15807{
d9b3f62e 15808 struct breakpoint *b;
d5551862 15809
d9b3f62e
PA
15810 ALL_TRACEPOINTS (b)
15811 {
15812 struct tracepoint *t = (struct tracepoint *) b;
15813
15814 if (t->number_on_target == num)
15815 return t;
15816 }
d5551862
SS
15817
15818 return NULL;
15819}
15820
1042e4c0 15821/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15822 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15823 If the argument is missing, the most recent tracepoint
15824 (tracepoint_count) is returned. */
15825
d9b3f62e 15826struct tracepoint *
197f0a60 15827get_tracepoint_by_number (char **arg,
5fa1d40e 15828 struct get_number_or_range_state *state)
1042e4c0 15829{
1042e4c0
SS
15830 struct breakpoint *t;
15831 int tpnum;
15832 char *instring = arg == NULL ? NULL : *arg;
15833
197f0a60
TT
15834 if (state)
15835 {
15836 gdb_assert (!state->finished);
15837 tpnum = get_number_or_range (state);
15838 }
15839 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15840 tpnum = tracepoint_count;
1042e4c0 15841 else
197f0a60 15842 tpnum = get_number (arg);
1042e4c0
SS
15843
15844 if (tpnum <= 0)
15845 {
15846 if (instring && *instring)
15847 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15848 instring);
15849 else
5fa1d40e 15850 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15851 return NULL;
15852 }
15853
15854 ALL_TRACEPOINTS (t)
15855 if (t->number == tpnum)
15856 {
d9b3f62e 15857 return (struct tracepoint *) t;
1042e4c0
SS
15858 }
15859
1042e4c0
SS
15860 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15861 return NULL;
15862}
15863
d9b3f62e
PA
15864void
15865print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15866{
15867 if (b->thread != -1)
15868 fprintf_unfiltered (fp, " thread %d", b->thread);
15869
15870 if (b->task != 0)
15871 fprintf_unfiltered (fp, " task %d", b->task);
15872
15873 fprintf_unfiltered (fp, "\n");
15874}
15875
6149aea9
PA
15876/* Save information on user settable breakpoints (watchpoints, etc) to
15877 a new script file named FILENAME. If FILTER is non-NULL, call it
15878 on each breakpoint and only include the ones for which it returns
15879 non-zero. */
15880
1042e4c0 15881static void
6149aea9
PA
15882save_breakpoints (char *filename, int from_tty,
15883 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15884{
15885 struct breakpoint *tp;
6149aea9 15886 int any = 0;
1042e4c0 15887 struct cleanup *cleanup;
a7bdde9e 15888 struct ui_file *fp;
6149aea9 15889 int extra_trace_bits = 0;
1042e4c0 15890
6149aea9
PA
15891 if (filename == 0 || *filename == 0)
15892 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15893
15894 /* See if we have anything to save. */
6149aea9 15895 ALL_BREAKPOINTS (tp)
1042e4c0 15896 {
6149aea9 15897 /* Skip internal and momentary breakpoints. */
09d682a4 15898 if (!user_breakpoint_p (tp))
6149aea9
PA
15899 continue;
15900
15901 /* If we have a filter, only save the breakpoints it accepts. */
15902 if (filter && !filter (tp))
15903 continue;
15904
15905 any = 1;
15906
15907 if (is_tracepoint (tp))
15908 {
15909 extra_trace_bits = 1;
15910
15911 /* We can stop searching. */
15912 break;
15913 }
1042e4c0 15914 }
6149aea9
PA
15915
15916 if (!any)
1042e4c0 15917 {
6149aea9 15918 warning (_("Nothing to save."));
1042e4c0
SS
15919 return;
15920 }
15921
c718be47
PA
15922 filename = tilde_expand (filename);
15923 cleanup = make_cleanup (xfree, filename);
15924 fp = gdb_fopen (filename, "w");
059fb39f 15925 if (!fp)
6149aea9
PA
15926 error (_("Unable to open file '%s' for saving (%s)"),
15927 filename, safe_strerror (errno));
a7bdde9e 15928 make_cleanup_ui_file_delete (fp);
8bf6485c 15929
6149aea9
PA
15930 if (extra_trace_bits)
15931 save_trace_state_variables (fp);
8bf6485c 15932
6149aea9 15933 ALL_BREAKPOINTS (tp)
1042e4c0 15934 {
6149aea9 15935 /* Skip internal and momentary breakpoints. */
09d682a4 15936 if (!user_breakpoint_p (tp))
6149aea9 15937 continue;
8bf6485c 15938
6149aea9
PA
15939 /* If we have a filter, only save the breakpoints it accepts. */
15940 if (filter && !filter (tp))
15941 continue;
15942
348d480f 15943 tp->ops->print_recreate (tp, fp);
1042e4c0 15944
6149aea9
PA
15945 /* Note, we can't rely on tp->number for anything, as we can't
15946 assume the recreated breakpoint numbers will match. Use $bpnum
15947 instead. */
15948
15949 if (tp->cond_string)
15950 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15951
15952 if (tp->ignore_count)
15953 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15954
2d9442cc 15955 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15956 {
a7bdde9e
VP
15957 volatile struct gdb_exception ex;
15958
6149aea9 15959 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15960
79a45e25 15961 ui_out_redirect (current_uiout, fp);
14dba4b4 15962 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15963 {
79a45e25 15964 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15965 }
79a45e25 15966 ui_out_redirect (current_uiout, NULL);
1042e4c0 15967
a7bdde9e
VP
15968 if (ex.reason < 0)
15969 throw_exception (ex);
1042e4c0 15970
a7bdde9e 15971 fprintf_unfiltered (fp, " end\n");
1042e4c0 15972 }
6149aea9
PA
15973
15974 if (tp->enable_state == bp_disabled)
15975 fprintf_unfiltered (fp, "disable\n");
15976
15977 /* If this is a multi-location breakpoint, check if the locations
15978 should be individually disabled. Watchpoint locations are
15979 special, and not user visible. */
15980 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15981 {
15982 struct bp_location *loc;
15983 int n = 1;
15984
15985 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15986 if (!loc->enabled)
15987 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15988 }
1042e4c0 15989 }
8bf6485c 15990
6149aea9 15991 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15992 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15993
1042e4c0 15994 if (from_tty)
6149aea9 15995 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15996 do_cleanups (cleanup);
6149aea9
PA
15997}
15998
15999/* The `save breakpoints' command. */
16000
16001static void
16002save_breakpoints_command (char *args, int from_tty)
16003{
16004 save_breakpoints (args, from_tty, NULL);
16005}
16006
16007/* The `save tracepoints' command. */
16008
16009static void
16010save_tracepoints_command (char *args, int from_tty)
16011{
16012 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
16013}
16014
16015/* Create a vector of all tracepoints. */
16016
16017VEC(breakpoint_p) *
eeae04df 16018all_tracepoints (void)
1042e4c0
SS
16019{
16020 VEC(breakpoint_p) *tp_vec = 0;
16021 struct breakpoint *tp;
16022
16023 ALL_TRACEPOINTS (tp)
16024 {
16025 VEC_safe_push (breakpoint_p, tp_vec, tp);
16026 }
16027
16028 return tp_vec;
16029}
16030
c906108c 16031\f
4a64f543
MS
16032/* This help string is used for the break, hbreak, tbreak and thbreak
16033 commands. It is defined as a macro to prevent duplication.
16034 COMMAND should be a string constant containing the name of the
16035 command. */
31e2b00f 16036#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
16037command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
16038PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16039probe point. Accepted values are `-probe' (for a generic, automatically\n\
16040guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
16041LOCATION may be a line number, function name, or \"*\" and an address.\n\
16042If a line number is specified, break at start of code for that line.\n\
16043If a function is specified, break at start of code for that function.\n\
16044If an address is specified, break at that exact address.\n\
dc10affe
PA
16045With no LOCATION, uses current execution address of the selected\n\
16046stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
16047\n\
16048THREADNUM is the number from \"info threads\".\n\
16049CONDITION is a boolean expression.\n\
16050\n\
d41c0fc8
PA
16051Multiple breakpoints at one place are permitted, and useful if their\n\
16052conditions are different.\n\
31e2b00f
AS
16053\n\
16054Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16055
44feb3ce
TT
16056/* List of subcommands for "catch". */
16057static struct cmd_list_element *catch_cmdlist;
16058
16059/* List of subcommands for "tcatch". */
16060static struct cmd_list_element *tcatch_cmdlist;
16061
9ac4176b 16062void
44feb3ce 16063add_catch_command (char *name, char *docstring,
82ae6c8d 16064 cmd_sfunc_ftype *sfunc,
625e8578 16065 completer_ftype *completer,
44feb3ce
TT
16066 void *user_data_catch,
16067 void *user_data_tcatch)
16068{
16069 struct cmd_list_element *command;
16070
16071 command = add_cmd (name, class_breakpoint, NULL, docstring,
16072 &catch_cmdlist);
16073 set_cmd_sfunc (command, sfunc);
16074 set_cmd_context (command, user_data_catch);
a96d9b2e 16075 set_cmd_completer (command, completer);
44feb3ce
TT
16076
16077 command = add_cmd (name, class_breakpoint, NULL, docstring,
16078 &tcatch_cmdlist);
16079 set_cmd_sfunc (command, sfunc);
16080 set_cmd_context (command, user_data_tcatch);
a96d9b2e 16081 set_cmd_completer (command, completer);
44feb3ce
TT
16082}
16083
6c95b8df 16084static void
a79b8f6e 16085clear_syscall_counts (struct inferior *inf)
6c95b8df 16086{
fa3064dd
YQ
16087 struct catch_syscall_inferior_data *inf_data
16088 = get_catch_syscall_inferior_data (inf);
16089
16090 inf_data->total_syscalls_count = 0;
16091 inf_data->any_syscall_count = 0;
16092 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
16093}
16094
6149aea9
PA
16095static void
16096save_command (char *arg, int from_tty)
16097{
3e43a32a
MS
16098 printf_unfiltered (_("\"save\" must be followed by "
16099 "the name of a save subcommand.\n"));
635c7e8a 16100 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
16101}
16102
84f4c1fe
PM
16103struct breakpoint *
16104iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16105 void *data)
16106{
35df4500 16107 struct breakpoint *b, *b_tmp;
84f4c1fe 16108
35df4500 16109 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
16110 {
16111 if ((*callback) (b, data))
16112 return b;
16113 }
16114
16115 return NULL;
16116}
16117
0574c78f
GB
16118/* Zero if any of the breakpoint's locations could be a location where
16119 functions have been inlined, nonzero otherwise. */
16120
16121static int
16122is_non_inline_function (struct breakpoint *b)
16123{
16124 /* The shared library event breakpoint is set on the address of a
16125 non-inline function. */
16126 if (b->type == bp_shlib_event)
16127 return 1;
16128
16129 return 0;
16130}
16131
16132/* Nonzero if the specified PC cannot be a location where functions
16133 have been inlined. */
16134
16135int
09ac7c10
TT
16136pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16137 const struct target_waitstatus *ws)
0574c78f
GB
16138{
16139 struct breakpoint *b;
16140 struct bp_location *bl;
16141
16142 ALL_BREAKPOINTS (b)
16143 {
16144 if (!is_non_inline_function (b))
16145 continue;
16146
16147 for (bl = b->loc; bl != NULL; bl = bl->next)
16148 {
16149 if (!bl->shlib_disabled
09ac7c10 16150 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16151 return 1;
16152 }
16153 }
16154
16155 return 0;
16156}
16157
2f202fde
JK
16158/* Remove any references to OBJFILE which is going to be freed. */
16159
16160void
16161breakpoint_free_objfile (struct objfile *objfile)
16162{
16163 struct bp_location **locp, *loc;
16164
16165 ALL_BP_LOCATIONS (loc, locp)
16166 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
16167 loc->symtab = NULL;
16168}
16169
2060206e
PA
16170void
16171initialize_breakpoint_ops (void)
16172{
16173 static int initialized = 0;
16174
16175 struct breakpoint_ops *ops;
16176
16177 if (initialized)
16178 return;
16179 initialized = 1;
16180
16181 /* The breakpoint_ops structure to be inherit by all kinds of
16182 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16183 internal and momentary breakpoints, etc.). */
16184 ops = &bkpt_base_breakpoint_ops;
16185 *ops = base_breakpoint_ops;
16186 ops->re_set = bkpt_re_set;
16187 ops->insert_location = bkpt_insert_location;
16188 ops->remove_location = bkpt_remove_location;
16189 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16190 ops->create_sals_from_address = bkpt_create_sals_from_address;
16191 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16192 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16193
16194 /* The breakpoint_ops structure to be used in regular breakpoints. */
16195 ops = &bkpt_breakpoint_ops;
16196 *ops = bkpt_base_breakpoint_ops;
16197 ops->re_set = bkpt_re_set;
16198 ops->resources_needed = bkpt_resources_needed;
16199 ops->print_it = bkpt_print_it;
16200 ops->print_mention = bkpt_print_mention;
16201 ops->print_recreate = bkpt_print_recreate;
16202
16203 /* Ranged breakpoints. */
16204 ops = &ranged_breakpoint_ops;
16205 *ops = bkpt_breakpoint_ops;
16206 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16207 ops->resources_needed = resources_needed_ranged_breakpoint;
16208 ops->print_it = print_it_ranged_breakpoint;
16209 ops->print_one = print_one_ranged_breakpoint;
16210 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16211 ops->print_mention = print_mention_ranged_breakpoint;
16212 ops->print_recreate = print_recreate_ranged_breakpoint;
16213
16214 /* Internal breakpoints. */
16215 ops = &internal_breakpoint_ops;
16216 *ops = bkpt_base_breakpoint_ops;
16217 ops->re_set = internal_bkpt_re_set;
16218 ops->check_status = internal_bkpt_check_status;
16219 ops->print_it = internal_bkpt_print_it;
16220 ops->print_mention = internal_bkpt_print_mention;
16221
16222 /* Momentary breakpoints. */
16223 ops = &momentary_breakpoint_ops;
16224 *ops = bkpt_base_breakpoint_ops;
16225 ops->re_set = momentary_bkpt_re_set;
16226 ops->check_status = momentary_bkpt_check_status;
16227 ops->print_it = momentary_bkpt_print_it;
16228 ops->print_mention = momentary_bkpt_print_mention;
16229
e2e4d78b
JK
16230 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16231 ops = &longjmp_breakpoint_ops;
16232 *ops = momentary_breakpoint_ops;
16233 ops->dtor = longjmp_bkpt_dtor;
16234
55aa24fb
SDJ
16235 /* Probe breakpoints. */
16236 ops = &bkpt_probe_breakpoint_ops;
16237 *ops = bkpt_breakpoint_ops;
16238 ops->insert_location = bkpt_probe_insert_location;
16239 ops->remove_location = bkpt_probe_remove_location;
16240 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16241 ops->decode_linespec = bkpt_probe_decode_linespec;
16242
2060206e
PA
16243 /* Watchpoints. */
16244 ops = &watchpoint_breakpoint_ops;
16245 *ops = base_breakpoint_ops;
3a5c3e22 16246 ops->dtor = dtor_watchpoint;
2060206e
PA
16247 ops->re_set = re_set_watchpoint;
16248 ops->insert_location = insert_watchpoint;
16249 ops->remove_location = remove_watchpoint;
16250 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16251 ops->check_status = check_status_watchpoint;
16252 ops->resources_needed = resources_needed_watchpoint;
16253 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16254 ops->print_it = print_it_watchpoint;
16255 ops->print_mention = print_mention_watchpoint;
16256 ops->print_recreate = print_recreate_watchpoint;
427cd150 16257 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16258
16259 /* Masked watchpoints. */
16260 ops = &masked_watchpoint_breakpoint_ops;
16261 *ops = watchpoint_breakpoint_ops;
16262 ops->insert_location = insert_masked_watchpoint;
16263 ops->remove_location = remove_masked_watchpoint;
16264 ops->resources_needed = resources_needed_masked_watchpoint;
16265 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16266 ops->print_it = print_it_masked_watchpoint;
16267 ops->print_one_detail = print_one_detail_masked_watchpoint;
16268 ops->print_mention = print_mention_masked_watchpoint;
16269 ops->print_recreate = print_recreate_masked_watchpoint;
16270
16271 /* Tracepoints. */
16272 ops = &tracepoint_breakpoint_ops;
16273 *ops = base_breakpoint_ops;
16274 ops->re_set = tracepoint_re_set;
16275 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16276 ops->print_one_detail = tracepoint_print_one_detail;
16277 ops->print_mention = tracepoint_print_mention;
16278 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16279 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16280 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16281 ops->decode_linespec = tracepoint_decode_linespec;
16282
55aa24fb
SDJ
16283 /* Probe tracepoints. */
16284 ops = &tracepoint_probe_breakpoint_ops;
16285 *ops = tracepoint_breakpoint_ops;
16286 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16287 ops->decode_linespec = tracepoint_probe_decode_linespec;
16288
983af33b
SDJ
16289 /* Static tracepoints with marker (`-m'). */
16290 ops = &strace_marker_breakpoint_ops;
16291 *ops = tracepoint_breakpoint_ops;
16292 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16293 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16294 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16295
16296 /* Fork catchpoints. */
16297 ops = &catch_fork_breakpoint_ops;
16298 *ops = base_breakpoint_ops;
16299 ops->insert_location = insert_catch_fork;
16300 ops->remove_location = remove_catch_fork;
16301 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16302 ops->print_it = print_it_catch_fork;
16303 ops->print_one = print_one_catch_fork;
16304 ops->print_mention = print_mention_catch_fork;
16305 ops->print_recreate = print_recreate_catch_fork;
16306
16307 /* Vfork catchpoints. */
16308 ops = &catch_vfork_breakpoint_ops;
16309 *ops = base_breakpoint_ops;
16310 ops->insert_location = insert_catch_vfork;
16311 ops->remove_location = remove_catch_vfork;
16312 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16313 ops->print_it = print_it_catch_vfork;
16314 ops->print_one = print_one_catch_vfork;
16315 ops->print_mention = print_mention_catch_vfork;
16316 ops->print_recreate = print_recreate_catch_vfork;
16317
16318 /* Exec catchpoints. */
16319 ops = &catch_exec_breakpoint_ops;
16320 *ops = base_breakpoint_ops;
16321 ops->dtor = dtor_catch_exec;
16322 ops->insert_location = insert_catch_exec;
16323 ops->remove_location = remove_catch_exec;
16324 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16325 ops->print_it = print_it_catch_exec;
16326 ops->print_one = print_one_catch_exec;
16327 ops->print_mention = print_mention_catch_exec;
16328 ops->print_recreate = print_recreate_catch_exec;
16329
16330 /* Syscall catchpoints. */
16331 ops = &catch_syscall_breakpoint_ops;
16332 *ops = base_breakpoint_ops;
16333 ops->dtor = dtor_catch_syscall;
16334 ops->insert_location = insert_catch_syscall;
16335 ops->remove_location = remove_catch_syscall;
16336 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16337 ops->print_it = print_it_catch_syscall;
16338 ops->print_one = print_one_catch_syscall;
16339 ops->print_mention = print_mention_catch_syscall;
16340 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16341
16342 /* Solib-related catchpoints. */
16343 ops = &catch_solib_breakpoint_ops;
16344 *ops = base_breakpoint_ops;
16345 ops->dtor = dtor_catch_solib;
16346 ops->insert_location = insert_catch_solib;
16347 ops->remove_location = remove_catch_solib;
16348 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16349 ops->check_status = check_status_catch_solib;
16350 ops->print_it = print_it_catch_solib;
16351 ops->print_one = print_one_catch_solib;
16352 ops->print_mention = print_mention_catch_solib;
16353 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16354
16355 ops = &dprintf_breakpoint_ops;
16356 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16357 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16358 ops->resources_needed = bkpt_resources_needed;
16359 ops->print_it = bkpt_print_it;
16360 ops->print_mention = bkpt_print_mention;
2d9442cc 16361 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16362 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16363 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16364}
16365
8bfd80db
YQ
16366/* Chain containing all defined "enable breakpoint" subcommands. */
16367
16368static struct cmd_list_element *enablebreaklist = NULL;
16369
c906108c 16370void
fba45db2 16371_initialize_breakpoint (void)
c906108c
SS
16372{
16373 struct cmd_list_element *c;
16374
2060206e
PA
16375 initialize_breakpoint_ops ();
16376
84acb35a 16377 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16378 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16379 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16380 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16381
55aa24fb
SDJ
16382 breakpoint_objfile_key
16383 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16384
fa3064dd 16385 catch_syscall_inferior_data
8e260fc0
TT
16386 = register_inferior_data_with_cleanup (NULL,
16387 catch_syscall_inferior_data_cleanup);
fa3064dd 16388
c906108c
SS
16389 breakpoint_chain = 0;
16390 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16391 before a breakpoint is set. */
16392 breakpoint_count = 0;
16393
1042e4c0
SS
16394 tracepoint_count = 0;
16395
1bedd215
AC
16396 add_com ("ignore", class_breakpoint, ignore_command, _("\
16397Set ignore-count of breakpoint number N to COUNT.\n\
16398Usage is `ignore N COUNT'."));
c906108c 16399 if (xdb_commands)
c5aa993b 16400 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16401
1bedd215
AC
16402 add_com ("commands", class_breakpoint, commands_command, _("\
16403Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16404Give breakpoint number as argument after \"commands\".\n\
16405With no argument, the targeted breakpoint is the last one set.\n\
16406The commands themselves follow starting on the next line.\n\
16407Type a line containing \"end\" to indicate the end of them.\n\
16408Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16409then no output is printed when it is hit, except what the commands print."));
c906108c 16410
d55637df 16411 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16412Specify breakpoint number N to break only if COND is true.\n\
c906108c 16413Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16414expression to be evaluated whenever breakpoint N is reached."));
d55637df 16415 set_cmd_completer (c, condition_completer);
c906108c 16416
1bedd215 16417 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16418Set a temporary breakpoint.\n\
c906108c
SS
16419Like \"break\" except the breakpoint is only temporary,\n\
16420so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16421by using \"enable delete\" on the breakpoint number.\n\
16422\n"
16423BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16424 set_cmd_completer (c, location_completer);
c94fdfd0 16425
1bedd215 16426 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16427Set a hardware assisted breakpoint.\n\
c906108c 16428Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16429some target hardware may not have this support.\n\
16430\n"
16431BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16432 set_cmd_completer (c, location_completer);
c906108c 16433
1bedd215 16434 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16435Set a temporary hardware assisted breakpoint.\n\
c906108c 16436Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16437so it will be deleted when hit.\n\
16438\n"
16439BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16440 set_cmd_completer (c, location_completer);
c906108c 16441
1bedd215
AC
16442 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16443Enable some breakpoints.\n\
c906108c
SS
16444Give breakpoint numbers (separated by spaces) as arguments.\n\
16445With no subcommand, breakpoints are enabled until you command otherwise.\n\
16446This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16447With a subcommand you can enable temporarily."),
c906108c
SS
16448 &enablelist, "enable ", 1, &cmdlist);
16449 if (xdb_commands)
1bedd215
AC
16450 add_com ("ab", class_breakpoint, enable_command, _("\
16451Enable some breakpoints.\n\
c906108c
SS
16452Give breakpoint numbers (separated by spaces) as arguments.\n\
16453With no subcommand, breakpoints are enabled until you command otherwise.\n\
16454This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16455With a subcommand you can enable temporarily."));
c906108c
SS
16456
16457 add_com_alias ("en", "enable", class_breakpoint, 1);
16458
84951ab5 16459 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16460Enable some breakpoints.\n\
c906108c
SS
16461Give breakpoint numbers (separated by spaces) as arguments.\n\
16462This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16463May be abbreviated to simply \"enable\".\n"),
c5aa993b 16464 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16465
1a966eab
AC
16466 add_cmd ("once", no_class, enable_once_command, _("\
16467Enable breakpoints for one hit. Give breakpoint numbers.\n\
16468If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16469 &enablebreaklist);
16470
1a966eab
AC
16471 add_cmd ("delete", no_class, enable_delete_command, _("\
16472Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16473If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16474 &enablebreaklist);
16475
816338b5
SS
16476 add_cmd ("count", no_class, enable_count_command, _("\
16477Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16478If a breakpoint is hit while enabled in this fashion,\n\
16479the count is decremented; when it reaches zero, the breakpoint is disabled."),
16480 &enablebreaklist);
16481
1a966eab
AC
16482 add_cmd ("delete", no_class, enable_delete_command, _("\
16483Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16484If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16485 &enablelist);
16486
1a966eab
AC
16487 add_cmd ("once", no_class, enable_once_command, _("\
16488Enable breakpoints for one hit. Give breakpoint numbers.\n\
16489If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16490 &enablelist);
16491
16492 add_cmd ("count", no_class, enable_count_command, _("\
16493Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16494If a breakpoint is hit while enabled in this fashion,\n\
16495the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16496 &enablelist);
16497
1bedd215
AC
16498 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16499Disable some breakpoints.\n\
c906108c
SS
16500Arguments are breakpoint numbers with spaces in between.\n\
16501To disable all breakpoints, give no argument.\n\
64b9b334 16502A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16503 &disablelist, "disable ", 1, &cmdlist);
16504 add_com_alias ("dis", "disable", class_breakpoint, 1);
16505 add_com_alias ("disa", "disable", class_breakpoint, 1);
16506 if (xdb_commands)
1bedd215
AC
16507 add_com ("sb", class_breakpoint, disable_command, _("\
16508Disable some breakpoints.\n\
c906108c
SS
16509Arguments are breakpoint numbers with spaces in between.\n\
16510To disable all breakpoints, give no argument.\n\
64b9b334 16511A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16512
1a966eab
AC
16513 add_cmd ("breakpoints", class_alias, disable_command, _("\
16514Disable some breakpoints.\n\
c906108c
SS
16515Arguments are breakpoint numbers with spaces in between.\n\
16516To disable all breakpoints, give no argument.\n\
64b9b334 16517A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16518This command may be abbreviated \"disable\"."),
c906108c
SS
16519 &disablelist);
16520
1bedd215
AC
16521 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16522Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16523Arguments are breakpoint numbers with spaces in between.\n\
16524To delete all breakpoints, give no argument.\n\
16525\n\
16526Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16527The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16528 &deletelist, "delete ", 1, &cmdlist);
16529 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16530 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16531 if (xdb_commands)
1bedd215
AC
16532 add_com ("db", class_breakpoint, delete_command, _("\
16533Delete some breakpoints.\n\
c906108c 16534Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16535To delete all breakpoints, give no argument.\n"));
c906108c 16536
1a966eab
AC
16537 add_cmd ("breakpoints", class_alias, delete_command, _("\
16538Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16539Arguments are breakpoint numbers with spaces in between.\n\
16540To delete all breakpoints, give no argument.\n\
1a966eab 16541This command may be abbreviated \"delete\"."),
c906108c
SS
16542 &deletelist);
16543
1bedd215
AC
16544 add_com ("clear", class_breakpoint, clear_command, _("\
16545Clear breakpoint at specified line or function.\n\
c906108c
SS
16546Argument may be line number, function name, or \"*\" and an address.\n\
16547If line number is specified, all breakpoints in that line are cleared.\n\
16548If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16549If an address is specified, breakpoints at that address are cleared.\n\
16550\n\
16551With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16552is executing in.\n\
16553\n\
1bedd215 16554See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16555 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16556
1bedd215 16557 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16558Set breakpoint at specified line or function.\n"
16559BREAK_ARGS_HELP ("break")));
5ba2abeb 16560 set_cmd_completer (c, location_completer);
c94fdfd0 16561
c906108c
SS
16562 add_com_alias ("b", "break", class_run, 1);
16563 add_com_alias ("br", "break", class_run, 1);
16564 add_com_alias ("bre", "break", class_run, 1);
16565 add_com_alias ("brea", "break", class_run, 1);
16566
7681d515
PM
16567 if (xdb_commands)
16568 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16569
16570 if (dbx_commands)
16571 {
1bedd215
AC
16572 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16573Break in function/address or break at a line in the current file."),
c5aa993b
JM
16574 &stoplist, "stop ", 1, &cmdlist);
16575 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16576 _("Break in function or address."), &stoplist);
c5aa993b 16577 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16578 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16579 add_com ("status", class_info, breakpoints_info, _("\
16580Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16581The \"Type\" column indicates one of:\n\
16582\tbreakpoint - normal breakpoint\n\
16583\twatchpoint - watchpoint\n\
16584The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16585the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16586breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16587address and file/line number respectively.\n\
16588\n\
16589Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16590are set to the address of the last breakpoint listed unless the command\n\
16591is prefixed with \"server \".\n\n\
c906108c 16592Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16593breakpoint set."));
c906108c
SS
16594 }
16595
1bedd215 16596 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16597Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16598The \"Type\" column indicates one of:\n\
16599\tbreakpoint - normal breakpoint\n\
16600\twatchpoint - watchpoint\n\
16601The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16602the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16603breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16604address and file/line number respectively.\n\
16605\n\
16606Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16607are set to the address of the last breakpoint listed unless the command\n\
16608is prefixed with \"server \".\n\n\
c906108c 16609Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16610breakpoint set."));
c906108c 16611
6b04bdb7
MS
16612 add_info_alias ("b", "breakpoints", 1);
16613
c906108c 16614 if (xdb_commands)
1bedd215
AC
16615 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16616Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16617The \"Type\" column indicates one of:\n\
16618\tbreakpoint - normal breakpoint\n\
16619\twatchpoint - watchpoint\n\
16620The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16621the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16622breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16623address and file/line number respectively.\n\
16624\n\
16625Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16626are set to the address of the last breakpoint listed unless the command\n\
16627is prefixed with \"server \".\n\n\
c906108c 16628Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16629breakpoint set."));
c906108c 16630
1a966eab
AC
16631 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16632Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16633The \"Type\" column indicates one of:\n\
16634\tbreakpoint - normal breakpoint\n\
16635\twatchpoint - watchpoint\n\
16636\tlongjmp - internal breakpoint used to step through longjmp()\n\
16637\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16638\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16639\tfinish - internal breakpoint used by the \"finish\" command\n\
16640The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16641the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16642breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16643address and file/line number respectively.\n\
16644\n\
16645Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16646are set to the address of the last breakpoint listed unless the command\n\
16647is prefixed with \"server \".\n\n\
c906108c 16648Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16649breakpoint set."),
c906108c
SS
16650 &maintenanceinfolist);
16651
44feb3ce
TT
16652 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16653Set catchpoints to catch events."),
16654 &catch_cmdlist, "catch ",
16655 0/*allow-unknown*/, &cmdlist);
16656
16657 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16658Set temporary catchpoints to catch events."),
16659 &tcatch_cmdlist, "tcatch ",
16660 0/*allow-unknown*/, &cmdlist);
16661
44feb3ce
TT
16662 add_catch_command ("fork", _("Catch calls to fork."),
16663 catch_fork_command_1,
a96d9b2e 16664 NULL,
44feb3ce
TT
16665 (void *) (uintptr_t) catch_fork_permanent,
16666 (void *) (uintptr_t) catch_fork_temporary);
16667 add_catch_command ("vfork", _("Catch calls to vfork."),
16668 catch_fork_command_1,
a96d9b2e 16669 NULL,
44feb3ce
TT
16670 (void *) (uintptr_t) catch_vfork_permanent,
16671 (void *) (uintptr_t) catch_vfork_temporary);
16672 add_catch_command ("exec", _("Catch calls to exec."),
16673 catch_exec_command_1,
a96d9b2e
SDJ
16674 NULL,
16675 CATCH_PERMANENT,
16676 CATCH_TEMPORARY);
edcc5120
TT
16677 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16678Usage: catch load [REGEX]\n\
16679If REGEX is given, only stop for libraries matching the regular expression."),
16680 catch_load_command_1,
16681 NULL,
16682 CATCH_PERMANENT,
16683 CATCH_TEMPORARY);
16684 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16685Usage: catch unload [REGEX]\n\
16686If REGEX is given, only stop for libraries matching the regular expression."),
16687 catch_unload_command_1,
16688 NULL,
16689 CATCH_PERMANENT,
16690 CATCH_TEMPORARY);
a96d9b2e
SDJ
16691 add_catch_command ("syscall", _("\
16692Catch system calls by their names and/or numbers.\n\
16693Arguments say which system calls to catch. If no arguments\n\
16694are given, every system call will be caught.\n\
16695Arguments, if given, should be one or more system call names\n\
16696(if your system supports that), or system call numbers."),
16697 catch_syscall_command_1,
16698 catch_syscall_completer,
44feb3ce
TT
16699 CATCH_PERMANENT,
16700 CATCH_TEMPORARY);
c5aa993b 16701
1bedd215
AC
16702 c = add_com ("watch", class_breakpoint, watch_command, _("\
16703Set a watchpoint for an expression.\n\
06a64a0b 16704Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16705A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16706an expression changes.\n\
16707If -l or -location is given, this evaluates EXPRESSION and watches\n\
16708the memory to which it refers."));
65d12d83 16709 set_cmd_completer (c, expression_completer);
c906108c 16710
1bedd215
AC
16711 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16712Set a read watchpoint for an expression.\n\
06a64a0b 16713Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16714A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16715an expression is read.\n\
16716If -l or -location is given, this evaluates EXPRESSION and watches\n\
16717the memory to which it refers."));
65d12d83 16718 set_cmd_completer (c, expression_completer);
c906108c 16719
1bedd215
AC
16720 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16721Set a watchpoint for an expression.\n\
06a64a0b 16722Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16723A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16724an expression is either read or written.\n\
16725If -l or -location is given, this evaluates EXPRESSION and watches\n\
16726the memory to which it refers."));
65d12d83 16727 set_cmd_completer (c, expression_completer);
c906108c 16728
d77f58be 16729 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16730Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16731
920d2a44
AC
16732 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16733 respond to changes - contrary to the description. */
85c07804
AC
16734 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16735 &can_use_hw_watchpoints, _("\
16736Set debugger's willingness to use watchpoint hardware."), _("\
16737Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16738If zero, gdb will not use hardware for new watchpoints, even if\n\
16739such is available. (However, any hardware watchpoints that were\n\
16740created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16741hardware.)"),
16742 NULL,
920d2a44 16743 show_can_use_hw_watchpoints,
85c07804 16744 &setlist, &showlist);
c906108c
SS
16745
16746 can_use_hw_watchpoints = 1;
fa8d40ab 16747
1042e4c0
SS
16748 /* Tracepoint manipulation commands. */
16749
16750 c = add_com ("trace", class_breakpoint, trace_command, _("\
16751Set a tracepoint at specified line or function.\n\
16752\n"
16753BREAK_ARGS_HELP ("trace") "\n\
16754Do \"help tracepoints\" for info on other tracepoint commands."));
16755 set_cmd_completer (c, location_completer);
16756
16757 add_com_alias ("tp", "trace", class_alias, 0);
16758 add_com_alias ("tr", "trace", class_alias, 1);
16759 add_com_alias ("tra", "trace", class_alias, 1);
16760 add_com_alias ("trac", "trace", class_alias, 1);
16761
7a697b8d
SS
16762 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16763Set a fast tracepoint at specified line or function.\n\
16764\n"
16765BREAK_ARGS_HELP ("ftrace") "\n\
16766Do \"help tracepoints\" for info on other tracepoint commands."));
16767 set_cmd_completer (c, location_completer);
16768
0fb4aa4b
PA
16769 c = add_com ("strace", class_breakpoint, strace_command, _("\
16770Set a static tracepoint at specified line, function or marker.\n\
16771\n\
16772strace [LOCATION] [if CONDITION]\n\
16773LOCATION may be a line number, function name, \"*\" and an address,\n\
16774or -m MARKER_ID.\n\
16775If a line number is specified, probe the marker at start of code\n\
16776for that line. If a function is specified, probe the marker at start\n\
16777of code for that function. If an address is specified, probe the marker\n\
16778at that exact address. If a marker id is specified, probe the marker\n\
16779with that name. With no LOCATION, uses current execution address of\n\
16780the selected stack frame.\n\
16781Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16782This collects arbitrary user data passed in the probe point call to the\n\
16783tracing library. You can inspect it when analyzing the trace buffer,\n\
16784by printing the $_sdata variable like any other convenience variable.\n\
16785\n\
16786CONDITION is a boolean expression.\n\
16787\n\
d41c0fc8
PA
16788Multiple tracepoints at one place are permitted, and useful if their\n\
16789conditions are different.\n\
0fb4aa4b
PA
16790\n\
16791Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16792Do \"help tracepoints\" for info on other tracepoint commands."));
16793 set_cmd_completer (c, location_completer);
16794
1042e4c0 16795 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16796Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16797Convenience variable \"$tpnum\" contains the number of the\n\
16798last tracepoint set."));
16799
16800 add_info_alias ("tp", "tracepoints", 1);
16801
16802 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16803Delete specified tracepoints.\n\
16804Arguments are tracepoint numbers, separated by spaces.\n\
16805No argument means delete all tracepoints."),
16806 &deletelist);
7e20dfcd 16807 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16808
16809 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16810Disable specified tracepoints.\n\
16811Arguments are tracepoint numbers, separated by spaces.\n\
16812No argument means disable all tracepoints."),
16813 &disablelist);
16814 deprecate_cmd (c, "disable");
16815
16816 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16817Enable specified tracepoints.\n\
16818Arguments are tracepoint numbers, separated by spaces.\n\
16819No argument means enable all tracepoints."),
16820 &enablelist);
16821 deprecate_cmd (c, "enable");
16822
16823 add_com ("passcount", class_trace, trace_pass_command, _("\
16824Set the passcount for a tracepoint.\n\
16825The trace will end when the tracepoint has been passed 'count' times.\n\
16826Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16827if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16828
6149aea9
PA
16829 add_prefix_cmd ("save", class_breakpoint, save_command,
16830 _("Save breakpoint definitions as a script."),
16831 &save_cmdlist, "save ",
16832 0/*allow-unknown*/, &cmdlist);
16833
16834 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16835Save current breakpoint definitions as a script.\n\
cce7e648 16836This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16837catchpoints, tracepoints). Use the 'source' command in another debug\n\
16838session to restore them."),
16839 &save_cmdlist);
16840 set_cmd_completer (c, filename_completer);
16841
16842 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16843Save current tracepoint definitions as a script.\n\
6149aea9
PA
16844Use the 'source' command in another debug session to restore them."),
16845 &save_cmdlist);
1042e4c0
SS
16846 set_cmd_completer (c, filename_completer);
16847
6149aea9
PA
16848 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16849 deprecate_cmd (c, "save tracepoints");
16850
1bedd215 16851 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16852Breakpoint specific settings\n\
16853Configure various breakpoint-specific variables such as\n\
1bedd215 16854pending breakpoint behavior"),
fa8d40ab
JJ
16855 &breakpoint_set_cmdlist, "set breakpoint ",
16856 0/*allow-unknown*/, &setlist);
1bedd215 16857 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16858Breakpoint specific settings\n\
16859Configure various breakpoint-specific variables such as\n\
1bedd215 16860pending breakpoint behavior"),
fa8d40ab
JJ
16861 &breakpoint_show_cmdlist, "show breakpoint ",
16862 0/*allow-unknown*/, &showlist);
16863
7915a72c
AC
16864 add_setshow_auto_boolean_cmd ("pending", no_class,
16865 &pending_break_support, _("\
16866Set debugger's behavior regarding pending breakpoints."), _("\
16867Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16868If on, an unrecognized breakpoint location will cause gdb to create a\n\
16869pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16870an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16871user-query to see if a pending breakpoint should be created."),
2c5b56ce 16872 NULL,
920d2a44 16873 show_pending_break_support,
6e1d7d6c
AC
16874 &breakpoint_set_cmdlist,
16875 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16876
16877 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16878
16879 add_setshow_boolean_cmd ("auto-hw", no_class,
16880 &automatic_hardware_breakpoints, _("\
16881Set automatic usage of hardware breakpoints."), _("\
16882Show automatic usage of hardware breakpoints."), _("\
16883If set, the debugger will automatically use hardware breakpoints for\n\
16884breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16885a warning will be emitted for such breakpoints."),
16886 NULL,
16887 show_automatic_hardware_breakpoints,
16888 &breakpoint_set_cmdlist,
16889 &breakpoint_show_cmdlist);
74960c60 16890
72d0e2c5
YQ
16891 add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16892 &always_inserted_mode, _("\
74960c60
VP
16893Set mode for inserting breakpoints."), _("\
16894Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
16895When this mode is off, breakpoints are inserted in inferior when it is\n\
16896resumed, and removed when execution stops. When this mode is on,\n\
16897breakpoints are inserted immediately and removed only when the user\n\
16898deletes the breakpoint. When this mode is auto (which is the default),\n\
16899the behaviour depends on the non-stop setting (see help set non-stop).\n\
16900In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16901behaves as if always-inserted mode is on; if gdb is controlling the\n\
16902inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
72d0e2c5
YQ
16903 NULL,
16904 &show_always_inserted_mode,
16905 &breakpoint_set_cmdlist,
16906 &breakpoint_show_cmdlist);
f1310107 16907
b775012e
LM
16908 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16909 condition_evaluation_enums,
16910 &condition_evaluation_mode_1, _("\
16911Set mode of breakpoint condition evaluation."), _("\
16912Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16913When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16914evaluated on the host's side by GDB. When it is set to \"target\",\n\
16915breakpoint conditions will be downloaded to the target (if the target\n\
16916supports such feature) and conditions will be evaluated on the target's side.\n\
16917If this is set to \"auto\" (default), this will be automatically set to\n\
16918\"target\" if it supports condition evaluation, otherwise it will\n\
16919be set to \"gdb\""),
16920 &set_condition_evaluation_mode,
16921 &show_condition_evaluation_mode,
16922 &breakpoint_set_cmdlist,
16923 &breakpoint_show_cmdlist);
16924
f1310107
TJB
16925 add_com ("break-range", class_breakpoint, break_range_command, _("\
16926Set a breakpoint for an address range.\n\
16927break-range START-LOCATION, END-LOCATION\n\
16928where START-LOCATION and END-LOCATION can be one of the following:\n\
16929 LINENUM, for that line in the current file,\n\
16930 FILE:LINENUM, for that line in that file,\n\
16931 +OFFSET, for that number of lines after the current line\n\
16932 or the start of the range\n\
16933 FUNCTION, for the first line in that function,\n\
16934 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16935 *ADDRESS, for the instruction at that address.\n\
16936\n\
16937The breakpoint will stop execution of the inferior whenever it executes\n\
16938an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16939range (including START-LOCATION and END-LOCATION)."));
16940
e7e0cddf
SS
16941 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16942Set a dynamic printf at specified line or function.\n\
16943dprintf location,format string,arg1,arg2,...\n\
16944location may be a line number, function name, or \"*\" and an address.\n\
16945If a line number is specified, break at start of code for that line.\n\
0e4777df 16946If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16947 set_cmd_completer (c, location_completer);
16948
16949 add_setshow_enum_cmd ("dprintf-style", class_support,
16950 dprintf_style_enums, &dprintf_style, _("\
16951Set the style of usage for dynamic printf."), _("\
16952Show the style of usage for dynamic printf."), _("\
16953This setting chooses how GDB will do a dynamic printf.\n\
16954If the value is \"gdb\", then the printing is done by GDB to its own\n\
16955console, as with the \"printf\" command.\n\
16956If the value is \"call\", the print is done by calling a function in your\n\
16957program; by default printf(), but you can choose a different function or\n\
16958output stream by setting dprintf-function and dprintf-channel."),
16959 update_dprintf_commands, NULL,
16960 &setlist, &showlist);
16961
16962 dprintf_function = xstrdup ("printf");
16963 add_setshow_string_cmd ("dprintf-function", class_support,
16964 &dprintf_function, _("\
16965Set the function to use for dynamic printf"), _("\
16966Show the function to use for dynamic printf"), NULL,
16967 update_dprintf_commands, NULL,
16968 &setlist, &showlist);
16969
16970 dprintf_channel = xstrdup ("");
16971 add_setshow_string_cmd ("dprintf-channel", class_support,
16972 &dprintf_channel, _("\
16973Set the channel to use for dynamic printf"), _("\
16974Show the channel to use for dynamic printf"), NULL,
16975 update_dprintf_commands, NULL,
16976 &setlist, &showlist);
16977
d3ce09f5
SS
16978 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16979 &disconnected_dprintf, _("\
16980Set whether dprintf continues after GDB disconnects."), _("\
16981Show whether dprintf continues after GDB disconnects."), _("\
16982Use this to let dprintf commands continue to hit and produce output\n\
16983even if GDB disconnects or detaches from the target."),
16984 NULL,
16985 NULL,
16986 &setlist, &showlist);
16987
16988 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16989agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16990(target agent only) This is useful for formatted output in user-defined commands."));
16991
765dc015 16992 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16993
16994 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16995 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16996}
This page took 2.527277 seconds and 4 git commands to generate.