PR gdb/12623: non-stop crashes inferior, PC adjustment and 1-byte insns
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
ecd75fc8 3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
fe898f56 50#include "block.h"
a77053c2 51#include "solib.h"
84acb35a
JJ
52#include "solist.h"
53#include "observer.h"
765dc015 54#include "memattr.h"
f7f9143b 55#include "ada-lang.h"
d1aa2f50 56#include "top.h"
79a45b7d 57#include "valprint.h"
4efc6507 58#include "jit.h"
a96d9b2e 59#include "xml-syscall.h"
65d79d4b 60#include "parser-defs.h"
55aa24fb
SDJ
61#include "gdb_regex.h"
62#include "probe.h"
e9cafbcc 63#include "cli/cli-utils.h"
be34f849 64#include "continuations.h"
1bfeeb0f
JL
65#include "stack.h"
66#include "skip.h"
b775012e 67#include "ax-gdb.h"
e2e4d78b 68#include "dummy-frame.h"
c906108c 69
d3ce09f5
SS
70#include "format.h"
71
1042e4c0
SS
72/* readline include files */
73#include "readline/readline.h"
74#include "readline/history.h"
75
76/* readline defines this. */
77#undef savestring
78
034dad6f 79#include "mi/mi-common.h"
6dddc817 80#include "extension.h"
104c1213 81
e7e8980f
YQ
82/* Enums for exception-handling support. */
83enum exception_event_kind
84{
85 EX_EVENT_THROW,
591f19e8 86 EX_EVENT_RETHROW,
e7e8980f
YQ
87 EX_EVENT_CATCH
88};
89
4a64f543 90/* Prototypes for local functions. */
c906108c 91
a14ed312 92static void enable_delete_command (char *, int);
c906108c 93
a14ed312 94static void enable_once_command (char *, int);
c906108c 95
816338b5
SS
96static void enable_count_command (char *, int);
97
a14ed312 98static void disable_command (char *, int);
c906108c 99
a14ed312 100static void enable_command (char *, int);
c906108c 101
95a42b64
TT
102static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
103 void *),
104 void *);
c906108c 105
a14ed312 106static void ignore_command (char *, int);
c906108c 107
4efb68b1 108static int breakpoint_re_set_one (void *);
c906108c 109
348d480f
PA
110static void breakpoint_re_set_default (struct breakpoint *);
111
983af33b
SDJ
112static void create_sals_from_address_default (char **,
113 struct linespec_result *,
114 enum bptype, char *,
115 char **);
116
117static void create_breakpoints_sal_default (struct gdbarch *,
118 struct linespec_result *,
e7e0cddf 119 char *, char *, enum bptype,
983af33b
SDJ
120 enum bpdisp, int, int,
121 int,
122 const struct breakpoint_ops *,
44f238bb 123 int, int, int, unsigned);
983af33b
SDJ
124
125static void decode_linespec_default (struct breakpoint *, char **,
126 struct symtabs_and_lines *);
127
a14ed312 128static void clear_command (char *, int);
c906108c 129
a14ed312 130static void catch_command (char *, int);
c906108c 131
a9634178 132static int can_use_hardware_watchpoint (struct value *);
c906108c 133
98deb0da 134static void break_command_1 (char *, int, int);
c906108c 135
a14ed312 136static void mention (struct breakpoint *);
c906108c 137
348d480f
PA
138static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
139 enum bptype,
c0a91b2b 140 const struct breakpoint_ops *);
3742cc8b
YQ
141static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
142 const struct symtab_and_line *);
143
4a64f543
MS
144/* This function is used in gdbtk sources and thus can not be made
145 static. */
63c252f8 146struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 147 struct symtab_and_line,
c0a91b2b
TT
148 enum bptype,
149 const struct breakpoint_ops *);
c906108c 150
06edf0c0
PA
151static struct breakpoint *
152 momentary_breakpoint_from_master (struct breakpoint *orig,
153 enum bptype type,
a1aa2221
LM
154 const struct breakpoint_ops *ops,
155 int loc_enabled);
06edf0c0 156
76897487
KB
157static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
158
a6d9a66e
UW
159static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
160 CORE_ADDR bpaddr,
88f7da05 161 enum bptype bptype);
76897487 162
6c95b8df
PA
163static void describe_other_breakpoints (struct gdbarch *,
164 struct program_space *, CORE_ADDR,
5af949e3 165 struct obj_section *, int);
c906108c 166
85d721b8
PA
167static int watchpoint_locations_match (struct bp_location *loc1,
168 struct bp_location *loc2);
169
f1310107
TJB
170static int breakpoint_location_address_match (struct bp_location *bl,
171 struct address_space *aspace,
172 CORE_ADDR addr);
173
a14ed312 174static void breakpoints_info (char *, int);
c906108c 175
d77f58be
SS
176static void watchpoints_info (char *, int);
177
e5a67952
MS
178static int breakpoint_1 (char *, int,
179 int (*) (const struct breakpoint *));
c906108c 180
4efb68b1 181static int breakpoint_cond_eval (void *);
c906108c 182
4efb68b1 183static void cleanup_executing_breakpoints (void *);
c906108c 184
a14ed312 185static void commands_command (char *, int);
c906108c 186
a14ed312 187static void condition_command (char *, int);
c906108c 188
c5aa993b
JM
189typedef enum
190 {
191 mark_inserted,
192 mark_uninserted
193 }
194insertion_state_t;
c906108c 195
0bde7532 196static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 197static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 198
e514a9d6 199static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 200
4efb68b1 201static int watchpoint_check (void *);
c906108c 202
a14ed312 203static void maintenance_info_breakpoints (char *, int);
c906108c 204
a14ed312 205static int hw_breakpoint_used_count (void);
c906108c 206
a1398e0c
PA
207static int hw_watchpoint_use_count (struct breakpoint *);
208
209static int hw_watchpoint_used_count_others (struct breakpoint *except,
210 enum bptype type,
211 int *other_type_used);
c906108c 212
a14ed312 213static void hbreak_command (char *, int);
c906108c 214
a14ed312 215static void thbreak_command (char *, int);
c906108c 216
816338b5
SS
217static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
218 int count);
c906108c 219
a14ed312 220static void stop_command (char *arg, int from_tty);
7a292a7a 221
a14ed312 222static void stopin_command (char *arg, int from_tty);
7a292a7a 223
a14ed312 224static void stopat_command (char *arg, int from_tty);
7a292a7a 225
a14ed312 226static void tcatch_command (char *arg, int from_tty);
7a292a7a 227
fe3f5fa8 228static void free_bp_location (struct bp_location *loc);
f431efe5
PA
229static void incref_bp_location (struct bp_location *loc);
230static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 231
39d61571 232static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 233
44702360
PA
234/* update_global_location_list's modes of operation wrt to whether to
235 insert locations now. */
236enum ugll_insert_mode
237{
238 /* Don't insert any breakpoint locations into the inferior, only
239 remove already-inserted locations that no longer should be
240 inserted. Functions that delete a breakpoint or breakpoints
241 should specify this mode, so that deleting a breakpoint doesn't
242 have the side effect of inserting the locations of other
243 breakpoints that are marked not-inserted, but should_be_inserted
244 returns true on them.
245
246 This behavior is useful is situations close to tear-down -- e.g.,
247 after an exec, while the target still has execution, but
248 breakpoint shadows of the previous executable image should *NOT*
249 be restored to the new image; or before detaching, where the
250 target still has execution and wants to delete breakpoints from
251 GDB's lists, and all breakpoints had already been removed from
252 the inferior. */
253 UGLL_DONT_INSERT,
254
a25a5a45
PA
255 /* May insert breakpoints iff breakpoints_should_be_inserted_now
256 claims breakpoints should be inserted now. */
04086b45
PA
257 UGLL_MAY_INSERT,
258
a25a5a45
PA
259 /* Insert locations now, irrespective of
260 breakpoints_should_be_inserted_now. E.g., say all threads are
261 stopped right now, and the user did "continue". We need to
262 insert breakpoints _before_ resuming the target, but
263 UGLL_MAY_INSERT wouldn't insert them, because
264 breakpoints_should_be_inserted_now returns false at that point,
265 as no thread is running yet. */
04086b45 266 UGLL_INSERT
44702360
PA
267};
268
269static void update_global_location_list (enum ugll_insert_mode);
a5606eee 270
44702360 271static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 272
d77f58be 273static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
274
275static void insert_breakpoint_locations (void);
a5606eee 276
a96d9b2e
SDJ
277static int syscall_catchpoint_p (struct breakpoint *b);
278
1042e4c0
SS
279static void tracepoints_info (char *, int);
280
281static void delete_trace_command (char *, int);
282
283static void enable_trace_command (char *, int);
284
285static void disable_trace_command (char *, int);
286
287static void trace_pass_command (char *, int);
288
558a9d82
YQ
289static void set_tracepoint_count (int num);
290
9c06b0b4
TJB
291static int is_masked_watchpoint (const struct breakpoint *b);
292
b775012e
LM
293static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
983af33b
SDJ
295/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296 otherwise. */
297
298static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 299
2060206e
PA
300/* The abstract base class all breakpoint_ops structures inherit
301 from. */
ab04a2af 302struct breakpoint_ops base_breakpoint_ops;
2060206e
PA
303
304/* The breakpoint_ops structure to be inherited by all breakpoint_ops
305 that are implemented on top of software or hardware breakpoints
306 (user breakpoints, internal and momentary breakpoints, etc.). */
307static struct breakpoint_ops bkpt_base_breakpoint_ops;
308
309/* Internal breakpoints class type. */
06edf0c0 310static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
311
312/* Momentary breakpoints class type. */
06edf0c0
PA
313static struct breakpoint_ops momentary_breakpoint_ops;
314
e2e4d78b
JK
315/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
316static struct breakpoint_ops longjmp_breakpoint_ops;
317
2060206e
PA
318/* The breakpoint_ops structure to be used in regular user created
319 breakpoints. */
320struct breakpoint_ops bkpt_breakpoint_ops;
321
55aa24fb
SDJ
322/* Breakpoints set on probes. */
323static struct breakpoint_ops bkpt_probe_breakpoint_ops;
324
e7e0cddf 325/* Dynamic printf class type. */
c5867ab6 326struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 327
d3ce09f5
SS
328/* The style in which to perform a dynamic printf. This is a user
329 option because different output options have different tradeoffs;
330 if GDB does the printing, there is better error handling if there
331 is a problem with any of the arguments, but using an inferior
332 function lets you have special-purpose printers and sending of
333 output to the same place as compiled-in print functions. */
334
335static const char dprintf_style_gdb[] = "gdb";
336static const char dprintf_style_call[] = "call";
337static const char dprintf_style_agent[] = "agent";
338static const char *const dprintf_style_enums[] = {
339 dprintf_style_gdb,
340 dprintf_style_call,
341 dprintf_style_agent,
342 NULL
343};
344static const char *dprintf_style = dprintf_style_gdb;
345
346/* The function to use for dynamic printf if the preferred style is to
347 call into the inferior. The value is simply a string that is
348 copied into the command, so it can be anything that GDB can
349 evaluate to a callable address, not necessarily a function name. */
350
351static char *dprintf_function = "";
352
353/* The channel to use for dynamic printf if the preferred style is to
354 call into the inferior; if a nonempty string, it will be passed to
355 the call as the first argument, with the format string as the
356 second. As with the dprintf function, this can be anything that
357 GDB knows how to evaluate, so in addition to common choices like
358 "stderr", this could be an app-specific expression like
359 "mystreams[curlogger]". */
360
361static char *dprintf_channel = "";
362
363/* True if dprintf commands should continue to operate even if GDB
364 has disconnected. */
365static int disconnected_dprintf = 1;
366
5cea2a26
PA
367/* A reference-counted struct command_line. This lets multiple
368 breakpoints share a single command list. */
369struct counted_command_line
370{
371 /* The reference count. */
372 int refc;
373
374 /* The command list. */
375 struct command_line *commands;
376};
377
378struct command_line *
379breakpoint_commands (struct breakpoint *b)
380{
381 return b->commands ? b->commands->commands : NULL;
382}
3daf8fe5 383
f3b1572e
PA
384/* Flag indicating that a command has proceeded the inferior past the
385 current breakpoint. */
386
387static int breakpoint_proceeded;
388
956a9fb9 389const char *
2cec12e5
AR
390bpdisp_text (enum bpdisp disp)
391{
4a64f543
MS
392 /* NOTE: the following values are a part of MI protocol and
393 represent values of 'disp' field returned when inferior stops at
394 a breakpoint. */
bc043ef3 395 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 396
2cec12e5
AR
397 return bpdisps[(int) disp];
398}
c906108c 399
4a64f543 400/* Prototypes for exported functions. */
c906108c 401/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 402 if such is available. */
c906108c
SS
403static int can_use_hw_watchpoints;
404
920d2a44
AC
405static void
406show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
407 struct cmd_list_element *c,
408 const char *value)
409{
3e43a32a
MS
410 fprintf_filtered (file,
411 _("Debugger's willingness to use "
412 "watchpoint hardware is %s.\n"),
920d2a44
AC
413 value);
414}
415
fa8d40ab
JJ
416/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
417 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 418 for unrecognized breakpoint locations.
fa8d40ab
JJ
419 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
420static enum auto_boolean pending_break_support;
920d2a44
AC
421static void
422show_pending_break_support (struct ui_file *file, int from_tty,
423 struct cmd_list_element *c,
424 const char *value)
425{
3e43a32a
MS
426 fprintf_filtered (file,
427 _("Debugger's behavior regarding "
428 "pending breakpoints is %s.\n"),
920d2a44
AC
429 value);
430}
fa8d40ab 431
765dc015 432/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 433 set with "break" but falling in read-only memory.
765dc015
VP
434 If 0, gdb will warn about such breakpoints, but won't automatically
435 use hardware breakpoints. */
436static int automatic_hardware_breakpoints;
437static void
438show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
439 struct cmd_list_element *c,
440 const char *value)
441{
3e43a32a
MS
442 fprintf_filtered (file,
443 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
444 value);
445}
446
a25a5a45
PA
447/* If on, GDB keeps breakpoints inserted even if the inferior is
448 stopped, and immediately inserts any new breakpoints as soon as
449 they're created. If off (default), GDB keeps breakpoints off of
450 the target as long as possible. That is, it delays inserting
451 breakpoints until the next resume, and removes them again when the
452 target fully stops. This is a bit safer in case GDB crashes while
453 processing user input. */
454static int always_inserted_mode = 0;
72d0e2c5 455
33e5cbd6 456static void
74960c60 457show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 458 struct cmd_list_element *c, const char *value)
74960c60 459{
a25a5a45
PA
460 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
461 value);
74960c60
VP
462}
463
b57bacec
PA
464/* See breakpoint.h. */
465
33e5cbd6 466int
a25a5a45 467breakpoints_should_be_inserted_now (void)
33e5cbd6 468{
a25a5a45
PA
469 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
470 {
471 /* If breakpoints are global, they should be inserted even if no
472 thread under gdb's control is running, or even if there are
473 no threads under GDB's control yet. */
474 return 1;
475 }
476 else if (target_has_execution)
477 {
a25a5a45
PA
478 if (always_inserted_mode)
479 {
480 /* The user wants breakpoints inserted even if all threads
481 are stopped. */
482 return 1;
483 }
484
b57bacec
PA
485 if (threads_are_executing ())
486 return 1;
a25a5a45
PA
487 }
488 return 0;
33e5cbd6 489}
765dc015 490
b775012e
LM
491static const char condition_evaluation_both[] = "host or target";
492
493/* Modes for breakpoint condition evaluation. */
494static const char condition_evaluation_auto[] = "auto";
495static const char condition_evaluation_host[] = "host";
496static const char condition_evaluation_target[] = "target";
497static const char *const condition_evaluation_enums[] = {
498 condition_evaluation_auto,
499 condition_evaluation_host,
500 condition_evaluation_target,
501 NULL
502};
503
504/* Global that holds the current mode for breakpoint condition evaluation. */
505static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
506
507/* Global that we use to display information to the user (gets its value from
508 condition_evaluation_mode_1. */
509static const char *condition_evaluation_mode = condition_evaluation_auto;
510
511/* Translate a condition evaluation mode MODE into either "host"
512 or "target". This is used mostly to translate from "auto" to the
513 real setting that is being used. It returns the translated
514 evaluation mode. */
515
516static const char *
517translate_condition_evaluation_mode (const char *mode)
518{
519 if (mode == condition_evaluation_auto)
520 {
521 if (target_supports_evaluation_of_breakpoint_conditions ())
522 return condition_evaluation_target;
523 else
524 return condition_evaluation_host;
525 }
526 else
527 return mode;
528}
529
530/* Discovers what condition_evaluation_auto translates to. */
531
532static const char *
533breakpoint_condition_evaluation_mode (void)
534{
535 return translate_condition_evaluation_mode (condition_evaluation_mode);
536}
537
538/* Return true if GDB should evaluate breakpoint conditions or false
539 otherwise. */
540
541static int
542gdb_evaluates_breakpoint_condition_p (void)
543{
544 const char *mode = breakpoint_condition_evaluation_mode ();
545
546 return (mode == condition_evaluation_host);
547}
548
a14ed312 549void _initialize_breakpoint (void);
c906108c 550
c906108c
SS
551/* Are we executing breakpoint commands? */
552static int executing_breakpoint_commands;
553
c02f5703
MS
554/* Are overlay event breakpoints enabled? */
555static int overlay_events_enabled;
556
e09342b5
TJB
557/* See description in breakpoint.h. */
558int target_exact_watchpoints = 0;
559
c906108c 560/* Walk the following statement or block through all breakpoints.
e5dd4106 561 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 562 current breakpoint. */
c906108c 563
5c44784c 564#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 565
5c44784c
JM
566#define ALL_BREAKPOINTS_SAFE(B,TMP) \
567 for (B = breakpoint_chain; \
568 B ? (TMP=B->next, 1): 0; \
569 B = TMP)
c906108c 570
4a64f543
MS
571/* Similar iterator for the low-level breakpoints. SAFE variant is
572 not provided so update_global_location_list must not be called
573 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 574
876fa593
JK
575#define ALL_BP_LOCATIONS(B,BP_TMP) \
576 for (BP_TMP = bp_location; \
577 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
578 BP_TMP++)
7cc221ef 579
b775012e
LM
580/* Iterates through locations with address ADDRESS for the currently selected
581 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
582 to where the loop should start from.
583 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
584 appropriate location to start with. */
585
586#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
587 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
588 BP_LOCP_TMP = BP_LOCP_START; \
589 BP_LOCP_START \
590 && (BP_LOCP_TMP < bp_location + bp_location_count \
591 && (*BP_LOCP_TMP)->address == ADDRESS); \
592 BP_LOCP_TMP++)
593
1042e4c0
SS
594/* Iterator for tracepoints only. */
595
596#define ALL_TRACEPOINTS(B) \
597 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 598 if (is_tracepoint (B))
1042e4c0 599
7cc221ef 600/* Chains of all breakpoints defined. */
c906108c
SS
601
602struct breakpoint *breakpoint_chain;
603
876fa593
JK
604/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
605
606static struct bp_location **bp_location;
607
608/* Number of elements of BP_LOCATION. */
609
610static unsigned bp_location_count;
611
4a64f543
MS
612/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
613 ADDRESS for the current elements of BP_LOCATION which get a valid
614 result from bp_location_has_shadow. You can use it for roughly
615 limiting the subrange of BP_LOCATION to scan for shadow bytes for
616 an address you need to read. */
876fa593
JK
617
618static CORE_ADDR bp_location_placed_address_before_address_max;
619
4a64f543
MS
620/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
621 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
622 BP_LOCATION which get a valid result from bp_location_has_shadow.
623 You can use it for roughly limiting the subrange of BP_LOCATION to
624 scan for shadow bytes for an address you need to read. */
876fa593
JK
625
626static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 627
4a64f543
MS
628/* The locations that no longer correspond to any breakpoint, unlinked
629 from bp_location array, but for which a hit may still be reported
630 by a target. */
20874c92
VP
631VEC(bp_location_p) *moribund_locations = NULL;
632
c906108c
SS
633/* Number of last breakpoint made. */
634
95a42b64
TT
635static int breakpoint_count;
636
86b17b60
PA
637/* The value of `breakpoint_count' before the last command that
638 created breakpoints. If the last (break-like) command created more
639 than one breakpoint, then the difference between BREAKPOINT_COUNT
640 and PREV_BREAKPOINT_COUNT is more than one. */
641static int prev_breakpoint_count;
c906108c 642
1042e4c0
SS
643/* Number of last tracepoint made. */
644
95a42b64 645static int tracepoint_count;
1042e4c0 646
6149aea9
PA
647static struct cmd_list_element *breakpoint_set_cmdlist;
648static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 649struct cmd_list_element *save_cmdlist;
6149aea9 650
468d015d
JJ
651/* Return whether a breakpoint is an active enabled breakpoint. */
652static int
653breakpoint_enabled (struct breakpoint *b)
654{
0d381245 655 return (b->enable_state == bp_enabled);
468d015d
JJ
656}
657
c906108c
SS
658/* Set breakpoint count to NUM. */
659
95a42b64 660static void
fba45db2 661set_breakpoint_count (int num)
c906108c 662{
86b17b60 663 prev_breakpoint_count = breakpoint_count;
c906108c 664 breakpoint_count = num;
4fa62494 665 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
666}
667
86b17b60
PA
668/* Used by `start_rbreak_breakpoints' below, to record the current
669 breakpoint count before "rbreak" creates any breakpoint. */
670static int rbreak_start_breakpoint_count;
671
95a42b64
TT
672/* Called at the start an "rbreak" command to record the first
673 breakpoint made. */
86b17b60 674
95a42b64
TT
675void
676start_rbreak_breakpoints (void)
677{
86b17b60 678 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
679}
680
681/* Called at the end of an "rbreak" command to record the last
682 breakpoint made. */
86b17b60 683
95a42b64
TT
684void
685end_rbreak_breakpoints (void)
686{
86b17b60 687 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
688}
689
4a64f543 690/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
691
692void
fba45db2 693clear_breakpoint_hit_counts (void)
c906108c
SS
694{
695 struct breakpoint *b;
696
697 ALL_BREAKPOINTS (b)
698 b->hit_count = 0;
699}
700
9add0f1b
TT
701/* Allocate a new counted_command_line with reference count of 1.
702 The new structure owns COMMANDS. */
703
704static struct counted_command_line *
705alloc_counted_command_line (struct command_line *commands)
706{
707 struct counted_command_line *result
708 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 709
9add0f1b
TT
710 result->refc = 1;
711 result->commands = commands;
712 return result;
713}
714
715/* Increment reference count. This does nothing if CMD is NULL. */
716
717static void
718incref_counted_command_line (struct counted_command_line *cmd)
719{
720 if (cmd)
721 ++cmd->refc;
722}
723
724/* Decrement reference count. If the reference count reaches 0,
725 destroy the counted_command_line. Sets *CMDP to NULL. This does
726 nothing if *CMDP is NULL. */
727
728static void
729decref_counted_command_line (struct counted_command_line **cmdp)
730{
731 if (*cmdp)
732 {
733 if (--(*cmdp)->refc == 0)
734 {
735 free_command_lines (&(*cmdp)->commands);
736 xfree (*cmdp);
737 }
738 *cmdp = NULL;
739 }
740}
741
742/* A cleanup function that calls decref_counted_command_line. */
743
744static void
745do_cleanup_counted_command_line (void *arg)
746{
747 decref_counted_command_line (arg);
748}
749
750/* Create a cleanup that calls decref_counted_command_line on the
751 argument. */
752
753static struct cleanup *
754make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
755{
756 return make_cleanup (do_cleanup_counted_command_line, cmdp);
757}
758
c906108c 759\f
48cb2d85
VP
760/* Return the breakpoint with the specified number, or NULL
761 if the number does not refer to an existing breakpoint. */
762
763struct breakpoint *
764get_breakpoint (int num)
765{
766 struct breakpoint *b;
767
768 ALL_BREAKPOINTS (b)
769 if (b->number == num)
770 return b;
771
772 return NULL;
773}
5c44784c 774
c906108c 775\f
adc36818 776
b775012e
LM
777/* Mark locations as "conditions have changed" in case the target supports
778 evaluating conditions on its side. */
779
780static void
781mark_breakpoint_modified (struct breakpoint *b)
782{
783 struct bp_location *loc;
784
785 /* This is only meaningful if the target is
786 evaluating conditions and if the user has
787 opted for condition evaluation on the target's
788 side. */
789 if (gdb_evaluates_breakpoint_condition_p ()
790 || !target_supports_evaluation_of_breakpoint_conditions ())
791 return;
792
793 if (!is_breakpoint (b))
794 return;
795
796 for (loc = b->loc; loc; loc = loc->next)
797 loc->condition_changed = condition_modified;
798}
799
800/* Mark location as "conditions have changed" in case the target supports
801 evaluating conditions on its side. */
802
803static void
804mark_breakpoint_location_modified (struct bp_location *loc)
805{
806 /* This is only meaningful if the target is
807 evaluating conditions and if the user has
808 opted for condition evaluation on the target's
809 side. */
810 if (gdb_evaluates_breakpoint_condition_p ()
811 || !target_supports_evaluation_of_breakpoint_conditions ())
812
813 return;
814
815 if (!is_breakpoint (loc->owner))
816 return;
817
818 loc->condition_changed = condition_modified;
819}
820
821/* Sets the condition-evaluation mode using the static global
822 condition_evaluation_mode. */
823
824static void
825set_condition_evaluation_mode (char *args, int from_tty,
826 struct cmd_list_element *c)
827{
b775012e
LM
828 const char *old_mode, *new_mode;
829
830 if ((condition_evaluation_mode_1 == condition_evaluation_target)
831 && !target_supports_evaluation_of_breakpoint_conditions ())
832 {
833 condition_evaluation_mode_1 = condition_evaluation_mode;
834 warning (_("Target does not support breakpoint condition evaluation.\n"
835 "Using host evaluation mode instead."));
836 return;
837 }
838
839 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
840 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
841
abf1152a
JK
842 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
843 settings was "auto". */
844 condition_evaluation_mode = condition_evaluation_mode_1;
845
b775012e
LM
846 /* Only update the mode if the user picked a different one. */
847 if (new_mode != old_mode)
848 {
849 struct bp_location *loc, **loc_tmp;
850 /* If the user switched to a different evaluation mode, we
851 need to synch the changes with the target as follows:
852
853 "host" -> "target": Send all (valid) conditions to the target.
854 "target" -> "host": Remove all the conditions from the target.
855 */
856
b775012e
LM
857 if (new_mode == condition_evaluation_target)
858 {
859 /* Mark everything modified and synch conditions with the
860 target. */
861 ALL_BP_LOCATIONS (loc, loc_tmp)
862 mark_breakpoint_location_modified (loc);
863 }
864 else
865 {
866 /* Manually mark non-duplicate locations to synch conditions
867 with the target. We do this to remove all the conditions the
868 target knows about. */
869 ALL_BP_LOCATIONS (loc, loc_tmp)
870 if (is_breakpoint (loc->owner) && loc->inserted)
871 loc->needs_update = 1;
872 }
873
874 /* Do the update. */
44702360 875 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
876 }
877
878 return;
879}
880
881/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
882 what "auto" is translating to. */
883
884static void
885show_condition_evaluation_mode (struct ui_file *file, int from_tty,
886 struct cmd_list_element *c, const char *value)
887{
888 if (condition_evaluation_mode == condition_evaluation_auto)
889 fprintf_filtered (file,
890 _("Breakpoint condition evaluation "
891 "mode is %s (currently %s).\n"),
892 value,
893 breakpoint_condition_evaluation_mode ());
894 else
895 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
896 value);
897}
898
899/* A comparison function for bp_location AP and BP that is used by
900 bsearch. This comparison function only cares about addresses, unlike
901 the more general bp_location_compare function. */
902
903static int
904bp_location_compare_addrs (const void *ap, const void *bp)
905{
906 struct bp_location *a = *(void **) ap;
907 struct bp_location *b = *(void **) bp;
908
909 if (a->address == b->address)
910 return 0;
911 else
912 return ((a->address > b->address) - (a->address < b->address));
913}
914
915/* Helper function to skip all bp_locations with addresses
916 less than ADDRESS. It returns the first bp_location that
917 is greater than or equal to ADDRESS. If none is found, just
918 return NULL. */
919
920static struct bp_location **
921get_first_locp_gte_addr (CORE_ADDR address)
922{
923 struct bp_location dummy_loc;
924 struct bp_location *dummy_locp = &dummy_loc;
925 struct bp_location **locp_found = NULL;
926
927 /* Initialize the dummy location's address field. */
928 memset (&dummy_loc, 0, sizeof (struct bp_location));
929 dummy_loc.address = address;
930
931 /* Find a close match to the first location at ADDRESS. */
932 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
933 sizeof (struct bp_location **),
934 bp_location_compare_addrs);
935
936 /* Nothing was found, nothing left to do. */
937 if (locp_found == NULL)
938 return NULL;
939
940 /* We may have found a location that is at ADDRESS but is not the first in the
941 location's list. Go backwards (if possible) and locate the first one. */
942 while ((locp_found - 1) >= bp_location
943 && (*(locp_found - 1))->address == address)
944 locp_found--;
945
946 return locp_found;
947}
948
adc36818
PM
949void
950set_breakpoint_condition (struct breakpoint *b, char *exp,
951 int from_tty)
952{
3a5c3e22
PA
953 xfree (b->cond_string);
954 b->cond_string = NULL;
adc36818 955
3a5c3e22 956 if (is_watchpoint (b))
adc36818 957 {
3a5c3e22
PA
958 struct watchpoint *w = (struct watchpoint *) b;
959
960 xfree (w->cond_exp);
961 w->cond_exp = NULL;
962 }
963 else
964 {
965 struct bp_location *loc;
966
967 for (loc = b->loc; loc; loc = loc->next)
968 {
969 xfree (loc->cond);
970 loc->cond = NULL;
b775012e
LM
971
972 /* No need to free the condition agent expression
973 bytecode (if we have one). We will handle this
974 when we go through update_global_location_list. */
3a5c3e22 975 }
adc36818 976 }
adc36818
PM
977
978 if (*exp == 0)
979 {
980 if (from_tty)
981 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
982 }
983 else
984 {
bbc13ae3 985 const char *arg = exp;
cc59ec59 986
adc36818
PM
987 /* I don't know if it matters whether this is the string the user
988 typed in or the decompiled expression. */
989 b->cond_string = xstrdup (arg);
990 b->condition_not_parsed = 0;
991
992 if (is_watchpoint (b))
993 {
3a5c3e22
PA
994 struct watchpoint *w = (struct watchpoint *) b;
995
adc36818
PM
996 innermost_block = NULL;
997 arg = exp;
1bb9788d 998 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
999 if (*arg)
1000 error (_("Junk at end of expression"));
3a5c3e22 1001 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1002 }
1003 else
1004 {
3a5c3e22
PA
1005 struct bp_location *loc;
1006
adc36818
PM
1007 for (loc = b->loc; loc; loc = loc->next)
1008 {
1009 arg = exp;
1010 loc->cond =
1bb9788d
TT
1011 parse_exp_1 (&arg, loc->address,
1012 block_for_pc (loc->address), 0);
adc36818
PM
1013 if (*arg)
1014 error (_("Junk at end of expression"));
1015 }
1016 }
1017 }
b775012e
LM
1018 mark_breakpoint_modified (b);
1019
8d3788bd 1020 observer_notify_breakpoint_modified (b);
adc36818
PM
1021}
1022
d55637df
TT
1023/* Completion for the "condition" command. */
1024
1025static VEC (char_ptr) *
6f937416
PA
1026condition_completer (struct cmd_list_element *cmd,
1027 const char *text, const char *word)
d55637df 1028{
6f937416 1029 const char *space;
d55637df 1030
6f937416
PA
1031 text = skip_spaces_const (text);
1032 space = skip_to_space_const (text);
d55637df
TT
1033 if (*space == '\0')
1034 {
1035 int len;
1036 struct breakpoint *b;
1037 VEC (char_ptr) *result = NULL;
1038
1039 if (text[0] == '$')
1040 {
1041 /* We don't support completion of history indices. */
1042 if (isdigit (text[1]))
1043 return NULL;
1044 return complete_internalvar (&text[1]);
1045 }
1046
1047 /* We're completing the breakpoint number. */
1048 len = strlen (text);
1049
1050 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1051 {
1052 char number[50];
1053
1054 xsnprintf (number, sizeof (number), "%d", b->number);
1055
1056 if (strncmp (number, text, len) == 0)
1057 VEC_safe_push (char_ptr, result, xstrdup (number));
1058 }
d55637df
TT
1059
1060 return result;
1061 }
1062
1063 /* We're completing the expression part. */
6f937416 1064 text = skip_spaces_const (space);
d55637df
TT
1065 return expression_completer (cmd, text, word);
1066}
1067
c906108c
SS
1068/* condition N EXP -- set break condition of breakpoint N to EXP. */
1069
1070static void
fba45db2 1071condition_command (char *arg, int from_tty)
c906108c 1072{
52f0bd74 1073 struct breakpoint *b;
c906108c 1074 char *p;
52f0bd74 1075 int bnum;
c906108c
SS
1076
1077 if (arg == 0)
e2e0b3e5 1078 error_no_arg (_("breakpoint number"));
c906108c
SS
1079
1080 p = arg;
1081 bnum = get_number (&p);
5c44784c 1082 if (bnum == 0)
8a3fe4f8 1083 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1084
1085 ALL_BREAKPOINTS (b)
1086 if (b->number == bnum)
2f069f6f 1087 {
6dddc817
DE
1088 /* Check if this breakpoint has a "stop" method implemented in an
1089 extension language. This method and conditions entered into GDB
1090 from the CLI are mutually exclusive. */
1091 const struct extension_language_defn *extlang
1092 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1093
1094 if (extlang != NULL)
1095 {
1096 error (_("Only one stop condition allowed. There is currently"
1097 " a %s stop condition defined for this breakpoint."),
1098 ext_lang_capitalized_name (extlang));
1099 }
2566ad2d 1100 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1101
1102 if (is_breakpoint (b))
44702360 1103 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1104
2f069f6f
JB
1105 return;
1106 }
c906108c 1107
8a3fe4f8 1108 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1109}
1110
a7bdde9e
VP
1111/* Check that COMMAND do not contain commands that are suitable
1112 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1113 Throw if any such commands is found. */
1114
a7bdde9e
VP
1115static void
1116check_no_tracepoint_commands (struct command_line *commands)
1117{
1118 struct command_line *c;
cc59ec59 1119
a7bdde9e
VP
1120 for (c = commands; c; c = c->next)
1121 {
1122 int i;
1123
1124 if (c->control_type == while_stepping_control)
3e43a32a
MS
1125 error (_("The 'while-stepping' command can "
1126 "only be used for tracepoints"));
a7bdde9e
VP
1127
1128 for (i = 0; i < c->body_count; ++i)
1129 check_no_tracepoint_commands ((c->body_list)[i]);
1130
1131 /* Not that command parsing removes leading whitespace and comment
4a64f543 1132 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1133 command directly. */
1134 if (strstr (c->line, "collect ") == c->line)
1135 error (_("The 'collect' command can only be used for tracepoints"));
1136
51661e93
VP
1137 if (strstr (c->line, "teval ") == c->line)
1138 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1139 }
1140}
1141
d77f58be
SS
1142/* Encapsulate tests for different types of tracepoints. */
1143
d9b3f62e
PA
1144static int
1145is_tracepoint_type (enum bptype type)
1146{
1147 return (type == bp_tracepoint
1148 || type == bp_fast_tracepoint
1149 || type == bp_static_tracepoint);
1150}
1151
a7bdde9e 1152int
d77f58be 1153is_tracepoint (const struct breakpoint *b)
a7bdde9e 1154{
d9b3f62e 1155 return is_tracepoint_type (b->type);
a7bdde9e 1156}
d9b3f62e 1157
e5dd4106 1158/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1159 breakpoint. This function will throw an exception if a problem is
1160 found. */
48cb2d85 1161
95a42b64
TT
1162static void
1163validate_commands_for_breakpoint (struct breakpoint *b,
1164 struct command_line *commands)
48cb2d85 1165{
d77f58be 1166 if (is_tracepoint (b))
a7bdde9e 1167 {
c9a6ce02 1168 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1169 struct command_line *c;
1170 struct command_line *while_stepping = 0;
c9a6ce02
PA
1171
1172 /* Reset the while-stepping step count. The previous commands
1173 might have included a while-stepping action, while the new
1174 ones might not. */
1175 t->step_count = 0;
1176
1177 /* We need to verify that each top-level element of commands is
1178 valid for tracepoints, that there's at most one
1179 while-stepping element, and that the while-stepping's body
1180 has valid tracing commands excluding nested while-stepping.
1181 We also need to validate the tracepoint action line in the
1182 context of the tracepoint --- validate_actionline actually
1183 has side effects, like setting the tracepoint's
1184 while-stepping STEP_COUNT, in addition to checking if the
1185 collect/teval actions parse and make sense in the
1186 tracepoint's context. */
a7bdde9e
VP
1187 for (c = commands; c; c = c->next)
1188 {
a7bdde9e
VP
1189 if (c->control_type == while_stepping_control)
1190 {
1191 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1192 error (_("The 'while-stepping' command "
1193 "cannot be used for fast tracepoint"));
0fb4aa4b 1194 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1195 error (_("The 'while-stepping' command "
1196 "cannot be used for static tracepoint"));
a7bdde9e
VP
1197
1198 if (while_stepping)
3e43a32a
MS
1199 error (_("The 'while-stepping' command "
1200 "can be used only once"));
a7bdde9e
VP
1201 else
1202 while_stepping = c;
1203 }
c9a6ce02
PA
1204
1205 validate_actionline (c->line, b);
a7bdde9e
VP
1206 }
1207 if (while_stepping)
1208 {
1209 struct command_line *c2;
1210
1211 gdb_assert (while_stepping->body_count == 1);
1212 c2 = while_stepping->body_list[0];
1213 for (; c2; c2 = c2->next)
1214 {
a7bdde9e
VP
1215 if (c2->control_type == while_stepping_control)
1216 error (_("The 'while-stepping' command cannot be nested"));
1217 }
1218 }
1219 }
1220 else
1221 {
1222 check_no_tracepoint_commands (commands);
1223 }
95a42b64
TT
1224}
1225
0fb4aa4b
PA
1226/* Return a vector of all the static tracepoints set at ADDR. The
1227 caller is responsible for releasing the vector. */
1228
1229VEC(breakpoint_p) *
1230static_tracepoints_here (CORE_ADDR addr)
1231{
1232 struct breakpoint *b;
1233 VEC(breakpoint_p) *found = 0;
1234 struct bp_location *loc;
1235
1236 ALL_BREAKPOINTS (b)
1237 if (b->type == bp_static_tracepoint)
1238 {
1239 for (loc = b->loc; loc; loc = loc->next)
1240 if (loc->address == addr)
1241 VEC_safe_push(breakpoint_p, found, b);
1242 }
1243
1244 return found;
1245}
1246
95a42b64 1247/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1248 validate that only allowed commands are included. */
95a42b64
TT
1249
1250void
4a64f543
MS
1251breakpoint_set_commands (struct breakpoint *b,
1252 struct command_line *commands)
95a42b64
TT
1253{
1254 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1255
9add0f1b
TT
1256 decref_counted_command_line (&b->commands);
1257 b->commands = alloc_counted_command_line (commands);
8d3788bd 1258 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1259}
1260
45a43567
TT
1261/* Set the internal `silent' flag on the breakpoint. Note that this
1262 is not the same as the "silent" that may appear in the breakpoint's
1263 commands. */
1264
1265void
1266breakpoint_set_silent (struct breakpoint *b, int silent)
1267{
1268 int old_silent = b->silent;
1269
1270 b->silent = silent;
1271 if (old_silent != silent)
8d3788bd 1272 observer_notify_breakpoint_modified (b);
45a43567
TT
1273}
1274
1275/* Set the thread for this breakpoint. If THREAD is -1, make the
1276 breakpoint work for any thread. */
1277
1278void
1279breakpoint_set_thread (struct breakpoint *b, int thread)
1280{
1281 int old_thread = b->thread;
1282
1283 b->thread = thread;
1284 if (old_thread != thread)
8d3788bd 1285 observer_notify_breakpoint_modified (b);
45a43567
TT
1286}
1287
1288/* Set the task for this breakpoint. If TASK is 0, make the
1289 breakpoint work for any task. */
1290
1291void
1292breakpoint_set_task (struct breakpoint *b, int task)
1293{
1294 int old_task = b->task;
1295
1296 b->task = task;
1297 if (old_task != task)
8d3788bd 1298 observer_notify_breakpoint_modified (b);
45a43567
TT
1299}
1300
95a42b64
TT
1301void
1302check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1303{
1304 struct breakpoint *b = closure;
cc59ec59 1305
6f937416 1306 validate_actionline (line, b);
a7bdde9e
VP
1307}
1308
95a42b64
TT
1309/* A structure used to pass information through
1310 map_breakpoint_numbers. */
1311
1312struct commands_info
1313{
1314 /* True if the command was typed at a tty. */
1315 int from_tty;
86b17b60
PA
1316
1317 /* The breakpoint range spec. */
1318 char *arg;
1319
95a42b64
TT
1320 /* Non-NULL if the body of the commands are being read from this
1321 already-parsed command. */
1322 struct command_line *control;
86b17b60 1323
95a42b64
TT
1324 /* The command lines read from the user, or NULL if they have not
1325 yet been read. */
1326 struct counted_command_line *cmd;
1327};
1328
1329/* A callback for map_breakpoint_numbers that sets the commands for
1330 commands_command. */
1331
c906108c 1332static void
95a42b64 1333do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1334{
95a42b64 1335 struct commands_info *info = data;
c906108c 1336
95a42b64
TT
1337 if (info->cmd == NULL)
1338 {
1339 struct command_line *l;
5c44784c 1340
95a42b64
TT
1341 if (info->control != NULL)
1342 l = copy_command_lines (info->control->body_list[0]);
1343 else
86b17b60
PA
1344 {
1345 struct cleanup *old_chain;
1346 char *str;
c5aa993b 1347
3e43a32a
MS
1348 str = xstrprintf (_("Type commands for breakpoint(s) "
1349 "%s, one per line."),
86b17b60
PA
1350 info->arg);
1351
1352 old_chain = make_cleanup (xfree, str);
1353
1354 l = read_command_lines (str,
1355 info->from_tty, 1,
d77f58be 1356 (is_tracepoint (b)
86b17b60
PA
1357 ? check_tracepoint_command : 0),
1358 b);
1359
1360 do_cleanups (old_chain);
1361 }
a7bdde9e 1362
95a42b64
TT
1363 info->cmd = alloc_counted_command_line (l);
1364 }
1365
1366 /* If a breakpoint was on the list more than once, we don't need to
1367 do anything. */
1368 if (b->commands != info->cmd)
1369 {
1370 validate_commands_for_breakpoint (b, info->cmd->commands);
1371 incref_counted_command_line (info->cmd);
1372 decref_counted_command_line (&b->commands);
1373 b->commands = info->cmd;
8d3788bd 1374 observer_notify_breakpoint_modified (b);
c5aa993b 1375 }
95a42b64
TT
1376}
1377
1378static void
4a64f543
MS
1379commands_command_1 (char *arg, int from_tty,
1380 struct command_line *control)
95a42b64
TT
1381{
1382 struct cleanup *cleanups;
1383 struct commands_info info;
1384
1385 info.from_tty = from_tty;
1386 info.control = control;
1387 info.cmd = NULL;
1388 /* If we read command lines from the user, then `info' will hold an
1389 extra reference to the commands that we must clean up. */
1390 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1391
1392 if (arg == NULL || !*arg)
1393 {
86b17b60 1394 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1395 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1396 breakpoint_count);
95a42b64
TT
1397 else if (breakpoint_count > 0)
1398 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1399 else
1400 {
1401 /* So that we don't try to free the incoming non-NULL
1402 argument in the cleanup below. Mapping breakpoint
1403 numbers will fail in this case. */
1404 arg = NULL;
1405 }
95a42b64 1406 }
9766ced4
SS
1407 else
1408 /* The command loop has some static state, so we need to preserve
1409 our argument. */
1410 arg = xstrdup (arg);
86b17b60
PA
1411
1412 if (arg != NULL)
1413 make_cleanup (xfree, arg);
1414
1415 info.arg = arg;
95a42b64
TT
1416
1417 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1418
1419 if (info.cmd == NULL)
1420 error (_("No breakpoints specified."));
1421
1422 do_cleanups (cleanups);
1423}
1424
1425static void
1426commands_command (char *arg, int from_tty)
1427{
1428 commands_command_1 (arg, from_tty, NULL);
c906108c 1429}
40c03ae8
EZ
1430
1431/* Like commands_command, but instead of reading the commands from
1432 input stream, takes them from an already parsed command structure.
1433
1434 This is used by cli-script.c to DTRT with breakpoint commands
1435 that are part of if and while bodies. */
1436enum command_control_type
1437commands_from_control_command (char *arg, struct command_line *cmd)
1438{
95a42b64
TT
1439 commands_command_1 (arg, 0, cmd);
1440 return simple_control;
40c03ae8 1441}
876fa593
JK
1442
1443/* Return non-zero if BL->TARGET_INFO contains valid information. */
1444
1445static int
1446bp_location_has_shadow (struct bp_location *bl)
1447{
1448 if (bl->loc_type != bp_loc_software_breakpoint)
1449 return 0;
1450 if (!bl->inserted)
1451 return 0;
1452 if (bl->target_info.shadow_len == 0)
e5dd4106 1453 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1454 return 0;
1455 return 1;
1456}
1457
9d497a19
PA
1458/* Update BUF, which is LEN bytes read from the target address
1459 MEMADDR, by replacing a memory breakpoint with its shadowed
1460 contents.
1461
1462 If READBUF is not NULL, this buffer must not overlap with the of
1463 the breakpoint location's shadow_contents buffer. Otherwise, a
1464 failed assertion internal error will be raised. */
1465
1466static void
1467one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1468 const gdb_byte *writebuf_org,
1469 ULONGEST memaddr, LONGEST len,
1470 struct bp_target_info *target_info,
1471 struct gdbarch *gdbarch)
1472{
1473 /* Now do full processing of the found relevant range of elements. */
1474 CORE_ADDR bp_addr = 0;
1475 int bp_size = 0;
1476 int bptoffset = 0;
1477
1478 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1479 current_program_space->aspace, 0))
1480 {
1481 /* The breakpoint is inserted in a different address space. */
1482 return;
1483 }
1484
1485 /* Addresses and length of the part of the breakpoint that
1486 we need to copy. */
1487 bp_addr = target_info->placed_address;
1488 bp_size = target_info->shadow_len;
1489
1490 if (bp_addr + bp_size <= memaddr)
1491 {
1492 /* The breakpoint is entirely before the chunk of memory we are
1493 reading. */
1494 return;
1495 }
1496
1497 if (bp_addr >= memaddr + len)
1498 {
1499 /* The breakpoint is entirely after the chunk of memory we are
1500 reading. */
1501 return;
1502 }
1503
1504 /* Offset within shadow_contents. */
1505 if (bp_addr < memaddr)
1506 {
1507 /* Only copy the second part of the breakpoint. */
1508 bp_size -= memaddr - bp_addr;
1509 bptoffset = memaddr - bp_addr;
1510 bp_addr = memaddr;
1511 }
1512
1513 if (bp_addr + bp_size > memaddr + len)
1514 {
1515 /* Only copy the first part of the breakpoint. */
1516 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1517 }
1518
1519 if (readbuf != NULL)
1520 {
1521 /* Verify that the readbuf buffer does not overlap with the
1522 shadow_contents buffer. */
1523 gdb_assert (target_info->shadow_contents >= readbuf + len
1524 || readbuf >= (target_info->shadow_contents
1525 + target_info->shadow_len));
1526
1527 /* Update the read buffer with this inserted breakpoint's
1528 shadow. */
1529 memcpy (readbuf + bp_addr - memaddr,
1530 target_info->shadow_contents + bptoffset, bp_size);
1531 }
1532 else
1533 {
1534 const unsigned char *bp;
0d5ed153
MR
1535 CORE_ADDR addr = target_info->reqstd_address;
1536 int placed_size;
9d497a19
PA
1537
1538 /* Update the shadow with what we want to write to memory. */
1539 memcpy (target_info->shadow_contents + bptoffset,
1540 writebuf_org + bp_addr - memaddr, bp_size);
1541
1542 /* Determine appropriate breakpoint contents and size for this
1543 address. */
0d5ed153 1544 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1545
1546 /* Update the final write buffer with this inserted
1547 breakpoint's INSN. */
1548 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1549 }
1550}
1551
8defab1a 1552/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1553 by replacing any memory breakpoints with their shadowed contents.
1554
35c63cd8
JB
1555 If READBUF is not NULL, this buffer must not overlap with any of
1556 the breakpoint location's shadow_contents buffers. Otherwise,
1557 a failed assertion internal error will be raised.
1558
876fa593 1559 The range of shadowed area by each bp_location is:
35df4500
TJB
1560 bl->address - bp_location_placed_address_before_address_max
1561 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1562 The range we were requested to resolve shadows for is:
1563 memaddr ... memaddr + len
1564 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1565 memaddr + len <= (bl->address
1566 - bp_location_placed_address_before_address_max)
876fa593 1567 and:
35df4500 1568 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1569
8defab1a 1570void
f0ba3972
PA
1571breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1572 const gdb_byte *writebuf_org,
1573 ULONGEST memaddr, LONGEST len)
c906108c 1574{
4a64f543
MS
1575 /* Left boundary, right boundary and median element of our binary
1576 search. */
876fa593 1577 unsigned bc_l, bc_r, bc;
9d497a19 1578 size_t i;
876fa593 1579
4a64f543
MS
1580 /* Find BC_L which is a leftmost element which may affect BUF
1581 content. It is safe to report lower value but a failure to
1582 report higher one. */
876fa593
JK
1583
1584 bc_l = 0;
1585 bc_r = bp_location_count;
1586 while (bc_l + 1 < bc_r)
1587 {
35df4500 1588 struct bp_location *bl;
876fa593
JK
1589
1590 bc = (bc_l + bc_r) / 2;
35df4500 1591 bl = bp_location[bc];
876fa593 1592
4a64f543
MS
1593 /* Check first BL->ADDRESS will not overflow due to the added
1594 constant. Then advance the left boundary only if we are sure
1595 the BC element can in no way affect the BUF content (MEMADDR
1596 to MEMADDR + LEN range).
876fa593 1597
4a64f543
MS
1598 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1599 offset so that we cannot miss a breakpoint with its shadow
1600 range tail still reaching MEMADDR. */
c5aa993b 1601
35df4500
TJB
1602 if ((bl->address + bp_location_shadow_len_after_address_max
1603 >= bl->address)
1604 && (bl->address + bp_location_shadow_len_after_address_max
1605 <= memaddr))
876fa593
JK
1606 bc_l = bc;
1607 else
1608 bc_r = bc;
1609 }
1610
128070bb
PA
1611 /* Due to the binary search above, we need to make sure we pick the
1612 first location that's at BC_L's address. E.g., if there are
1613 multiple locations at the same address, BC_L may end up pointing
1614 at a duplicate location, and miss the "master"/"inserted"
1615 location. Say, given locations L1, L2 and L3 at addresses A and
1616 B:
1617
1618 L1@A, L2@A, L3@B, ...
1619
1620 BC_L could end up pointing at location L2, while the "master"
1621 location could be L1. Since the `loc->inserted' flag is only set
1622 on "master" locations, we'd forget to restore the shadow of L1
1623 and L2. */
1624 while (bc_l > 0
1625 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1626 bc_l--;
1627
876fa593
JK
1628 /* Now do full processing of the found relevant range of elements. */
1629
1630 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1631 {
35df4500 1632 struct bp_location *bl = bp_location[bc];
876fa593
JK
1633 CORE_ADDR bp_addr = 0;
1634 int bp_size = 0;
1635 int bptoffset = 0;
1636
35df4500
TJB
1637 /* bp_location array has BL->OWNER always non-NULL. */
1638 if (bl->owner->type == bp_none)
8a3fe4f8 1639 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1640 bl->owner->number);
ffce0d52 1641
e5dd4106 1642 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1643 content. */
1644
35df4500
TJB
1645 if (bl->address >= bp_location_placed_address_before_address_max
1646 && memaddr + len <= (bl->address
1647 - bp_location_placed_address_before_address_max))
876fa593
JK
1648 break;
1649
35df4500 1650 if (!bp_location_has_shadow (bl))
c5aa993b 1651 continue;
6c95b8df 1652
9d497a19
PA
1653 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1654 memaddr, len, &bl->target_info, bl->gdbarch);
1655 }
c906108c 1656}
9d497a19 1657
c906108c 1658\f
c5aa993b 1659
b775012e
LM
1660/* Return true if BPT is either a software breakpoint or a hardware
1661 breakpoint. */
1662
1663int
1664is_breakpoint (const struct breakpoint *bpt)
1665{
1666 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1667 || bpt->type == bp_hardware_breakpoint
1668 || bpt->type == bp_dprintf);
b775012e
LM
1669}
1670
60e1c644
PA
1671/* Return true if BPT is of any hardware watchpoint kind. */
1672
a5606eee 1673static int
d77f58be 1674is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1675{
1676 return (bpt->type == bp_hardware_watchpoint
1677 || bpt->type == bp_read_watchpoint
1678 || bpt->type == bp_access_watchpoint);
1679}
7270d8f2 1680
60e1c644
PA
1681/* Return true if BPT is of any watchpoint kind, hardware or
1682 software. */
1683
3a5c3e22 1684int
d77f58be 1685is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1686{
1687 return (is_hardware_watchpoint (bpt)
1688 || bpt->type == bp_watchpoint);
1689}
1690
3a5c3e22
PA
1691/* Returns true if the current thread and its running state are safe
1692 to evaluate or update watchpoint B. Watchpoints on local
1693 expressions need to be evaluated in the context of the thread that
1694 was current when the watchpoint was created, and, that thread needs
1695 to be stopped to be able to select the correct frame context.
1696 Watchpoints on global expressions can be evaluated on any thread,
1697 and in any state. It is presently left to the target allowing
1698 memory accesses when threads are running. */
f6bc2008
PA
1699
1700static int
3a5c3e22 1701watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1702{
d0d8b0c6
JK
1703 return (b->base.pspace == current_program_space
1704 && (ptid_equal (b->watchpoint_thread, null_ptid)
1705 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1706 && !is_executing (inferior_ptid))));
f6bc2008
PA
1707}
1708
d0fb5eae
JK
1709/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1710 associated bp_watchpoint_scope breakpoint. */
1711
1712static void
3a5c3e22 1713watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1714{
3a5c3e22 1715 struct breakpoint *b = &w->base;
d0fb5eae
JK
1716
1717 if (b->related_breakpoint != b)
1718 {
1719 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1720 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1721 b->related_breakpoint->disposition = disp_del_at_next_stop;
1722 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1723 b->related_breakpoint = b;
1724 }
1725 b->disposition = disp_del_at_next_stop;
1726}
1727
bb9d5f81
PP
1728/* Extract a bitfield value from value VAL using the bit parameters contained in
1729 watchpoint W. */
1730
1731static struct value *
1732extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1733{
1734 struct value *bit_val;
1735
1736 if (val == NULL)
1737 return NULL;
1738
1739 bit_val = allocate_value (value_type (val));
1740
1741 unpack_value_bitfield (bit_val,
1742 w->val_bitpos,
1743 w->val_bitsize,
1744 value_contents_for_printing (val),
1745 value_offset (val),
1746 val);
1747
1748 return bit_val;
1749}
1750
567e1b4e
JB
1751/* Assuming that B is a watchpoint:
1752 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1753 - Evaluate expression and store the result in B->val
567e1b4e
JB
1754 - Evaluate the condition if there is one, and store the result
1755 in b->loc->cond.
a5606eee
VP
1756 - Update the list of values that must be watched in B->loc.
1757
4a64f543
MS
1758 If the watchpoint disposition is disp_del_at_next_stop, then do
1759 nothing. If this is local watchpoint that is out of scope, delete
1760 it.
1761
1762 Even with `set breakpoint always-inserted on' the watchpoints are
1763 removed + inserted on each stop here. Normal breakpoints must
1764 never be removed because they might be missed by a running thread
1765 when debugging in non-stop mode. On the other hand, hardware
1766 watchpoints (is_hardware_watchpoint; processed here) are specific
1767 to each LWP since they are stored in each LWP's hardware debug
1768 registers. Therefore, such LWP must be stopped first in order to
1769 be able to modify its hardware watchpoints.
1770
1771 Hardware watchpoints must be reset exactly once after being
1772 presented to the user. It cannot be done sooner, because it would
1773 reset the data used to present the watchpoint hit to the user. And
1774 it must not be done later because it could display the same single
1775 watchpoint hit during multiple GDB stops. Note that the latter is
1776 relevant only to the hardware watchpoint types bp_read_watchpoint
1777 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1778 not user-visible - its hit is suppressed if the memory content has
1779 not changed.
1780
1781 The following constraints influence the location where we can reset
1782 hardware watchpoints:
1783
1784 * target_stopped_by_watchpoint and target_stopped_data_address are
1785 called several times when GDB stops.
1786
1787 [linux]
1788 * Multiple hardware watchpoints can be hit at the same time,
1789 causing GDB to stop. GDB only presents one hardware watchpoint
1790 hit at a time as the reason for stopping, and all the other hits
1791 are presented later, one after the other, each time the user
1792 requests the execution to be resumed. Execution is not resumed
1793 for the threads still having pending hit event stored in
1794 LWP_INFO->STATUS. While the watchpoint is already removed from
1795 the inferior on the first stop the thread hit event is kept being
1796 reported from its cached value by linux_nat_stopped_data_address
1797 until the real thread resume happens after the watchpoint gets
1798 presented and thus its LWP_INFO->STATUS gets reset.
1799
1800 Therefore the hardware watchpoint hit can get safely reset on the
1801 watchpoint removal from inferior. */
a79d3c27 1802
b40ce68a 1803static void
3a5c3e22 1804update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1805{
a5606eee 1806 int within_current_scope;
a5606eee 1807 struct frame_id saved_frame_id;
66076460 1808 int frame_saved;
a5606eee 1809
f6bc2008
PA
1810 /* If this is a local watchpoint, we only want to check if the
1811 watchpoint frame is in scope if the current thread is the thread
1812 that was used to create the watchpoint. */
1813 if (!watchpoint_in_thread_scope (b))
1814 return;
1815
3a5c3e22 1816 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1817 return;
1818
66076460 1819 frame_saved = 0;
a5606eee
VP
1820
1821 /* Determine if the watchpoint is within scope. */
1822 if (b->exp_valid_block == NULL)
1823 within_current_scope = 1;
1824 else
1825 {
b5db5dfc
UW
1826 struct frame_info *fi = get_current_frame ();
1827 struct gdbarch *frame_arch = get_frame_arch (fi);
1828 CORE_ADDR frame_pc = get_frame_pc (fi);
1829
1830 /* If we're in a function epilogue, unwinding may not work
1831 properly, so do not attempt to recreate locations at this
1832 point. See similar comments in watchpoint_check. */
1833 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1834 return;
66076460
DJ
1835
1836 /* Save the current frame's ID so we can restore it after
1837 evaluating the watchpoint expression on its own frame. */
1838 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1839 took a frame parameter, so that we didn't have to change the
1840 selected frame. */
1841 frame_saved = 1;
1842 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1843
a5606eee
VP
1844 fi = frame_find_by_id (b->watchpoint_frame);
1845 within_current_scope = (fi != NULL);
1846 if (within_current_scope)
1847 select_frame (fi);
1848 }
1849
b5db5dfc
UW
1850 /* We don't free locations. They are stored in the bp_location array
1851 and update_global_location_list will eventually delete them and
1852 remove breakpoints if needed. */
3a5c3e22 1853 b->base.loc = NULL;
b5db5dfc 1854
a5606eee
VP
1855 if (within_current_scope && reparse)
1856 {
bbc13ae3 1857 const char *s;
d63d0675 1858
a5606eee
VP
1859 if (b->exp)
1860 {
1861 xfree (b->exp);
1862 b->exp = NULL;
1863 }
d63d0675 1864 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1865 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1866 /* If the meaning of expression itself changed, the old value is
1867 no longer relevant. We don't want to report a watchpoint hit
1868 to the user when the old value and the new value may actually
1869 be completely different objects. */
1870 value_free (b->val);
fa4727a6
DJ
1871 b->val = NULL;
1872 b->val_valid = 0;
60e1c644
PA
1873
1874 /* Note that unlike with breakpoints, the watchpoint's condition
1875 expression is stored in the breakpoint object, not in the
1876 locations (re)created below. */
3a5c3e22 1877 if (b->base.cond_string != NULL)
60e1c644
PA
1878 {
1879 if (b->cond_exp != NULL)
1880 {
1881 xfree (b->cond_exp);
1882 b->cond_exp = NULL;
1883 }
1884
3a5c3e22 1885 s = b->base.cond_string;
1bb9788d 1886 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1887 }
a5606eee 1888 }
a5606eee
VP
1889
1890 /* If we failed to parse the expression, for example because
1891 it refers to a global variable in a not-yet-loaded shared library,
1892 don't try to insert watchpoint. We don't automatically delete
1893 such watchpoint, though, since failure to parse expression
1894 is different from out-of-scope watchpoint. */
e8369a73 1895 if (!target_has_execution)
2d134ed3
PA
1896 {
1897 /* Without execution, memory can't change. No use to try and
1898 set watchpoint locations. The watchpoint will be reset when
1899 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1900 if (!can_use_hw_watchpoints)
1901 {
1902 if (b->base.ops->works_in_software_mode (&b->base))
1903 b->base.type = bp_watchpoint;
1904 else
638aa5a1
AB
1905 error (_("Can't set read/access watchpoint when "
1906 "hardware watchpoints are disabled."));
e8369a73 1907 }
2d134ed3
PA
1908 }
1909 else if (within_current_scope && b->exp)
a5606eee 1910 {
0cf6dd15 1911 int pc = 0;
fa4727a6 1912 struct value *val_chain, *v, *result, *next;
2d134ed3 1913 struct program_space *frame_pspace;
a5606eee 1914
3a1115a0 1915 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1916
a5606eee
VP
1917 /* Avoid setting b->val if it's already set. The meaning of
1918 b->val is 'the last value' user saw, and we should update
1919 it only if we reported that last value to user. As it
9c06b0b4
TJB
1920 happens, the code that reports it updates b->val directly.
1921 We don't keep track of the memory value for masked
1922 watchpoints. */
3a5c3e22 1923 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1924 {
bb9d5f81
PP
1925 if (b->val_bitsize != 0)
1926 {
1927 v = extract_bitfield_from_watchpoint_value (b, v);
1928 if (v != NULL)
1929 release_value (v);
1930 }
fa4727a6
DJ
1931 b->val = v;
1932 b->val_valid = 1;
1933 }
a5606eee 1934
2d134ed3
PA
1935 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1936
a5606eee 1937 /* Look at each value on the value chain. */
9fa40276 1938 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1939 {
1940 /* If it's a memory location, and GDB actually needed
1941 its contents to evaluate the expression, then we
fa4727a6
DJ
1942 must watch it. If the first value returned is
1943 still lazy, that means an error occurred reading it;
1944 watch it anyway in case it becomes readable. */
a5606eee 1945 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1946 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1947 {
1948 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1949
a5606eee
VP
1950 /* We only watch structs and arrays if user asked
1951 for it explicitly, never if they just happen to
1952 appear in the middle of some value chain. */
fa4727a6 1953 if (v == result
a5606eee
VP
1954 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1955 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1956 {
1957 CORE_ADDR addr;
744a8059 1958 int type;
a5606eee 1959 struct bp_location *loc, **tmp;
bb9d5f81
PP
1960 int bitpos = 0, bitsize = 0;
1961
1962 if (value_bitsize (v) != 0)
1963 {
1964 /* Extract the bit parameters out from the bitfield
1965 sub-expression. */
1966 bitpos = value_bitpos (v);
1967 bitsize = value_bitsize (v);
1968 }
1969 else if (v == result && b->val_bitsize != 0)
1970 {
1971 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1972 lvalue whose bit parameters are saved in the fields
1973 VAL_BITPOS and VAL_BITSIZE. */
1974 bitpos = b->val_bitpos;
1975 bitsize = b->val_bitsize;
1976 }
a5606eee 1977
42ae5230 1978 addr = value_address (v);
bb9d5f81
PP
1979 if (bitsize != 0)
1980 {
1981 /* Skip the bytes that don't contain the bitfield. */
1982 addr += bitpos / 8;
1983 }
1984
a5606eee 1985 type = hw_write;
3a5c3e22 1986 if (b->base.type == bp_read_watchpoint)
a5606eee 1987 type = hw_read;
3a5c3e22 1988 else if (b->base.type == bp_access_watchpoint)
a5606eee 1989 type = hw_access;
3a5c3e22
PA
1990
1991 loc = allocate_bp_location (&b->base);
1992 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1993 ;
1994 *tmp = loc;
a6d9a66e 1995 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1996
1997 loc->pspace = frame_pspace;
a5606eee 1998 loc->address = addr;
bb9d5f81
PP
1999
2000 if (bitsize != 0)
2001 {
2002 /* Just cover the bytes that make up the bitfield. */
2003 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2004 }
2005 else
2006 loc->length = TYPE_LENGTH (value_type (v));
2007
a5606eee
VP
2008 loc->watchpoint_type = type;
2009 }
2010 }
9fa40276
TJB
2011 }
2012
2013 /* Change the type of breakpoint between hardware assisted or
2014 an ordinary watchpoint depending on the hardware support
2015 and free hardware slots. REPARSE is set when the inferior
2016 is started. */
a9634178 2017 if (reparse)
9fa40276 2018 {
e09342b5 2019 int reg_cnt;
9fa40276
TJB
2020 enum bp_loc_type loc_type;
2021 struct bp_location *bl;
a5606eee 2022
a9634178 2023 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2024
2025 if (reg_cnt)
9fa40276
TJB
2026 {
2027 int i, target_resources_ok, other_type_used;
a1398e0c 2028 enum bptype type;
9fa40276 2029
a9634178
TJB
2030 /* Use an exact watchpoint when there's only one memory region to be
2031 watched, and only one debug register is needed to watch it. */
2032 b->exact = target_exact_watchpoints && reg_cnt == 1;
2033
9fa40276 2034 /* We need to determine how many resources are already
e09342b5
TJB
2035 used for all other hardware watchpoints plus this one
2036 to see if we still have enough resources to also fit
a1398e0c
PA
2037 this watchpoint in as well. */
2038
2039 /* If this is a software watchpoint, we try to turn it
2040 to a hardware one -- count resources as if B was of
2041 hardware watchpoint type. */
2042 type = b->base.type;
2043 if (type == bp_watchpoint)
2044 type = bp_hardware_watchpoint;
2045
2046 /* This watchpoint may or may not have been placed on
2047 the list yet at this point (it won't be in the list
2048 if we're trying to create it for the first time,
2049 through watch_command), so always account for it
2050 manually. */
2051
2052 /* Count resources used by all watchpoints except B. */
2053 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2054
2055 /* Add in the resources needed for B. */
2056 i += hw_watchpoint_use_count (&b->base);
2057
2058 target_resources_ok
2059 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2060 if (target_resources_ok <= 0)
a9634178 2061 {
3a5c3e22 2062 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2063
2064 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2065 error (_("Target does not support this type of "
2066 "hardware watchpoint."));
9c06b0b4
TJB
2067 else if (target_resources_ok < 0 && !sw_mode)
2068 error (_("There are not enough available hardware "
2069 "resources for this watchpoint."));
a1398e0c
PA
2070
2071 /* Downgrade to software watchpoint. */
2072 b->base.type = bp_watchpoint;
2073 }
2074 else
2075 {
2076 /* If this was a software watchpoint, we've just
2077 found we have enough resources to turn it to a
2078 hardware watchpoint. Otherwise, this is a
2079 nop. */
2080 b->base.type = type;
a9634178 2081 }
9fa40276 2082 }
3a5c3e22 2083 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2084 {
2085 if (!can_use_hw_watchpoints)
2086 error (_("Can't set read/access watchpoint when "
2087 "hardware watchpoints are disabled."));
2088 else
2089 error (_("Expression cannot be implemented with "
2090 "read/access watchpoint."));
2091 }
9fa40276 2092 else
3a5c3e22 2093 b->base.type = bp_watchpoint;
9fa40276 2094
3a5c3e22 2095 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2096 : bp_loc_hardware_watchpoint);
3a5c3e22 2097 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2098 bl->loc_type = loc_type;
2099 }
2100
2101 for (v = val_chain; v; v = next)
2102 {
a5606eee
VP
2103 next = value_next (v);
2104 if (v != b->val)
2105 value_free (v);
2106 }
2107
c7437ca6
PA
2108 /* If a software watchpoint is not watching any memory, then the
2109 above left it without any location set up. But,
2110 bpstat_stop_status requires a location to be able to report
2111 stops, so make sure there's at least a dummy one. */
3a5c3e22 2112 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2113 {
3a5c3e22
PA
2114 struct breakpoint *base = &b->base;
2115 base->loc = allocate_bp_location (base);
2116 base->loc->pspace = frame_pspace;
2117 base->loc->address = -1;
2118 base->loc->length = -1;
2119 base->loc->watchpoint_type = -1;
c7437ca6 2120 }
a5606eee
VP
2121 }
2122 else if (!within_current_scope)
7270d8f2 2123 {
ac74f770
MS
2124 printf_filtered (_("\
2125Watchpoint %d deleted because the program has left the block\n\
2126in which its expression is valid.\n"),
3a5c3e22 2127 b->base.number);
d0fb5eae 2128 watchpoint_del_at_next_stop (b);
7270d8f2 2129 }
a5606eee
VP
2130
2131 /* Restore the selected frame. */
66076460
DJ
2132 if (frame_saved)
2133 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2134}
2135
a5606eee 2136
74960c60 2137/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2138 inserted in the inferior. We don't differentiate the type of BL's owner
2139 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2140 breakpoint_ops is not defined, because in insert_bp_location,
2141 tracepoint's insert_location will not be called. */
74960c60 2142static int
35df4500 2143should_be_inserted (struct bp_location *bl)
74960c60 2144{
35df4500 2145 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2146 return 0;
2147
35df4500 2148 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2149 return 0;
2150
35df4500 2151 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2152 return 0;
2153
f8eba3c6
TT
2154 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2155 return 0;
2156
56710373
PA
2157 /* This is set for example, when we're attached to the parent of a
2158 vfork, and have detached from the child. The child is running
2159 free, and we expect it to do an exec or exit, at which point the
2160 OS makes the parent schedulable again (and the target reports
2161 that the vfork is done). Until the child is done with the shared
2162 memory region, do not insert breakpoints in the parent, otherwise
2163 the child could still trip on the parent's breakpoints. Since
2164 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2165 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2166 return 0;
2167
31e77af2
PA
2168 /* Don't insert a breakpoint if we're trying to step past its
2169 location. */
2170 if ((bl->loc_type == bp_loc_software_breakpoint
2171 || bl->loc_type == bp_loc_hardware_breakpoint)
2172 && stepping_past_instruction_at (bl->pspace->aspace,
2173 bl->address))
e558d7c1
PA
2174 {
2175 if (debug_infrun)
2176 {
2177 fprintf_unfiltered (gdb_stdlog,
2178 "infrun: skipping breakpoint: "
2179 "stepping past insn at: %s\n",
2180 paddress (bl->gdbarch, bl->address));
2181 }
2182 return 0;
2183 }
31e77af2 2184
963f9c80
PA
2185 /* Don't insert watchpoints if we're trying to step past the
2186 instruction that triggered one. */
2187 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2188 && stepping_past_nonsteppable_watchpoint ())
2189 {
2190 if (debug_infrun)
2191 {
2192 fprintf_unfiltered (gdb_stdlog,
2193 "infrun: stepping past non-steppable watchpoint. "
2194 "skipping watchpoint at %s:%d\n",
2195 paddress (bl->gdbarch, bl->address),
2196 bl->length);
2197 }
2198 return 0;
2199 }
2200
74960c60
VP
2201 return 1;
2202}
2203
934709f0
PW
2204/* Same as should_be_inserted but does the check assuming
2205 that the location is not duplicated. */
2206
2207static int
2208unduplicated_should_be_inserted (struct bp_location *bl)
2209{
2210 int result;
2211 const int save_duplicate = bl->duplicate;
2212
2213 bl->duplicate = 0;
2214 result = should_be_inserted (bl);
2215 bl->duplicate = save_duplicate;
2216 return result;
2217}
2218
b775012e
LM
2219/* Parses a conditional described by an expression COND into an
2220 agent expression bytecode suitable for evaluation
2221 by the bytecode interpreter. Return NULL if there was
2222 any error during parsing. */
2223
2224static struct agent_expr *
2225parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2226{
2227 struct agent_expr *aexpr = NULL;
b775012e
LM
2228 volatile struct gdb_exception ex;
2229
2230 if (!cond)
2231 return NULL;
2232
2233 /* We don't want to stop processing, so catch any errors
2234 that may show up. */
2235 TRY_CATCH (ex, RETURN_MASK_ERROR)
2236 {
2237 aexpr = gen_eval_for_expr (scope, cond);
2238 }
2239
2240 if (ex.reason < 0)
2241 {
2242 /* If we got here, it means the condition could not be parsed to a valid
2243 bytecode expression and thus can't be evaluated on the target's side.
2244 It's no use iterating through the conditions. */
2245 return NULL;
2246 }
2247
2248 /* We have a valid agent expression. */
2249 return aexpr;
2250}
2251
2252/* Based on location BL, create a list of breakpoint conditions to be
2253 passed on to the target. If we have duplicated locations with different
2254 conditions, we will add such conditions to the list. The idea is that the
2255 target will evaluate the list of conditions and will only notify GDB when
2256 one of them is true. */
2257
2258static void
2259build_target_condition_list (struct bp_location *bl)
2260{
2261 struct bp_location **locp = NULL, **loc2p;
2262 int null_condition_or_parse_error = 0;
2263 int modified = bl->needs_update;
2264 struct bp_location *loc;
2265
8b4f3082
PA
2266 /* Release conditions left over from a previous insert. */
2267 VEC_free (agent_expr_p, bl->target_info.conditions);
2268
b775012e
LM
2269 /* This is only meaningful if the target is
2270 evaluating conditions and if the user has
2271 opted for condition evaluation on the target's
2272 side. */
2273 if (gdb_evaluates_breakpoint_condition_p ()
2274 || !target_supports_evaluation_of_breakpoint_conditions ())
2275 return;
2276
2277 /* Do a first pass to check for locations with no assigned
2278 conditions or conditions that fail to parse to a valid agent expression
2279 bytecode. If any of these happen, then it's no use to send conditions
2280 to the target since this location will always trigger and generate a
2281 response back to GDB. */
2282 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2283 {
2284 loc = (*loc2p);
2285 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2286 {
2287 if (modified)
2288 {
2289 struct agent_expr *aexpr;
2290
2291 /* Re-parse the conditions since something changed. In that
2292 case we already freed the condition bytecodes (see
2293 force_breakpoint_reinsertion). We just
2294 need to parse the condition to bytecodes again. */
2295 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2296 loc->cond_bytecode = aexpr;
2297
2298 /* Check if we managed to parse the conditional expression
2299 correctly. If not, we will not send this condition
2300 to the target. */
2301 if (aexpr)
2302 continue;
2303 }
2304
2305 /* If we have a NULL bytecode expression, it means something
2306 went wrong or we have a null condition expression. */
2307 if (!loc->cond_bytecode)
2308 {
2309 null_condition_or_parse_error = 1;
2310 break;
2311 }
2312 }
2313 }
2314
2315 /* If any of these happened, it means we will have to evaluate the conditions
2316 for the location's address on gdb's side. It is no use keeping bytecodes
2317 for all the other duplicate locations, thus we free all of them here.
2318
2319 This is so we have a finer control over which locations' conditions are
2320 being evaluated by GDB or the remote stub. */
2321 if (null_condition_or_parse_error)
2322 {
2323 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2324 {
2325 loc = (*loc2p);
2326 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2327 {
2328 /* Only go as far as the first NULL bytecode is
2329 located. */
2330 if (!loc->cond_bytecode)
2331 return;
2332
2333 free_agent_expr (loc->cond_bytecode);
2334 loc->cond_bytecode = NULL;
2335 }
2336 }
2337 }
2338
2339 /* No NULL conditions or failed bytecode generation. Build a condition list
2340 for this location's address. */
2341 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2342 {
2343 loc = (*loc2p);
2344 if (loc->cond
2345 && is_breakpoint (loc->owner)
2346 && loc->pspace->num == bl->pspace->num
2347 && loc->owner->enable_state == bp_enabled
2348 && loc->enabled)
2349 /* Add the condition to the vector. This will be used later to send the
2350 conditions to the target. */
2351 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2352 loc->cond_bytecode);
2353 }
2354
2355 return;
2356}
2357
d3ce09f5
SS
2358/* Parses a command described by string CMD into an agent expression
2359 bytecode suitable for evaluation by the bytecode interpreter.
2360 Return NULL if there was any error during parsing. */
2361
2362static struct agent_expr *
2363parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2364{
2365 struct cleanup *old_cleanups = 0;
2366 struct expression *expr, **argvec;
2367 struct agent_expr *aexpr = NULL;
d3ce09f5 2368 volatile struct gdb_exception ex;
bbc13ae3
KS
2369 const char *cmdrest;
2370 const char *format_start, *format_end;
d3ce09f5
SS
2371 struct format_piece *fpieces;
2372 int nargs;
2373 struct gdbarch *gdbarch = get_current_arch ();
2374
2375 if (!cmd)
2376 return NULL;
2377
2378 cmdrest = cmd;
2379
2380 if (*cmdrest == ',')
2381 ++cmdrest;
bbc13ae3 2382 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2383
2384 if (*cmdrest++ != '"')
2385 error (_("No format string following the location"));
2386
2387 format_start = cmdrest;
2388
2389 fpieces = parse_format_string (&cmdrest);
2390
2391 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2392
2393 format_end = cmdrest;
2394
2395 if (*cmdrest++ != '"')
2396 error (_("Bad format string, non-terminated '\"'."));
2397
bbc13ae3 2398 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2399
2400 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2401 error (_("Invalid argument syntax"));
2402
2403 if (*cmdrest == ',')
2404 cmdrest++;
bbc13ae3 2405 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2406
2407 /* For each argument, make an expression. */
2408
2409 argvec = (struct expression **) alloca (strlen (cmd)
2410 * sizeof (struct expression *));
2411
2412 nargs = 0;
2413 while (*cmdrest != '\0')
2414 {
bbc13ae3 2415 const char *cmd1;
d3ce09f5
SS
2416
2417 cmd1 = cmdrest;
2418 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2419 argvec[nargs++] = expr;
2420 cmdrest = cmd1;
2421 if (*cmdrest == ',')
2422 ++cmdrest;
2423 }
2424
2425 /* We don't want to stop processing, so catch any errors
2426 that may show up. */
2427 TRY_CATCH (ex, RETURN_MASK_ERROR)
2428 {
2429 aexpr = gen_printf (scope, gdbarch, 0, 0,
2430 format_start, format_end - format_start,
2431 fpieces, nargs, argvec);
2432 }
2433
752eb8b4
TT
2434 do_cleanups (old_cleanups);
2435
d3ce09f5
SS
2436 if (ex.reason < 0)
2437 {
2438 /* If we got here, it means the command could not be parsed to a valid
2439 bytecode expression and thus can't be evaluated on the target's side.
2440 It's no use iterating through the other commands. */
2441 return NULL;
2442 }
2443
d3ce09f5
SS
2444 /* We have a valid agent expression, return it. */
2445 return aexpr;
2446}
2447
2448/* Based on location BL, create a list of breakpoint commands to be
2449 passed on to the target. If we have duplicated locations with
2450 different commands, we will add any such to the list. */
2451
2452static void
2453build_target_command_list (struct bp_location *bl)
2454{
2455 struct bp_location **locp = NULL, **loc2p;
2456 int null_command_or_parse_error = 0;
2457 int modified = bl->needs_update;
2458 struct bp_location *loc;
2459
8b4f3082
PA
2460 /* Release commands left over from a previous insert. */
2461 VEC_free (agent_expr_p, bl->target_info.tcommands);
2462
41fac0cf 2463 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2464 return;
2465
41fac0cf
PA
2466 /* For now, limit to agent-style dprintf breakpoints. */
2467 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2468 return;
2469
41fac0cf
PA
2470 /* For now, if we have any duplicate location that isn't a dprintf,
2471 don't install the target-side commands, as that would make the
2472 breakpoint not be reported to the core, and we'd lose
2473 control. */
2474 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2475 {
2476 loc = (*loc2p);
2477 if (is_breakpoint (loc->owner)
2478 && loc->pspace->num == bl->pspace->num
2479 && loc->owner->type != bp_dprintf)
2480 return;
2481 }
2482
d3ce09f5
SS
2483 /* Do a first pass to check for locations with no assigned
2484 conditions or conditions that fail to parse to a valid agent expression
2485 bytecode. If any of these happen, then it's no use to send conditions
2486 to the target since this location will always trigger and generate a
2487 response back to GDB. */
2488 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2489 {
2490 loc = (*loc2p);
2491 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2492 {
2493 if (modified)
2494 {
2495 struct agent_expr *aexpr;
2496
2497 /* Re-parse the commands since something changed. In that
2498 case we already freed the command bytecodes (see
2499 force_breakpoint_reinsertion). We just
2500 need to parse the command to bytecodes again. */
2501 aexpr = parse_cmd_to_aexpr (bl->address,
2502 loc->owner->extra_string);
2503 loc->cmd_bytecode = aexpr;
2504
2505 if (!aexpr)
2506 continue;
2507 }
2508
2509 /* If we have a NULL bytecode expression, it means something
2510 went wrong or we have a null command expression. */
2511 if (!loc->cmd_bytecode)
2512 {
2513 null_command_or_parse_error = 1;
2514 break;
2515 }
2516 }
2517 }
2518
2519 /* If anything failed, then we're not doing target-side commands,
2520 and so clean up. */
2521 if (null_command_or_parse_error)
2522 {
2523 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2524 {
2525 loc = (*loc2p);
2526 if (is_breakpoint (loc->owner)
2527 && loc->pspace->num == bl->pspace->num)
2528 {
2529 /* Only go as far as the first NULL bytecode is
2530 located. */
40fb6c5e 2531 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2532 return;
2533
40fb6c5e
HZ
2534 free_agent_expr (loc->cmd_bytecode);
2535 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2536 }
2537 }
2538 }
2539
2540 /* No NULL commands or failed bytecode generation. Build a command list
2541 for this location's address. */
2542 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2543 {
2544 loc = (*loc2p);
2545 if (loc->owner->extra_string
2546 && is_breakpoint (loc->owner)
2547 && loc->pspace->num == bl->pspace->num
2548 && loc->owner->enable_state == bp_enabled
2549 && loc->enabled)
2550 /* Add the command to the vector. This will be used later
2551 to send the commands to the target. */
2552 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2553 loc->cmd_bytecode);
2554 }
2555
2556 bl->target_info.persist = 0;
2557 /* Maybe flag this location as persistent. */
2558 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2559 bl->target_info.persist = 1;
2560}
2561
35df4500
TJB
2562/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2563 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2564 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2565 Returns 0 for success, 1 if the bp_location type is not supported or
2566 -1 for failure.
879bfdc2 2567
4a64f543
MS
2568 NOTE drow/2003-09-09: This routine could be broken down to an
2569 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2570static int
35df4500 2571insert_bp_location (struct bp_location *bl,
26bb91f3 2572 struct ui_file *tmp_error_stream,
3fbb6ffa 2573 int *disabled_breaks,
dd61ec5c
MW
2574 int *hw_breakpoint_error,
2575 int *hw_bp_error_explained_already)
879bfdc2 2576{
0000e5cc
PA
2577 enum errors bp_err = GDB_NO_ERROR;
2578 const char *bp_err_message = NULL;
c90a6fb7 2579 volatile struct gdb_exception e;
879bfdc2 2580
b775012e 2581 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2582 return 0;
2583
35c63cd8
JB
2584 /* Note we don't initialize bl->target_info, as that wipes out
2585 the breakpoint location's shadow_contents if the breakpoint
2586 is still inserted at that location. This in turn breaks
2587 target_read_memory which depends on these buffers when
2588 a memory read is requested at the breakpoint location:
2589 Once the target_info has been wiped, we fail to see that
2590 we have a breakpoint inserted at that address and thus
2591 read the breakpoint instead of returning the data saved in
2592 the breakpoint location's shadow contents. */
0d5ed153 2593 bl->target_info.reqstd_address = bl->address;
35df4500 2594 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2595 bl->target_info.length = bl->length;
8181d85f 2596
b775012e
LM
2597 /* When working with target-side conditions, we must pass all the conditions
2598 for the same breakpoint address down to the target since GDB will not
2599 insert those locations. With a list of breakpoint conditions, the target
2600 can decide when to stop and notify GDB. */
2601
2602 if (is_breakpoint (bl->owner))
2603 {
2604 build_target_condition_list (bl);
d3ce09f5
SS
2605 build_target_command_list (bl);
2606 /* Reset the modification marker. */
b775012e
LM
2607 bl->needs_update = 0;
2608 }
2609
35df4500
TJB
2610 if (bl->loc_type == bp_loc_software_breakpoint
2611 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2612 {
35df4500 2613 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2614 {
2615 /* If the explicitly specified breakpoint type
2616 is not hardware breakpoint, check the memory map to see
2617 if the breakpoint address is in read only memory or not.
4a64f543 2618
765dc015
VP
2619 Two important cases are:
2620 - location type is not hardware breakpoint, memory
2621 is readonly. We change the type of the location to
2622 hardware breakpoint.
4a64f543
MS
2623 - location type is hardware breakpoint, memory is
2624 read-write. This means we've previously made the
2625 location hardware one, but then the memory map changed,
2626 so we undo.
765dc015 2627
4a64f543
MS
2628 When breakpoints are removed, remove_breakpoints will use
2629 location types we've just set here, the only possible
2630 problem is that memory map has changed during running
2631 program, but it's not going to work anyway with current
2632 gdb. */
765dc015 2633 struct mem_region *mr
0d5ed153 2634 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2635
2636 if (mr)
2637 {
2638 if (automatic_hardware_breakpoints)
2639 {
765dc015
VP
2640 enum bp_loc_type new_type;
2641
2642 if (mr->attrib.mode != MEM_RW)
2643 new_type = bp_loc_hardware_breakpoint;
2644 else
2645 new_type = bp_loc_software_breakpoint;
2646
35df4500 2647 if (new_type != bl->loc_type)
765dc015
VP
2648 {
2649 static int said = 0;
cc59ec59 2650
35df4500 2651 bl->loc_type = new_type;
765dc015
VP
2652 if (!said)
2653 {
3e43a32a
MS
2654 fprintf_filtered (gdb_stdout,
2655 _("Note: automatically using "
2656 "hardware breakpoints for "
2657 "read-only addresses.\n"));
765dc015
VP
2658 said = 1;
2659 }
2660 }
2661 }
35df4500 2662 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2663 && mr->attrib.mode != MEM_RW)
2664 {
2665 fprintf_unfiltered (tmp_error_stream,
2666 _("Cannot insert breakpoint %d.\n"
2667 "Cannot set software breakpoint "
2668 "at read-only address %s\n"),
2669 bl->owner->number,
2670 paddress (bl->gdbarch, bl->address));
2671 return 1;
2672 }
765dc015
VP
2673 }
2674 }
2675
879bfdc2
DJ
2676 /* First check to see if we have to handle an overlay. */
2677 if (overlay_debugging == ovly_off
35df4500
TJB
2678 || bl->section == NULL
2679 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2680 {
2681 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2682 TRY_CATCH (e, RETURN_MASK_ALL)
2683 {
0000e5cc
PA
2684 int val;
2685
dd61ec5c 2686 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2687 if (val)
2688 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2689 }
2690 if (e.reason < 0)
2691 {
0000e5cc
PA
2692 bp_err = e.error;
2693 bp_err_message = e.message;
dd61ec5c 2694 }
879bfdc2
DJ
2695 }
2696 else
2697 {
4a64f543 2698 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2699 Shall we set a breakpoint at the LMA? */
2700 if (!overlay_events_enabled)
2701 {
2702 /* Yes -- overlay event support is not active,
2703 so we must try to set a breakpoint at the LMA.
2704 This will not work for a hardware breakpoint. */
35df4500 2705 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2706 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2707 bl->owner->number);
879bfdc2
DJ
2708 else
2709 {
35df4500
TJB
2710 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2711 bl->section);
879bfdc2 2712 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2713 bl->overlay_target_info = bl->target_info;
0d5ed153 2714 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2715
2716 /* No overlay handling: just set the breakpoint. */
2717 TRY_CATCH (e, RETURN_MASK_ALL)
2718 {
2719 int val;
2720
2721 val = target_insert_breakpoint (bl->gdbarch,
2722 &bl->overlay_target_info);
2723 if (val)
2724 bp_err = GENERIC_ERROR;
2725 }
2726 if (e.reason < 0)
2727 {
2728 bp_err = e.error;
2729 bp_err_message = e.message;
2730 }
2731
2732 if (bp_err != GDB_NO_ERROR)
99361f52 2733 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2734 "Overlay breakpoint %d "
2735 "failed: in ROM?\n",
35df4500 2736 bl->owner->number);
879bfdc2
DJ
2737 }
2738 }
2739 /* Shall we set a breakpoint at the VMA? */
35df4500 2740 if (section_is_mapped (bl->section))
879bfdc2
DJ
2741 {
2742 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2743 TRY_CATCH (e, RETURN_MASK_ALL)
2744 {
0000e5cc
PA
2745 int val;
2746
dd61ec5c 2747 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2748 if (val)
2749 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2750 }
2751 if (e.reason < 0)
2752 {
0000e5cc
PA
2753 bp_err = e.error;
2754 bp_err_message = e.message;
dd61ec5c 2755 }
879bfdc2
DJ
2756 }
2757 else
2758 {
2759 /* No. This breakpoint will not be inserted.
2760 No error, but do not mark the bp as 'inserted'. */
2761 return 0;
2762 }
2763 }
2764
0000e5cc 2765 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2766 {
2767 /* Can't set the breakpoint. */
0000e5cc
PA
2768
2769 /* In some cases, we might not be able to insert a
2770 breakpoint in a shared library that has already been
2771 removed, but we have not yet processed the shlib unload
2772 event. Unfortunately, some targets that implement
076855f9
PA
2773 breakpoint insertion themselves can't tell why the
2774 breakpoint insertion failed (e.g., the remote target
2775 doesn't define error codes), so we must treat generic
2776 errors as memory errors. */
0000e5cc 2777 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2778 && bl->loc_type == bp_loc_software_breakpoint
08351840 2779 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2780 || shared_objfile_contains_address_p (bl->pspace,
2781 bl->address)))
879bfdc2 2782 {
4a64f543 2783 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2784 bl->shlib_disabled = 1;
8d3788bd 2785 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2786 if (!*disabled_breaks)
2787 {
2788 fprintf_unfiltered (tmp_error_stream,
2789 "Cannot insert breakpoint %d.\n",
2790 bl->owner->number);
2791 fprintf_unfiltered (tmp_error_stream,
2792 "Temporarily disabling shared "
2793 "library breakpoints:\n");
2794 }
2795 *disabled_breaks = 1;
879bfdc2 2796 fprintf_unfiltered (tmp_error_stream,
35df4500 2797 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2798 return 0;
879bfdc2
DJ
2799 }
2800 else
879bfdc2 2801 {
35df4500 2802 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2803 {
0000e5cc
PA
2804 *hw_breakpoint_error = 1;
2805 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2806 fprintf_unfiltered (tmp_error_stream,
2807 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2808 bl->owner->number, bp_err_message ? ":" : ".\n");
2809 if (bp_err_message != NULL)
2810 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2811 }
2812 else
2813 {
0000e5cc
PA
2814 if (bp_err_message == NULL)
2815 {
2816 char *message
2817 = memory_error_message (TARGET_XFER_E_IO,
2818 bl->gdbarch, bl->address);
2819 struct cleanup *old_chain = make_cleanup (xfree, message);
2820
2821 fprintf_unfiltered (tmp_error_stream,
2822 "Cannot insert breakpoint %d.\n"
2823 "%s\n",
2824 bl->owner->number, message);
2825 do_cleanups (old_chain);
2826 }
2827 else
2828 {
2829 fprintf_unfiltered (tmp_error_stream,
2830 "Cannot insert breakpoint %d: %s\n",
2831 bl->owner->number,
2832 bp_err_message);
2833 }
879bfdc2 2834 }
0000e5cc 2835 return 1;
879bfdc2
DJ
2836
2837 }
2838 }
2839 else
35df4500 2840 bl->inserted = 1;
879bfdc2 2841
0000e5cc 2842 return 0;
879bfdc2
DJ
2843 }
2844
35df4500 2845 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2846 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2847 watchpoints. It's not clear that it's necessary... */
35df4500 2848 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2849 {
0000e5cc
PA
2850 int val;
2851
77b06cd7
TJB
2852 gdb_assert (bl->owner->ops != NULL
2853 && bl->owner->ops->insert_location != NULL);
2854
2855 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2856
2857 /* If trying to set a read-watchpoint, and it turns out it's not
2858 supported, try emulating one with an access watchpoint. */
35df4500 2859 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2860 {
2861 struct bp_location *loc, **loc_temp;
2862
2863 /* But don't try to insert it, if there's already another
2864 hw_access location that would be considered a duplicate
2865 of this one. */
2866 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2867 if (loc != bl
85d721b8 2868 && loc->watchpoint_type == hw_access
35df4500 2869 && watchpoint_locations_match (bl, loc))
85d721b8 2870 {
35df4500
TJB
2871 bl->duplicate = 1;
2872 bl->inserted = 1;
2873 bl->target_info = loc->target_info;
2874 bl->watchpoint_type = hw_access;
85d721b8
PA
2875 val = 0;
2876 break;
2877 }
2878
2879 if (val == 1)
2880 {
77b06cd7
TJB
2881 bl->watchpoint_type = hw_access;
2882 val = bl->owner->ops->insert_location (bl);
2883
2884 if (val)
2885 /* Back to the original value. */
2886 bl->watchpoint_type = hw_read;
85d721b8
PA
2887 }
2888 }
2889
35df4500 2890 bl->inserted = (val == 0);
879bfdc2
DJ
2891 }
2892
35df4500 2893 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2894 {
0000e5cc
PA
2895 int val;
2896
77b06cd7
TJB
2897 gdb_assert (bl->owner->ops != NULL
2898 && bl->owner->ops->insert_location != NULL);
2899
2900 val = bl->owner->ops->insert_location (bl);
2901 if (val)
2902 {
2903 bl->owner->enable_state = bp_disabled;
2904
2905 if (val == 1)
2906 warning (_("\
2907Error inserting catchpoint %d: Your system does not support this type\n\
2908of catchpoint."), bl->owner->number);
2909 else
2910 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2911 }
2912
2913 bl->inserted = (val == 0);
1640b821
DJ
2914
2915 /* We've already printed an error message if there was a problem
2916 inserting this catchpoint, and we've disabled the catchpoint,
2917 so just return success. */
2918 return 0;
879bfdc2
DJ
2919 }
2920
2921 return 0;
2922}
2923
6c95b8df
PA
2924/* This function is called when program space PSPACE is about to be
2925 deleted. It takes care of updating breakpoints to not reference
2926 PSPACE anymore. */
2927
2928void
2929breakpoint_program_space_exit (struct program_space *pspace)
2930{
2931 struct breakpoint *b, *b_temp;
876fa593 2932 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2933
2934 /* Remove any breakpoint that was set through this program space. */
2935 ALL_BREAKPOINTS_SAFE (b, b_temp)
2936 {
2937 if (b->pspace == pspace)
2938 delete_breakpoint (b);
2939 }
2940
2941 /* Breakpoints set through other program spaces could have locations
2942 bound to PSPACE as well. Remove those. */
876fa593 2943 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2944 {
2945 struct bp_location *tmp;
2946
2947 if (loc->pspace == pspace)
2948 {
2bdf28a0 2949 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2950 if (loc->owner->loc == loc)
2951 loc->owner->loc = loc->next;
2952 else
2953 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2954 if (tmp->next == loc)
2955 {
2956 tmp->next = loc->next;
2957 break;
2958 }
2959 }
2960 }
2961
2962 /* Now update the global location list to permanently delete the
2963 removed locations above. */
44702360 2964 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2965}
2966
74960c60
VP
2967/* Make sure all breakpoints are inserted in inferior.
2968 Throws exception on any error.
2969 A breakpoint that is already inserted won't be inserted
2970 again, so calling this function twice is safe. */
2971void
2972insert_breakpoints (void)
2973{
2974 struct breakpoint *bpt;
2975
2976 ALL_BREAKPOINTS (bpt)
2977 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2978 {
2979 struct watchpoint *w = (struct watchpoint *) bpt;
2980
2981 update_watchpoint (w, 0 /* don't reparse. */);
2982 }
74960c60 2983
04086b45
PA
2984 /* Updating watchpoints creates new locations, so update the global
2985 location list. Explicitly tell ugll to insert locations and
2986 ignore breakpoints_always_inserted_mode. */
2987 update_global_location_list (UGLL_INSERT);
74960c60
VP
2988}
2989
20388dd6
YQ
2990/* Invoke CALLBACK for each of bp_location. */
2991
2992void
2993iterate_over_bp_locations (walk_bp_location_callback callback)
2994{
2995 struct bp_location *loc, **loc_tmp;
2996
2997 ALL_BP_LOCATIONS (loc, loc_tmp)
2998 {
2999 callback (loc, NULL);
3000 }
3001}
3002
b775012e
LM
3003/* This is used when we need to synch breakpoint conditions between GDB and the
3004 target. It is the case with deleting and disabling of breakpoints when using
3005 always-inserted mode. */
3006
3007static void
3008update_inserted_breakpoint_locations (void)
3009{
3010 struct bp_location *bl, **blp_tmp;
3011 int error_flag = 0;
3012 int val = 0;
3013 int disabled_breaks = 0;
3014 int hw_breakpoint_error = 0;
dd61ec5c 3015 int hw_bp_details_reported = 0;
b775012e
LM
3016
3017 struct ui_file *tmp_error_stream = mem_fileopen ();
3018 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3019
3020 /* Explicitly mark the warning -- this will only be printed if
3021 there was an error. */
3022 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3023
3024 save_current_space_and_thread ();
3025
3026 ALL_BP_LOCATIONS (bl, blp_tmp)
3027 {
3028 /* We only want to update software breakpoints and hardware
3029 breakpoints. */
3030 if (!is_breakpoint (bl->owner))
3031 continue;
3032
3033 /* We only want to update locations that are already inserted
3034 and need updating. This is to avoid unwanted insertion during
3035 deletion of breakpoints. */
3036 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3037 continue;
3038
3039 switch_to_program_space_and_thread (bl->pspace);
3040
3041 /* For targets that support global breakpoints, there's no need
3042 to select an inferior to insert breakpoint to. In fact, even
3043 if we aren't attached to any process yet, we should still
3044 insert breakpoints. */
f5656ead 3045 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3046 && ptid_equal (inferior_ptid, null_ptid))
3047 continue;
3048
3049 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3050 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3051 if (val)
3052 error_flag = val;
3053 }
3054
3055 if (error_flag)
3056 {
3057 target_terminal_ours_for_output ();
3058 error_stream (tmp_error_stream);
3059 }
3060
3061 do_cleanups (cleanups);
3062}
3063
c30eee59 3064/* Used when starting or continuing the program. */
c906108c 3065
74960c60
VP
3066static void
3067insert_breakpoint_locations (void)
c906108c 3068{
a5606eee 3069 struct breakpoint *bpt;
35df4500 3070 struct bp_location *bl, **blp_tmp;
eacd795a 3071 int error_flag = 0;
c906108c 3072 int val = 0;
3fbb6ffa 3073 int disabled_breaks = 0;
81d0cc19 3074 int hw_breakpoint_error = 0;
dd61ec5c 3075 int hw_bp_error_explained_already = 0;
c906108c 3076
81d0cc19 3077 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3078 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3079
81d0cc19
GS
3080 /* Explicitly mark the warning -- this will only be printed if
3081 there was an error. */
3082 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3083
3084 save_current_space_and_thread ();
3085
35df4500 3086 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3087 {
b775012e 3088 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3089 continue;
3090
4a64f543
MS
3091 /* There is no point inserting thread-specific breakpoints if
3092 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3093 has BL->OWNER always non-NULL. */
35df4500
TJB
3094 if (bl->owner->thread != -1
3095 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3096 continue;
3097
35df4500 3098 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3099
3100 /* For targets that support global breakpoints, there's no need
3101 to select an inferior to insert breakpoint to. In fact, even
3102 if we aren't attached to any process yet, we should still
3103 insert breakpoints. */
f5656ead 3104 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3105 && ptid_equal (inferior_ptid, null_ptid))
3106 continue;
3107
3fbb6ffa 3108 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3109 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3110 if (val)
eacd795a 3111 error_flag = val;
879bfdc2 3112 }
c906108c 3113
4a64f543
MS
3114 /* If we failed to insert all locations of a watchpoint, remove
3115 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3116 ALL_BREAKPOINTS (bpt)
3117 {
3118 int some_failed = 0;
3119 struct bp_location *loc;
3120
3121 if (!is_hardware_watchpoint (bpt))
3122 continue;
3123
d6b74ac4 3124 if (!breakpoint_enabled (bpt))
a5606eee 3125 continue;
74960c60
VP
3126
3127 if (bpt->disposition == disp_del_at_next_stop)
3128 continue;
a5606eee
VP
3129
3130 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3131 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3132 {
3133 some_failed = 1;
3134 break;
3135 }
3136 if (some_failed)
3137 {
3138 for (loc = bpt->loc; loc; loc = loc->next)
3139 if (loc->inserted)
3140 remove_breakpoint (loc, mark_uninserted);
3141
3142 hw_breakpoint_error = 1;
3143 fprintf_unfiltered (tmp_error_stream,
3144 "Could not insert hardware watchpoint %d.\n",
3145 bpt->number);
eacd795a 3146 error_flag = -1;
a5606eee
VP
3147 }
3148 }
3149
eacd795a 3150 if (error_flag)
81d0cc19
GS
3151 {
3152 /* If a hardware breakpoint or watchpoint was inserted, add a
3153 message about possibly exhausted resources. */
dd61ec5c 3154 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3155 {
c6510018
MS
3156 fprintf_unfiltered (tmp_error_stream,
3157 "Could not insert hardware breakpoints:\n\
3158You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3159 }
81d0cc19
GS
3160 target_terminal_ours_for_output ();
3161 error_stream (tmp_error_stream);
3162 }
f7545552
TT
3163
3164 do_cleanups (cleanups);
c906108c
SS
3165}
3166
c30eee59
TJB
3167/* Used when the program stops.
3168 Returns zero if successful, or non-zero if there was a problem
3169 removing a breakpoint location. */
3170
c906108c 3171int
fba45db2 3172remove_breakpoints (void)
c906108c 3173{
35df4500 3174 struct bp_location *bl, **blp_tmp;
3a1bae8e 3175 int val = 0;
c906108c 3176
35df4500 3177 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3178 {
1e4d1764 3179 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3180 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3181 }
3a1bae8e 3182 return val;
c906108c
SS
3183}
3184
49fa26b0
PA
3185/* When a thread exits, remove breakpoints that are related to
3186 that thread. */
3187
3188static void
3189remove_threaded_breakpoints (struct thread_info *tp, int silent)
3190{
3191 struct breakpoint *b, *b_tmp;
3192
3193 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3194 {
96181529 3195 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3196 {
3197 b->disposition = disp_del_at_next_stop;
3198
3199 printf_filtered (_("\
46ecd527 3200Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3201 b->number, tp->num);
3202
3203 /* Hide it from the user. */
3204 b->number = 0;
3205 }
3206 }
3207}
3208
6c95b8df
PA
3209/* Remove breakpoints of process PID. */
3210
3211int
3212remove_breakpoints_pid (int pid)
3213{
35df4500 3214 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3215 int val;
3216 struct inferior *inf = find_inferior_pid (pid);
3217
35df4500 3218 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3219 {
35df4500 3220 if (bl->pspace != inf->pspace)
6c95b8df
PA
3221 continue;
3222
d3ce09f5
SS
3223 if (bl->owner->type == bp_dprintf)
3224 continue;
3225
35df4500 3226 if (bl->inserted)
6c95b8df 3227 {
35df4500 3228 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3229 if (val != 0)
3230 return val;
3231 }
3232 }
3233 return 0;
3234}
3235
c906108c 3236int
fba45db2 3237reattach_breakpoints (int pid)
c906108c 3238{
6c95b8df 3239 struct cleanup *old_chain;
35df4500 3240 struct bp_location *bl, **blp_tmp;
c906108c 3241 int val;
86b887df 3242 struct ui_file *tmp_error_stream;
dd61ec5c 3243 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3244 struct inferior *inf;
3245 struct thread_info *tp;
3246
3247 tp = any_live_thread_of_process (pid);
3248 if (tp == NULL)
3249 return 1;
3250
3251 inf = find_inferior_pid (pid);
3252 old_chain = save_inferior_ptid ();
3253
3254 inferior_ptid = tp->ptid;
a4954f26 3255
86b887df 3256 tmp_error_stream = mem_fileopen ();
a4954f26 3257 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3258
35df4500 3259 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3260 {
35df4500 3261 if (bl->pspace != inf->pspace)
6c95b8df
PA
3262 continue;
3263
35df4500 3264 if (bl->inserted)
c5aa993b 3265 {
35df4500 3266 bl->inserted = 0;
dd61ec5c 3267 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3268 if (val != 0)
3269 {
ce696e05 3270 do_cleanups (old_chain);
c5aa993b
JM
3271 return val;
3272 }
3273 }
3274 }
ce696e05 3275 do_cleanups (old_chain);
c906108c
SS
3276 return 0;
3277}
3278
e58b0e63
PA
3279static int internal_breakpoint_number = -1;
3280
84f4c1fe
PM
3281/* Set the breakpoint number of B, depending on the value of INTERNAL.
3282 If INTERNAL is non-zero, the breakpoint number will be populated
3283 from internal_breakpoint_number and that variable decremented.
e5dd4106 3284 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3285 breakpoint_count and that value incremented. Internal breakpoints
3286 do not set the internal var bpnum. */
3287static void
3288set_breakpoint_number (int internal, struct breakpoint *b)
3289{
3290 if (internal)
3291 b->number = internal_breakpoint_number--;
3292 else
3293 {
3294 set_breakpoint_count (breakpoint_count + 1);
3295 b->number = breakpoint_count;
3296 }
3297}
3298
e62c965a 3299static struct breakpoint *
a6d9a66e 3300create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3301 CORE_ADDR address, enum bptype type,
c0a91b2b 3302 const struct breakpoint_ops *ops)
e62c965a 3303{
e62c965a
PP
3304 struct symtab_and_line sal;
3305 struct breakpoint *b;
3306
4a64f543 3307 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3308
3309 sal.pc = address;
3310 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3311 sal.pspace = current_program_space;
e62c965a 3312
06edf0c0 3313 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3314 b->number = internal_breakpoint_number--;
3315 b->disposition = disp_donttouch;
3316
3317 return b;
3318}
3319
17450429
PP
3320static const char *const longjmp_names[] =
3321 {
3322 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3323 };
3324#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3325
3326/* Per-objfile data private to breakpoint.c. */
3327struct breakpoint_objfile_data
3328{
3329 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3330 struct bound_minimal_symbol overlay_msym;
17450429
PP
3331
3332 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3333 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3334
28106bc2
SDJ
3335 /* True if we have looked for longjmp probes. */
3336 int longjmp_searched;
3337
3338 /* SystemTap probe points for longjmp (if any). */
3339 VEC (probe_p) *longjmp_probes;
3340
17450429 3341 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3342 struct bound_minimal_symbol terminate_msym;
17450429
PP
3343
3344 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3345 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3346
3347 /* True if we have looked for exception probes. */
3348 int exception_searched;
3349
3350 /* SystemTap probe points for unwinding (if any). */
3351 VEC (probe_p) *exception_probes;
17450429
PP
3352};
3353
3354static const struct objfile_data *breakpoint_objfile_key;
3355
3356/* Minimal symbol not found sentinel. */
3357static struct minimal_symbol msym_not_found;
3358
3359/* Returns TRUE if MSYM point to the "not found" sentinel. */
3360
3361static int
3362msym_not_found_p (const struct minimal_symbol *msym)
3363{
3364 return msym == &msym_not_found;
3365}
3366
3367/* Return per-objfile data needed by breakpoint.c.
3368 Allocate the data if necessary. */
3369
3370static struct breakpoint_objfile_data *
3371get_breakpoint_objfile_data (struct objfile *objfile)
3372{
3373 struct breakpoint_objfile_data *bp_objfile_data;
3374
3375 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3376 if (bp_objfile_data == NULL)
3377 {
3378 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3379 sizeof (*bp_objfile_data));
3380
3381 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3382 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3383 }
3384 return bp_objfile_data;
3385}
3386
28106bc2
SDJ
3387static void
3388free_breakpoint_probes (struct objfile *obj, void *data)
3389{
3390 struct breakpoint_objfile_data *bp_objfile_data = data;
3391
3392 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3393 VEC_free (probe_p, bp_objfile_data->exception_probes);
3394}
3395
e62c965a 3396static void
af02033e 3397create_overlay_event_breakpoint (void)
e62c965a 3398{
69de3c6a 3399 struct objfile *objfile;
af02033e 3400 const char *const func_name = "_ovly_debug_event";
e62c965a 3401
69de3c6a
PP
3402 ALL_OBJFILES (objfile)
3403 {
3404 struct breakpoint *b;
17450429
PP
3405 struct breakpoint_objfile_data *bp_objfile_data;
3406 CORE_ADDR addr;
69de3c6a 3407
17450429
PP
3408 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3409
3b7344d5 3410 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3411 continue;
3412
3b7344d5 3413 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3414 {
3b7344d5 3415 struct bound_minimal_symbol m;
17450429
PP
3416
3417 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3418 if (m.minsym == NULL)
17450429
PP
3419 {
3420 /* Avoid future lookups in this objfile. */
3b7344d5 3421 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3422 continue;
3423 }
3424 bp_objfile_data->overlay_msym = m;
3425 }
e62c965a 3426
77e371c0 3427 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3428 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3429 bp_overlay_event,
3430 &internal_breakpoint_ops);
69de3c6a 3431 b->addr_string = xstrdup (func_name);
e62c965a 3432
69de3c6a
PP
3433 if (overlay_debugging == ovly_auto)
3434 {
3435 b->enable_state = bp_enabled;
3436 overlay_events_enabled = 1;
3437 }
3438 else
3439 {
3440 b->enable_state = bp_disabled;
3441 overlay_events_enabled = 0;
3442 }
e62c965a 3443 }
44702360 3444 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3445}
3446
0fd8e87f 3447static void
af02033e 3448create_longjmp_master_breakpoint (void)
0fd8e87f 3449{
6c95b8df 3450 struct program_space *pspace;
6c95b8df
PA
3451 struct cleanup *old_chain;
3452
3453 old_chain = save_current_program_space ();
0fd8e87f 3454
6c95b8df 3455 ALL_PSPACES (pspace)
af02033e
PP
3456 {
3457 struct objfile *objfile;
3458
3459 set_current_program_space (pspace);
3460
3461 ALL_OBJFILES (objfile)
0fd8e87f 3462 {
af02033e
PP
3463 int i;
3464 struct gdbarch *gdbarch;
17450429 3465 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3466
af02033e 3467 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3468
17450429
PP
3469 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3470
28106bc2
SDJ
3471 if (!bp_objfile_data->longjmp_searched)
3472 {
25f9533e
SDJ
3473 VEC (probe_p) *ret;
3474
3475 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3476 if (ret != NULL)
3477 {
3478 /* We are only interested in checking one element. */
3479 struct probe *p = VEC_index (probe_p, ret, 0);
3480
3481 if (!can_evaluate_probe_arguments (p))
3482 {
3483 /* We cannot use the probe interface here, because it does
3484 not know how to evaluate arguments. */
3485 VEC_free (probe_p, ret);
3486 ret = NULL;
3487 }
3488 }
3489 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3490 bp_objfile_data->longjmp_searched = 1;
3491 }
3492
3493 if (bp_objfile_data->longjmp_probes != NULL)
3494 {
3495 int i;
3496 struct probe *probe;
3497 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3498
3499 for (i = 0;
3500 VEC_iterate (probe_p,
3501 bp_objfile_data->longjmp_probes,
3502 i, probe);
3503 ++i)
3504 {
3505 struct breakpoint *b;
3506
729662a5
TT
3507 b = create_internal_breakpoint (gdbarch,
3508 get_probe_address (probe,
3509 objfile),
28106bc2
SDJ
3510 bp_longjmp_master,
3511 &internal_breakpoint_ops);
3512 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3513 b->enable_state = bp_disabled;
3514 }
3515
3516 continue;
3517 }
3518
0569175e
TSD
3519 if (!gdbarch_get_longjmp_target_p (gdbarch))
3520 continue;
3521
17450429 3522 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3523 {
3524 struct breakpoint *b;
af02033e 3525 const char *func_name;
17450429 3526 CORE_ADDR addr;
6c95b8df 3527
3b7344d5 3528 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3529 continue;
0fd8e87f 3530
17450429 3531 func_name = longjmp_names[i];
3b7344d5 3532 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3533 {
3b7344d5 3534 struct bound_minimal_symbol m;
17450429
PP
3535
3536 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3537 if (m.minsym == NULL)
17450429
PP
3538 {
3539 /* Prevent future lookups in this objfile. */
3b7344d5 3540 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3541 continue;
3542 }
3543 bp_objfile_data->longjmp_msym[i] = m;
3544 }
3545
77e371c0 3546 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3547 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3548 &internal_breakpoint_ops);
af02033e
PP
3549 b->addr_string = xstrdup (func_name);
3550 b->enable_state = bp_disabled;
3551 }
0fd8e87f 3552 }
af02033e 3553 }
44702360 3554 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3555
3556 do_cleanups (old_chain);
0fd8e87f
UW
3557}
3558
af02033e 3559/* Create a master std::terminate breakpoint. */
aa7d318d 3560static void
af02033e 3561create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3562{
3563 struct program_space *pspace;
aa7d318d 3564 struct cleanup *old_chain;
af02033e 3565 const char *const func_name = "std::terminate()";
aa7d318d
TT
3566
3567 old_chain = save_current_program_space ();
3568
3569 ALL_PSPACES (pspace)
17450429
PP
3570 {
3571 struct objfile *objfile;
3572 CORE_ADDR addr;
3573
3574 set_current_program_space (pspace);
3575
aa7d318d
TT
3576 ALL_OBJFILES (objfile)
3577 {
3578 struct breakpoint *b;
17450429 3579 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3580
17450429 3581 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3582
3b7344d5 3583 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3584 continue;
3585
3b7344d5 3586 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3587 {
3b7344d5 3588 struct bound_minimal_symbol m;
17450429
PP
3589
3590 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3591 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3592 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3593 {
3594 /* Prevent future lookups in this objfile. */
3b7344d5 3595 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3596 continue;
3597 }
3598 bp_objfile_data->terminate_msym = m;
3599 }
aa7d318d 3600
77e371c0 3601 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3602 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3603 bp_std_terminate_master,
3604 &internal_breakpoint_ops);
aa7d318d
TT
3605 b->addr_string = xstrdup (func_name);
3606 b->enable_state = bp_disabled;
3607 }
17450429
PP
3608 }
3609
44702360 3610 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3611
3612 do_cleanups (old_chain);
3613}
3614
186c406b
TT
3615/* Install a master breakpoint on the unwinder's debug hook. */
3616
70221824 3617static void
186c406b
TT
3618create_exception_master_breakpoint (void)
3619{
3620 struct objfile *objfile;
17450429 3621 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3622
3623 ALL_OBJFILES (objfile)
3624 {
17450429
PP
3625 struct breakpoint *b;
3626 struct gdbarch *gdbarch;
3627 struct breakpoint_objfile_data *bp_objfile_data;
3628 CORE_ADDR addr;
3629
3630 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3631
28106bc2
SDJ
3632 /* We prefer the SystemTap probe point if it exists. */
3633 if (!bp_objfile_data->exception_searched)
3634 {
25f9533e
SDJ
3635 VEC (probe_p) *ret;
3636
3637 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3638
3639 if (ret != NULL)
3640 {
3641 /* We are only interested in checking one element. */
3642 struct probe *p = VEC_index (probe_p, ret, 0);
3643
3644 if (!can_evaluate_probe_arguments (p))
3645 {
3646 /* We cannot use the probe interface here, because it does
3647 not know how to evaluate arguments. */
3648 VEC_free (probe_p, ret);
3649 ret = NULL;
3650 }
3651 }
3652 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3653 bp_objfile_data->exception_searched = 1;
3654 }
3655
3656 if (bp_objfile_data->exception_probes != NULL)
3657 {
3658 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3659 int i;
3660 struct probe *probe;
3661
3662 for (i = 0;
3663 VEC_iterate (probe_p,
3664 bp_objfile_data->exception_probes,
3665 i, probe);
3666 ++i)
3667 {
3668 struct breakpoint *b;
3669
729662a5
TT
3670 b = create_internal_breakpoint (gdbarch,
3671 get_probe_address (probe,
3672 objfile),
28106bc2
SDJ
3673 bp_exception_master,
3674 &internal_breakpoint_ops);
3675 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3676 b->enable_state = bp_disabled;
3677 }
3678
3679 continue;
3680 }
3681
3682 /* Otherwise, try the hook function. */
3683
3b7344d5 3684 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3685 continue;
3686
3687 gdbarch = get_objfile_arch (objfile);
186c406b 3688
3b7344d5 3689 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3690 {
3b7344d5 3691 struct bound_minimal_symbol debug_hook;
186c406b 3692
17450429 3693 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3694 if (debug_hook.minsym == NULL)
17450429 3695 {
3b7344d5 3696 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3697 continue;
3698 }
3699
3700 bp_objfile_data->exception_msym = debug_hook;
186c406b 3701 }
17450429 3702
77e371c0 3703 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3704 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3705 &current_target);
06edf0c0
PA
3706 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3707 &internal_breakpoint_ops);
17450429
PP
3708 b->addr_string = xstrdup (func_name);
3709 b->enable_state = bp_disabled;
186c406b
TT
3710 }
3711
44702360 3712 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3713}
3714
c906108c 3715void
fba45db2 3716update_breakpoints_after_exec (void)
c906108c 3717{
35df4500 3718 struct breakpoint *b, *b_tmp;
876fa593 3719 struct bp_location *bploc, **bplocp_tmp;
c906108c 3720
25b22b0a
PA
3721 /* We're about to delete breakpoints from GDB's lists. If the
3722 INSERTED flag is true, GDB will try to lift the breakpoints by
3723 writing the breakpoints' "shadow contents" back into memory. The
3724 "shadow contents" are NOT valid after an exec, so GDB should not
3725 do that. Instead, the target is responsible from marking
3726 breakpoints out as soon as it detects an exec. We don't do that
3727 here instead, because there may be other attempts to delete
3728 breakpoints after detecting an exec and before reaching here. */
876fa593 3729 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3730 if (bploc->pspace == current_program_space)
3731 gdb_assert (!bploc->inserted);
c906108c 3732
35df4500 3733 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3734 {
6c95b8df
PA
3735 if (b->pspace != current_program_space)
3736 continue;
3737
4a64f543 3738 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3739 if (b->type == bp_shlib_event)
3740 {
3741 delete_breakpoint (b);
3742 continue;
3743 }
c906108c 3744
4a64f543 3745 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3746 if (b->type == bp_jit_event)
3747 {
3748 delete_breakpoint (b);
3749 continue;
3750 }
3751
1900040c 3752 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3753 as must overlay event and longjmp master breakpoints. */
3754 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3755 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3756 || b->type == bp_exception_master)
c4093a6a
JM
3757 {
3758 delete_breakpoint (b);
3759 continue;
3760 }
3761
4a64f543 3762 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3763 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3764 {
3765 delete_breakpoint (b);
3766 continue;
3767 }
3768
7c16b83e
PA
3769 /* Just like single-step breakpoints. */
3770 if (b->type == bp_single_step)
3771 {
3772 delete_breakpoint (b);
3773 continue;
3774 }
3775
611c83ae
PA
3776 /* Longjmp and longjmp-resume breakpoints are also meaningless
3777 after an exec. */
186c406b 3778 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3779 || b->type == bp_longjmp_call_dummy
186c406b 3780 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3781 {
3782 delete_breakpoint (b);
3783 continue;
3784 }
3785
ce78b96d
JB
3786 if (b->type == bp_catchpoint)
3787 {
3788 /* For now, none of the bp_catchpoint breakpoints need to
3789 do anything at this point. In the future, if some of
3790 the catchpoints need to something, we will need to add
3791 a new method, and call this method from here. */
3792 continue;
3793 }
3794
c5aa993b
JM
3795 /* bp_finish is a special case. The only way we ought to be able
3796 to see one of these when an exec() has happened, is if the user
3797 caught a vfork, and then said "finish". Ordinarily a finish just
3798 carries them to the call-site of the current callee, by setting
3799 a temporary bp there and resuming. But in this case, the finish
3800 will carry them entirely through the vfork & exec.
3801
3802 We don't want to allow a bp_finish to remain inserted now. But
3803 we can't safely delete it, 'cause finish_command has a handle to
3804 the bp on a bpstat, and will later want to delete it. There's a
3805 chance (and I've seen it happen) that if we delete the bp_finish
3806 here, that its storage will get reused by the time finish_command
3807 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3808 We really must allow finish_command to delete a bp_finish.
3809
e5dd4106 3810 In the absence of a general solution for the "how do we know
53a5351d
JM
3811 it's safe to delete something others may have handles to?"
3812 problem, what we'll do here is just uninsert the bp_finish, and
3813 let finish_command delete it.
3814
3815 (We know the bp_finish is "doomed" in the sense that it's
3816 momentary, and will be deleted as soon as finish_command sees
3817 the inferior stopped. So it doesn't matter that the bp's
3818 address is probably bogus in the new a.out, unlike e.g., the
3819 solib breakpoints.) */
c5aa993b 3820
c5aa993b
JM
3821 if (b->type == bp_finish)
3822 {
3823 continue;
3824 }
3825
3826 /* Without a symbolic address, we have little hope of the
3827 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3828 a.out. */
c5aa993b
JM
3829 if (b->addr_string == NULL)
3830 {
3831 delete_breakpoint (b);
3832 continue;
3833 }
c5aa993b 3834 }
c906108c
SS
3835}
3836
3837int
d80ee84f 3838detach_breakpoints (ptid_t ptid)
c906108c 3839{
35df4500 3840 struct bp_location *bl, **blp_tmp;
3a1bae8e 3841 int val = 0;
ce696e05 3842 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3843 struct inferior *inf = current_inferior ();
c5aa993b 3844
dfd4cc63 3845 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3846 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3847
6c95b8df 3848 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3849 inferior_ptid = ptid;
35df4500 3850 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3851 {
35df4500 3852 if (bl->pspace != inf->pspace)
6c95b8df
PA
3853 continue;
3854
bd9673a4
PW
3855 /* This function must physically remove breakpoints locations
3856 from the specified ptid, without modifying the breakpoint
3857 package's state. Locations of type bp_loc_other are only
3858 maintained at GDB side. So, there is no need to remove
3859 these bp_loc_other locations. Moreover, removing these
3860 would modify the breakpoint package's state. */
3861 if (bl->loc_type == bp_loc_other)
3862 continue;
3863
35df4500
TJB
3864 if (bl->inserted)
3865 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3866 }
d03285ec 3867
ce696e05 3868 do_cleanups (old_chain);
3a1bae8e 3869 return val;
c906108c
SS
3870}
3871
35df4500 3872/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3873 Note that this is used to detach breakpoints from a child fork.
3874 When we get here, the child isn't in the inferior list, and neither
3875 do we have objects to represent its address space --- we should
35df4500 3876 *not* look at bl->pspace->aspace here. */
6c95b8df 3877
c906108c 3878static int
35df4500 3879remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3880{
3881 int val;
c5aa993b 3882
35df4500
TJB
3883 /* BL is never in moribund_locations by our callers. */
3884 gdb_assert (bl->owner != NULL);
2bdf28a0 3885
35df4500 3886 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3887 /* Permanent breakpoints cannot be inserted or removed. */
3888 return 0;
3889
74960c60
VP
3890 /* The type of none suggests that owner is actually deleted.
3891 This should not ever happen. */
35df4500 3892 gdb_assert (bl->owner->type != bp_none);
0bde7532 3893
35df4500
TJB
3894 if (bl->loc_type == bp_loc_software_breakpoint
3895 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3896 {
c02f5703
MS
3897 /* "Normal" instruction breakpoint: either the standard
3898 trap-instruction bp (bp_breakpoint), or a
3899 bp_hardware_breakpoint. */
3900
3901 /* First check to see if we have to handle an overlay. */
3902 if (overlay_debugging == ovly_off
35df4500
TJB
3903 || bl->section == NULL
3904 || !(section_is_overlay (bl->section)))
c02f5703
MS
3905 {
3906 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3907
3908 /* If we're trying to uninsert a memory breakpoint that we
3909 know is set in a dynamic object that is marked
3910 shlib_disabled, then either the dynamic object was
3911 removed with "remove-symbol-file" or with
3912 "nosharedlibrary". In the former case, we don't know
3913 whether another dynamic object might have loaded over the
3914 breakpoint's address -- the user might well let us know
3915 about it next with add-symbol-file (the whole point of
d03de421 3916 add-symbol-file is letting the user manually maintain a
08351840
PA
3917 list of dynamically loaded objects). If we have the
3918 breakpoint's shadow memory, that is, this is a software
3919 breakpoint managed by GDB, check whether the breakpoint
3920 is still inserted in memory, to avoid overwriting wrong
3921 code with stale saved shadow contents. Note that HW
3922 breakpoints don't have shadow memory, as they're
3923 implemented using a mechanism that is not dependent on
3924 being able to modify the target's memory, and as such
3925 they should always be removed. */
3926 if (bl->shlib_disabled
3927 && bl->target_info.shadow_len != 0
3928 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3929 val = 0;
3930 else
3931 val = bl->owner->ops->remove_location (bl);
c02f5703 3932 }
c906108c
SS
3933 else
3934 {
4a64f543 3935 /* This breakpoint is in an overlay section.
c02f5703
MS
3936 Did we set a breakpoint at the LMA? */
3937 if (!overlay_events_enabled)
3938 {
3939 /* Yes -- overlay event support is not active, so we
3940 should have set a breakpoint at the LMA. Remove it.
3941 */
c02f5703
MS
3942 /* Ignore any failures: if the LMA is in ROM, we will
3943 have already warned when we failed to insert it. */
35df4500
TJB
3944 if (bl->loc_type == bp_loc_hardware_breakpoint)
3945 target_remove_hw_breakpoint (bl->gdbarch,
3946 &bl->overlay_target_info);
c02f5703 3947 else
35df4500
TJB
3948 target_remove_breakpoint (bl->gdbarch,
3949 &bl->overlay_target_info);
c02f5703
MS
3950 }
3951 /* Did we set a breakpoint at the VMA?
3952 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3953 if (bl->inserted)
c906108c 3954 {
c02f5703
MS
3955 /* Yes -- remove it. Previously we did not bother to
3956 remove the breakpoint if the section had been
3957 unmapped, but let's not rely on that being safe. We
3958 don't know what the overlay manager might do. */
aa67235e
UW
3959
3960 /* However, we should remove *software* breakpoints only
3961 if the section is still mapped, or else we overwrite
3962 wrong code with the saved shadow contents. */
348d480f
PA
3963 if (bl->loc_type == bp_loc_hardware_breakpoint
3964 || section_is_mapped (bl->section))
3965 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3966 else
3967 val = 0;
c906108c 3968 }
c02f5703
MS
3969 else
3970 {
3971 /* No -- not inserted, so no need to remove. No error. */
3972 val = 0;
3973 }
c906108c 3974 }
879d1e6b 3975
08351840
PA
3976 /* In some cases, we might not be able to remove a breakpoint in
3977 a shared library that has already been removed, but we have
3978 not yet processed the shlib unload event. Similarly for an
3979 unloaded add-symbol-file object - the user might not yet have
3980 had the chance to remove-symbol-file it. shlib_disabled will
3981 be set if the library/object has already been removed, but
3982 the breakpoint hasn't been uninserted yet, e.g., after
3983 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3984 always-inserted mode. */
076855f9 3985 if (val
08351840
PA
3986 && (bl->loc_type == bp_loc_software_breakpoint
3987 && (bl->shlib_disabled
3988 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3989 || shared_objfile_contains_address_p (bl->pspace,
3990 bl->address))))
879d1e6b
UW
3991 val = 0;
3992
c906108c
SS
3993 if (val)
3994 return val;
35df4500 3995 bl->inserted = (is == mark_inserted);
c906108c 3996 }
35df4500 3997 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3998 {
77b06cd7
TJB
3999 gdb_assert (bl->owner->ops != NULL
4000 && bl->owner->ops->remove_location != NULL);
4001
35df4500 4002 bl->inserted = (is == mark_inserted);
77b06cd7 4003 bl->owner->ops->remove_location (bl);
2e70b7b9 4004
c906108c 4005 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4006 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4007 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4008 bl->owner->number);
c906108c 4009 }
35df4500
TJB
4010 else if (bl->owner->type == bp_catchpoint
4011 && breakpoint_enabled (bl->owner)
4012 && !bl->duplicate)
ce78b96d 4013 {
77b06cd7
TJB
4014 gdb_assert (bl->owner->ops != NULL
4015 && bl->owner->ops->remove_location != NULL);
ce78b96d 4016
77b06cd7 4017 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4018 if (val)
4019 return val;
77b06cd7 4020
35df4500 4021 bl->inserted = (is == mark_inserted);
ce78b96d 4022 }
c906108c
SS
4023
4024 return 0;
4025}
4026
6c95b8df 4027static int
35df4500 4028remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4029{
4030 int ret;
4031 struct cleanup *old_chain;
4032
35df4500
TJB
4033 /* BL is never in moribund_locations by our callers. */
4034 gdb_assert (bl->owner != NULL);
2bdf28a0 4035
35df4500 4036 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
4037 /* Permanent breakpoints cannot be inserted or removed. */
4038 return 0;
4039
4040 /* The type of none suggests that owner is actually deleted.
4041 This should not ever happen. */
35df4500 4042 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4043
4044 old_chain = save_current_space_and_thread ();
4045
35df4500 4046 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4047
35df4500 4048 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4049
4050 do_cleanups (old_chain);
4051 return ret;
4052}
4053
c906108c
SS
4054/* Clear the "inserted" flag in all breakpoints. */
4055
25b22b0a 4056void
fba45db2 4057mark_breakpoints_out (void)
c906108c 4058{
35df4500 4059 struct bp_location *bl, **blp_tmp;
c906108c 4060
35df4500
TJB
4061 ALL_BP_LOCATIONS (bl, blp_tmp)
4062 if (bl->pspace == current_program_space)
4063 bl->inserted = 0;
c906108c
SS
4064}
4065
53a5351d
JM
4066/* Clear the "inserted" flag in all breakpoints and delete any
4067 breakpoints which should go away between runs of the program.
c906108c
SS
4068
4069 Plus other such housekeeping that has to be done for breakpoints
4070 between runs.
4071
53a5351d
JM
4072 Note: this function gets called at the end of a run (by
4073 generic_mourn_inferior) and when a run begins (by
4a64f543 4074 init_wait_for_inferior). */
c906108c
SS
4075
4076
4077
4078void
fba45db2 4079breakpoint_init_inferior (enum inf_context context)
c906108c 4080{
35df4500
TJB
4081 struct breakpoint *b, *b_tmp;
4082 struct bp_location *bl, **blp_tmp;
1c5cfe86 4083 int ix;
6c95b8df 4084 struct program_space *pspace = current_program_space;
c906108c 4085
50c71eaf
PA
4086 /* If breakpoint locations are shared across processes, then there's
4087 nothing to do. */
f5656ead 4088 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4089 return;
4090
35df4500 4091 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 4092 {
35df4500
TJB
4093 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4094 if (bl->pspace == pspace
4095 && bl->owner->enable_state != bp_permanent)
4096 bl->inserted = 0;
6c95b8df 4097 }
075f6582 4098
35df4500 4099 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4100 {
6c95b8df
PA
4101 if (b->loc && b->loc->pspace != pspace)
4102 continue;
4103
c5aa993b
JM
4104 switch (b->type)
4105 {
4106 case bp_call_dummy:
e2e4d78b 4107 case bp_longjmp_call_dummy:
c906108c 4108
c5aa993b 4109 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4110 cause problems when the inferior is rerun, so we better get
4111 rid of it. */
4112
4113 case bp_watchpoint_scope:
4114
4115 /* Also get rid of scope breakpoints. */
4116
4117 case bp_shlib_event:
4118
4119 /* Also remove solib event breakpoints. Their addresses may
4120 have changed since the last time we ran the program.
4121 Actually we may now be debugging against different target;
4122 and so the solib backend that installed this breakpoint may
4123 not be used in by the target. E.g.,
4124
4125 (gdb) file prog-linux
4126 (gdb) run # native linux target
4127 ...
4128 (gdb) kill
4129 (gdb) file prog-win.exe
4130 (gdb) tar rem :9999 # remote Windows gdbserver.
4131 */
c906108c 4132
f59f708a
PA
4133 case bp_step_resume:
4134
4135 /* Also remove step-resume breakpoints. */
4136
7c16b83e
PA
4137 case bp_single_step:
4138
4139 /* Also remove single-step breakpoints. */
4140
c5aa993b
JM
4141 delete_breakpoint (b);
4142 break;
c906108c 4143
c5aa993b
JM
4144 case bp_watchpoint:
4145 case bp_hardware_watchpoint:
4146 case bp_read_watchpoint:
4147 case bp_access_watchpoint:
3a5c3e22
PA
4148 {
4149 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4150
3a5c3e22
PA
4151 /* Likewise for watchpoints on local expressions. */
4152 if (w->exp_valid_block != NULL)
4153 delete_breakpoint (b);
4154 else if (context == inf_starting)
4155 {
4156 /* Reset val field to force reread of starting value in
4157 insert_breakpoints. */
4158 if (w->val)
4159 value_free (w->val);
4160 w->val = NULL;
4161 w->val_valid = 0;
c860120c 4162 }
3a5c3e22 4163 }
c5aa993b
JM
4164 break;
4165 default:
c5aa993b
JM
4166 break;
4167 }
4168 }
1c5cfe86
PA
4169
4170 /* Get rid of the moribund locations. */
35df4500
TJB
4171 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4172 decref_bp_location (&bl);
1c5cfe86 4173 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4174}
4175
6c95b8df
PA
4176/* These functions concern about actual breakpoints inserted in the
4177 target --- to e.g. check if we need to do decr_pc adjustment or if
4178 we need to hop over the bkpt --- so we check for address space
4179 match, not program space. */
4180
c2c6d25f
JM
4181/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4182 exists at PC. It returns ordinary_breakpoint_here if it's an
4183 ordinary breakpoint, or permanent_breakpoint_here if it's a
4184 permanent breakpoint.
4185 - When continuing from a location with an ordinary breakpoint, we
4186 actually single step once before calling insert_breakpoints.
e5dd4106 4187 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4188 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4189 the target, to advance the PC past the breakpoint. */
c906108c 4190
c2c6d25f 4191enum breakpoint_here
6c95b8df 4192breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4193{
35df4500 4194 struct bp_location *bl, **blp_tmp;
c2c6d25f 4195 int any_breakpoint_here = 0;
c906108c 4196
35df4500 4197 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4198 {
35df4500
TJB
4199 if (bl->loc_type != bp_loc_software_breakpoint
4200 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4201 continue;
4202
f1310107 4203 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
4204 if ((breakpoint_enabled (bl->owner)
4205 || bl->owner->enable_state == bp_permanent)
f1310107 4206 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4207 {
4208 if (overlay_debugging
35df4500
TJB
4209 && section_is_overlay (bl->section)
4210 && !section_is_mapped (bl->section))
075f6582 4211 continue; /* unmapped overlay -- can't be a match */
35df4500 4212 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
4213 return permanent_breakpoint_here;
4214 else
4215 any_breakpoint_here = 1;
4216 }
4217 }
c906108c 4218
c2c6d25f 4219 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4220}
4221
1c5cfe86
PA
4222/* Return true if there's a moribund breakpoint at PC. */
4223
4224int
6c95b8df 4225moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4226{
4227 struct bp_location *loc;
4228 int ix;
4229
4230 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4231 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4232 return 1;
4233
4234 return 0;
4235}
c2c6d25f 4236
a1fd2fa5 4237/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4238
4239int
a1fd2fa5 4240breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4241{
35df4500 4242 struct bp_location *bl, **blp_tmp;
c906108c 4243
35df4500 4244 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4245 {
35df4500
TJB
4246 if (bl->loc_type != bp_loc_software_breakpoint
4247 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4248 continue;
4249
35df4500 4250 if (bl->inserted
f1310107 4251 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4252 {
4253 if (overlay_debugging
35df4500
TJB
4254 && section_is_overlay (bl->section)
4255 && !section_is_mapped (bl->section))
075f6582
DJ
4256 continue; /* unmapped overlay -- can't be a match */
4257 else
4258 return 1;
4259 }
c5aa993b 4260 }
c36b740a
VP
4261 return 0;
4262}
4263
a1fd2fa5
PA
4264/* This function returns non-zero iff there is a software breakpoint
4265 inserted at PC. */
c36b740a
VP
4266
4267int
a1fd2fa5
PA
4268software_breakpoint_inserted_here_p (struct address_space *aspace,
4269 CORE_ADDR pc)
4fa8626c 4270{
35df4500 4271 struct bp_location *bl, **blp_tmp;
4fa8626c 4272
35df4500 4273 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 4274 {
35df4500 4275 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4276 continue;
4277
35df4500
TJB
4278 if (bl->inserted
4279 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 4280 aspace, pc))
4fa8626c
DJ
4281 {
4282 if (overlay_debugging
35df4500
TJB
4283 && section_is_overlay (bl->section)
4284 && !section_is_mapped (bl->section))
4fa8626c
DJ
4285 continue; /* unmapped overlay -- can't be a match */
4286 else
a1fd2fa5 4287 return 1;
4fa8626c
DJ
4288 }
4289 }
4290
4291 return 0;
4292}
4293
9093389c
PA
4294int
4295hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4296 CORE_ADDR addr, ULONGEST len)
4297{
4298 struct breakpoint *bpt;
4299
4300 ALL_BREAKPOINTS (bpt)
4301 {
4302 struct bp_location *loc;
4303
4304 if (bpt->type != bp_hardware_watchpoint
4305 && bpt->type != bp_access_watchpoint)
4306 continue;
4307
4308 if (!breakpoint_enabled (bpt))
4309 continue;
4310
4311 for (loc = bpt->loc; loc; loc = loc->next)
4312 if (loc->pspace->aspace == aspace && loc->inserted)
4313 {
4314 CORE_ADDR l, h;
4315
4316 /* Check for intersection. */
4317 l = max (loc->address, addr);
4318 h = min (loc->address + loc->length, addr + len);
4319 if (l < h)
4320 return 1;
4321 }
4322 }
4323 return 0;
4324}
4325
075f6582
DJ
4326/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4327 PC is valid for process/thread PTID. */
c906108c
SS
4328
4329int
6c95b8df
PA
4330breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4331 ptid_t ptid)
c906108c 4332{
35df4500 4333 struct bp_location *bl, **blp_tmp;
4a306c9a 4334 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4335 int thread = -1;
4a306c9a 4336 int task = 0;
a6f1cd96 4337
35df4500 4338 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4339 {
35df4500
TJB
4340 if (bl->loc_type != bp_loc_software_breakpoint
4341 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4342 continue;
4343
35df4500
TJB
4344 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4345 if (!breakpoint_enabled (bl->owner)
4346 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4347 continue;
4348
f1310107 4349 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4350 continue;
4351
35df4500 4352 if (bl->owner->thread != -1)
075f6582 4353 {
a6f1cd96
JB
4354 /* This is a thread-specific breakpoint. Check that ptid
4355 matches that thread. If thread hasn't been computed yet,
4356 it is now time to do so. */
4357 if (thread == -1)
4358 thread = pid_to_thread_id (ptid);
35df4500 4359 if (bl->owner->thread != thread)
a6f1cd96 4360 continue;
075f6582 4361 }
a6f1cd96 4362
35df4500 4363 if (bl->owner->task != 0)
4a306c9a
JB
4364 {
4365 /* This is a task-specific breakpoint. Check that ptid
4366 matches that task. If task hasn't been computed yet,
4367 it is now time to do so. */
4368 if (task == 0)
4369 task = ada_get_task_number (ptid);
35df4500 4370 if (bl->owner->task != task)
4a306c9a
JB
4371 continue;
4372 }
4373
a6f1cd96 4374 if (overlay_debugging
35df4500
TJB
4375 && section_is_overlay (bl->section)
4376 && !section_is_mapped (bl->section))
a6f1cd96
JB
4377 continue; /* unmapped overlay -- can't be a match */
4378
4379 return 1;
c5aa993b 4380 }
c906108c
SS
4381
4382 return 0;
4383}
c906108c 4384\f
c5aa993b 4385
c906108c
SS
4386/* bpstat stuff. External routines' interfaces are documented
4387 in breakpoint.h. */
4388
4389int
c326b90e 4390is_catchpoint (struct breakpoint *ep)
c906108c 4391{
533be4dd 4392 return (ep->type == bp_catchpoint);
c906108c
SS
4393}
4394
f431efe5
PA
4395/* Frees any storage that is part of a bpstat. Does not walk the
4396 'next' chain. */
4397
4398static void
198757a8
VP
4399bpstat_free (bpstat bs)
4400{
4401 if (bs->old_val != NULL)
4402 value_free (bs->old_val);
9add0f1b 4403 decref_counted_command_line (&bs->commands);
f431efe5 4404 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4405 xfree (bs);
4406}
4407
c906108c
SS
4408/* Clear a bpstat so that it says we are not at any breakpoint.
4409 Also free any storage that is part of a bpstat. */
4410
4411void
fba45db2 4412bpstat_clear (bpstat *bsp)
c906108c
SS
4413{
4414 bpstat p;
4415 bpstat q;
4416
4417 if (bsp == 0)
4418 return;
4419 p = *bsp;
4420 while (p != NULL)
4421 {
4422 q = p->next;
198757a8 4423 bpstat_free (p);
c906108c
SS
4424 p = q;
4425 }
4426 *bsp = NULL;
4427}
4428
4429/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4430 is part of the bpstat is copied as well. */
4431
4432bpstat
fba45db2 4433bpstat_copy (bpstat bs)
c906108c
SS
4434{
4435 bpstat p = NULL;
4436 bpstat tmp;
4437 bpstat retval = NULL;
4438
4439 if (bs == NULL)
4440 return bs;
4441
4442 for (; bs != NULL; bs = bs->next)
4443 {
4444 tmp = (bpstat) xmalloc (sizeof (*tmp));
4445 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4446 incref_counted_command_line (tmp->commands);
f431efe5 4447 incref_bp_location (tmp->bp_location_at);
31cc81e9 4448 if (bs->old_val != NULL)
3c3185ac
JK
4449 {
4450 tmp->old_val = value_copy (bs->old_val);
4451 release_value (tmp->old_val);
4452 }
31cc81e9 4453
c906108c
SS
4454 if (p == NULL)
4455 /* This is the first thing in the chain. */
4456 retval = tmp;
4457 else
4458 p->next = tmp;
4459 p = tmp;
4460 }
4461 p->next = NULL;
4462 return retval;
4463}
4464
4a64f543 4465/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4466
4467bpstat
fba45db2 4468bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4469{
c5aa993b
JM
4470 if (bsp == NULL)
4471 return NULL;
c906108c 4472
c5aa993b
JM
4473 for (; bsp != NULL; bsp = bsp->next)
4474 {
f431efe5 4475 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4476 return bsp;
4477 }
c906108c
SS
4478 return NULL;
4479}
4480
ab04a2af
TT
4481/* See breakpoint.h. */
4482
47591c29 4483int
427cd150 4484bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4485{
ab04a2af
TT
4486 for (; bsp != NULL; bsp = bsp->next)
4487 {
427cd150
TT
4488 if (bsp->breakpoint_at == NULL)
4489 {
4490 /* A moribund location can never explain a signal other than
4491 GDB_SIGNAL_TRAP. */
4492 if (sig == GDB_SIGNAL_TRAP)
47591c29 4493 return 1;
427cd150
TT
4494 }
4495 else
47591c29
PA
4496 {
4497 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4498 sig))
4499 return 1;
4500 }
ab04a2af
TT
4501 }
4502
47591c29 4503 return 0;
ab04a2af
TT
4504}
4505
4a64f543
MS
4506/* Put in *NUM the breakpoint number of the first breakpoint we are
4507 stopped at. *BSP upon return is a bpstat which points to the
4508 remaining breakpoints stopped at (but which is not guaranteed to be
4509 good for anything but further calls to bpstat_num).
4510
8671a17b
PA
4511 Return 0 if passed a bpstat which does not indicate any breakpoints.
4512 Return -1 if stopped at a breakpoint that has been deleted since
4513 we set it.
4514 Return 1 otherwise. */
c906108c
SS
4515
4516int
8671a17b 4517bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4518{
4519 struct breakpoint *b;
4520
4521 if ((*bsp) == NULL)
4522 return 0; /* No more breakpoint values */
8671a17b 4523
4a64f543
MS
4524 /* We assume we'll never have several bpstats that correspond to a
4525 single breakpoint -- otherwise, this function might return the
4526 same number more than once and this will look ugly. */
f431efe5 4527 b = (*bsp)->breakpoint_at;
8671a17b
PA
4528 *bsp = (*bsp)->next;
4529 if (b == NULL)
4530 return -1; /* breakpoint that's been deleted since */
4531
4532 *num = b->number; /* We have its number */
4533 return 1;
c906108c
SS
4534}
4535
e93ca019 4536/* See breakpoint.h. */
c906108c
SS
4537
4538void
e93ca019 4539bpstat_clear_actions (void)
c906108c 4540{
e93ca019
JK
4541 struct thread_info *tp;
4542 bpstat bs;
4543
4544 if (ptid_equal (inferior_ptid, null_ptid))
4545 return;
4546
4547 tp = find_thread_ptid (inferior_ptid);
4548 if (tp == NULL)
4549 return;
4550
4551 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4552 {
9add0f1b 4553 decref_counted_command_line (&bs->commands);
abf85f46 4554
c906108c
SS
4555 if (bs->old_val != NULL)
4556 {
4557 value_free (bs->old_val);
4558 bs->old_val = NULL;
4559 }
4560 }
4561}
4562
f3b1572e
PA
4563/* Called when a command is about to proceed the inferior. */
4564
4565static void
4566breakpoint_about_to_proceed (void)
4567{
4568 if (!ptid_equal (inferior_ptid, null_ptid))
4569 {
4570 struct thread_info *tp = inferior_thread ();
4571
4572 /* Allow inferior function calls in breakpoint commands to not
4573 interrupt the command list. When the call finishes
4574 successfully, the inferior will be standing at the same
4575 breakpoint as if nothing happened. */
16c381f0 4576 if (tp->control.in_infcall)
f3b1572e
PA
4577 return;
4578 }
4579
4580 breakpoint_proceeded = 1;
4581}
4582
4a64f543
MS
4583/* Stub for cleaning up our state if we error-out of a breakpoint
4584 command. */
c906108c 4585static void
4efb68b1 4586cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4587{
4588 executing_breakpoint_commands = 0;
4589}
4590
abf85f46
JK
4591/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4592 or its equivalent. */
4593
4594static int
4595command_line_is_silent (struct command_line *cmd)
4596{
4597 return cmd && (strcmp ("silent", cmd->line) == 0
4598 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4599}
4600
4a64f543
MS
4601/* Execute all the commands associated with all the breakpoints at
4602 this location. Any of these commands could cause the process to
4603 proceed beyond this point, etc. We look out for such changes by
4604 checking the global "breakpoint_proceeded" after each command.
c906108c 4605
347bddb7
PA
4606 Returns true if a breakpoint command resumed the inferior. In that
4607 case, it is the caller's responsibility to recall it again with the
4608 bpstat of the current thread. */
4609
4610static int
4611bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4612{
4613 bpstat bs;
4614 struct cleanup *old_chain;
347bddb7 4615 int again = 0;
c906108c
SS
4616
4617 /* Avoid endless recursion if a `source' command is contained
4618 in bs->commands. */
4619 if (executing_breakpoint_commands)
347bddb7 4620 return 0;
c906108c
SS
4621
4622 executing_breakpoint_commands = 1;
4623 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4624
cf6c5ffb
TT
4625 prevent_dont_repeat ();
4626
4a64f543 4627 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4628 bs = *bsp;
4629
4630 breakpoint_proceeded = 0;
4631 for (; bs != NULL; bs = bs->next)
4632 {
9add0f1b 4633 struct counted_command_line *ccmd;
6c50ab1c
JB
4634 struct command_line *cmd;
4635 struct cleanup *this_cmd_tree_chain;
4636
4637 /* Take ownership of the BSP's command tree, if it has one.
4638
4639 The command tree could legitimately contain commands like
4640 'step' and 'next', which call clear_proceed_status, which
4641 frees stop_bpstat's command tree. To make sure this doesn't
4642 free the tree we're executing out from under us, we need to
4643 take ownership of the tree ourselves. Since a given bpstat's
4644 commands are only executed once, we don't need to copy it; we
4645 can clear the pointer in the bpstat, and make sure we free
4646 the tree when we're done. */
9add0f1b
TT
4647 ccmd = bs->commands;
4648 bs->commands = NULL;
abf85f46
JK
4649 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4650 cmd = ccmd ? ccmd->commands : NULL;
4651 if (command_line_is_silent (cmd))
4652 {
4653 /* The action has been already done by bpstat_stop_status. */
4654 cmd = cmd->next;
4655 }
6c50ab1c 4656
c906108c
SS
4657 while (cmd != NULL)
4658 {
4659 execute_control_command (cmd);
4660
4661 if (breakpoint_proceeded)
4662 break;
4663 else
4664 cmd = cmd->next;
4665 }
6c50ab1c
JB
4666
4667 /* We can free this command tree now. */
4668 do_cleanups (this_cmd_tree_chain);
4669
c906108c 4670 if (breakpoint_proceeded)
32c1e744
VP
4671 {
4672 if (target_can_async_p ())
347bddb7
PA
4673 /* If we are in async mode, then the target might be still
4674 running, not stopped at any breakpoint, so nothing for
4675 us to do here -- just return to the event loop. */
4676 ;
32c1e744
VP
4677 else
4678 /* In sync mode, when execute_control_command returns
4679 we're already standing on the next breakpoint.
347bddb7
PA
4680 Breakpoint commands for that stop were not run, since
4681 execute_command does not run breakpoint commands --
4682 only command_line_handler does, but that one is not
4683 involved in execution of breakpoint commands. So, we
4684 can now execute breakpoint commands. It should be
4685 noted that making execute_command do bpstat actions is
4686 not an option -- in this case we'll have recursive
4687 invocation of bpstat for each breakpoint with a
4688 command, and can easily blow up GDB stack. Instead, we
4689 return true, which will trigger the caller to recall us
4690 with the new stop_bpstat. */
4691 again = 1;
4692 break;
32c1e744 4693 }
c906108c 4694 }
c2b8ed2c 4695 do_cleanups (old_chain);
347bddb7
PA
4696 return again;
4697}
4698
4699void
4700bpstat_do_actions (void)
4701{
353d1d73
JK
4702 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4703
347bddb7
PA
4704 /* Do any commands attached to breakpoint we are stopped at. */
4705 while (!ptid_equal (inferior_ptid, null_ptid)
4706 && target_has_execution
4707 && !is_exited (inferior_ptid)
4708 && !is_executing (inferior_ptid))
4709 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4710 and only return when it is stopped at the next breakpoint, we
4711 keep doing breakpoint actions until it returns false to
4712 indicate the inferior was not resumed. */
16c381f0 4713 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4714 break;
353d1d73
JK
4715
4716 discard_cleanups (cleanup_if_error);
c906108c
SS
4717}
4718
fa4727a6
DJ
4719/* Print out the (old or new) value associated with a watchpoint. */
4720
4721static void
4722watchpoint_value_print (struct value *val, struct ui_file *stream)
4723{
4724 if (val == NULL)
4725 fprintf_unfiltered (stream, _("<unreadable>"));
4726 else
79a45b7d
TT
4727 {
4728 struct value_print_options opts;
4729 get_user_print_options (&opts);
4730 value_print (val, stream, &opts);
4731 }
fa4727a6
DJ
4732}
4733
e514a9d6 4734/* Generic routine for printing messages indicating why we
4a64f543 4735 stopped. The behavior of this function depends on the value
e514a9d6
JM
4736 'print_it' in the bpstat structure. Under some circumstances we
4737 may decide not to print anything here and delegate the task to
4a64f543 4738 normal_stop(). */
e514a9d6
JM
4739
4740static enum print_stop_action
4741print_bp_stop_message (bpstat bs)
4742{
4743 switch (bs->print_it)
4744 {
4745 case print_it_noop:
4a64f543 4746 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4747 return PRINT_UNKNOWN;
4748 break;
4749
4750 case print_it_done:
4751 /* We still want to print the frame, but we already printed the
4a64f543 4752 relevant messages. */
e514a9d6
JM
4753 return PRINT_SRC_AND_LOC;
4754 break;
4755
4756 case print_it_normal:
4f8d1dc6 4757 {
f431efe5
PA
4758 struct breakpoint *b = bs->breakpoint_at;
4759
1a6a67de
TJB
4760 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4761 which has since been deleted. */
4762 if (b == NULL)
4763 return PRINT_UNKNOWN;
4764
348d480f
PA
4765 /* Normal case. Call the breakpoint's print_it method. */
4766 return b->ops->print_it (bs);
4f8d1dc6 4767 }
348d480f 4768 break;
3086aeae 4769
e514a9d6 4770 default:
8e65ff28 4771 internal_error (__FILE__, __LINE__,
e2e0b3e5 4772 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4773 break;
c906108c 4774 }
c906108c
SS
4775}
4776
edcc5120
TT
4777/* A helper function that prints a shared library stopped event. */
4778
4779static void
4780print_solib_event (int is_catchpoint)
4781{
4782 int any_deleted
4783 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4784 int any_added
4785 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4786
4787 if (!is_catchpoint)
4788 {
4789 if (any_added || any_deleted)
4790 ui_out_text (current_uiout,
4791 _("Stopped due to shared library event:\n"));
4792 else
4793 ui_out_text (current_uiout,
4794 _("Stopped due to shared library event (no "
4795 "libraries added or removed)\n"));
4796 }
4797
4798 if (ui_out_is_mi_like_p (current_uiout))
4799 ui_out_field_string (current_uiout, "reason",
4800 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4801
4802 if (any_deleted)
4803 {
4804 struct cleanup *cleanup;
4805 char *name;
4806 int ix;
4807
4808 ui_out_text (current_uiout, _(" Inferior unloaded "));
4809 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4810 "removed");
4811 for (ix = 0;
4812 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4813 ix, name);
4814 ++ix)
4815 {
4816 if (ix > 0)
4817 ui_out_text (current_uiout, " ");
4818 ui_out_field_string (current_uiout, "library", name);
4819 ui_out_text (current_uiout, "\n");
4820 }
4821
4822 do_cleanups (cleanup);
4823 }
4824
4825 if (any_added)
4826 {
4827 struct so_list *iter;
4828 int ix;
4829 struct cleanup *cleanup;
4830
4831 ui_out_text (current_uiout, _(" Inferior loaded "));
4832 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4833 "added");
4834 for (ix = 0;
4835 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4836 ix, iter);
4837 ++ix)
4838 {
4839 if (ix > 0)
4840 ui_out_text (current_uiout, " ");
4841 ui_out_field_string (current_uiout, "library", iter->so_name);
4842 ui_out_text (current_uiout, "\n");
4843 }
4844
4845 do_cleanups (cleanup);
4846 }
4847}
4848
e514a9d6
JM
4849/* Print a message indicating what happened. This is called from
4850 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4851 list - a list of the eventpoints that caused this stop. KIND is
4852 the target_waitkind for the stopping event. This
e514a9d6
JM
4853 routine calls the generic print routine for printing a message
4854 about reasons for stopping. This will print (for example) the
4855 "Breakpoint n," part of the output. The return value of this
4856 routine is one of:
c906108c 4857
4a64f543 4858 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4859 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4860 code to print the location. An example is
c5aa993b
JM
4861 "Breakpoint 1, " which should be followed by
4862 the location.
917317f4 4863 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4864 to also print the location part of the message.
4865 An example is the catch/throw messages, which
4a64f543 4866 don't require a location appended to the end.
917317f4 4867 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4868 further info to be printed. */
c906108c 4869
917317f4 4870enum print_stop_action
36dfb11c 4871bpstat_print (bpstat bs, int kind)
c906108c
SS
4872{
4873 int val;
c5aa993b 4874
c906108c 4875 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4876 (Currently all watchpoints go on the bpstat whether hit or not.
4877 That probably could (should) be changed, provided care is taken
c906108c 4878 with respect to bpstat_explains_signal). */
e514a9d6
JM
4879 for (; bs; bs = bs->next)
4880 {
4881 val = print_bp_stop_message (bs);
4882 if (val == PRINT_SRC_ONLY
4883 || val == PRINT_SRC_AND_LOC
4884 || val == PRINT_NOTHING)
4885 return val;
4886 }
c906108c 4887
36dfb11c
TT
4888 /* If we had hit a shared library event breakpoint,
4889 print_bp_stop_message would print out this message. If we hit an
4890 OS-level shared library event, do the same thing. */
4891 if (kind == TARGET_WAITKIND_LOADED)
4892 {
edcc5120 4893 print_solib_event (0);
36dfb11c
TT
4894 return PRINT_NOTHING;
4895 }
4896
e514a9d6 4897 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4898 with and nothing was printed. */
917317f4 4899 return PRINT_UNKNOWN;
c906108c
SS
4900}
4901
c42bd95a
DE
4902/* Evaluate the expression EXP and return 1 if value is zero.
4903 This returns the inverse of the condition because it is called
4904 from catch_errors which returns 0 if an exception happened, and if an
4905 exception happens we want execution to stop.
4a64f543 4906 The argument is a "struct expression *" that has been cast to a
c42bd95a 4907 "void *" to make it pass through catch_errors. */
c906108c
SS
4908
4909static int
4efb68b1 4910breakpoint_cond_eval (void *exp)
c906108c 4911{
278cd55f 4912 struct value *mark = value_mark ();
c5aa993b 4913 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4914
c906108c
SS
4915 value_free_to_mark (mark);
4916 return i;
4917}
4918
5760d0ab 4919/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4920
4921static bpstat
5760d0ab 4922bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4923{
4924 bpstat bs;
4925
4926 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4927 bs->next = NULL;
4928 **bs_link_pointer = bs;
4929 *bs_link_pointer = &bs->next;
f431efe5
PA
4930 bs->breakpoint_at = bl->owner;
4931 bs->bp_location_at = bl;
4932 incref_bp_location (bl);
c906108c
SS
4933 /* If the condition is false, etc., don't do the commands. */
4934 bs->commands = NULL;
4935 bs->old_val = NULL;
4936 bs->print_it = print_it_normal;
4937 return bs;
4938}
4939\f
d983da9c
DJ
4940/* The target has stopped with waitstatus WS. Check if any hardware
4941 watchpoints have triggered, according to the target. */
4942
4943int
4944watchpoints_triggered (struct target_waitstatus *ws)
4945{
d92524f1 4946 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4947 CORE_ADDR addr;
4948 struct breakpoint *b;
4949
4950 if (!stopped_by_watchpoint)
4951 {
4952 /* We were not stopped by a watchpoint. Mark all watchpoints
4953 as not triggered. */
4954 ALL_BREAKPOINTS (b)
cc60f2e3 4955 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4956 {
4957 struct watchpoint *w = (struct watchpoint *) b;
4958
4959 w->watchpoint_triggered = watch_triggered_no;
4960 }
d983da9c
DJ
4961
4962 return 0;
4963 }
4964
4965 if (!target_stopped_data_address (&current_target, &addr))
4966 {
4967 /* We were stopped by a watchpoint, but we don't know where.
4968 Mark all watchpoints as unknown. */
4969 ALL_BREAKPOINTS (b)
cc60f2e3 4970 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4971 {
4972 struct watchpoint *w = (struct watchpoint *) b;
4973
4974 w->watchpoint_triggered = watch_triggered_unknown;
4975 }
d983da9c 4976
3c4797ba 4977 return 1;
d983da9c
DJ
4978 }
4979
4980 /* The target could report the data address. Mark watchpoints
4981 affected by this data address as triggered, and all others as not
4982 triggered. */
4983
4984 ALL_BREAKPOINTS (b)
cc60f2e3 4985 if (is_hardware_watchpoint (b))
d983da9c 4986 {
3a5c3e22 4987 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4988 struct bp_location *loc;
d983da9c 4989
3a5c3e22 4990 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4991 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4992 {
3a5c3e22 4993 if (is_masked_watchpoint (b))
9c06b0b4 4994 {
3a5c3e22
PA
4995 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4996 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4997
4998 if (newaddr == start)
4999 {
3a5c3e22 5000 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5001 break;
5002 }
5003 }
5004 /* Exact match not required. Within range is sufficient. */
5005 else if (target_watchpoint_addr_within_range (&current_target,
5006 addr, loc->address,
5007 loc->length))
5008 {
3a5c3e22 5009 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5010 break;
5011 }
5012 }
d983da9c
DJ
5013 }
5014
5015 return 1;
5016}
5017
c906108c
SS
5018/* Possible return values for watchpoint_check (this can't be an enum
5019 because of check_errors). */
5020/* The watchpoint has been deleted. */
5021#define WP_DELETED 1
5022/* The value has changed. */
5023#define WP_VALUE_CHANGED 2
5024/* The value has not changed. */
5025#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5026/* Ignore this watchpoint, no matter if the value changed or not. */
5027#define WP_IGNORE 4
c906108c
SS
5028
5029#define BP_TEMPFLAG 1
5030#define BP_HARDWAREFLAG 2
5031
4a64f543
MS
5032/* Evaluate watchpoint condition expression and check if its value
5033 changed.
553e4c11
JB
5034
5035 P should be a pointer to struct bpstat, but is defined as a void *
5036 in order for this function to be usable with catch_errors. */
c906108c
SS
5037
5038static int
4efb68b1 5039watchpoint_check (void *p)
c906108c
SS
5040{
5041 bpstat bs = (bpstat) p;
3a5c3e22 5042 struct watchpoint *b;
c906108c
SS
5043 struct frame_info *fr;
5044 int within_current_scope;
5045
f431efe5 5046 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5047 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5048 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5049
f6bc2008
PA
5050 /* If this is a local watchpoint, we only want to check if the
5051 watchpoint frame is in scope if the current thread is the thread
5052 that was used to create the watchpoint. */
5053 if (!watchpoint_in_thread_scope (b))
60e1c644 5054 return WP_IGNORE;
f6bc2008 5055
c906108c
SS
5056 if (b->exp_valid_block == NULL)
5057 within_current_scope = 1;
5058 else
5059 {
edb3359d
DJ
5060 struct frame_info *frame = get_current_frame ();
5061 struct gdbarch *frame_arch = get_frame_arch (frame);
5062 CORE_ADDR frame_pc = get_frame_pc (frame);
5063
4a64f543
MS
5064 /* in_function_epilogue_p() returns a non-zero value if we're
5065 still in the function but the stack frame has already been
5066 invalidated. Since we can't rely on the values of local
5067 variables after the stack has been destroyed, we are treating
5068 the watchpoint in that state as `not changed' without further
5069 checking. Don't mark watchpoints as changed if the current
5070 frame is in an epilogue - even if they are in some other
5071 frame, our view of the stack is likely to be wrong and
5072 frame_find_by_id could error out. */
a0f49112 5073 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 5074 return WP_IGNORE;
a0f49112 5075
101dcfbe 5076 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5077 within_current_scope = (fr != NULL);
69fbadd5
DJ
5078
5079 /* If we've gotten confused in the unwinder, we might have
5080 returned a frame that can't describe this variable. */
edb3359d
DJ
5081 if (within_current_scope)
5082 {
5083 struct symbol *function;
5084
5085 function = get_frame_function (fr);
5086 if (function == NULL
5087 || !contained_in (b->exp_valid_block,
5088 SYMBOL_BLOCK_VALUE (function)))
5089 within_current_scope = 0;
5090 }
69fbadd5 5091
edb3359d 5092 if (within_current_scope)
c906108c
SS
5093 /* If we end up stopping, the current frame will get selected
5094 in normal_stop. So this call to select_frame won't affect
5095 the user. */
0f7d239c 5096 select_frame (fr);
c906108c 5097 }
c5aa993b 5098
c906108c
SS
5099 if (within_current_scope)
5100 {
4a64f543
MS
5101 /* We use value_{,free_to_}mark because it could be a *long*
5102 time before we return to the command level and call
5103 free_all_values. We can't call free_all_values because we
5104 might be in the middle of evaluating a function call. */
c906108c 5105
0cf6dd15 5106 int pc = 0;
9c06b0b4 5107 struct value *mark;
fa4727a6
DJ
5108 struct value *new_val;
5109
3a5c3e22 5110 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5111 /* Since we don't know the exact trigger address (from
5112 stopped_data_address), just tell the user we've triggered
5113 a mask watchpoint. */
5114 return WP_VALUE_CHANGED;
5115
5116 mark = value_mark ();
3a1115a0 5117 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5118
bb9d5f81
PP
5119 if (b->val_bitsize != 0)
5120 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5121
4a64f543
MS
5122 /* We use value_equal_contents instead of value_equal because
5123 the latter coerces an array to a pointer, thus comparing just
5124 the address of the array instead of its contents. This is
5125 not what we want. */
fa4727a6 5126 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5127 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5128 {
fa4727a6
DJ
5129 if (new_val != NULL)
5130 {
5131 release_value (new_val);
5132 value_free_to_mark (mark);
5133 }
c906108c
SS
5134 bs->old_val = b->val;
5135 b->val = new_val;
fa4727a6 5136 b->val_valid = 1;
c906108c
SS
5137 return WP_VALUE_CHANGED;
5138 }
5139 else
5140 {
60e1c644 5141 /* Nothing changed. */
c906108c 5142 value_free_to_mark (mark);
c906108c
SS
5143 return WP_VALUE_NOT_CHANGED;
5144 }
5145 }
5146 else
5147 {
79a45e25
PA
5148 struct ui_out *uiout = current_uiout;
5149
c906108c 5150 /* This seems like the only logical thing to do because
c5aa993b
JM
5151 if we temporarily ignored the watchpoint, then when
5152 we reenter the block in which it is valid it contains
5153 garbage (in the case of a function, it may have two
5154 garbage values, one before and one after the prologue).
5155 So we can't even detect the first assignment to it and
5156 watch after that (since the garbage may or may not equal
5157 the first value assigned). */
348d480f
PA
5158 /* We print all the stop information in
5159 breakpoint_ops->print_it, but in this case, by the time we
5160 call breakpoint_ops->print_it this bp will be deleted
5161 already. So we have no choice but print the information
5162 here. */
9dc5e2a9 5163 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5164 ui_out_field_string
5165 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5166 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5167 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5168 ui_out_text (uiout,
5169 " deleted because the program has left the block in\n\
8b93c638 5170which its expression is valid.\n");
4ce44c66 5171
cdac0397 5172 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5173 decref_counted_command_line (&b->base.commands);
d0fb5eae 5174 watchpoint_del_at_next_stop (b);
c906108c
SS
5175
5176 return WP_DELETED;
5177 }
5178}
5179
18a18393 5180/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5181 breakpoint location BL. This function does not check if we should
5182 stop, only if BL explains the stop. */
5183
18a18393 5184static int
6c95b8df 5185bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5186 struct address_space *aspace, CORE_ADDR bp_addr,
5187 const struct target_waitstatus *ws)
18a18393
VP
5188{
5189 struct breakpoint *b = bl->owner;
5190
348d480f 5191 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5192 gdb_assert (b != NULL);
5193
09ac7c10 5194 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5195}
5196
3a5c3e22
PA
5197/* Determine if the watched values have actually changed, and we
5198 should stop. If not, set BS->stop to 0. */
5199
18a18393
VP
5200static void
5201bpstat_check_watchpoint (bpstat bs)
5202{
2bdf28a0 5203 const struct bp_location *bl;
3a5c3e22 5204 struct watchpoint *b;
2bdf28a0
JK
5205
5206 /* BS is built for existing struct breakpoint. */
f431efe5 5207 bl = bs->bp_location_at;
2bdf28a0 5208 gdb_assert (bl != NULL);
3a5c3e22 5209 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5210 gdb_assert (b != NULL);
18a18393 5211
18a18393 5212 {
18a18393
VP
5213 int must_check_value = 0;
5214
3a5c3e22 5215 if (b->base.type == bp_watchpoint)
18a18393
VP
5216 /* For a software watchpoint, we must always check the
5217 watched value. */
5218 must_check_value = 1;
5219 else if (b->watchpoint_triggered == watch_triggered_yes)
5220 /* We have a hardware watchpoint (read, write, or access)
5221 and the target earlier reported an address watched by
5222 this watchpoint. */
5223 must_check_value = 1;
5224 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5225 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5226 /* We were stopped by a hardware watchpoint, but the target could
5227 not report the data address. We must check the watchpoint's
5228 value. Access and read watchpoints are out of luck; without
5229 a data address, we can't figure it out. */
5230 must_check_value = 1;
3a5c3e22 5231
18a18393
VP
5232 if (must_check_value)
5233 {
3e43a32a
MS
5234 char *message
5235 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5236 b->base.number);
18a18393
VP
5237 struct cleanup *cleanups = make_cleanup (xfree, message);
5238 int e = catch_errors (watchpoint_check, bs, message,
5239 RETURN_MASK_ALL);
5240 do_cleanups (cleanups);
5241 switch (e)
5242 {
5243 case WP_DELETED:
5244 /* We've already printed what needs to be printed. */
5245 bs->print_it = print_it_done;
5246 /* Stop. */
5247 break;
60e1c644
PA
5248 case WP_IGNORE:
5249 bs->print_it = print_it_noop;
5250 bs->stop = 0;
5251 break;
18a18393 5252 case WP_VALUE_CHANGED:
3a5c3e22 5253 if (b->base.type == bp_read_watchpoint)
18a18393 5254 {
85d721b8
PA
5255 /* There are two cases to consider here:
5256
4a64f543 5257 1. We're watching the triggered memory for reads.
85d721b8
PA
5258 In that case, trust the target, and always report
5259 the watchpoint hit to the user. Even though
5260 reads don't cause value changes, the value may
5261 have changed since the last time it was read, and
5262 since we're not trapping writes, we will not see
5263 those, and as such we should ignore our notion of
5264 old value.
5265
4a64f543 5266 2. We're watching the triggered memory for both
85d721b8
PA
5267 reads and writes. There are two ways this may
5268 happen:
5269
4a64f543 5270 2.1. This is a target that can't break on data
85d721b8
PA
5271 reads only, but can break on accesses (reads or
5272 writes), such as e.g., x86. We detect this case
5273 at the time we try to insert read watchpoints.
5274
4a64f543 5275 2.2. Otherwise, the target supports read
85d721b8
PA
5276 watchpoints, but, the user set an access or write
5277 watchpoint watching the same memory as this read
5278 watchpoint.
5279
5280 If we're watching memory writes as well as reads,
5281 ignore watchpoint hits when we find that the
5282 value hasn't changed, as reads don't cause
5283 changes. This still gives false positives when
5284 the program writes the same value to memory as
5285 what there was already in memory (we will confuse
5286 it for a read), but it's much better than
5287 nothing. */
5288
5289 int other_write_watchpoint = 0;
5290
5291 if (bl->watchpoint_type == hw_read)
5292 {
5293 struct breakpoint *other_b;
5294
5295 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5296 if (other_b->type == bp_hardware_watchpoint
5297 || other_b->type == bp_access_watchpoint)
85d721b8 5298 {
3a5c3e22
PA
5299 struct watchpoint *other_w =
5300 (struct watchpoint *) other_b;
5301
5302 if (other_w->watchpoint_triggered
5303 == watch_triggered_yes)
5304 {
5305 other_write_watchpoint = 1;
5306 break;
5307 }
85d721b8
PA
5308 }
5309 }
5310
5311 if (other_write_watchpoint
5312 || bl->watchpoint_type == hw_access)
5313 {
5314 /* We're watching the same memory for writes,
5315 and the value changed since the last time we
5316 updated it, so this trap must be for a write.
5317 Ignore it. */
5318 bs->print_it = print_it_noop;
5319 bs->stop = 0;
5320 }
18a18393
VP
5321 }
5322 break;
5323 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5324 if (b->base.type == bp_hardware_watchpoint
5325 || b->base.type == bp_watchpoint)
18a18393
VP
5326 {
5327 /* Don't stop: write watchpoints shouldn't fire if
5328 the value hasn't changed. */
5329 bs->print_it = print_it_noop;
5330 bs->stop = 0;
5331 }
5332 /* Stop. */
5333 break;
5334 default:
5335 /* Can't happen. */
5336 case 0:
5337 /* Error from catch_errors. */
3a5c3e22 5338 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5339 watchpoint_del_at_next_stop (b);
18a18393
VP
5340 /* We've already printed what needs to be printed. */
5341 bs->print_it = print_it_done;
5342 break;
5343 }
5344 }
5345 else /* must_check_value == 0 */
5346 {
5347 /* This is a case where some watchpoint(s) triggered, but
5348 not at the address of this watchpoint, or else no
5349 watchpoint triggered after all. So don't print
5350 anything for this watchpoint. */
5351 bs->print_it = print_it_noop;
5352 bs->stop = 0;
5353 }
5354 }
5355}
5356
7d4df6a4
DE
5357/* For breakpoints that are currently marked as telling gdb to stop,
5358 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5359 of breakpoint referred to by BS. If we should not stop for this
5360 breakpoint, set BS->stop to 0. */
f431efe5 5361
18a18393
VP
5362static void
5363bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5364{
2bdf28a0
JK
5365 const struct bp_location *bl;
5366 struct breakpoint *b;
7d4df6a4
DE
5367 int value_is_zero = 0;
5368 struct expression *cond;
5369
5370 gdb_assert (bs->stop);
2bdf28a0
JK
5371
5372 /* BS is built for existing struct breakpoint. */
f431efe5 5373 bl = bs->bp_location_at;
2bdf28a0 5374 gdb_assert (bl != NULL);
f431efe5 5375 b = bs->breakpoint_at;
2bdf28a0 5376 gdb_assert (b != NULL);
18a18393 5377
b775012e
LM
5378 /* Even if the target evaluated the condition on its end and notified GDB, we
5379 need to do so again since GDB does not know if we stopped due to a
5380 breakpoint or a single step breakpoint. */
5381
18a18393 5382 if (frame_id_p (b->frame_id)
edb3359d 5383 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5384 {
7d4df6a4
DE
5385 bs->stop = 0;
5386 return;
5387 }
60e1c644 5388
12ab52e9
PA
5389 /* If this is a thread/task-specific breakpoint, don't waste cpu
5390 evaluating the condition if this isn't the specified
5391 thread/task. */
5392 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5393 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5394
6c1b0f7b
DE
5395 {
5396 bs->stop = 0;
5397 return;
5398 }
5399
6dddc817
DE
5400 /* Evaluate extension language breakpoints that have a "stop" method
5401 implemented. */
5402 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5403
7d4df6a4
DE
5404 if (is_watchpoint (b))
5405 {
5406 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5407
7d4df6a4
DE
5408 cond = w->cond_exp;
5409 }
5410 else
5411 cond = bl->cond;
60e1c644 5412
7d4df6a4
DE
5413 if (cond && b->disposition != disp_del_at_next_stop)
5414 {
5415 int within_current_scope = 1;
5416 struct watchpoint * w;
60e1c644 5417
7d4df6a4
DE
5418 /* We use value_mark and value_free_to_mark because it could
5419 be a long time before we return to the command level and
5420 call free_all_values. We can't call free_all_values
5421 because we might be in the middle of evaluating a
5422 function call. */
5423 struct value *mark = value_mark ();
5424
5425 if (is_watchpoint (b))
5426 w = (struct watchpoint *) b;
5427 else
5428 w = NULL;
5429
5430 /* Need to select the frame, with all that implies so that
5431 the conditions will have the right context. Because we
5432 use the frame, we will not see an inlined function's
5433 variables when we arrive at a breakpoint at the start
5434 of the inlined function; the current frame will be the
5435 call site. */
5436 if (w == NULL || w->cond_exp_valid_block == NULL)
5437 select_frame (get_current_frame ());
5438 else
18a18393 5439 {
7d4df6a4
DE
5440 struct frame_info *frame;
5441
5442 /* For local watchpoint expressions, which particular
5443 instance of a local is being watched matters, so we
5444 keep track of the frame to evaluate the expression
5445 in. To evaluate the condition however, it doesn't
5446 really matter which instantiation of the function
5447 where the condition makes sense triggers the
5448 watchpoint. This allows an expression like "watch
5449 global if q > 10" set in `func', catch writes to
5450 global on all threads that call `func', or catch
5451 writes on all recursive calls of `func' by a single
5452 thread. We simply always evaluate the condition in
5453 the innermost frame that's executing where it makes
5454 sense to evaluate the condition. It seems
5455 intuitive. */
5456 frame = block_innermost_frame (w->cond_exp_valid_block);
5457 if (frame != NULL)
5458 select_frame (frame);
5459 else
5460 within_current_scope = 0;
18a18393 5461 }
7d4df6a4
DE
5462 if (within_current_scope)
5463 value_is_zero
5464 = catch_errors (breakpoint_cond_eval, cond,
5465 "Error in testing breakpoint condition:\n",
5466 RETURN_MASK_ALL);
5467 else
18a18393 5468 {
7d4df6a4
DE
5469 warning (_("Watchpoint condition cannot be tested "
5470 "in the current scope"));
5471 /* If we failed to set the right context for this
5472 watchpoint, unconditionally report it. */
5473 value_is_zero = 0;
18a18393 5474 }
7d4df6a4
DE
5475 /* FIXME-someday, should give breakpoint #. */
5476 value_free_to_mark (mark);
18a18393 5477 }
7d4df6a4
DE
5478
5479 if (cond && value_is_zero)
5480 {
5481 bs->stop = 0;
5482 }
7d4df6a4
DE
5483 else if (b->ignore_count > 0)
5484 {
5485 b->ignore_count--;
5486 bs->stop = 0;
5487 /* Increase the hit count even though we don't stop. */
5488 ++(b->hit_count);
5489 observer_notify_breakpoint_modified (b);
5490 }
18a18393
VP
5491}
5492
5493
9709f61c 5494/* Get a bpstat associated with having just stopped at address
d983da9c 5495 BP_ADDR in thread PTID.
c906108c 5496
d983da9c 5497 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5498 don't understand this stop. Result is a chain of bpstat's such
5499 that:
c906108c 5500
c5aa993b 5501 if we don't understand the stop, the result is a null pointer.
c906108c 5502
c5aa993b 5503 if we understand why we stopped, the result is not null.
c906108c 5504
c5aa993b
JM
5505 Each element of the chain refers to a particular breakpoint or
5506 watchpoint at which we have stopped. (We may have stopped for
5507 several reasons concurrently.)
c906108c 5508
c5aa993b
JM
5509 Each element of the chain has valid next, breakpoint_at,
5510 commands, FIXME??? fields. */
c906108c
SS
5511
5512bpstat
6c95b8df 5513bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5514 CORE_ADDR bp_addr, ptid_t ptid,
5515 const struct target_waitstatus *ws)
c906108c 5516{
0d381245 5517 struct breakpoint *b = NULL;
afe38095 5518 struct bp_location *bl;
20874c92 5519 struct bp_location *loc;
5760d0ab
JK
5520 /* First item of allocated bpstat's. */
5521 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5522 /* Pointer to the last thing in the chain currently. */
5760d0ab 5523 bpstat bs;
20874c92 5524 int ix;
429374b8 5525 int need_remove_insert;
f431efe5 5526 int removed_any;
c906108c 5527
f431efe5
PA
5528 /* First, build the bpstat chain with locations that explain a
5529 target stop, while being careful to not set the target running,
5530 as that may invalidate locations (in particular watchpoint
5531 locations are recreated). Resuming will happen here with
5532 breakpoint conditions or watchpoint expressions that include
5533 inferior function calls. */
c5aa993b 5534
429374b8
JK
5535 ALL_BREAKPOINTS (b)
5536 {
5537 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5538 continue;
a5606eee 5539
429374b8
JK
5540 for (bl = b->loc; bl != NULL; bl = bl->next)
5541 {
4a64f543
MS
5542 /* For hardware watchpoints, we look only at the first
5543 location. The watchpoint_check function will work on the
5544 entire expression, not the individual locations. For
5545 read watchpoints, the watchpoints_triggered function has
5546 checked all locations already. */
429374b8
JK
5547 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5548 break;
18a18393 5549
f6592439 5550 if (!bl->enabled || bl->shlib_disabled)
429374b8 5551 continue;
c5aa993b 5552
09ac7c10 5553 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5554 continue;
c5aa993b 5555
4a64f543
MS
5556 /* Come here if it's a watchpoint, or if the break address
5557 matches. */
c5aa993b 5558
4a64f543
MS
5559 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5560 explain stop. */
c5aa993b 5561
f431efe5
PA
5562 /* Assume we stop. Should we find a watchpoint that is not
5563 actually triggered, or if the condition of the breakpoint
5564 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5565 bs->stop = 1;
5566 bs->print = 1;
d983da9c 5567
f431efe5
PA
5568 /* If this is a scope breakpoint, mark the associated
5569 watchpoint as triggered so that we will handle the
5570 out-of-scope event. We'll get to the watchpoint next
5571 iteration. */
d0fb5eae 5572 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5573 {
5574 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5575
5576 w->watchpoint_triggered = watch_triggered_yes;
5577 }
f431efe5
PA
5578 }
5579 }
5580
7c16b83e 5581 /* Check if a moribund breakpoint explains the stop. */
f431efe5
PA
5582 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5583 {
f1310107 5584 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5585 {
5760d0ab 5586 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5587 /* For hits of moribund locations, we should just proceed. */
5588 bs->stop = 0;
5589 bs->print = 0;
5590 bs->print_it = print_it_noop;
5591 }
5592 }
5593
edcc5120
TT
5594 /* A bit of special processing for shlib breakpoints. We need to
5595 process solib loading here, so that the lists of loaded and
5596 unloaded libraries are correct before we handle "catch load" and
5597 "catch unload". */
5598 for (bs = bs_head; bs != NULL; bs = bs->next)
5599 {
5d268276 5600 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5601 {
5602 handle_solib_event ();
5603 break;
5604 }
5605 }
5606
f431efe5
PA
5607 /* Now go through the locations that caused the target to stop, and
5608 check whether we're interested in reporting this stop to higher
5609 layers, or whether we should resume the target transparently. */
5610
5611 removed_any = 0;
5612
5760d0ab 5613 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5614 {
5615 if (!bs->stop)
5616 continue;
5617
f431efe5 5618 b = bs->breakpoint_at;
348d480f
PA
5619 b->ops->check_status (bs);
5620 if (bs->stop)
28010a5d 5621 {
348d480f 5622 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5623
429374b8
JK
5624 if (bs->stop)
5625 {
5626 ++(b->hit_count);
8d3788bd 5627 observer_notify_breakpoint_modified (b);
c906108c 5628
4a64f543 5629 /* We will stop here. */
429374b8
JK
5630 if (b->disposition == disp_disable)
5631 {
816338b5
SS
5632 --(b->enable_count);
5633 if (b->enable_count <= 0
5634 && b->enable_state != bp_permanent)
429374b8 5635 b->enable_state = bp_disabled;
f431efe5 5636 removed_any = 1;
429374b8
JK
5637 }
5638 if (b->silent)
5639 bs->print = 0;
5640 bs->commands = b->commands;
9add0f1b 5641 incref_counted_command_line (bs->commands);
abf85f46
JK
5642 if (command_line_is_silent (bs->commands
5643 ? bs->commands->commands : NULL))
5644 bs->print = 0;
9d6e6e84
HZ
5645
5646 b->ops->after_condition_true (bs);
429374b8
JK
5647 }
5648
348d480f 5649 }
a9b3a50f
PA
5650
5651 /* Print nothing for this entry if we don't stop or don't
5652 print. */
5653 if (!bs->stop || !bs->print)
5654 bs->print_it = print_it_noop;
429374b8 5655 }
876fa593 5656
d983da9c
DJ
5657 /* If we aren't stopping, the value of some hardware watchpoint may
5658 not have changed, but the intermediate memory locations we are
5659 watching may have. Don't bother if we're stopping; this will get
5660 done later. */
d832cb68 5661 need_remove_insert = 0;
5760d0ab
JK
5662 if (! bpstat_causes_stop (bs_head))
5663 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5664 if (!bs->stop
f431efe5
PA
5665 && bs->breakpoint_at
5666 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5667 {
3a5c3e22
PA
5668 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5669
5670 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5671 need_remove_insert = 1;
d983da9c
DJ
5672 }
5673
d832cb68 5674 if (need_remove_insert)
44702360 5675 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5676 else if (removed_any)
44702360 5677 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5678
5760d0ab 5679 return bs_head;
c906108c 5680}
628fe4e4
JK
5681
5682static void
5683handle_jit_event (void)
5684{
5685 struct frame_info *frame;
5686 struct gdbarch *gdbarch;
5687
5688 /* Switch terminal for any messages produced by
5689 breakpoint_re_set. */
5690 target_terminal_ours_for_output ();
5691
5692 frame = get_current_frame ();
5693 gdbarch = get_frame_arch (frame);
5694
5695 jit_event_handler (gdbarch);
5696
5697 target_terminal_inferior ();
5698}
5699
5700/* Prepare WHAT final decision for infrun. */
5701
5702/* Decide what infrun needs to do with this bpstat. */
5703
c906108c 5704struct bpstat_what
0e30163f 5705bpstat_what (bpstat bs_head)
c906108c 5706{
c906108c 5707 struct bpstat_what retval;
628fe4e4 5708 int jit_event = 0;
0e30163f 5709 bpstat bs;
c906108c 5710
628fe4e4 5711 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5712 retval.call_dummy = STOP_NONE;
186c406b 5713 retval.is_longjmp = 0;
628fe4e4 5714
0e30163f 5715 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5716 {
628fe4e4
JK
5717 /* Extract this BS's action. After processing each BS, we check
5718 if its action overrides all we've seem so far. */
5719 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5720 enum bptype bptype;
5721
c906108c 5722 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5723 {
5724 /* I suspect this can happen if it was a momentary
5725 breakpoint which has since been deleted. */
5726 bptype = bp_none;
5727 }
20874c92 5728 else
f431efe5 5729 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5730
5731 switch (bptype)
c906108c
SS
5732 {
5733 case bp_none:
628fe4e4 5734 break;
c906108c
SS
5735 case bp_breakpoint:
5736 case bp_hardware_breakpoint:
7c16b83e 5737 case bp_single_step:
c906108c
SS
5738 case bp_until:
5739 case bp_finish:
a9b3a50f 5740 case bp_shlib_event:
c906108c
SS
5741 if (bs->stop)
5742 {
5743 if (bs->print)
628fe4e4 5744 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5745 else
628fe4e4 5746 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5747 }
5748 else
628fe4e4 5749 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5750 break;
5751 case bp_watchpoint:
5752 case bp_hardware_watchpoint:
5753 case bp_read_watchpoint:
5754 case bp_access_watchpoint:
5755 if (bs->stop)
5756 {
5757 if (bs->print)
628fe4e4 5758 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5759 else
628fe4e4 5760 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5761 }
5762 else
628fe4e4
JK
5763 {
5764 /* There was a watchpoint, but we're not stopping.
5765 This requires no further action. */
5766 }
c906108c
SS
5767 break;
5768 case bp_longjmp:
e2e4d78b 5769 case bp_longjmp_call_dummy:
186c406b 5770 case bp_exception:
628fe4e4 5771 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5772 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5773 break;
5774 case bp_longjmp_resume:
186c406b 5775 case bp_exception_resume:
628fe4e4 5776 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5777 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5778 break;
5779 case bp_step_resume:
5780 if (bs->stop)
628fe4e4
JK
5781 this_action = BPSTAT_WHAT_STEP_RESUME;
5782 else
c906108c 5783 {
628fe4e4
JK
5784 /* It is for the wrong frame. */
5785 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5786 }
c906108c 5787 break;
2c03e5be
PA
5788 case bp_hp_step_resume:
5789 if (bs->stop)
5790 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5791 else
5792 {
5793 /* It is for the wrong frame. */
5794 this_action = BPSTAT_WHAT_SINGLE;
5795 }
5796 break;
c906108c 5797 case bp_watchpoint_scope:
c4093a6a 5798 case bp_thread_event:
1900040c 5799 case bp_overlay_event:
0fd8e87f 5800 case bp_longjmp_master:
aa7d318d 5801 case bp_std_terminate_master:
186c406b 5802 case bp_exception_master:
628fe4e4 5803 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5804 break;
ce78b96d 5805 case bp_catchpoint:
c5aa993b
JM
5806 if (bs->stop)
5807 {
5808 if (bs->print)
628fe4e4 5809 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5810 else
628fe4e4 5811 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5812 }
5813 else
628fe4e4
JK
5814 {
5815 /* There was a catchpoint, but we're not stopping.
5816 This requires no further action. */
5817 }
5818 break;
628fe4e4
JK
5819 case bp_jit_event:
5820 jit_event = 1;
5821 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5822 break;
c906108c 5823 case bp_call_dummy:
53a5351d
JM
5824 /* Make sure the action is stop (silent or noisy),
5825 so infrun.c pops the dummy frame. */
aa7d318d 5826 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5827 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5828 break;
5829 case bp_std_terminate:
5830 /* Make sure the action is stop (silent or noisy),
5831 so infrun.c pops the dummy frame. */
aa7d318d 5832 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5833 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5834 break;
1042e4c0 5835 case bp_tracepoint:
7a697b8d 5836 case bp_fast_tracepoint:
0fb4aa4b 5837 case bp_static_tracepoint:
1042e4c0
SS
5838 /* Tracepoint hits should not be reported back to GDB, and
5839 if one got through somehow, it should have been filtered
5840 out already. */
5841 internal_error (__FILE__, __LINE__,
7a697b8d 5842 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5843 break;
5844 case bp_gnu_ifunc_resolver:
5845 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5846 this_action = BPSTAT_WHAT_SINGLE;
5847 break;
5848 case bp_gnu_ifunc_resolver_return:
5849 /* The breakpoint will be removed, execution will restart from the
5850 PC of the former breakpoint. */
5851 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5852 break;
e7e0cddf
SS
5853
5854 case bp_dprintf:
a11cfd87
HZ
5855 if (bs->stop)
5856 this_action = BPSTAT_WHAT_STOP_SILENT;
5857 else
5858 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5859 break;
5860
628fe4e4
JK
5861 default:
5862 internal_error (__FILE__, __LINE__,
5863 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5864 }
628fe4e4
JK
5865
5866 retval.main_action = max (retval.main_action, this_action);
c906108c 5867 }
628fe4e4 5868
0e30163f
JK
5869 /* These operations may affect the bs->breakpoint_at state so they are
5870 delayed after MAIN_ACTION is decided above. */
5871
628fe4e4
JK
5872 if (jit_event)
5873 {
5874 if (debug_infrun)
5875 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5876
5877 handle_jit_event ();
5878 }
5879
0e30163f
JK
5880 for (bs = bs_head; bs != NULL; bs = bs->next)
5881 {
5882 struct breakpoint *b = bs->breakpoint_at;
5883
5884 if (b == NULL)
5885 continue;
5886 switch (b->type)
5887 {
5888 case bp_gnu_ifunc_resolver:
5889 gnu_ifunc_resolver_stop (b);
5890 break;
5891 case bp_gnu_ifunc_resolver_return:
5892 gnu_ifunc_resolver_return_stop (b);
5893 break;
5894 }
5895 }
5896
c906108c
SS
5897 return retval;
5898}
5899
5900/* Nonzero if we should step constantly (e.g. watchpoints on machines
5901 without hardware support). This isn't related to a specific bpstat,
5902 just to things like whether watchpoints are set. */
5903
c5aa993b 5904int
fba45db2 5905bpstat_should_step (void)
c906108c
SS
5906{
5907 struct breakpoint *b;
cc59ec59 5908
c906108c 5909 ALL_BREAKPOINTS (b)
717a8278 5910 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5911 return 1;
c906108c
SS
5912 return 0;
5913}
5914
67822962
PA
5915int
5916bpstat_causes_stop (bpstat bs)
5917{
5918 for (; bs != NULL; bs = bs->next)
5919 if (bs->stop)
5920 return 1;
5921
5922 return 0;
5923}
5924
c906108c 5925\f
c5aa993b 5926
170b53b2
UW
5927/* Compute a string of spaces suitable to indent the next line
5928 so it starts at the position corresponding to the table column
5929 named COL_NAME in the currently active table of UIOUT. */
5930
5931static char *
5932wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5933{
5934 static char wrap_indent[80];
5935 int i, total_width, width, align;
5936 char *text;
5937
5938 total_width = 0;
5939 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5940 {
5941 if (strcmp (text, col_name) == 0)
5942 {
5943 gdb_assert (total_width < sizeof wrap_indent);
5944 memset (wrap_indent, ' ', total_width);
5945 wrap_indent[total_width] = 0;
5946
5947 return wrap_indent;
5948 }
5949
5950 total_width += width + 1;
5951 }
5952
5953 return NULL;
5954}
5955
b775012e
LM
5956/* Determine if the locations of this breakpoint will have their conditions
5957 evaluated by the target, host or a mix of both. Returns the following:
5958
5959 "host": Host evals condition.
5960 "host or target": Host or Target evals condition.
5961 "target": Target evals condition.
5962*/
5963
5964static const char *
5965bp_condition_evaluator (struct breakpoint *b)
5966{
5967 struct bp_location *bl;
5968 char host_evals = 0;
5969 char target_evals = 0;
5970
5971 if (!b)
5972 return NULL;
5973
5974 if (!is_breakpoint (b))
5975 return NULL;
5976
5977 if (gdb_evaluates_breakpoint_condition_p ()
5978 || !target_supports_evaluation_of_breakpoint_conditions ())
5979 return condition_evaluation_host;
5980
5981 for (bl = b->loc; bl; bl = bl->next)
5982 {
5983 if (bl->cond_bytecode)
5984 target_evals++;
5985 else
5986 host_evals++;
5987 }
5988
5989 if (host_evals && target_evals)
5990 return condition_evaluation_both;
5991 else if (target_evals)
5992 return condition_evaluation_target;
5993 else
5994 return condition_evaluation_host;
5995}
5996
5997/* Determine the breakpoint location's condition evaluator. This is
5998 similar to bp_condition_evaluator, but for locations. */
5999
6000static const char *
6001bp_location_condition_evaluator (struct bp_location *bl)
6002{
6003 if (bl && !is_breakpoint (bl->owner))
6004 return NULL;
6005
6006 if (gdb_evaluates_breakpoint_condition_p ()
6007 || !target_supports_evaluation_of_breakpoint_conditions ())
6008 return condition_evaluation_host;
6009
6010 if (bl && bl->cond_bytecode)
6011 return condition_evaluation_target;
6012 else
6013 return condition_evaluation_host;
6014}
6015
859825b8
JK
6016/* Print the LOC location out of the list of B->LOC locations. */
6017
170b53b2
UW
6018static void
6019print_breakpoint_location (struct breakpoint *b,
6020 struct bp_location *loc)
0d381245 6021{
79a45e25 6022 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6023 struct cleanup *old_chain = save_current_program_space ();
6024
859825b8
JK
6025 if (loc != NULL && loc->shlib_disabled)
6026 loc = NULL;
6027
6c95b8df
PA
6028 if (loc != NULL)
6029 set_current_program_space (loc->pspace);
6030
56435ebe
TT
6031 if (b->display_canonical)
6032 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 6033 else if (loc && loc->symtab)
0d381245
VP
6034 {
6035 struct symbol *sym
6036 = find_pc_sect_function (loc->address, loc->section);
6037 if (sym)
6038 {
6039 ui_out_text (uiout, "in ");
6040 ui_out_field_string (uiout, "func",
6041 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6042 ui_out_text (uiout, " ");
6043 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6044 ui_out_text (uiout, "at ");
0d381245 6045 }
05cba821
JK
6046 ui_out_field_string (uiout, "file",
6047 symtab_to_filename_for_display (loc->symtab));
0d381245 6048 ui_out_text (uiout, ":");
05cba821 6049
0d381245 6050 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6051 ui_out_field_string (uiout, "fullname",
6052 symtab_to_fullname (loc->symtab));
0d381245 6053
f8eba3c6 6054 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6055 }
859825b8 6056 else if (loc)
0d381245 6057 {
f99d8bf4
PA
6058 struct ui_file *stb = mem_fileopen ();
6059 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6060
f99d8bf4 6061 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6062 demangle, "");
0d381245 6063 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6064
6065 do_cleanups (stb_chain);
0d381245 6066 }
859825b8
JK
6067 else
6068 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 6069
b775012e
LM
6070 if (loc && is_breakpoint (b)
6071 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6072 && bp_condition_evaluator (b) == condition_evaluation_both)
6073 {
6074 ui_out_text (uiout, " (");
6075 ui_out_field_string (uiout, "evaluated-by",
6076 bp_location_condition_evaluator (loc));
6077 ui_out_text (uiout, ")");
6078 }
6079
6c95b8df 6080 do_cleanups (old_chain);
0d381245
VP
6081}
6082
269b11a2
PA
6083static const char *
6084bptype_string (enum bptype type)
c906108c 6085{
c4093a6a
JM
6086 struct ep_type_description
6087 {
6088 enum bptype type;
6089 char *description;
6090 };
6091 static struct ep_type_description bptypes[] =
c906108c 6092 {
c5aa993b
JM
6093 {bp_none, "?deleted?"},
6094 {bp_breakpoint, "breakpoint"},
c906108c 6095 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6096 {bp_single_step, "sw single-step"},
c5aa993b
JM
6097 {bp_until, "until"},
6098 {bp_finish, "finish"},
6099 {bp_watchpoint, "watchpoint"},
c906108c 6100 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6101 {bp_read_watchpoint, "read watchpoint"},
6102 {bp_access_watchpoint, "acc watchpoint"},
6103 {bp_longjmp, "longjmp"},
6104 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6105 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6106 {bp_exception, "exception"},
6107 {bp_exception_resume, "exception resume"},
c5aa993b 6108 {bp_step_resume, "step resume"},
2c03e5be 6109 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6110 {bp_watchpoint_scope, "watchpoint scope"},
6111 {bp_call_dummy, "call dummy"},
aa7d318d 6112 {bp_std_terminate, "std::terminate"},
c5aa993b 6113 {bp_shlib_event, "shlib events"},
c4093a6a 6114 {bp_thread_event, "thread events"},
1900040c 6115 {bp_overlay_event, "overlay events"},
0fd8e87f 6116 {bp_longjmp_master, "longjmp master"},
aa7d318d 6117 {bp_std_terminate_master, "std::terminate master"},
186c406b 6118 {bp_exception_master, "exception master"},
ce78b96d 6119 {bp_catchpoint, "catchpoint"},
1042e4c0 6120 {bp_tracepoint, "tracepoint"},
7a697b8d 6121 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6122 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6123 {bp_dprintf, "dprintf"},
4efc6507 6124 {bp_jit_event, "jit events"},
0e30163f
JK
6125 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6126 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6127 };
269b11a2
PA
6128
6129 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6130 || ((int) type != bptypes[(int) type].type))
6131 internal_error (__FILE__, __LINE__,
6132 _("bptypes table does not describe type #%d."),
6133 (int) type);
6134
6135 return bptypes[(int) type].description;
6136}
6137
998580f1
MK
6138/* For MI, output a field named 'thread-groups' with a list as the value.
6139 For CLI, prefix the list with the string 'inf'. */
6140
6141static void
6142output_thread_groups (struct ui_out *uiout,
6143 const char *field_name,
6144 VEC(int) *inf_num,
6145 int mi_only)
6146{
752eb8b4 6147 struct cleanup *back_to;
998580f1
MK
6148 int is_mi = ui_out_is_mi_like_p (uiout);
6149 int inf;
6150 int i;
6151
6152 /* For backward compatibility, don't display inferiors in CLI unless
6153 there are several. Always display them for MI. */
6154 if (!is_mi && mi_only)
6155 return;
6156
752eb8b4
TT
6157 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6158
998580f1
MK
6159 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6160 {
6161 if (is_mi)
6162 {
6163 char mi_group[10];
6164
6165 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6166 ui_out_field_string (uiout, NULL, mi_group);
6167 }
6168 else
6169 {
6170 if (i == 0)
6171 ui_out_text (uiout, " inf ");
6172 else
6173 ui_out_text (uiout, ", ");
6174
6175 ui_out_text (uiout, plongest (inf));
6176 }
6177 }
6178
6179 do_cleanups (back_to);
6180}
6181
269b11a2
PA
6182/* Print B to gdb_stdout. */
6183
6184static void
6185print_one_breakpoint_location (struct breakpoint *b,
6186 struct bp_location *loc,
6187 int loc_number,
6188 struct bp_location **last_loc,
269b11a2
PA
6189 int allflag)
6190{
6191 struct command_line *l;
c2c6d25f 6192 static char bpenables[] = "nynny";
c906108c 6193
79a45e25 6194 struct ui_out *uiout = current_uiout;
0d381245
VP
6195 int header_of_multiple = 0;
6196 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6197 struct value_print_options opts;
6198
6199 get_user_print_options (&opts);
0d381245
VP
6200
6201 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6202 /* See comment in print_one_breakpoint concerning treatment of
6203 breakpoints with single disabled location. */
0d381245
VP
6204 if (loc == NULL
6205 && (b->loc != NULL
6206 && (b->loc->next != NULL || !b->loc->enabled)))
6207 header_of_multiple = 1;
6208 if (loc == NULL)
6209 loc = b->loc;
6210
c4093a6a
JM
6211 annotate_record ();
6212
6213 /* 1 */
6214 annotate_field (0);
0d381245
VP
6215 if (part_of_multiple)
6216 {
6217 char *formatted;
0c6773c1 6218 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6219 ui_out_field_string (uiout, "number", formatted);
6220 xfree (formatted);
6221 }
6222 else
6223 {
6224 ui_out_field_int (uiout, "number", b->number);
6225 }
c4093a6a
JM
6226
6227 /* 2 */
6228 annotate_field (1);
0d381245
VP
6229 if (part_of_multiple)
6230 ui_out_field_skip (uiout, "type");
269b11a2
PA
6231 else
6232 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6233
6234 /* 3 */
6235 annotate_field (2);
0d381245
VP
6236 if (part_of_multiple)
6237 ui_out_field_skip (uiout, "disp");
6238 else
2cec12e5 6239 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6240
c4093a6a
JM
6241
6242 /* 4 */
6243 annotate_field (3);
0d381245 6244 if (part_of_multiple)
54e52265 6245 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6246 else
4a64f543
MS
6247 ui_out_field_fmt (uiout, "enabled", "%c",
6248 bpenables[(int) b->enable_state]);
54e52265 6249 ui_out_spaces (uiout, 2);
0d381245 6250
c4093a6a
JM
6251
6252 /* 5 and 6 */
3086aeae 6253 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6254 {
4a64f543
MS
6255 /* Although the print_one can possibly print all locations,
6256 calling it here is not likely to get any nice result. So,
6257 make sure there's just one location. */
0d381245 6258 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6259 b->ops->print_one (b, last_loc);
0d381245 6260 }
3086aeae
DJ
6261 else
6262 switch (b->type)
6263 {
6264 case bp_none:
6265 internal_error (__FILE__, __LINE__,
e2e0b3e5 6266 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6267 break;
c906108c 6268
3086aeae
DJ
6269 case bp_watchpoint:
6270 case bp_hardware_watchpoint:
6271 case bp_read_watchpoint:
6272 case bp_access_watchpoint:
3a5c3e22
PA
6273 {
6274 struct watchpoint *w = (struct watchpoint *) b;
6275
6276 /* Field 4, the address, is omitted (which makes the columns
6277 not line up too nicely with the headers, but the effect
6278 is relatively readable). */
6279 if (opts.addressprint)
6280 ui_out_field_skip (uiout, "addr");
6281 annotate_field (5);
6282 ui_out_field_string (uiout, "what", w->exp_string);
6283 }
3086aeae
DJ
6284 break;
6285
3086aeae
DJ
6286 case bp_breakpoint:
6287 case bp_hardware_breakpoint:
7c16b83e 6288 case bp_single_step:
3086aeae
DJ
6289 case bp_until:
6290 case bp_finish:
6291 case bp_longjmp:
6292 case bp_longjmp_resume:
e2e4d78b 6293 case bp_longjmp_call_dummy:
186c406b
TT
6294 case bp_exception:
6295 case bp_exception_resume:
3086aeae 6296 case bp_step_resume:
2c03e5be 6297 case bp_hp_step_resume:
3086aeae
DJ
6298 case bp_watchpoint_scope:
6299 case bp_call_dummy:
aa7d318d 6300 case bp_std_terminate:
3086aeae
DJ
6301 case bp_shlib_event:
6302 case bp_thread_event:
6303 case bp_overlay_event:
0fd8e87f 6304 case bp_longjmp_master:
aa7d318d 6305 case bp_std_terminate_master:
186c406b 6306 case bp_exception_master:
1042e4c0 6307 case bp_tracepoint:
7a697b8d 6308 case bp_fast_tracepoint:
0fb4aa4b 6309 case bp_static_tracepoint:
e7e0cddf 6310 case bp_dprintf:
4efc6507 6311 case bp_jit_event:
0e30163f
JK
6312 case bp_gnu_ifunc_resolver:
6313 case bp_gnu_ifunc_resolver_return:
79a45b7d 6314 if (opts.addressprint)
3086aeae
DJ
6315 {
6316 annotate_field (4);
54e52265 6317 if (header_of_multiple)
0d381245 6318 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6319 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6320 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6321 else
5af949e3
UW
6322 ui_out_field_core_addr (uiout, "addr",
6323 loc->gdbarch, loc->address);
3086aeae
DJ
6324 }
6325 annotate_field (5);
0d381245 6326 if (!header_of_multiple)
170b53b2 6327 print_breakpoint_location (b, loc);
0d381245 6328 if (b->loc)
a6d9a66e 6329 *last_loc = b->loc;
3086aeae
DJ
6330 break;
6331 }
c906108c 6332
6c95b8df 6333
998580f1 6334 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6335 {
6336 struct inferior *inf;
998580f1
MK
6337 VEC(int) *inf_num = NULL;
6338 int mi_only = 1;
6c95b8df 6339
998580f1 6340 ALL_INFERIORS (inf)
6c95b8df
PA
6341 {
6342 if (inf->pspace == loc->pspace)
998580f1 6343 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6344 }
998580f1
MK
6345
6346 /* For backward compatibility, don't display inferiors in CLI unless
6347 there are several. Always display for MI. */
6348 if (allflag
6349 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6350 && (number_of_program_spaces () > 1
6351 || number_of_inferiors () > 1)
6352 /* LOC is for existing B, it cannot be in
6353 moribund_locations and thus having NULL OWNER. */
6354 && loc->owner->type != bp_catchpoint))
6355 mi_only = 0;
6356 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6357 VEC_free (int, inf_num);
6c95b8df
PA
6358 }
6359
4a306c9a 6360 if (!part_of_multiple)
c4093a6a 6361 {
4a306c9a
JB
6362 if (b->thread != -1)
6363 {
6364 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6365 "stop only in" line a little further down. */
4a306c9a
JB
6366 ui_out_text (uiout, " thread ");
6367 ui_out_field_int (uiout, "thread", b->thread);
6368 }
6369 else if (b->task != 0)
6370 {
6371 ui_out_text (uiout, " task ");
6372 ui_out_field_int (uiout, "task", b->task);
6373 }
c4093a6a 6374 }
f1310107 6375
8b93c638 6376 ui_out_text (uiout, "\n");
f1310107 6377
348d480f 6378 if (!part_of_multiple)
f1310107
TJB
6379 b->ops->print_one_detail (b, uiout);
6380
0d381245 6381 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6382 {
6383 annotate_field (6);
8b93c638 6384 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6385 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6386 the frame ID. */
5af949e3
UW
6387 ui_out_field_core_addr (uiout, "frame",
6388 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6389 ui_out_text (uiout, "\n");
c4093a6a
JM
6390 }
6391
28010a5d 6392 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6393 {
6394 annotate_field (7);
d77f58be 6395 if (is_tracepoint (b))
1042e4c0
SS
6396 ui_out_text (uiout, "\ttrace only if ");
6397 else
6398 ui_out_text (uiout, "\tstop only if ");
0101ce28 6399 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6400
6401 /* Print whether the target is doing the breakpoint's condition
6402 evaluation. If GDB is doing the evaluation, don't print anything. */
6403 if (is_breakpoint (b)
6404 && breakpoint_condition_evaluation_mode ()
6405 == condition_evaluation_target)
6406 {
6407 ui_out_text (uiout, " (");
6408 ui_out_field_string (uiout, "evaluated-by",
6409 bp_condition_evaluator (b));
6410 ui_out_text (uiout, " evals)");
6411 }
0101ce28
JJ
6412 ui_out_text (uiout, "\n");
6413 }
6414
0d381245 6415 if (!part_of_multiple && b->thread != -1)
c4093a6a 6416 {
4a64f543 6417 /* FIXME should make an annotation for this. */
8b93c638
JM
6418 ui_out_text (uiout, "\tstop only in thread ");
6419 ui_out_field_int (uiout, "thread", b->thread);
6420 ui_out_text (uiout, "\n");
c4093a6a
JM
6421 }
6422
556ec64d
YQ
6423 if (!part_of_multiple)
6424 {
6425 if (b->hit_count)
31f56a27
YQ
6426 {
6427 /* FIXME should make an annotation for this. */
6428 if (is_catchpoint (b))
6429 ui_out_text (uiout, "\tcatchpoint");
6430 else if (is_tracepoint (b))
6431 ui_out_text (uiout, "\ttracepoint");
6432 else
6433 ui_out_text (uiout, "\tbreakpoint");
6434 ui_out_text (uiout, " already hit ");
6435 ui_out_field_int (uiout, "times", b->hit_count);
6436 if (b->hit_count == 1)
6437 ui_out_text (uiout, " time\n");
6438 else
6439 ui_out_text (uiout, " times\n");
6440 }
556ec64d
YQ
6441 else
6442 {
31f56a27
YQ
6443 /* Output the count also if it is zero, but only if this is mi. */
6444 if (ui_out_is_mi_like_p (uiout))
6445 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6446 }
6447 }
8b93c638 6448
0d381245 6449 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6450 {
6451 annotate_field (8);
8b93c638
JM
6452 ui_out_text (uiout, "\tignore next ");
6453 ui_out_field_int (uiout, "ignore", b->ignore_count);
6454 ui_out_text (uiout, " hits\n");
c4093a6a 6455 }
059fb39f 6456
816338b5
SS
6457 /* Note that an enable count of 1 corresponds to "enable once"
6458 behavior, which is reported by the combination of enablement and
6459 disposition, so we don't need to mention it here. */
6460 if (!part_of_multiple && b->enable_count > 1)
6461 {
6462 annotate_field (8);
6463 ui_out_text (uiout, "\tdisable after ");
6464 /* Tweak the wording to clarify that ignore and enable counts
6465 are distinct, and have additive effect. */
6466 if (b->ignore_count)
6467 ui_out_text (uiout, "additional ");
6468 else
6469 ui_out_text (uiout, "next ");
6470 ui_out_field_int (uiout, "enable", b->enable_count);
6471 ui_out_text (uiout, " hits\n");
6472 }
6473
f196051f
SS
6474 if (!part_of_multiple && is_tracepoint (b))
6475 {
6476 struct tracepoint *tp = (struct tracepoint *) b;
6477
6478 if (tp->traceframe_usage)
6479 {
6480 ui_out_text (uiout, "\ttrace buffer usage ");
6481 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6482 ui_out_text (uiout, " bytes\n");
6483 }
6484 }
d3ce09f5 6485
9add0f1b 6486 l = b->commands ? b->commands->commands : NULL;
059fb39f 6487 if (!part_of_multiple && l)
c4093a6a 6488 {
3b31d625
EZ
6489 struct cleanup *script_chain;
6490
c4093a6a 6491 annotate_field (9);
3b31d625 6492 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6493 print_command_lines (uiout, l, 4);
3b31d625 6494 do_cleanups (script_chain);
c4093a6a 6495 }
d24317b4 6496
d9b3f62e 6497 if (is_tracepoint (b))
1042e4c0 6498 {
d9b3f62e
PA
6499 struct tracepoint *t = (struct tracepoint *) b;
6500
6501 if (!part_of_multiple && t->pass_count)
6502 {
6503 annotate_field (10);
6504 ui_out_text (uiout, "\tpass count ");
6505 ui_out_field_int (uiout, "pass", t->pass_count);
6506 ui_out_text (uiout, " \n");
6507 }
f2a8bc8a
YQ
6508
6509 /* Don't display it when tracepoint or tracepoint location is
6510 pending. */
6511 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6512 {
6513 annotate_field (11);
6514
6515 if (ui_out_is_mi_like_p (uiout))
6516 ui_out_field_string (uiout, "installed",
6517 loc->inserted ? "y" : "n");
6518 else
6519 {
6520 if (loc->inserted)
6521 ui_out_text (uiout, "\t");
6522 else
6523 ui_out_text (uiout, "\tnot ");
6524 ui_out_text (uiout, "installed on target\n");
6525 }
6526 }
1042e4c0
SS
6527 }
6528
d24317b4
VP
6529 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6530 {
3a5c3e22
PA
6531 if (is_watchpoint (b))
6532 {
6533 struct watchpoint *w = (struct watchpoint *) b;
6534
6535 ui_out_field_string (uiout, "original-location", w->exp_string);
6536 }
6537 else if (b->addr_string)
d24317b4 6538 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6539 }
c4093a6a 6540}
c5aa993b 6541
0d381245
VP
6542static void
6543print_one_breakpoint (struct breakpoint *b,
4a64f543 6544 struct bp_location **last_loc,
6c95b8df 6545 int allflag)
0d381245 6546{
8d3788bd 6547 struct cleanup *bkpt_chain;
79a45e25 6548 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6549
6550 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6551
12c5a436 6552 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6553 do_cleanups (bkpt_chain);
0d381245
VP
6554
6555 /* If this breakpoint has custom print function,
6556 it's already printed. Otherwise, print individual
6557 locations, if any. */
6558 if (b->ops == NULL || b->ops->print_one == NULL)
6559 {
4a64f543
MS
6560 /* If breakpoint has a single location that is disabled, we
6561 print it as if it had several locations, since otherwise it's
6562 hard to represent "breakpoint enabled, location disabled"
6563 situation.
6564
6565 Note that while hardware watchpoints have several locations
a3be7890 6566 internally, that's not a property exposed to user. */
0d381245 6567 if (b->loc
a5606eee 6568 && !is_hardware_watchpoint (b)
8d3788bd 6569 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6570 {
6571 struct bp_location *loc;
6572 int n = 1;
8d3788bd 6573
0d381245 6574 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6575 {
6576 struct cleanup *inner2 =
6577 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6578 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6579 do_cleanups (inner2);
6580 }
0d381245
VP
6581 }
6582 }
6583}
6584
a6d9a66e
UW
6585static int
6586breakpoint_address_bits (struct breakpoint *b)
6587{
6588 int print_address_bits = 0;
6589 struct bp_location *loc;
6590
6591 for (loc = b->loc; loc; loc = loc->next)
6592 {
c7437ca6
PA
6593 int addr_bit;
6594
6595 /* Software watchpoints that aren't watching memory don't have
6596 an address to print. */
6597 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6598 continue;
6599
6600 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6601 if (addr_bit > print_address_bits)
6602 print_address_bits = addr_bit;
6603 }
6604
6605 return print_address_bits;
6606}
0d381245 6607
c4093a6a
JM
6608struct captured_breakpoint_query_args
6609 {
6610 int bnum;
6611 };
c5aa993b 6612
c4093a6a 6613static int
2b65245e 6614do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6615{
6616 struct captured_breakpoint_query_args *args = data;
52f0bd74 6617 struct breakpoint *b;
a6d9a66e 6618 struct bp_location *dummy_loc = NULL;
cc59ec59 6619
c4093a6a
JM
6620 ALL_BREAKPOINTS (b)
6621 {
6622 if (args->bnum == b->number)
c5aa993b 6623 {
12c5a436 6624 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6625 return GDB_RC_OK;
c5aa993b 6626 }
c4093a6a
JM
6627 }
6628 return GDB_RC_NONE;
6629}
c5aa993b 6630
c4093a6a 6631enum gdb_rc
4a64f543
MS
6632gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6633 char **error_message)
c4093a6a
JM
6634{
6635 struct captured_breakpoint_query_args args;
cc59ec59 6636
c4093a6a
JM
6637 args.bnum = bnum;
6638 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6639 an error. */
b0b13bb4
DJ
6640 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6641 error_message, RETURN_MASK_ALL) < 0)
6642 return GDB_RC_FAIL;
6643 else
6644 return GDB_RC_OK;
c4093a6a 6645}
c5aa993b 6646
09d682a4
TT
6647/* Return true if this breakpoint was set by the user, false if it is
6648 internal or momentary. */
6649
6650int
6651user_breakpoint_p (struct breakpoint *b)
6652{
46c6471b 6653 return b->number > 0;
09d682a4
TT
6654}
6655
7f3b0473 6656/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6657 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6658 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6659 FILTER is non-NULL, call it on each breakpoint and only include the
6660 ones for which it returns non-zero. Return the total number of
6661 breakpoints listed. */
c906108c 6662
d77f58be 6663static int
e5a67952 6664breakpoint_1 (char *args, int allflag,
4a64f543 6665 int (*filter) (const struct breakpoint *))
c4093a6a 6666{
52f0bd74 6667 struct breakpoint *b;
a6d9a66e 6668 struct bp_location *last_loc = NULL;
7f3b0473 6669 int nr_printable_breakpoints;
3b31d625 6670 struct cleanup *bkpttbl_chain;
79a45b7d 6671 struct value_print_options opts;
a6d9a66e 6672 int print_address_bits = 0;
269b11a2 6673 int print_type_col_width = 14;
79a45e25 6674 struct ui_out *uiout = current_uiout;
269b11a2 6675
79a45b7d
TT
6676 get_user_print_options (&opts);
6677
4a64f543
MS
6678 /* Compute the number of rows in the table, as well as the size
6679 required for address fields. */
7f3b0473
AC
6680 nr_printable_breakpoints = 0;
6681 ALL_BREAKPOINTS (b)
e5a67952
MS
6682 {
6683 /* If we have a filter, only list the breakpoints it accepts. */
6684 if (filter && !filter (b))
6685 continue;
6686
6687 /* If we have an "args" string, it is a list of breakpoints to
6688 accept. Skip the others. */
6689 if (args != NULL && *args != '\0')
6690 {
6691 if (allflag && parse_and_eval_long (args) != b->number)
6692 continue;
6693 if (!allflag && !number_is_in_list (args, b->number))
6694 continue;
6695 }
269b11a2 6696
e5a67952
MS
6697 if (allflag || user_breakpoint_p (b))
6698 {
6699 int addr_bit, type_len;
a6d9a66e 6700
e5a67952
MS
6701 addr_bit = breakpoint_address_bits (b);
6702 if (addr_bit > print_address_bits)
6703 print_address_bits = addr_bit;
269b11a2 6704
e5a67952
MS
6705 type_len = strlen (bptype_string (b->type));
6706 if (type_len > print_type_col_width)
6707 print_type_col_width = type_len;
6708
6709 nr_printable_breakpoints++;
6710 }
6711 }
7f3b0473 6712
79a45b7d 6713 if (opts.addressprint)
3b31d625 6714 bkpttbl_chain
3e43a32a
MS
6715 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6716 nr_printable_breakpoints,
3b31d625 6717 "BreakpointTable");
8b93c638 6718 else
3b31d625 6719 bkpttbl_chain
3e43a32a
MS
6720 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6721 nr_printable_breakpoints,
3b31d625 6722 "BreakpointTable");
8b93c638 6723
7f3b0473 6724 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6725 annotate_breakpoints_headers ();
6726 if (nr_printable_breakpoints > 0)
6727 annotate_field (0);
4a64f543 6728 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6729 if (nr_printable_breakpoints > 0)
6730 annotate_field (1);
269b11a2 6731 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6732 "type", "Type"); /* 2 */
d7faa9e7
AC
6733 if (nr_printable_breakpoints > 0)
6734 annotate_field (2);
4a64f543 6735 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6736 if (nr_printable_breakpoints > 0)
6737 annotate_field (3);
54e52265 6738 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6739 if (opts.addressprint)
e5a67952
MS
6740 {
6741 if (nr_printable_breakpoints > 0)
6742 annotate_field (4);
6743 if (print_address_bits <= 32)
6744 ui_out_table_header (uiout, 10, ui_left,
6745 "addr", "Address"); /* 5 */
6746 else
6747 ui_out_table_header (uiout, 18, ui_left,
6748 "addr", "Address"); /* 5 */
6749 }
d7faa9e7
AC
6750 if (nr_printable_breakpoints > 0)
6751 annotate_field (5);
6752 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6753 ui_out_table_body (uiout);
6754 if (nr_printable_breakpoints > 0)
6755 annotate_breakpoints_table ();
7f3b0473 6756
c4093a6a 6757 ALL_BREAKPOINTS (b)
e5a67952
MS
6758 {
6759 QUIT;
6760 /* If we have a filter, only list the breakpoints it accepts. */
6761 if (filter && !filter (b))
6762 continue;
6763
6764 /* If we have an "args" string, it is a list of breakpoints to
6765 accept. Skip the others. */
6766
6767 if (args != NULL && *args != '\0')
6768 {
6769 if (allflag) /* maintenance info breakpoint */
6770 {
6771 if (parse_and_eval_long (args) != b->number)
6772 continue;
6773 }
6774 else /* all others */
6775 {
6776 if (!number_is_in_list (args, b->number))
6777 continue;
6778 }
6779 }
6780 /* We only print out user settable breakpoints unless the
6781 allflag is set. */
6782 if (allflag || user_breakpoint_p (b))
12c5a436 6783 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6784 }
6785
3b31d625 6786 do_cleanups (bkpttbl_chain);
698384cd 6787
7f3b0473 6788 if (nr_printable_breakpoints == 0)
c906108c 6789 {
4a64f543
MS
6790 /* If there's a filter, let the caller decide how to report
6791 empty list. */
d77f58be
SS
6792 if (!filter)
6793 {
e5a67952 6794 if (args == NULL || *args == '\0')
d77f58be
SS
6795 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6796 else
4a64f543 6797 ui_out_message (uiout, 0,
e5a67952
MS
6798 "No breakpoint or watchpoint matching '%s'.\n",
6799 args);
d77f58be 6800 }
c906108c
SS
6801 }
6802 else
c4093a6a 6803 {
a6d9a66e
UW
6804 if (last_loc && !server_command)
6805 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6806 }
c906108c 6807
4a64f543 6808 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6809 there have been breakpoints? */
c906108c 6810 annotate_breakpoints_table_end ();
d77f58be
SS
6811
6812 return nr_printable_breakpoints;
c906108c
SS
6813}
6814
ad443146
SS
6815/* Display the value of default-collect in a way that is generally
6816 compatible with the breakpoint list. */
6817
6818static void
6819default_collect_info (void)
6820{
79a45e25
PA
6821 struct ui_out *uiout = current_uiout;
6822
ad443146
SS
6823 /* If it has no value (which is frequently the case), say nothing; a
6824 message like "No default-collect." gets in user's face when it's
6825 not wanted. */
6826 if (!*default_collect)
6827 return;
6828
6829 /* The following phrase lines up nicely with per-tracepoint collect
6830 actions. */
6831 ui_out_text (uiout, "default collect ");
6832 ui_out_field_string (uiout, "default-collect", default_collect);
6833 ui_out_text (uiout, " \n");
6834}
6835
c906108c 6836static void
e5a67952 6837breakpoints_info (char *args, int from_tty)
c906108c 6838{
e5a67952 6839 breakpoint_1 (args, 0, NULL);
ad443146
SS
6840
6841 default_collect_info ();
d77f58be
SS
6842}
6843
6844static void
e5a67952 6845watchpoints_info (char *args, int from_tty)
d77f58be 6846{
e5a67952 6847 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6848 struct ui_out *uiout = current_uiout;
d77f58be
SS
6849
6850 if (num_printed == 0)
6851 {
e5a67952 6852 if (args == NULL || *args == '\0')
d77f58be
SS
6853 ui_out_message (uiout, 0, "No watchpoints.\n");
6854 else
e5a67952 6855 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6856 }
c906108c
SS
6857}
6858
7a292a7a 6859static void
e5a67952 6860maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6861{
e5a67952 6862 breakpoint_1 (args, 1, NULL);
ad443146
SS
6863
6864 default_collect_info ();
c906108c
SS
6865}
6866
0d381245 6867static int
714835d5 6868breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6869 struct program_space *pspace,
714835d5 6870 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6871{
6872 struct bp_location *bl = b->loc;
cc59ec59 6873
0d381245
VP
6874 for (; bl; bl = bl->next)
6875 {
6c95b8df
PA
6876 if (bl->pspace == pspace
6877 && bl->address == pc
0d381245
VP
6878 && (!overlay_debugging || bl->section == section))
6879 return 1;
6880 }
6881 return 0;
6882}
6883
672f9b60 6884/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6885 concerns with logical breakpoints, so we match program spaces, not
6886 address spaces. */
c906108c
SS
6887
6888static void
6c95b8df
PA
6889describe_other_breakpoints (struct gdbarch *gdbarch,
6890 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6891 struct obj_section *section, int thread)
c906108c 6892{
52f0bd74
AC
6893 int others = 0;
6894 struct breakpoint *b;
c906108c
SS
6895
6896 ALL_BREAKPOINTS (b)
672f9b60
KP
6897 others += (user_breakpoint_p (b)
6898 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6899 if (others > 0)
6900 {
a3f17187
AC
6901 if (others == 1)
6902 printf_filtered (_("Note: breakpoint "));
6903 else /* if (others == ???) */
6904 printf_filtered (_("Note: breakpoints "));
c906108c 6905 ALL_BREAKPOINTS (b)
672f9b60 6906 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6907 {
6908 others--;
6909 printf_filtered ("%d", b->number);
6910 if (b->thread == -1 && thread != -1)
6911 printf_filtered (" (all threads)");
6912 else if (b->thread != -1)
6913 printf_filtered (" (thread %d)", b->thread);
6914 printf_filtered ("%s%s ",
059fb39f 6915 ((b->enable_state == bp_disabled
f8eba3c6 6916 || b->enable_state == bp_call_disabled)
0d381245
VP
6917 ? " (disabled)"
6918 : b->enable_state == bp_permanent
6919 ? " (permanent)"
6920 : ""),
6921 (others > 1) ? ","
6922 : ((others == 1) ? " and" : ""));
6923 }
a3f17187 6924 printf_filtered (_("also set at pc "));
5af949e3 6925 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6926 printf_filtered (".\n");
6927 }
6928}
6929\f
c906108c 6930
e4f237da
KB
6931/* Return true iff it is meaningful to use the address member of
6932 BPT. For some breakpoint types, the address member is irrelevant
6933 and it makes no sense to attempt to compare it to other addresses
6934 (or use it for any other purpose either).
6935
4a64f543
MS
6936 More specifically, each of the following breakpoint types will
6937 always have a zero valued address and we don't want to mark
6938 breakpoints of any of these types to be a duplicate of an actual
6939 breakpoint at address zero:
e4f237da
KB
6940
6941 bp_watchpoint
2d134ed3
PA
6942 bp_catchpoint
6943
6944*/
e4f237da
KB
6945
6946static int
6947breakpoint_address_is_meaningful (struct breakpoint *bpt)
6948{
6949 enum bptype type = bpt->type;
6950
2d134ed3
PA
6951 return (type != bp_watchpoint && type != bp_catchpoint);
6952}
6953
6954/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6955 true if LOC1 and LOC2 represent the same watchpoint location. */
6956
6957static int
4a64f543
MS
6958watchpoint_locations_match (struct bp_location *loc1,
6959 struct bp_location *loc2)
2d134ed3 6960{
3a5c3e22
PA
6961 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6962 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6963
6964 /* Both of them must exist. */
6965 gdb_assert (w1 != NULL);
6966 gdb_assert (w2 != NULL);
2bdf28a0 6967
4a64f543
MS
6968 /* If the target can evaluate the condition expression in hardware,
6969 then we we need to insert both watchpoints even if they are at
6970 the same place. Otherwise the watchpoint will only trigger when
6971 the condition of whichever watchpoint was inserted evaluates to
6972 true, not giving a chance for GDB to check the condition of the
6973 other watchpoint. */
3a5c3e22 6974 if ((w1->cond_exp
4a64f543
MS
6975 && target_can_accel_watchpoint_condition (loc1->address,
6976 loc1->length,
0cf6dd15 6977 loc1->watchpoint_type,
3a5c3e22
PA
6978 w1->cond_exp))
6979 || (w2->cond_exp
4a64f543
MS
6980 && target_can_accel_watchpoint_condition (loc2->address,
6981 loc2->length,
0cf6dd15 6982 loc2->watchpoint_type,
3a5c3e22 6983 w2->cond_exp)))
0cf6dd15
TJB
6984 return 0;
6985
85d721b8
PA
6986 /* Note that this checks the owner's type, not the location's. In
6987 case the target does not support read watchpoints, but does
6988 support access watchpoints, we'll have bp_read_watchpoint
6989 watchpoints with hw_access locations. Those should be considered
6990 duplicates of hw_read locations. The hw_read locations will
6991 become hw_access locations later. */
2d134ed3
PA
6992 return (loc1->owner->type == loc2->owner->type
6993 && loc1->pspace->aspace == loc2->pspace->aspace
6994 && loc1->address == loc2->address
6995 && loc1->length == loc2->length);
e4f237da
KB
6996}
6997
31e77af2 6998/* See breakpoint.h. */
6c95b8df 6999
31e77af2 7000int
6c95b8df
PA
7001breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7002 struct address_space *aspace2, CORE_ADDR addr2)
7003{
f5656ead 7004 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7005 || aspace1 == aspace2)
7006 && addr1 == addr2);
7007}
7008
f1310107
TJB
7009/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7010 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7011 matches ASPACE2. On targets that have global breakpoints, the address
7012 space doesn't really matter. */
7013
7014static int
7015breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7016 int len1, struct address_space *aspace2,
7017 CORE_ADDR addr2)
7018{
f5656ead 7019 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7020 || aspace1 == aspace2)
7021 && addr2 >= addr1 && addr2 < addr1 + len1);
7022}
7023
7024/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7025 a ranged breakpoint. In most targets, a match happens only if ASPACE
7026 matches the breakpoint's address space. On targets that have global
7027 breakpoints, the address space doesn't really matter. */
7028
7029static int
7030breakpoint_location_address_match (struct bp_location *bl,
7031 struct address_space *aspace,
7032 CORE_ADDR addr)
7033{
7034 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7035 aspace, addr)
7036 || (bl->length
7037 && breakpoint_address_match_range (bl->pspace->aspace,
7038 bl->address, bl->length,
7039 aspace, addr)));
7040}
7041
1e4d1764
YQ
7042/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7043 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7044 true, otherwise returns false. */
7045
7046static int
7047tracepoint_locations_match (struct bp_location *loc1,
7048 struct bp_location *loc2)
7049{
7050 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7051 /* Since tracepoint locations are never duplicated with others', tracepoint
7052 locations at the same address of different tracepoints are regarded as
7053 different locations. */
7054 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7055 else
7056 return 0;
7057}
7058
2d134ed3
PA
7059/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7060 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7061 represent the same location. */
7062
7063static int
4a64f543
MS
7064breakpoint_locations_match (struct bp_location *loc1,
7065 struct bp_location *loc2)
2d134ed3 7066{
2bdf28a0
JK
7067 int hw_point1, hw_point2;
7068
7069 /* Both of them must not be in moribund_locations. */
7070 gdb_assert (loc1->owner != NULL);
7071 gdb_assert (loc2->owner != NULL);
7072
7073 hw_point1 = is_hardware_watchpoint (loc1->owner);
7074 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7075
7076 if (hw_point1 != hw_point2)
7077 return 0;
7078 else if (hw_point1)
7079 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7080 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7081 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7082 else
f1310107
TJB
7083 /* We compare bp_location.length in order to cover ranged breakpoints. */
7084 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7085 loc2->pspace->aspace, loc2->address)
7086 && loc1->length == loc2->length);
2d134ed3
PA
7087}
7088
76897487
KB
7089static void
7090breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7091 int bnum, int have_bnum)
7092{
f63fbe86
MS
7093 /* The longest string possibly returned by hex_string_custom
7094 is 50 chars. These must be at least that big for safety. */
7095 char astr1[64];
7096 char astr2[64];
76897487 7097
bb599908
PH
7098 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7099 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7100 if (have_bnum)
8a3fe4f8 7101 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7102 bnum, astr1, astr2);
7103 else
8a3fe4f8 7104 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7105}
7106
4a64f543
MS
7107/* Adjust a breakpoint's address to account for architectural
7108 constraints on breakpoint placement. Return the adjusted address.
7109 Note: Very few targets require this kind of adjustment. For most
7110 targets, this function is simply the identity function. */
76897487
KB
7111
7112static CORE_ADDR
a6d9a66e
UW
7113adjust_breakpoint_address (struct gdbarch *gdbarch,
7114 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7115{
a6d9a66e 7116 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7117 {
7118 /* Very few targets need any kind of breakpoint adjustment. */
7119 return bpaddr;
7120 }
88f7da05
KB
7121 else if (bptype == bp_watchpoint
7122 || bptype == bp_hardware_watchpoint
7123 || bptype == bp_read_watchpoint
7124 || bptype == bp_access_watchpoint
fe798b75 7125 || bptype == bp_catchpoint)
88f7da05
KB
7126 {
7127 /* Watchpoints and the various bp_catch_* eventpoints should not
7128 have their addresses modified. */
7129 return bpaddr;
7130 }
7c16b83e
PA
7131 else if (bptype == bp_single_step)
7132 {
7133 /* Single-step breakpoints should not have their addresses
7134 modified. If there's any architectural constrain that
7135 applies to this address, then it should have already been
7136 taken into account when the breakpoint was created in the
7137 first place. If we didn't do this, stepping through e.g.,
7138 Thumb-2 IT blocks would break. */
7139 return bpaddr;
7140 }
76897487
KB
7141 else
7142 {
7143 CORE_ADDR adjusted_bpaddr;
7144
7145 /* Some targets have architectural constraints on the placement
7146 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7147 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7148
7149 /* An adjusted breakpoint address can significantly alter
7150 a user's expectations. Print a warning if an adjustment
7151 is required. */
7152 if (adjusted_bpaddr != bpaddr)
7153 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7154
7155 return adjusted_bpaddr;
7156 }
7157}
7158
28010a5d
PA
7159void
7160init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7161 struct breakpoint *owner)
7cc221ef 7162{
7cc221ef
DJ
7163 memset (loc, 0, sizeof (*loc));
7164
348d480f
PA
7165 gdb_assert (ops != NULL);
7166
28010a5d
PA
7167 loc->ops = ops;
7168 loc->owner = owner;
511a6cd4 7169 loc->cond = NULL;
b775012e 7170 loc->cond_bytecode = NULL;
0d381245
VP
7171 loc->shlib_disabled = 0;
7172 loc->enabled = 1;
e049a4b5 7173
28010a5d 7174 switch (owner->type)
e049a4b5
DJ
7175 {
7176 case bp_breakpoint:
7c16b83e 7177 case bp_single_step:
e049a4b5
DJ
7178 case bp_until:
7179 case bp_finish:
7180 case bp_longjmp:
7181 case bp_longjmp_resume:
e2e4d78b 7182 case bp_longjmp_call_dummy:
186c406b
TT
7183 case bp_exception:
7184 case bp_exception_resume:
e049a4b5 7185 case bp_step_resume:
2c03e5be 7186 case bp_hp_step_resume:
e049a4b5
DJ
7187 case bp_watchpoint_scope:
7188 case bp_call_dummy:
aa7d318d 7189 case bp_std_terminate:
e049a4b5
DJ
7190 case bp_shlib_event:
7191 case bp_thread_event:
7192 case bp_overlay_event:
4efc6507 7193 case bp_jit_event:
0fd8e87f 7194 case bp_longjmp_master:
aa7d318d 7195 case bp_std_terminate_master:
186c406b 7196 case bp_exception_master:
0e30163f
JK
7197 case bp_gnu_ifunc_resolver:
7198 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7199 case bp_dprintf:
e049a4b5 7200 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7201 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7202 break;
7203 case bp_hardware_breakpoint:
7204 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7205 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7206 break;
7207 case bp_hardware_watchpoint:
7208 case bp_read_watchpoint:
7209 case bp_access_watchpoint:
7210 loc->loc_type = bp_loc_hardware_watchpoint;
7211 break;
7212 case bp_watchpoint:
ce78b96d 7213 case bp_catchpoint:
15c3d785
PA
7214 case bp_tracepoint:
7215 case bp_fast_tracepoint:
0fb4aa4b 7216 case bp_static_tracepoint:
e049a4b5
DJ
7217 loc->loc_type = bp_loc_other;
7218 break;
7219 default:
e2e0b3e5 7220 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7221 }
7222
f431efe5 7223 loc->refc = 1;
28010a5d
PA
7224}
7225
7226/* Allocate a struct bp_location. */
7227
7228static struct bp_location *
7229allocate_bp_location (struct breakpoint *bpt)
7230{
348d480f
PA
7231 return bpt->ops->allocate_location (bpt);
7232}
7cc221ef 7233
f431efe5
PA
7234static void
7235free_bp_location (struct bp_location *loc)
fe3f5fa8 7236{
348d480f 7237 loc->ops->dtor (loc);
fe3f5fa8
VP
7238 xfree (loc);
7239}
7240
f431efe5
PA
7241/* Increment reference count. */
7242
7243static void
7244incref_bp_location (struct bp_location *bl)
7245{
7246 ++bl->refc;
7247}
7248
7249/* Decrement reference count. If the reference count reaches 0,
7250 destroy the bp_location. Sets *BLP to NULL. */
7251
7252static void
7253decref_bp_location (struct bp_location **blp)
7254{
0807b50c
PA
7255 gdb_assert ((*blp)->refc > 0);
7256
f431efe5
PA
7257 if (--(*blp)->refc == 0)
7258 free_bp_location (*blp);
7259 *blp = NULL;
7260}
7261
346774a9 7262/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7263
346774a9
PA
7264static void
7265add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7266{
346774a9 7267 struct breakpoint *b1;
c906108c 7268
346774a9
PA
7269 /* Add this breakpoint to the end of the chain so that a list of
7270 breakpoints will come out in order of increasing numbers. */
7271
7272 b1 = breakpoint_chain;
7273 if (b1 == 0)
7274 breakpoint_chain = b;
7275 else
7276 {
7277 while (b1->next)
7278 b1 = b1->next;
7279 b1->next = b;
7280 }
7281}
7282
7283/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7284
7285static void
7286init_raw_breakpoint_without_location (struct breakpoint *b,
7287 struct gdbarch *gdbarch,
28010a5d 7288 enum bptype bptype,
c0a91b2b 7289 const struct breakpoint_ops *ops)
346774a9 7290{
c906108c 7291 memset (b, 0, sizeof (*b));
2219d63c 7292
348d480f
PA
7293 gdb_assert (ops != NULL);
7294
28010a5d 7295 b->ops = ops;
4d28f7a8 7296 b->type = bptype;
a6d9a66e 7297 b->gdbarch = gdbarch;
c906108c
SS
7298 b->language = current_language->la_language;
7299 b->input_radix = input_radix;
7300 b->thread = -1;
b5de0fa7 7301 b->enable_state = bp_enabled;
c906108c
SS
7302 b->next = 0;
7303 b->silent = 0;
7304 b->ignore_count = 0;
7305 b->commands = NULL;
818dd999 7306 b->frame_id = null_frame_id;
0d381245 7307 b->condition_not_parsed = 0;
84f4c1fe 7308 b->py_bp_object = NULL;
d0fb5eae 7309 b->related_breakpoint = b;
346774a9
PA
7310}
7311
7312/* Helper to set_raw_breakpoint below. Creates a breakpoint
7313 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7314
7315static struct breakpoint *
7316set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7317 enum bptype bptype,
c0a91b2b 7318 const struct breakpoint_ops *ops)
346774a9
PA
7319{
7320 struct breakpoint *b = XNEW (struct breakpoint);
7321
348d480f 7322 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7323 add_to_breakpoint_chain (b);
0d381245
VP
7324 return b;
7325}
7326
0e30163f
JK
7327/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7328 resolutions should be made as the user specified the location explicitly
7329 enough. */
7330
0d381245 7331static void
0e30163f 7332set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7333{
2bdf28a0
JK
7334 gdb_assert (loc->owner != NULL);
7335
0d381245 7336 if (loc->owner->type == bp_breakpoint
1042e4c0 7337 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7338 || is_tracepoint (loc->owner))
0d381245 7339 {
0e30163f 7340 int is_gnu_ifunc;
2c02bd72 7341 const char *function_name;
6a3a010b 7342 CORE_ADDR func_addr;
0e30163f 7343
2c02bd72 7344 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7345 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7346
7347 if (is_gnu_ifunc && !explicit_loc)
7348 {
7349 struct breakpoint *b = loc->owner;
7350
7351 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7352 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7353 &loc->requested_address))
7354 {
7355 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7356 loc->address = adjust_breakpoint_address (loc->gdbarch,
7357 loc->requested_address,
7358 b->type);
7359 }
7360 else if (b->type == bp_breakpoint && b->loc == loc
7361 && loc->next == NULL && b->related_breakpoint == b)
7362 {
7363 /* Create only the whole new breakpoint of this type but do not
7364 mess more complicated breakpoints with multiple locations. */
7365 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7366 /* Remember the resolver's address for use by the return
7367 breakpoint. */
7368 loc->related_address = func_addr;
0e30163f
JK
7369 }
7370 }
7371
2c02bd72
DE
7372 if (function_name)
7373 loc->function_name = xstrdup (function_name);
0d381245
VP
7374 }
7375}
7376
a6d9a66e 7377/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7378struct gdbarch *
a6d9a66e
UW
7379get_sal_arch (struct symtab_and_line sal)
7380{
7381 if (sal.section)
7382 return get_objfile_arch (sal.section->objfile);
7383 if (sal.symtab)
7384 return get_objfile_arch (sal.symtab->objfile);
7385
7386 return NULL;
7387}
7388
346774a9
PA
7389/* Low level routine for partially initializing a breakpoint of type
7390 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7391 file name, and line number are provided by SAL.
0d381245
VP
7392
7393 It is expected that the caller will complete the initialization of
7394 the newly created breakpoint struct as well as output any status
c56053d2 7395 information regarding the creation of a new breakpoint. */
0d381245 7396
346774a9
PA
7397static void
7398init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7399 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7400 const struct breakpoint_ops *ops)
0d381245 7401{
28010a5d 7402 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7403
3742cc8b 7404 add_location_to_breakpoint (b, &sal);
0d381245 7405
6c95b8df
PA
7406 if (bptype != bp_catchpoint)
7407 gdb_assert (sal.pspace != NULL);
7408
f8eba3c6
TT
7409 /* Store the program space that was used to set the breakpoint,
7410 except for ordinary breakpoints, which are independent of the
7411 program space. */
7412 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7413 b->pspace = sal.pspace;
346774a9 7414}
c906108c 7415
346774a9
PA
7416/* set_raw_breakpoint is a low level routine for allocating and
7417 partially initializing a breakpoint of type BPTYPE. The newly
7418 created breakpoint's address, section, source file name, and line
7419 number are provided by SAL. The newly created and partially
7420 initialized breakpoint is added to the breakpoint chain and
7421 is also returned as the value of this function.
7422
7423 It is expected that the caller will complete the initialization of
7424 the newly created breakpoint struct as well as output any status
7425 information regarding the creation of a new breakpoint. In
7426 particular, set_raw_breakpoint does NOT set the breakpoint
7427 number! Care should be taken to not allow an error to occur
7428 prior to completing the initialization of the breakpoint. If this
7429 should happen, a bogus breakpoint will be left on the chain. */
7430
7431struct breakpoint *
7432set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7433 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7434 const struct breakpoint_ops *ops)
346774a9
PA
7435{
7436 struct breakpoint *b = XNEW (struct breakpoint);
7437
348d480f 7438 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7439 add_to_breakpoint_chain (b);
c906108c
SS
7440 return b;
7441}
7442
c2c6d25f
JM
7443
7444/* Note that the breakpoint object B describes a permanent breakpoint
7445 instruction, hard-wired into the inferior's code. */
7446void
7447make_breakpoint_permanent (struct breakpoint *b)
7448{
0d381245 7449 struct bp_location *bl;
cc59ec59 7450
b5de0fa7 7451 b->enable_state = bp_permanent;
c2c6d25f 7452
4a64f543
MS
7453 /* By definition, permanent breakpoints are already present in the
7454 code. Mark all locations as inserted. For now,
7455 make_breakpoint_permanent is called in just one place, so it's
7456 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7457 multiple locations or not, but it's easy to implement. */
0d381245
VP
7458 for (bl = b->loc; bl; bl = bl->next)
7459 bl->inserted = 1;
c2c6d25f
JM
7460}
7461
53a5351d 7462/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7463 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7464 initiated the operation. */
c906108c
SS
7465
7466void
186c406b 7467set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7468{
35df4500 7469 struct breakpoint *b, *b_tmp;
186c406b 7470 int thread = tp->num;
0fd8e87f
UW
7471
7472 /* To avoid having to rescan all objfile symbols at every step,
7473 we maintain a list of continually-inserted but always disabled
7474 longjmp "master" breakpoints. Here, we simply create momentary
7475 clones of those and enable them for the requested thread. */
35df4500 7476 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7477 if (b->pspace == current_program_space
186c406b
TT
7478 && (b->type == bp_longjmp_master
7479 || b->type == bp_exception_master))
0fd8e87f 7480 {
06edf0c0
PA
7481 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7482 struct breakpoint *clone;
cc59ec59 7483
e2e4d78b
JK
7484 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7485 after their removal. */
06edf0c0 7486 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7487 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7488 clone->thread = thread;
7489 }
186c406b
TT
7490
7491 tp->initiating_frame = frame;
c906108c
SS
7492}
7493
611c83ae 7494/* Delete all longjmp breakpoints from THREAD. */
c906108c 7495void
611c83ae 7496delete_longjmp_breakpoint (int thread)
c906108c 7497{
35df4500 7498 struct breakpoint *b, *b_tmp;
c906108c 7499
35df4500 7500 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7501 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7502 {
7503 if (b->thread == thread)
7504 delete_breakpoint (b);
7505 }
c906108c
SS
7506}
7507
f59f708a
PA
7508void
7509delete_longjmp_breakpoint_at_next_stop (int thread)
7510{
7511 struct breakpoint *b, *b_tmp;
7512
7513 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7514 if (b->type == bp_longjmp || b->type == bp_exception)
7515 {
7516 if (b->thread == thread)
7517 b->disposition = disp_del_at_next_stop;
7518 }
7519}
7520
e2e4d78b
JK
7521/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7522 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7523 pointer to any of them. Return NULL if this system cannot place longjmp
7524 breakpoints. */
7525
7526struct breakpoint *
7527set_longjmp_breakpoint_for_call_dummy (void)
7528{
7529 struct breakpoint *b, *retval = NULL;
7530
7531 ALL_BREAKPOINTS (b)
7532 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7533 {
7534 struct breakpoint *new_b;
7535
7536 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7537 &momentary_breakpoint_ops,
7538 1);
e2e4d78b
JK
7539 new_b->thread = pid_to_thread_id (inferior_ptid);
7540
7541 /* Link NEW_B into the chain of RETVAL breakpoints. */
7542
7543 gdb_assert (new_b->related_breakpoint == new_b);
7544 if (retval == NULL)
7545 retval = new_b;
7546 new_b->related_breakpoint = retval;
7547 while (retval->related_breakpoint != new_b->related_breakpoint)
7548 retval = retval->related_breakpoint;
7549 retval->related_breakpoint = new_b;
7550 }
7551
7552 return retval;
7553}
7554
7555/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7556 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7557 stack.
7558
7559 You should call this function only at places where it is safe to currently
7560 unwind the whole stack. Failed stack unwind would discard live dummy
7561 frames. */
7562
7563void
b67a2c6f 7564check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7565{
7566 struct breakpoint *b, *b_tmp;
7567
7568 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7569 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7570 {
7571 struct breakpoint *dummy_b = b->related_breakpoint;
7572
7573 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7574 dummy_b = dummy_b->related_breakpoint;
7575 if (dummy_b->type != bp_call_dummy
7576 || frame_find_by_id (dummy_b->frame_id) != NULL)
7577 continue;
7578
b67a2c6f 7579 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7580
7581 while (b->related_breakpoint != b)
7582 {
7583 if (b_tmp == b->related_breakpoint)
7584 b_tmp = b->related_breakpoint->next;
7585 delete_breakpoint (b->related_breakpoint);
7586 }
7587 delete_breakpoint (b);
7588 }
7589}
7590
1900040c
MS
7591void
7592enable_overlay_breakpoints (void)
7593{
52f0bd74 7594 struct breakpoint *b;
1900040c
MS
7595
7596 ALL_BREAKPOINTS (b)
7597 if (b->type == bp_overlay_event)
7598 {
7599 b->enable_state = bp_enabled;
44702360 7600 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7601 overlay_events_enabled = 1;
1900040c
MS
7602 }
7603}
7604
7605void
7606disable_overlay_breakpoints (void)
7607{
52f0bd74 7608 struct breakpoint *b;
1900040c
MS
7609
7610 ALL_BREAKPOINTS (b)
7611 if (b->type == bp_overlay_event)
7612 {
7613 b->enable_state = bp_disabled;
44702360 7614 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7615 overlay_events_enabled = 0;
1900040c
MS
7616 }
7617}
7618
aa7d318d
TT
7619/* Set an active std::terminate breakpoint for each std::terminate
7620 master breakpoint. */
7621void
7622set_std_terminate_breakpoint (void)
7623{
35df4500 7624 struct breakpoint *b, *b_tmp;
aa7d318d 7625
35df4500 7626 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7627 if (b->pspace == current_program_space
7628 && b->type == bp_std_terminate_master)
7629 {
06edf0c0 7630 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7631 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7632 }
7633}
7634
7635/* Delete all the std::terminate breakpoints. */
7636void
7637delete_std_terminate_breakpoint (void)
7638{
35df4500 7639 struct breakpoint *b, *b_tmp;
aa7d318d 7640
35df4500 7641 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7642 if (b->type == bp_std_terminate)
7643 delete_breakpoint (b);
7644}
7645
c4093a6a 7646struct breakpoint *
a6d9a66e 7647create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7648{
7649 struct breakpoint *b;
c4093a6a 7650
06edf0c0
PA
7651 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7652 &internal_breakpoint_ops);
7653
b5de0fa7 7654 b->enable_state = bp_enabled;
c4093a6a 7655 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7656 b->addr_string
7657 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7658
44702360 7659 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7660
c4093a6a
JM
7661 return b;
7662}
7663
7664void
7665remove_thread_event_breakpoints (void)
7666{
35df4500 7667 struct breakpoint *b, *b_tmp;
c4093a6a 7668
35df4500 7669 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7670 if (b->type == bp_thread_event
7671 && b->loc->pspace == current_program_space)
c4093a6a
JM
7672 delete_breakpoint (b);
7673}
7674
0101ce28
JJ
7675struct lang_and_radix
7676 {
7677 enum language lang;
7678 int radix;
7679 };
7680
4efc6507
DE
7681/* Create a breakpoint for JIT code registration and unregistration. */
7682
7683struct breakpoint *
7684create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7685{
7686 struct breakpoint *b;
7687
06edf0c0
PA
7688 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7689 &internal_breakpoint_ops);
44702360 7690 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7691 return b;
7692}
0101ce28 7693
03673fc7
PP
7694/* Remove JIT code registration and unregistration breakpoint(s). */
7695
7696void
7697remove_jit_event_breakpoints (void)
7698{
7699 struct breakpoint *b, *b_tmp;
7700
7701 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7702 if (b->type == bp_jit_event
7703 && b->loc->pspace == current_program_space)
7704 delete_breakpoint (b);
7705}
7706
cae688ec
JJ
7707void
7708remove_solib_event_breakpoints (void)
7709{
35df4500 7710 struct breakpoint *b, *b_tmp;
cae688ec 7711
35df4500 7712 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7713 if (b->type == bp_shlib_event
7714 && b->loc->pspace == current_program_space)
cae688ec
JJ
7715 delete_breakpoint (b);
7716}
7717
f37f681c
PA
7718/* See breakpoint.h. */
7719
7720void
7721remove_solib_event_breakpoints_at_next_stop (void)
7722{
7723 struct breakpoint *b, *b_tmp;
7724
7725 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7726 if (b->type == bp_shlib_event
7727 && b->loc->pspace == current_program_space)
7728 b->disposition = disp_del_at_next_stop;
7729}
7730
04086b45
PA
7731/* Helper for create_solib_event_breakpoint /
7732 create_and_insert_solib_event_breakpoint. Allows specifying which
7733 INSERT_MODE to pass through to update_global_location_list. */
7734
7735static struct breakpoint *
7736create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7737 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7738{
7739 struct breakpoint *b;
7740
06edf0c0
PA
7741 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7742 &internal_breakpoint_ops);
04086b45 7743 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7744 return b;
7745}
7746
04086b45
PA
7747struct breakpoint *
7748create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7749{
7750 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7751}
7752
f37f681c
PA
7753/* See breakpoint.h. */
7754
7755struct breakpoint *
7756create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7757{
7758 struct breakpoint *b;
7759
04086b45
PA
7760 /* Explicitly tell update_global_location_list to insert
7761 locations. */
7762 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7763 if (!b->loc->inserted)
7764 {
7765 delete_breakpoint (b);
7766 return NULL;
7767 }
7768 return b;
7769}
7770
cae688ec
JJ
7771/* Disable any breakpoints that are on code in shared libraries. Only
7772 apply to enabled breakpoints, disabled ones can just stay disabled. */
7773
7774void
cb851954 7775disable_breakpoints_in_shlibs (void)
cae688ec 7776{
876fa593 7777 struct bp_location *loc, **locp_tmp;
cae688ec 7778
876fa593 7779 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7780 {
2bdf28a0 7781 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7782 struct breakpoint *b = loc->owner;
2bdf28a0 7783
4a64f543
MS
7784 /* We apply the check to all breakpoints, including disabled for
7785 those with loc->duplicate set. This is so that when breakpoint
7786 becomes enabled, or the duplicate is removed, gdb will try to
7787 insert all breakpoints. If we don't set shlib_disabled here,
7788 we'll try to insert those breakpoints and fail. */
1042e4c0 7789 if (((b->type == bp_breakpoint)
508ccb1f 7790 || (b->type == bp_jit_event)
1042e4c0 7791 || (b->type == bp_hardware_breakpoint)
d77f58be 7792 || (is_tracepoint (b)))
6c95b8df 7793 && loc->pspace == current_program_space
0d381245 7794 && !loc->shlib_disabled
6c95b8df 7795 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7796 )
0d381245
VP
7797 {
7798 loc->shlib_disabled = 1;
7799 }
cae688ec
JJ
7800 }
7801}
7802
63644780
NB
7803/* Disable any breakpoints and tracepoints that are in SOLIB upon
7804 notification of unloaded_shlib. Only apply to enabled breakpoints,
7805 disabled ones can just stay disabled. */
84acb35a 7806
75149521 7807static void
84acb35a
JJ
7808disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7809{
876fa593 7810 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7811 int disabled_shlib_breaks = 0;
7812
c86cf029
VP
7813 /* SunOS a.out shared libraries are always mapped, so do not
7814 disable breakpoints; they will only be reported as unloaded
7815 through clear_solib when GDB discards its shared library
7816 list. See clear_solib for more information. */
7817 if (exec_bfd != NULL
7818 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7819 return;
7820
876fa593 7821 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7822 {
2bdf28a0 7823 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7824 struct breakpoint *b = loc->owner;
cc59ec59 7825
1e4d1764 7826 if (solib->pspace == loc->pspace
e2dd7057 7827 && !loc->shlib_disabled
1e4d1764
YQ
7828 && (((b->type == bp_breakpoint
7829 || b->type == bp_jit_event
7830 || b->type == bp_hardware_breakpoint)
7831 && (loc->loc_type == bp_loc_hardware_breakpoint
7832 || loc->loc_type == bp_loc_software_breakpoint))
7833 || is_tracepoint (b))
e2dd7057 7834 && solib_contains_address_p (solib, loc->address))
84acb35a 7835 {
e2dd7057
PP
7836 loc->shlib_disabled = 1;
7837 /* At this point, we cannot rely on remove_breakpoint
7838 succeeding so we must mark the breakpoint as not inserted
7839 to prevent future errors occurring in remove_breakpoints. */
7840 loc->inserted = 0;
8d3788bd
VP
7841
7842 /* This may cause duplicate notifications for the same breakpoint. */
7843 observer_notify_breakpoint_modified (b);
7844
e2dd7057
PP
7845 if (!disabled_shlib_breaks)
7846 {
7847 target_terminal_ours_for_output ();
3e43a32a
MS
7848 warning (_("Temporarily disabling breakpoints "
7849 "for unloaded shared library \"%s\""),
e2dd7057 7850 solib->so_name);
84acb35a 7851 }
e2dd7057 7852 disabled_shlib_breaks = 1;
84acb35a
JJ
7853 }
7854 }
84acb35a
JJ
7855}
7856
63644780
NB
7857/* Disable any breakpoints and tracepoints in OBJFILE upon
7858 notification of free_objfile. Only apply to enabled breakpoints,
7859 disabled ones can just stay disabled. */
7860
7861static void
7862disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7863{
7864 struct breakpoint *b;
7865
7866 if (objfile == NULL)
7867 return;
7868
d03de421
PA
7869 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7870 managed by the user with add-symbol-file/remove-symbol-file.
7871 Similarly to how breakpoints in shared libraries are handled in
7872 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7873 shlib_disabled so they end up uninserted on the next global
7874 location list update. Shared libraries not loaded by the user
7875 aren't handled here -- they're already handled in
7876 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7877 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7878 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7879 main objfile). */
7880 if ((objfile->flags & OBJF_SHARED) == 0
7881 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7882 return;
7883
7884 ALL_BREAKPOINTS (b)
7885 {
7886 struct bp_location *loc;
7887 int bp_modified = 0;
7888
7889 if (!is_breakpoint (b) && !is_tracepoint (b))
7890 continue;
7891
7892 for (loc = b->loc; loc != NULL; loc = loc->next)
7893 {
7894 CORE_ADDR loc_addr = loc->address;
7895
7896 if (loc->loc_type != bp_loc_hardware_breakpoint
7897 && loc->loc_type != bp_loc_software_breakpoint)
7898 continue;
7899
7900 if (loc->shlib_disabled != 0)
7901 continue;
7902
7903 if (objfile->pspace != loc->pspace)
7904 continue;
7905
7906 if (loc->loc_type != bp_loc_hardware_breakpoint
7907 && loc->loc_type != bp_loc_software_breakpoint)
7908 continue;
7909
7910 if (is_addr_in_objfile (loc_addr, objfile))
7911 {
7912 loc->shlib_disabled = 1;
08351840
PA
7913 /* At this point, we don't know whether the object was
7914 unmapped from the inferior or not, so leave the
7915 inserted flag alone. We'll handle failure to
7916 uninsert quietly, in case the object was indeed
7917 unmapped. */
63644780
NB
7918
7919 mark_breakpoint_location_modified (loc);
7920
7921 bp_modified = 1;
7922 }
7923 }
7924
7925 if (bp_modified)
7926 observer_notify_breakpoint_modified (b);
7927 }
7928}
7929
ce78b96d
JB
7930/* FORK & VFORK catchpoints. */
7931
e29a4733
PA
7932/* An instance of this type is used to represent a fork or vfork
7933 catchpoint. It includes a "struct breakpoint" as a kind of base
7934 class; users downcast to "struct breakpoint *" when needed. A
7935 breakpoint is really of this type iff its ops pointer points to
7936 CATCH_FORK_BREAKPOINT_OPS. */
7937
7938struct fork_catchpoint
7939{
7940 /* The base class. */
7941 struct breakpoint base;
7942
7943 /* Process id of a child process whose forking triggered this
7944 catchpoint. This field is only valid immediately after this
7945 catchpoint has triggered. */
7946 ptid_t forked_inferior_pid;
7947};
7948
4a64f543
MS
7949/* Implement the "insert" breakpoint_ops method for fork
7950 catchpoints. */
ce78b96d 7951
77b06cd7
TJB
7952static int
7953insert_catch_fork (struct bp_location *bl)
ce78b96d 7954{
dfd4cc63 7955 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7956}
7957
4a64f543
MS
7958/* Implement the "remove" breakpoint_ops method for fork
7959 catchpoints. */
ce78b96d
JB
7960
7961static int
77b06cd7 7962remove_catch_fork (struct bp_location *bl)
ce78b96d 7963{
dfd4cc63 7964 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7965}
7966
7967/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7968 catchpoints. */
7969
7970static int
f1310107 7971breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7972 struct address_space *aspace, CORE_ADDR bp_addr,
7973 const struct target_waitstatus *ws)
ce78b96d 7974{
e29a4733
PA
7975 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7976
f90263c1
TT
7977 if (ws->kind != TARGET_WAITKIND_FORKED)
7978 return 0;
7979
7980 c->forked_inferior_pid = ws->value.related_pid;
7981 return 1;
ce78b96d
JB
7982}
7983
4a64f543
MS
7984/* Implement the "print_it" breakpoint_ops method for fork
7985 catchpoints. */
ce78b96d
JB
7986
7987static enum print_stop_action
348d480f 7988print_it_catch_fork (bpstat bs)
ce78b96d 7989{
36dfb11c 7990 struct ui_out *uiout = current_uiout;
348d480f
PA
7991 struct breakpoint *b = bs->breakpoint_at;
7992 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7993
ce78b96d 7994 annotate_catchpoint (b->number);
36dfb11c
TT
7995 if (b->disposition == disp_del)
7996 ui_out_text (uiout, "\nTemporary catchpoint ");
7997 else
7998 ui_out_text (uiout, "\nCatchpoint ");
7999 if (ui_out_is_mi_like_p (uiout))
8000 {
8001 ui_out_field_string (uiout, "reason",
8002 async_reason_lookup (EXEC_ASYNC_FORK));
8003 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8004 }
8005 ui_out_field_int (uiout, "bkptno", b->number);
8006 ui_out_text (uiout, " (forked process ");
8007 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8008 ui_out_text (uiout, "), ");
ce78b96d
JB
8009 return PRINT_SRC_AND_LOC;
8010}
8011
4a64f543
MS
8012/* Implement the "print_one" breakpoint_ops method for fork
8013 catchpoints. */
ce78b96d
JB
8014
8015static void
a6d9a66e 8016print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8017{
e29a4733 8018 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8019 struct value_print_options opts;
79a45e25 8020 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8021
8022 get_user_print_options (&opts);
8023
4a64f543
MS
8024 /* Field 4, the address, is omitted (which makes the columns not
8025 line up too nicely with the headers, but the effect is relatively
8026 readable). */
79a45b7d 8027 if (opts.addressprint)
ce78b96d
JB
8028 ui_out_field_skip (uiout, "addr");
8029 annotate_field (5);
8030 ui_out_text (uiout, "fork");
e29a4733 8031 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8032 {
8033 ui_out_text (uiout, ", process ");
8034 ui_out_field_int (uiout, "what",
e29a4733 8035 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8036 ui_out_spaces (uiout, 1);
8037 }
8ac3646f
TT
8038
8039 if (ui_out_is_mi_like_p (uiout))
8040 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8041}
8042
8043/* Implement the "print_mention" breakpoint_ops method for fork
8044 catchpoints. */
8045
8046static void
8047print_mention_catch_fork (struct breakpoint *b)
8048{
8049 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8050}
8051
6149aea9
PA
8052/* Implement the "print_recreate" breakpoint_ops method for fork
8053 catchpoints. */
8054
8055static void
8056print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8057{
8058 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8059 print_recreate_thread (b, fp);
6149aea9
PA
8060}
8061
ce78b96d
JB
8062/* The breakpoint_ops structure to be used in fork catchpoints. */
8063
2060206e 8064static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8065
4a64f543
MS
8066/* Implement the "insert" breakpoint_ops method for vfork
8067 catchpoints. */
ce78b96d 8068
77b06cd7
TJB
8069static int
8070insert_catch_vfork (struct bp_location *bl)
ce78b96d 8071{
dfd4cc63 8072 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8073}
8074
4a64f543
MS
8075/* Implement the "remove" breakpoint_ops method for vfork
8076 catchpoints. */
ce78b96d
JB
8077
8078static int
77b06cd7 8079remove_catch_vfork (struct bp_location *bl)
ce78b96d 8080{
dfd4cc63 8081 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8082}
8083
8084/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8085 catchpoints. */
8086
8087static int
f1310107 8088breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8089 struct address_space *aspace, CORE_ADDR bp_addr,
8090 const struct target_waitstatus *ws)
ce78b96d 8091{
e29a4733
PA
8092 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8093
f90263c1
TT
8094 if (ws->kind != TARGET_WAITKIND_VFORKED)
8095 return 0;
8096
8097 c->forked_inferior_pid = ws->value.related_pid;
8098 return 1;
ce78b96d
JB
8099}
8100
4a64f543
MS
8101/* Implement the "print_it" breakpoint_ops method for vfork
8102 catchpoints. */
ce78b96d
JB
8103
8104static enum print_stop_action
348d480f 8105print_it_catch_vfork (bpstat bs)
ce78b96d 8106{
36dfb11c 8107 struct ui_out *uiout = current_uiout;
348d480f 8108 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8109 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8110
ce78b96d 8111 annotate_catchpoint (b->number);
36dfb11c
TT
8112 if (b->disposition == disp_del)
8113 ui_out_text (uiout, "\nTemporary catchpoint ");
8114 else
8115 ui_out_text (uiout, "\nCatchpoint ");
8116 if (ui_out_is_mi_like_p (uiout))
8117 {
8118 ui_out_field_string (uiout, "reason",
8119 async_reason_lookup (EXEC_ASYNC_VFORK));
8120 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8121 }
8122 ui_out_field_int (uiout, "bkptno", b->number);
8123 ui_out_text (uiout, " (vforked process ");
8124 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8125 ui_out_text (uiout, "), ");
ce78b96d
JB
8126 return PRINT_SRC_AND_LOC;
8127}
8128
4a64f543
MS
8129/* Implement the "print_one" breakpoint_ops method for vfork
8130 catchpoints. */
ce78b96d
JB
8131
8132static void
a6d9a66e 8133print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8134{
e29a4733 8135 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8136 struct value_print_options opts;
79a45e25 8137 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8138
8139 get_user_print_options (&opts);
4a64f543
MS
8140 /* Field 4, the address, is omitted (which makes the columns not
8141 line up too nicely with the headers, but the effect is relatively
8142 readable). */
79a45b7d 8143 if (opts.addressprint)
ce78b96d
JB
8144 ui_out_field_skip (uiout, "addr");
8145 annotate_field (5);
8146 ui_out_text (uiout, "vfork");
e29a4733 8147 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8148 {
8149 ui_out_text (uiout, ", process ");
8150 ui_out_field_int (uiout, "what",
e29a4733 8151 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8152 ui_out_spaces (uiout, 1);
8153 }
8ac3646f
TT
8154
8155 if (ui_out_is_mi_like_p (uiout))
8156 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8157}
8158
8159/* Implement the "print_mention" breakpoint_ops method for vfork
8160 catchpoints. */
8161
8162static void
8163print_mention_catch_vfork (struct breakpoint *b)
8164{
8165 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8166}
8167
6149aea9
PA
8168/* Implement the "print_recreate" breakpoint_ops method for vfork
8169 catchpoints. */
8170
8171static void
8172print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8173{
8174 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8175 print_recreate_thread (b, fp);
6149aea9
PA
8176}
8177
ce78b96d
JB
8178/* The breakpoint_ops structure to be used in vfork catchpoints. */
8179
2060206e 8180static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8181
edcc5120
TT
8182/* An instance of this type is used to represent an solib catchpoint.
8183 It includes a "struct breakpoint" as a kind of base class; users
8184 downcast to "struct breakpoint *" when needed. A breakpoint is
8185 really of this type iff its ops pointer points to
8186 CATCH_SOLIB_BREAKPOINT_OPS. */
8187
8188struct solib_catchpoint
8189{
8190 /* The base class. */
8191 struct breakpoint base;
8192
8193 /* True for "catch load", false for "catch unload". */
8194 unsigned char is_load;
8195
8196 /* Regular expression to match, if any. COMPILED is only valid when
8197 REGEX is non-NULL. */
8198 char *regex;
8199 regex_t compiled;
8200};
8201
8202static void
8203dtor_catch_solib (struct breakpoint *b)
8204{
8205 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8206
8207 if (self->regex)
8208 regfree (&self->compiled);
8209 xfree (self->regex);
8210
8211 base_breakpoint_ops.dtor (b);
8212}
8213
8214static int
8215insert_catch_solib (struct bp_location *ignore)
8216{
8217 return 0;
8218}
8219
8220static int
8221remove_catch_solib (struct bp_location *ignore)
8222{
8223 return 0;
8224}
8225
8226static int
8227breakpoint_hit_catch_solib (const struct bp_location *bl,
8228 struct address_space *aspace,
8229 CORE_ADDR bp_addr,
8230 const struct target_waitstatus *ws)
8231{
8232 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8233 struct breakpoint *other;
8234
8235 if (ws->kind == TARGET_WAITKIND_LOADED)
8236 return 1;
8237
8238 ALL_BREAKPOINTS (other)
8239 {
8240 struct bp_location *other_bl;
8241
8242 if (other == bl->owner)
8243 continue;
8244
8245 if (other->type != bp_shlib_event)
8246 continue;
8247
8248 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8249 continue;
8250
8251 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8252 {
8253 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8254 return 1;
8255 }
8256 }
8257
8258 return 0;
8259}
8260
8261static void
8262check_status_catch_solib (struct bpstats *bs)
8263{
8264 struct solib_catchpoint *self
8265 = (struct solib_catchpoint *) bs->breakpoint_at;
8266 int ix;
8267
8268 if (self->is_load)
8269 {
8270 struct so_list *iter;
8271
8272 for (ix = 0;
8273 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8274 ix, iter);
8275 ++ix)
8276 {
8277 if (!self->regex
8278 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8279 return;
8280 }
8281 }
8282 else
8283 {
8284 char *iter;
8285
8286 for (ix = 0;
8287 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8288 ix, iter);
8289 ++ix)
8290 {
8291 if (!self->regex
8292 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8293 return;
8294 }
8295 }
8296
8297 bs->stop = 0;
8298 bs->print_it = print_it_noop;
8299}
8300
8301static enum print_stop_action
8302print_it_catch_solib (bpstat bs)
8303{
8304 struct breakpoint *b = bs->breakpoint_at;
8305 struct ui_out *uiout = current_uiout;
8306
8307 annotate_catchpoint (b->number);
8308 if (b->disposition == disp_del)
8309 ui_out_text (uiout, "\nTemporary catchpoint ");
8310 else
8311 ui_out_text (uiout, "\nCatchpoint ");
8312 ui_out_field_int (uiout, "bkptno", b->number);
8313 ui_out_text (uiout, "\n");
8314 if (ui_out_is_mi_like_p (uiout))
8315 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8316 print_solib_event (1);
8317 return PRINT_SRC_AND_LOC;
8318}
8319
8320static void
8321print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8322{
8323 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8324 struct value_print_options opts;
8325 struct ui_out *uiout = current_uiout;
8326 char *msg;
8327
8328 get_user_print_options (&opts);
8329 /* Field 4, the address, is omitted (which makes the columns not
8330 line up too nicely with the headers, but the effect is relatively
8331 readable). */
8332 if (opts.addressprint)
8333 {
8334 annotate_field (4);
8335 ui_out_field_skip (uiout, "addr");
8336 }
8337
8338 annotate_field (5);
8339 if (self->is_load)
8340 {
8341 if (self->regex)
8342 msg = xstrprintf (_("load of library matching %s"), self->regex);
8343 else
8344 msg = xstrdup (_("load of library"));
8345 }
8346 else
8347 {
8348 if (self->regex)
8349 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8350 else
8351 msg = xstrdup (_("unload of library"));
8352 }
8353 ui_out_field_string (uiout, "what", msg);
8354 xfree (msg);
8ac3646f
TT
8355
8356 if (ui_out_is_mi_like_p (uiout))
8357 ui_out_field_string (uiout, "catch-type",
8358 self->is_load ? "load" : "unload");
edcc5120
TT
8359}
8360
8361static void
8362print_mention_catch_solib (struct breakpoint *b)
8363{
8364 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8365
8366 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8367 self->is_load ? "load" : "unload");
8368}
8369
8370static void
8371print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8372{
8373 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8374
8375 fprintf_unfiltered (fp, "%s %s",
8376 b->disposition == disp_del ? "tcatch" : "catch",
8377 self->is_load ? "load" : "unload");
8378 if (self->regex)
8379 fprintf_unfiltered (fp, " %s", self->regex);
8380 fprintf_unfiltered (fp, "\n");
8381}
8382
8383static struct breakpoint_ops catch_solib_breakpoint_ops;
8384
91985142
MG
8385/* Shared helper function (MI and CLI) for creating and installing
8386 a shared object event catchpoint. If IS_LOAD is non-zero then
8387 the events to be caught are load events, otherwise they are
8388 unload events. If IS_TEMP is non-zero the catchpoint is a
8389 temporary one. If ENABLED is non-zero the catchpoint is
8390 created in an enabled state. */
edcc5120 8391
91985142
MG
8392void
8393add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8394{
8395 struct solib_catchpoint *c;
8396 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8397 struct cleanup *cleanup;
8398
edcc5120
TT
8399 if (!arg)
8400 arg = "";
8401 arg = skip_spaces (arg);
8402
8403 c = XCNEW (struct solib_catchpoint);
8404 cleanup = make_cleanup (xfree, c);
8405
8406 if (*arg != '\0')
8407 {
8408 int errcode;
8409
8410 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8411 if (errcode != 0)
8412 {
8413 char *err = get_regcomp_error (errcode, &c->compiled);
8414
8415 make_cleanup (xfree, err);
8416 error (_("Invalid regexp (%s): %s"), err, arg);
8417 }
8418 c->regex = xstrdup (arg);
8419 }
8420
8421 c->is_load = is_load;
91985142 8422 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8423 &catch_solib_breakpoint_ops);
8424
91985142
MG
8425 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8426
edcc5120
TT
8427 discard_cleanups (cleanup);
8428 install_breakpoint (0, &c->base, 1);
8429}
8430
91985142
MG
8431/* A helper function that does all the work for "catch load" and
8432 "catch unload". */
8433
8434static void
8435catch_load_or_unload (char *arg, int from_tty, int is_load,
8436 struct cmd_list_element *command)
8437{
8438 int tempflag;
8439 const int enabled = 1;
8440
8441 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8442
8443 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8444}
8445
edcc5120
TT
8446static void
8447catch_load_command_1 (char *arg, int from_tty,
8448 struct cmd_list_element *command)
8449{
8450 catch_load_or_unload (arg, from_tty, 1, command);
8451}
8452
8453static void
8454catch_unload_command_1 (char *arg, int from_tty,
8455 struct cmd_list_element *command)
8456{
8457 catch_load_or_unload (arg, from_tty, 0, command);
8458}
8459
be5c67c1
PA
8460/* An instance of this type is used to represent a syscall catchpoint.
8461 It includes a "struct breakpoint" as a kind of base class; users
8462 downcast to "struct breakpoint *" when needed. A breakpoint is
8463 really of this type iff its ops pointer points to
8464 CATCH_SYSCALL_BREAKPOINT_OPS. */
8465
8466struct syscall_catchpoint
8467{
8468 /* The base class. */
8469 struct breakpoint base;
8470
8471 /* Syscall numbers used for the 'catch syscall' feature. If no
8472 syscall has been specified for filtering, its value is NULL.
8473 Otherwise, it holds a list of all syscalls to be caught. The
8474 list elements are allocated with xmalloc. */
8475 VEC(int) *syscalls_to_be_caught;
8476};
8477
8478/* Implement the "dtor" breakpoint_ops method for syscall
8479 catchpoints. */
8480
8481static void
8482dtor_catch_syscall (struct breakpoint *b)
8483{
8484 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8485
8486 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8487
2060206e 8488 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8489}
8490
fa3064dd
YQ
8491static const struct inferior_data *catch_syscall_inferior_data = NULL;
8492
8493struct catch_syscall_inferior_data
8494{
8495 /* We keep a count of the number of times the user has requested a
8496 particular syscall to be tracked, and pass this information to the
8497 target. This lets capable targets implement filtering directly. */
8498
8499 /* Number of times that "any" syscall is requested. */
8500 int any_syscall_count;
8501
8502 /* Count of each system call. */
8503 VEC(int) *syscalls_counts;
8504
8505 /* This counts all syscall catch requests, so we can readily determine
8506 if any catching is necessary. */
8507 int total_syscalls_count;
8508};
8509
8510static struct catch_syscall_inferior_data*
8511get_catch_syscall_inferior_data (struct inferior *inf)
8512{
8513 struct catch_syscall_inferior_data *inf_data;
8514
8515 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8516 if (inf_data == NULL)
8517 {
41bf6aca 8518 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8519 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8520 }
8521
8522 return inf_data;
8523}
8524
8525static void
8526catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8527{
8528 xfree (arg);
8529}
8530
8531
a96d9b2e
SDJ
8532/* Implement the "insert" breakpoint_ops method for syscall
8533 catchpoints. */
8534
77b06cd7
TJB
8535static int
8536insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8537{
be5c67c1 8538 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8539 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8540 struct catch_syscall_inferior_data *inf_data
8541 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8542
fa3064dd 8543 ++inf_data->total_syscalls_count;
be5c67c1 8544 if (!c->syscalls_to_be_caught)
fa3064dd 8545 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8546 else
8547 {
8548 int i, iter;
cc59ec59 8549
a96d9b2e 8550 for (i = 0;
be5c67c1 8551 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8552 i++)
8553 {
8554 int elem;
cc59ec59 8555
fa3064dd 8556 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8557 {
fa3064dd 8558 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8559 uintptr_t vec_addr_offset
8560 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8561 uintptr_t vec_addr;
fa3064dd
YQ
8562 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8563 vec_addr = ((uintptr_t) VEC_address (int,
8564 inf_data->syscalls_counts)
8565 + vec_addr_offset);
a96d9b2e
SDJ
8566 memset ((void *) vec_addr, 0,
8567 (iter + 1 - old_size) * sizeof (int));
8568 }
fa3064dd
YQ
8569 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8570 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8571 }
8572 }
8573
dfd4cc63 8574 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8575 inf_data->total_syscalls_count != 0,
8576 inf_data->any_syscall_count,
8577 VEC_length (int,
8578 inf_data->syscalls_counts),
8579 VEC_address (int,
8580 inf_data->syscalls_counts));
a96d9b2e
SDJ
8581}
8582
8583/* Implement the "remove" breakpoint_ops method for syscall
8584 catchpoints. */
8585
8586static int
77b06cd7 8587remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8588{
be5c67c1 8589 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8590 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8591 struct catch_syscall_inferior_data *inf_data
8592 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8593
fa3064dd 8594 --inf_data->total_syscalls_count;
be5c67c1 8595 if (!c->syscalls_to_be_caught)
fa3064dd 8596 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8597 else
8598 {
8599 int i, iter;
cc59ec59 8600
a96d9b2e 8601 for (i = 0;
be5c67c1 8602 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8603 i++)
8604 {
8605 int elem;
fa3064dd 8606 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8607 /* Shouldn't happen. */
8608 continue;
fa3064dd
YQ
8609 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8610 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8611 }
8612 }
8613
dfd4cc63 8614 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8615 inf_data->total_syscalls_count != 0,
8616 inf_data->any_syscall_count,
8617 VEC_length (int,
8618 inf_data->syscalls_counts),
3e43a32a 8619 VEC_address (int,
fa3064dd 8620 inf_data->syscalls_counts));
a96d9b2e
SDJ
8621}
8622
8623/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8624 catchpoints. */
8625
8626static int
f1310107 8627breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8628 struct address_space *aspace, CORE_ADDR bp_addr,
8629 const struct target_waitstatus *ws)
a96d9b2e 8630{
4a64f543
MS
8631 /* We must check if we are catching specific syscalls in this
8632 breakpoint. If we are, then we must guarantee that the called
8633 syscall is the same syscall we are catching. */
a96d9b2e 8634 int syscall_number = 0;
be5c67c1
PA
8635 const struct syscall_catchpoint *c
8636 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8637
f90263c1
TT
8638 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8639 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8640 return 0;
8641
f90263c1
TT
8642 syscall_number = ws->value.syscall_number;
8643
a96d9b2e 8644 /* Now, checking if the syscall is the same. */
be5c67c1 8645 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8646 {
8647 int i, iter;
cc59ec59 8648
a96d9b2e 8649 for (i = 0;
be5c67c1 8650 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8651 i++)
8652 if (syscall_number == iter)
4924df79
GKB
8653 return 1;
8654
8655 return 0;
a96d9b2e
SDJ
8656 }
8657
8658 return 1;
8659}
8660
8661/* Implement the "print_it" breakpoint_ops method for syscall
8662 catchpoints. */
8663
8664static enum print_stop_action
348d480f 8665print_it_catch_syscall (bpstat bs)
a96d9b2e 8666{
36dfb11c 8667 struct ui_out *uiout = current_uiout;
348d480f 8668 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8669 /* These are needed because we want to know in which state a
8670 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8671 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8672 must print "called syscall" or "returned from syscall". */
8673 ptid_t ptid;
8674 struct target_waitstatus last;
8675 struct syscall s;
a96d9b2e
SDJ
8676
8677 get_last_target_status (&ptid, &last);
8678
8679 get_syscall_by_number (last.value.syscall_number, &s);
8680
8681 annotate_catchpoint (b->number);
8682
36dfb11c
TT
8683 if (b->disposition == disp_del)
8684 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8685 else
36dfb11c
TT
8686 ui_out_text (uiout, "\nCatchpoint ");
8687 if (ui_out_is_mi_like_p (uiout))
8688 {
8689 ui_out_field_string (uiout, "reason",
8690 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8691 ? EXEC_ASYNC_SYSCALL_ENTRY
8692 : EXEC_ASYNC_SYSCALL_RETURN));
8693 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8694 }
8695 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8696
8697 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8698 ui_out_text (uiout, " (call to syscall ");
8699 else
8700 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8701
36dfb11c
TT
8702 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8703 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8704 if (s.name != NULL)
8705 ui_out_field_string (uiout, "syscall-name", s.name);
8706
8707 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8708
8709 return PRINT_SRC_AND_LOC;
8710}
8711
8712/* Implement the "print_one" breakpoint_ops method for syscall
8713 catchpoints. */
8714
8715static void
8716print_one_catch_syscall (struct breakpoint *b,
f1310107 8717 struct bp_location **last_loc)
a96d9b2e 8718{
be5c67c1 8719 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8720 struct value_print_options opts;
79a45e25 8721 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8722
8723 get_user_print_options (&opts);
4a64f543
MS
8724 /* Field 4, the address, is omitted (which makes the columns not
8725 line up too nicely with the headers, but the effect is relatively
8726 readable). */
a96d9b2e
SDJ
8727 if (opts.addressprint)
8728 ui_out_field_skip (uiout, "addr");
8729 annotate_field (5);
8730
be5c67c1
PA
8731 if (c->syscalls_to_be_caught
8732 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8733 ui_out_text (uiout, "syscalls \"");
8734 else
8735 ui_out_text (uiout, "syscall \"");
8736
be5c67c1 8737 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8738 {
8739 int i, iter;
8740 char *text = xstrprintf ("%s", "");
cc59ec59 8741
a96d9b2e 8742 for (i = 0;
be5c67c1 8743 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8744 i++)
8745 {
8746 char *x = text;
8747 struct syscall s;
8748 get_syscall_by_number (iter, &s);
8749
8750 if (s.name != NULL)
8751 text = xstrprintf ("%s%s, ", text, s.name);
8752 else
8753 text = xstrprintf ("%s%d, ", text, iter);
8754
8755 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8756 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8757 on every call. */
8758 xfree (x);
8759 }
8760 /* Remove the last comma. */
8761 text[strlen (text) - 2] = '\0';
8762 ui_out_field_string (uiout, "what", text);
8763 }
8764 else
8765 ui_out_field_string (uiout, "what", "<any syscall>");
8766 ui_out_text (uiout, "\" ");
8ac3646f
TT
8767
8768 if (ui_out_is_mi_like_p (uiout))
8769 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8770}
8771
8772/* Implement the "print_mention" breakpoint_ops method for syscall
8773 catchpoints. */
8774
8775static void
8776print_mention_catch_syscall (struct breakpoint *b)
8777{
be5c67c1
PA
8778 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8779
8780 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8781 {
8782 int i, iter;
8783
be5c67c1 8784 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8785 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8786 else
8787 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8788
8789 for (i = 0;
be5c67c1 8790 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8791 i++)
8792 {
8793 struct syscall s;
8794 get_syscall_by_number (iter, &s);
8795
8796 if (s.name)
8797 printf_filtered (" '%s' [%d]", s.name, s.number);
8798 else
8799 printf_filtered (" %d", s.number);
8800 }
8801 printf_filtered (")");
8802 }
8803 else
8804 printf_filtered (_("Catchpoint %d (any syscall)"),
8805 b->number);
8806}
8807
6149aea9
PA
8808/* Implement the "print_recreate" breakpoint_ops method for syscall
8809 catchpoints. */
8810
8811static void
8812print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8813{
be5c67c1
PA
8814 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8815
6149aea9
PA
8816 fprintf_unfiltered (fp, "catch syscall");
8817
be5c67c1 8818 if (c->syscalls_to_be_caught)
6149aea9
PA
8819 {
8820 int i, iter;
8821
8822 for (i = 0;
be5c67c1 8823 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8824 i++)
8825 {
8826 struct syscall s;
8827
8828 get_syscall_by_number (iter, &s);
8829 if (s.name)
8830 fprintf_unfiltered (fp, " %s", s.name);
8831 else
8832 fprintf_unfiltered (fp, " %d", s.number);
8833 }
8834 }
d9b3f62e 8835 print_recreate_thread (b, fp);
6149aea9
PA
8836}
8837
a96d9b2e
SDJ
8838/* The breakpoint_ops structure to be used in syscall catchpoints. */
8839
2060206e 8840static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8841
8842/* Returns non-zero if 'b' is a syscall catchpoint. */
8843
8844static int
8845syscall_catchpoint_p (struct breakpoint *b)
8846{
8847 return (b->ops == &catch_syscall_breakpoint_ops);
8848}
8849
346774a9
PA
8850/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8851 is non-zero, then make the breakpoint temporary. If COND_STRING is
8852 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8853 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8854
ab04a2af 8855void
346774a9
PA
8856init_catchpoint (struct breakpoint *b,
8857 struct gdbarch *gdbarch, int tempflag,
8858 char *cond_string,
c0a91b2b 8859 const struct breakpoint_ops *ops)
c906108c 8860{
c5aa993b 8861 struct symtab_and_line sal;
346774a9 8862
fe39c653 8863 init_sal (&sal);
6c95b8df 8864 sal.pspace = current_program_space;
c5aa993b 8865
28010a5d 8866 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8867
1b36a34b 8868 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8869 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8870}
8871
28010a5d 8872void
3ea46bff 8873install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8874{
8875 add_to_breakpoint_chain (b);
3a5c3e22 8876 set_breakpoint_number (internal, b);
558a9d82
YQ
8877 if (is_tracepoint (b))
8878 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8879 if (!internal)
8880 mention (b);
c56053d2 8881 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8882
8883 if (update_gll)
44702360 8884 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8885}
8886
9b70b993 8887static void
a6d9a66e
UW
8888create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8889 int tempflag, char *cond_string,
c0a91b2b 8890 const struct breakpoint_ops *ops)
c906108c 8891{
e29a4733 8892 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8893
e29a4733
PA
8894 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8895
8896 c->forked_inferior_pid = null_ptid;
8897
3ea46bff 8898 install_breakpoint (0, &c->base, 1);
c906108c
SS
8899}
8900
fe798b75
JB
8901/* Exec catchpoints. */
8902
b4d90040
PA
8903/* An instance of this type is used to represent an exec catchpoint.
8904 It includes a "struct breakpoint" as a kind of base class; users
8905 downcast to "struct breakpoint *" when needed. A breakpoint is
8906 really of this type iff its ops pointer points to
8907 CATCH_EXEC_BREAKPOINT_OPS. */
8908
8909struct exec_catchpoint
8910{
8911 /* The base class. */
8912 struct breakpoint base;
8913
8914 /* Filename of a program whose exec triggered this catchpoint.
8915 This field is only valid immediately after this catchpoint has
8916 triggered. */
8917 char *exec_pathname;
8918};
8919
8920/* Implement the "dtor" breakpoint_ops method for exec
8921 catchpoints. */
8922
8923static void
8924dtor_catch_exec (struct breakpoint *b)
8925{
8926 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8927
8928 xfree (c->exec_pathname);
348d480f 8929
2060206e 8930 base_breakpoint_ops.dtor (b);
b4d90040
PA
8931}
8932
77b06cd7
TJB
8933static int
8934insert_catch_exec (struct bp_location *bl)
c906108c 8935{
dfd4cc63 8936 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8937}
c906108c 8938
fe798b75 8939static int
77b06cd7 8940remove_catch_exec (struct bp_location *bl)
fe798b75 8941{
dfd4cc63 8942 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8943}
c906108c 8944
fe798b75 8945static int
f1310107 8946breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8947 struct address_space *aspace, CORE_ADDR bp_addr,
8948 const struct target_waitstatus *ws)
fe798b75 8949{
b4d90040
PA
8950 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8951
f90263c1
TT
8952 if (ws->kind != TARGET_WAITKIND_EXECD)
8953 return 0;
8954
8955 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8956 return 1;
fe798b75 8957}
c906108c 8958
fe798b75 8959static enum print_stop_action
348d480f 8960print_it_catch_exec (bpstat bs)
fe798b75 8961{
36dfb11c 8962 struct ui_out *uiout = current_uiout;
348d480f 8963 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8964 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8965
fe798b75 8966 annotate_catchpoint (b->number);
36dfb11c
TT
8967 if (b->disposition == disp_del)
8968 ui_out_text (uiout, "\nTemporary catchpoint ");
8969 else
8970 ui_out_text (uiout, "\nCatchpoint ");
8971 if (ui_out_is_mi_like_p (uiout))
8972 {
8973 ui_out_field_string (uiout, "reason",
8974 async_reason_lookup (EXEC_ASYNC_EXEC));
8975 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8976 }
8977 ui_out_field_int (uiout, "bkptno", b->number);
8978 ui_out_text (uiout, " (exec'd ");
8979 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8980 ui_out_text (uiout, "), ");
8981
fe798b75 8982 return PRINT_SRC_AND_LOC;
c906108c
SS
8983}
8984
fe798b75 8985static void
a6d9a66e 8986print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8987{
b4d90040 8988 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8989 struct value_print_options opts;
79a45e25 8990 struct ui_out *uiout = current_uiout;
fe798b75
JB
8991
8992 get_user_print_options (&opts);
8993
8994 /* Field 4, the address, is omitted (which makes the columns
8995 not line up too nicely with the headers, but the effect
8996 is relatively readable). */
8997 if (opts.addressprint)
8998 ui_out_field_skip (uiout, "addr");
8999 annotate_field (5);
9000 ui_out_text (uiout, "exec");
b4d90040 9001 if (c->exec_pathname != NULL)
fe798b75
JB
9002 {
9003 ui_out_text (uiout, ", program \"");
b4d90040 9004 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
9005 ui_out_text (uiout, "\" ");
9006 }
8ac3646f
TT
9007
9008 if (ui_out_is_mi_like_p (uiout))
9009 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
9010}
9011
9012static void
9013print_mention_catch_exec (struct breakpoint *b)
9014{
9015 printf_filtered (_("Catchpoint %d (exec)"), b->number);
9016}
9017
6149aea9
PA
9018/* Implement the "print_recreate" breakpoint_ops method for exec
9019 catchpoints. */
9020
9021static void
9022print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
9023{
9024 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 9025 print_recreate_thread (b, fp);
6149aea9
PA
9026}
9027
2060206e 9028static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 9029
a96d9b2e
SDJ
9030static void
9031create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 9032 const struct breakpoint_ops *ops)
a96d9b2e 9033{
be5c67c1 9034 struct syscall_catchpoint *c;
a96d9b2e 9035 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 9036
be5c67c1
PA
9037 c = XNEW (struct syscall_catchpoint);
9038 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
9039 c->syscalls_to_be_caught = filter;
a96d9b2e 9040
3ea46bff 9041 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
9042}
9043
c906108c 9044static int
fba45db2 9045hw_breakpoint_used_count (void)
c906108c 9046{
c906108c 9047 int i = 0;
f1310107
TJB
9048 struct breakpoint *b;
9049 struct bp_location *bl;
c906108c
SS
9050
9051 ALL_BREAKPOINTS (b)
c5aa993b 9052 {
d6b74ac4 9053 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
9054 for (bl = b->loc; bl; bl = bl->next)
9055 {
9056 /* Special types of hardware breakpoints may use more than
9057 one register. */
348d480f 9058 i += b->ops->resources_needed (bl);
f1310107 9059 }
c5aa993b 9060 }
c906108c
SS
9061
9062 return i;
9063}
9064
a1398e0c
PA
9065/* Returns the resources B would use if it were a hardware
9066 watchpoint. */
9067
c906108c 9068static int
a1398e0c 9069hw_watchpoint_use_count (struct breakpoint *b)
c906108c 9070{
c906108c 9071 int i = 0;
e09342b5 9072 struct bp_location *bl;
c906108c 9073
a1398e0c
PA
9074 if (!breakpoint_enabled (b))
9075 return 0;
9076
9077 for (bl = b->loc; bl; bl = bl->next)
9078 {
9079 /* Special types of hardware watchpoints may use more than
9080 one register. */
9081 i += b->ops->resources_needed (bl);
9082 }
9083
9084 return i;
9085}
9086
9087/* Returns the sum the used resources of all hardware watchpoints of
9088 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9089 the sum of the used resources of all hardware watchpoints of other
9090 types _not_ TYPE. */
9091
9092static int
9093hw_watchpoint_used_count_others (struct breakpoint *except,
9094 enum bptype type, int *other_type_used)
9095{
9096 int i = 0;
9097 struct breakpoint *b;
9098
c906108c
SS
9099 *other_type_used = 0;
9100 ALL_BREAKPOINTS (b)
e09342b5 9101 {
a1398e0c
PA
9102 if (b == except)
9103 continue;
e09342b5
TJB
9104 if (!breakpoint_enabled (b))
9105 continue;
9106
a1398e0c
PA
9107 if (b->type == type)
9108 i += hw_watchpoint_use_count (b);
9109 else if (is_hardware_watchpoint (b))
9110 *other_type_used = 1;
e09342b5
TJB
9111 }
9112
c906108c
SS
9113 return i;
9114}
9115
c906108c 9116void
fba45db2 9117disable_watchpoints_before_interactive_call_start (void)
c906108c 9118{
c5aa993b 9119 struct breakpoint *b;
c906108c
SS
9120
9121 ALL_BREAKPOINTS (b)
c5aa993b 9122 {
cc60f2e3 9123 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 9124 {
b5de0fa7 9125 b->enable_state = bp_call_disabled;
44702360 9126 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
9127 }
9128 }
c906108c
SS
9129}
9130
9131void
fba45db2 9132enable_watchpoints_after_interactive_call_stop (void)
c906108c 9133{
c5aa993b 9134 struct breakpoint *b;
c906108c
SS
9135
9136 ALL_BREAKPOINTS (b)
c5aa993b 9137 {
cc60f2e3 9138 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 9139 {
b5de0fa7 9140 b->enable_state = bp_enabled;
44702360 9141 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
9142 }
9143 }
c906108c
SS
9144}
9145
8bea4e01
UW
9146void
9147disable_breakpoints_before_startup (void)
9148{
6c95b8df 9149 current_program_space->executing_startup = 1;
44702360 9150 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
9151}
9152
9153void
9154enable_breakpoints_after_startup (void)
9155{
6c95b8df 9156 current_program_space->executing_startup = 0;
f8eba3c6 9157 breakpoint_re_set ();
8bea4e01
UW
9158}
9159
7c16b83e
PA
9160/* Create a new single-step breakpoint for thread THREAD, with no
9161 locations. */
c906108c 9162
7c16b83e
PA
9163static struct breakpoint *
9164new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
9165{
9166 struct breakpoint *b = XNEW (struct breakpoint);
9167
9168 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
9169 &momentary_breakpoint_ops);
9170
9171 b->disposition = disp_donttouch;
9172 b->frame_id = null_frame_id;
9173
9174 b->thread = thread;
9175 gdb_assert (b->thread != 0);
9176
9177 add_to_breakpoint_chain (b);
9178
9179 return b;
9180}
9181
9182/* Set a momentary breakpoint of type TYPE at address specified by
9183 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
9184 frame. */
c906108c
SS
9185
9186struct breakpoint *
a6d9a66e
UW
9187set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9188 struct frame_id frame_id, enum bptype type)
c906108c 9189{
52f0bd74 9190 struct breakpoint *b;
edb3359d 9191
193facb3
JK
9192 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9193 tail-called one. */
9194 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9195
06edf0c0 9196 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9197 b->enable_state = bp_enabled;
9198 b->disposition = disp_donttouch;
818dd999 9199 b->frame_id = frame_id;
c906108c 9200
4a64f543
MS
9201 /* If we're debugging a multi-threaded program, then we want
9202 momentary breakpoints to be active in only a single thread of
9203 control. */
39f77062
KB
9204 if (in_thread_list (inferior_ptid))
9205 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9206
44702360 9207 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 9208
c906108c
SS
9209 return b;
9210}
611c83ae 9211
06edf0c0 9212/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
9213 The new breakpoint will have type TYPE, use OPS as its
9214 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 9215
06edf0c0
PA
9216static struct breakpoint *
9217momentary_breakpoint_from_master (struct breakpoint *orig,
9218 enum bptype type,
a1aa2221
LM
9219 const struct breakpoint_ops *ops,
9220 int loc_enabled)
e58b0e63
PA
9221{
9222 struct breakpoint *copy;
9223
06edf0c0 9224 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9225 copy->loc = allocate_bp_location (copy);
0e30163f 9226 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9227
a6d9a66e 9228 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9229 copy->loc->requested_address = orig->loc->requested_address;
9230 copy->loc->address = orig->loc->address;
9231 copy->loc->section = orig->loc->section;
6c95b8df 9232 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9233 copy->loc->probe = orig->loc->probe;
f8eba3c6 9234 copy->loc->line_number = orig->loc->line_number;
2f202fde 9235 copy->loc->symtab = orig->loc->symtab;
a1aa2221 9236 copy->loc->enabled = loc_enabled;
e58b0e63
PA
9237 copy->frame_id = orig->frame_id;
9238 copy->thread = orig->thread;
6c95b8df 9239 copy->pspace = orig->pspace;
e58b0e63
PA
9240
9241 copy->enable_state = bp_enabled;
9242 copy->disposition = disp_donttouch;
9243 copy->number = internal_breakpoint_number--;
9244
44702360 9245 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
9246 return copy;
9247}
9248
06edf0c0
PA
9249/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9250 ORIG is NULL. */
9251
9252struct breakpoint *
9253clone_momentary_breakpoint (struct breakpoint *orig)
9254{
9255 /* If there's nothing to clone, then return nothing. */
9256 if (orig == NULL)
9257 return NULL;
9258
a1aa2221 9259 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9260}
9261
611c83ae 9262struct breakpoint *
a6d9a66e
UW
9263set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9264 enum bptype type)
611c83ae
PA
9265{
9266 struct symtab_and_line sal;
9267
9268 sal = find_pc_line (pc, 0);
9269 sal.pc = pc;
9270 sal.section = find_pc_overlay (pc);
9271 sal.explicit_pc = 1;
9272
a6d9a66e 9273 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9274}
c906108c 9275\f
c5aa993b 9276
c906108c
SS
9277/* Tell the user we have just set a breakpoint B. */
9278
9279static void
fba45db2 9280mention (struct breakpoint *b)
c906108c 9281{
348d480f 9282 b->ops->print_mention (b);
79a45e25 9283 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9284 return;
c906108c
SS
9285 printf_filtered ("\n");
9286}
c906108c 9287\f
c5aa993b 9288
0d381245 9289static struct bp_location *
39d61571 9290add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9291 const struct symtab_and_line *sal)
9292{
9293 struct bp_location *loc, **tmp;
3742cc8b
YQ
9294 CORE_ADDR adjusted_address;
9295 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9296
9297 if (loc_gdbarch == NULL)
9298 loc_gdbarch = b->gdbarch;
9299
9300 /* Adjust the breakpoint's address prior to allocating a location.
9301 Once we call allocate_bp_location(), that mostly uninitialized
9302 location will be placed on the location chain. Adjustment of the
9303 breakpoint may cause target_read_memory() to be called and we do
9304 not want its scan of the location chain to find a breakpoint and
9305 location that's only been partially initialized. */
9306 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9307 sal->pc, b->type);
0d381245 9308
d30113d4 9309 /* Sort the locations by their ADDRESS. */
39d61571 9310 loc = allocate_bp_location (b);
d30113d4
JK
9311 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9312 tmp = &((*tmp)->next))
0d381245 9313 ;
d30113d4 9314 loc->next = *tmp;
0d381245 9315 *tmp = loc;
3742cc8b 9316
0d381245 9317 loc->requested_address = sal->pc;
3742cc8b 9318 loc->address = adjusted_address;
6c95b8df 9319 loc->pspace = sal->pspace;
729662a5
TT
9320 loc->probe.probe = sal->probe;
9321 loc->probe.objfile = sal->objfile;
6c95b8df 9322 gdb_assert (loc->pspace != NULL);
0d381245 9323 loc->section = sal->section;
3742cc8b 9324 loc->gdbarch = loc_gdbarch;
f8eba3c6 9325 loc->line_number = sal->line;
2f202fde 9326 loc->symtab = sal->symtab;
f8eba3c6 9327
0e30163f
JK
9328 set_breakpoint_location_function (loc,
9329 sal->explicit_pc || sal->explicit_line);
0d381245
VP
9330 return loc;
9331}
514f746b
AR
9332\f
9333
9334/* Return 1 if LOC is pointing to a permanent breakpoint,
9335 return 0 otherwise. */
9336
9337static int
9338bp_loc_is_permanent (struct bp_location *loc)
9339{
9340 int len;
9341 CORE_ADDR addr;
1afeeb75 9342 const gdb_byte *bpoint;
514f746b 9343 gdb_byte *target_mem;
939c61fa
JK
9344 struct cleanup *cleanup;
9345 int retval = 0;
514f746b
AR
9346
9347 gdb_assert (loc != NULL);
9348
9349 addr = loc->address;
1afeeb75 9350 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9351
939c61fa 9352 /* Software breakpoints unsupported? */
1afeeb75 9353 if (bpoint == NULL)
939c61fa
JK
9354 return 0;
9355
514f746b
AR
9356 target_mem = alloca (len);
9357
939c61fa
JK
9358 /* Enable the automatic memory restoration from breakpoints while
9359 we read the memory. Otherwise we could say about our temporary
9360 breakpoints they are permanent. */
6c95b8df
PA
9361 cleanup = save_current_space_and_thread ();
9362
9363 switch_to_program_space_and_thread (loc->pspace);
9364 make_show_memory_breakpoints_cleanup (0);
939c61fa 9365
514f746b 9366 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9367 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9368 retval = 1;
514f746b 9369
939c61fa
JK
9370 do_cleanups (cleanup);
9371
9372 return retval;
514f746b
AR
9373}
9374
e7e0cddf
SS
9375/* Build a command list for the dprintf corresponding to the current
9376 settings of the dprintf style options. */
9377
9378static void
9379update_dprintf_command_list (struct breakpoint *b)
9380{
9381 char *dprintf_args = b->extra_string;
9382 char *printf_line = NULL;
9383
9384 if (!dprintf_args)
9385 return;
9386
9387 dprintf_args = skip_spaces (dprintf_args);
9388
9389 /* Allow a comma, as it may have terminated a location, but don't
9390 insist on it. */
9391 if (*dprintf_args == ',')
9392 ++dprintf_args;
9393 dprintf_args = skip_spaces (dprintf_args);
9394
9395 if (*dprintf_args != '"')
9396 error (_("Bad format string, missing '\"'."));
9397
d3ce09f5 9398 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9399 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9400 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9401 {
9402 if (!dprintf_function)
9403 error (_("No function supplied for dprintf call"));
9404
9405 if (dprintf_channel && strlen (dprintf_channel) > 0)
9406 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9407 dprintf_function,
9408 dprintf_channel,
9409 dprintf_args);
9410 else
9411 printf_line = xstrprintf ("call (void) %s (%s)",
9412 dprintf_function,
9413 dprintf_args);
9414 }
d3ce09f5
SS
9415 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9416 {
9417 if (target_can_run_breakpoint_commands ())
9418 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9419 else
9420 {
9421 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9422 printf_line = xstrprintf ("printf %s", dprintf_args);
9423 }
9424 }
e7e0cddf
SS
9425 else
9426 internal_error (__FILE__, __LINE__,
9427 _("Invalid dprintf style."));
9428
f28045c2 9429 gdb_assert (printf_line != NULL);
9d6e6e84 9430 /* Manufacture a printf sequence. */
f28045c2 9431 {
9d6e6e84
HZ
9432 struct command_line *printf_cmd_line
9433 = xmalloc (sizeof (struct command_line));
e7e0cddf 9434
f28045c2
YQ
9435 printf_cmd_line = xmalloc (sizeof (struct command_line));
9436 printf_cmd_line->control_type = simple_control;
9437 printf_cmd_line->body_count = 0;
9438 printf_cmd_line->body_list = NULL;
9d6e6e84 9439 printf_cmd_line->next = NULL;
f28045c2 9440 printf_cmd_line->line = printf_line;
e7e0cddf 9441
f28045c2
YQ
9442 breakpoint_set_commands (b, printf_cmd_line);
9443 }
e7e0cddf
SS
9444}
9445
9446/* Update all dprintf commands, making their command lists reflect
9447 current style settings. */
9448
9449static void
9450update_dprintf_commands (char *args, int from_tty,
9451 struct cmd_list_element *c)
9452{
9453 struct breakpoint *b;
9454
9455 ALL_BREAKPOINTS (b)
9456 {
9457 if (b->type == bp_dprintf)
9458 update_dprintf_command_list (b);
9459 }
9460}
c3f6f71d 9461
018d34a4
VP
9462/* Create a breakpoint with SAL as location. Use ADDR_STRING
9463 as textual description of the location, and COND_STRING
db107f19 9464 as condition expression. */
018d34a4
VP
9465
9466static void
d9b3f62e
PA
9467init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9468 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9469 char *filter, char *cond_string,
e7e0cddf 9470 char *extra_string,
d9b3f62e
PA
9471 enum bptype type, enum bpdisp disposition,
9472 int thread, int task, int ignore_count,
c0a91b2b 9473 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9474 int enabled, int internal, unsigned flags,
9475 int display_canonical)
018d34a4 9476{
0d381245 9477 int i;
018d34a4
VP
9478
9479 if (type == bp_hardware_breakpoint)
9480 {
fbbd034e
AS
9481 int target_resources_ok;
9482
9483 i = hw_breakpoint_used_count ();
9484 target_resources_ok =
9485 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9486 i + 1, 0);
9487 if (target_resources_ok == 0)
9488 error (_("No hardware breakpoint support in the target."));
9489 else if (target_resources_ok < 0)
9490 error (_("Hardware breakpoints used exceeds limit."));
9491 }
9492
6c95b8df
PA
9493 gdb_assert (sals.nelts > 0);
9494
0d381245
VP
9495 for (i = 0; i < sals.nelts; ++i)
9496 {
9497 struct symtab_and_line sal = sals.sals[i];
9498 struct bp_location *loc;
9499
9500 if (from_tty)
5af949e3
UW
9501 {
9502 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9503 if (!loc_gdbarch)
9504 loc_gdbarch = gdbarch;
9505
9506 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9507 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9508 }
0d381245
VP
9509
9510 if (i == 0)
9511 {
d9b3f62e 9512 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9513 b->thread = thread;
4a306c9a 9514 b->task = task;
855a6e68 9515
0d381245 9516 b->cond_string = cond_string;
e7e0cddf 9517 b->extra_string = extra_string;
0d381245 9518 b->ignore_count = ignore_count;
41447f92 9519 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9520 b->disposition = disposition;
6c95b8df 9521
44f238bb
PA
9522 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9523 b->loc->inserted = 1;
9524
0fb4aa4b
PA
9525 if (type == bp_static_tracepoint)
9526 {
d9b3f62e 9527 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9528 struct static_tracepoint_marker marker;
9529
983af33b 9530 if (strace_marker_p (b))
0fb4aa4b
PA
9531 {
9532 /* We already know the marker exists, otherwise, we
9533 wouldn't see a sal for it. */
9534 char *p = &addr_string[3];
9535 char *endp;
9536 char *marker_str;
0fb4aa4b 9537
e9cafbcc 9538 p = skip_spaces (p);
0fb4aa4b 9539
e9cafbcc 9540 endp = skip_to_space (p);
0fb4aa4b
PA
9541
9542 marker_str = savestring (p, endp - p);
d9b3f62e 9543 t->static_trace_marker_id = marker_str;
0fb4aa4b 9544
3e43a32a
MS
9545 printf_filtered (_("Probed static tracepoint "
9546 "marker \"%s\"\n"),
d9b3f62e 9547 t->static_trace_marker_id);
0fb4aa4b
PA
9548 }
9549 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9550 {
d9b3f62e 9551 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9552 release_static_tracepoint_marker (&marker);
9553
3e43a32a
MS
9554 printf_filtered (_("Probed static tracepoint "
9555 "marker \"%s\"\n"),
d9b3f62e 9556 t->static_trace_marker_id);
0fb4aa4b
PA
9557 }
9558 else
3e43a32a
MS
9559 warning (_("Couldn't determine the static "
9560 "tracepoint marker to probe"));
0fb4aa4b
PA
9561 }
9562
0d381245
VP
9563 loc = b->loc;
9564 }
9565 else
018d34a4 9566 {
39d61571 9567 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9568 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9569 loc->inserted = 1;
0d381245
VP
9570 }
9571
514f746b
AR
9572 if (bp_loc_is_permanent (loc))
9573 make_breakpoint_permanent (b);
9574
0d381245
VP
9575 if (b->cond_string)
9576 {
bbc13ae3
KS
9577 const char *arg = b->cond_string;
9578
1bb9788d
TT
9579 loc->cond = parse_exp_1 (&arg, loc->address,
9580 block_for_pc (loc->address), 0);
0d381245 9581 if (*arg)
588ae58c 9582 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9583 }
e7e0cddf
SS
9584
9585 /* Dynamic printf requires and uses additional arguments on the
9586 command line, otherwise it's an error. */
9587 if (type == bp_dprintf)
9588 {
9589 if (b->extra_string)
9590 update_dprintf_command_list (b);
9591 else
9592 error (_("Format string required"));
9593 }
9594 else if (b->extra_string)
588ae58c 9595 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9596 }
018d34a4 9597
56435ebe 9598 b->display_canonical = display_canonical;
018d34a4
VP
9599 if (addr_string)
9600 b->addr_string = addr_string;
9601 else
9602 /* addr_string has to be used or breakpoint_re_set will delete
9603 me. */
5af949e3
UW
9604 b->addr_string
9605 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9606 b->filter = filter;
d9b3f62e 9607}
018d34a4 9608
d9b3f62e
PA
9609static void
9610create_breakpoint_sal (struct gdbarch *gdbarch,
9611 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9612 char *filter, char *cond_string,
e7e0cddf 9613 char *extra_string,
d9b3f62e
PA
9614 enum bptype type, enum bpdisp disposition,
9615 int thread, int task, int ignore_count,
c0a91b2b 9616 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9617 int enabled, int internal, unsigned flags,
9618 int display_canonical)
d9b3f62e
PA
9619{
9620 struct breakpoint *b;
9621 struct cleanup *old_chain;
9622
9623 if (is_tracepoint_type (type))
9624 {
9625 struct tracepoint *t;
9626
9627 t = XCNEW (struct tracepoint);
9628 b = &t->base;
9629 }
9630 else
9631 b = XNEW (struct breakpoint);
9632
9633 old_chain = make_cleanup (xfree, b);
9634
9635 init_breakpoint_sal (b, gdbarch,
9636 sals, addr_string,
e7e0cddf 9637 filter, cond_string, extra_string,
d9b3f62e
PA
9638 type, disposition,
9639 thread, task, ignore_count,
9640 ops, from_tty,
44f238bb
PA
9641 enabled, internal, flags,
9642 display_canonical);
d9b3f62e
PA
9643 discard_cleanups (old_chain);
9644
3ea46bff 9645 install_breakpoint (internal, b, 0);
018d34a4
VP
9646}
9647
9648/* Add SALS.nelts breakpoints to the breakpoint table. For each
9649 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9650 value. COND_STRING, if not NULL, specified the condition to be
9651 used for all breakpoints. Essentially the only case where
9652 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9653 function. In that case, it's still not possible to specify
9654 separate conditions for different overloaded functions, so
9655 we take just a single condition string.
9656
c3f6f71d 9657 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9658 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9659 array contents). If the function fails (error() is called), the
9660 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9661 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9662
9663static void
8cdf0e15 9664create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9665 struct linespec_result *canonical,
e7e0cddf 9666 char *cond_string, char *extra_string,
8cdf0e15
VP
9667 enum bptype type, enum bpdisp disposition,
9668 int thread, int task, int ignore_count,
c0a91b2b 9669 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9670 int enabled, int internal, unsigned flags)
c906108c 9671{
018d34a4 9672 int i;
f8eba3c6 9673 struct linespec_sals *lsal;
cc59ec59 9674
f8eba3c6
TT
9675 if (canonical->pre_expanded)
9676 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9677
9678 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9679 {
f8eba3c6
TT
9680 /* Note that 'addr_string' can be NULL in the case of a plain
9681 'break', without arguments. */
9682 char *addr_string = (canonical->addr_string
9683 ? xstrdup (canonical->addr_string)
9684 : NULL);
9685 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9686 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9687
f8eba3c6
TT
9688 make_cleanup (xfree, filter_string);
9689 create_breakpoint_sal (gdbarch, lsal->sals,
9690 addr_string,
9691 filter_string,
e7e0cddf
SS
9692 cond_string, extra_string,
9693 type, disposition,
84f4c1fe 9694 thread, task, ignore_count, ops,
44f238bb 9695 from_tty, enabled, internal, flags,
56435ebe 9696 canonical->special_display);
f8eba3c6 9697 discard_cleanups (inner);
c3f6f71d 9698 }
c3f6f71d 9699}
c906108c 9700
9998af43 9701/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9702 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9703 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9704 address strings. ADDRESS points to the end of the SAL.
9705
9706 The array and the line spec strings are allocated on the heap, it is
9707 the caller's responsibility to free them. */
c906108c 9708
b9362cc7 9709static void
c3f6f71d 9710parse_breakpoint_sals (char **address,
58438ac1 9711 struct linespec_result *canonical)
c3f6f71d 9712{
c3f6f71d 9713 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9714 breakpoint. */
c3f6f71d
JM
9715 if ((*address) == NULL
9716 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9717 {
1bfeeb0f
JL
9718 /* The last displayed codepoint, if it's valid, is our default breakpoint
9719 address. */
9720 if (last_displayed_sal_is_valid ())
c906108c 9721 {
f8eba3c6 9722 struct linespec_sals lsal;
c3f6f71d 9723 struct symtab_and_line sal;
1c8cdcb1 9724 CORE_ADDR pc;
cc59ec59 9725
4a64f543 9726 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9727 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9728 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9729
9730 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9731 corresponding to the last call to print_frame_info.
9732 Be sure to reinitialize LINE with NOTCURRENT == 0
9733 as the breakpoint line number is inappropriate otherwise.
9734 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9735 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9736 pc = sal.pc;
9737 sal = find_pc_line (pc, 0);
00903456 9738
4a64f543 9739 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9740 where PC is the last displayed codepoint's address. So
9741 make sure to set sal.explicit_pc to prevent GDB from
9742 trying to expand the list of sals to include all other
9743 instances with the same symtab and line. */
1c8cdcb1 9744 sal.pc = pc;
00903456
JK
9745 sal.explicit_pc = 1;
9746
f8eba3c6
TT
9747 lsal.sals.sals[0] = sal;
9748 lsal.sals.nelts = 1;
9749 lsal.canonical = NULL;
9750
9751 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9752 }
9753 else
8a3fe4f8 9754 error (_("No default breakpoint address now."));
c906108c
SS
9755 }
9756 else
9757 {
cc80f267
JK
9758 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9759
c906108c 9760 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9761 current_source_symtab (which is decode_line_1's default).
9762 This should produce the results we want almost all of the
cc80f267
JK
9763 time while leaving default_breakpoint_* alone.
9764
9765 ObjC: However, don't match an Objective-C method name which
9766 may have a '+' or '-' succeeded by a '['. */
9767 if (last_displayed_sal_is_valid ()
9768 && (!cursal.symtab
9769 || ((strchr ("+-", (*address)[0]) != NULL)
9770 && ((*address)[1] != '['))))
f8eba3c6
TT
9771 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9772 get_last_displayed_symtab (),
9773 get_last_displayed_line (),
9774 canonical, NULL, NULL);
c906108c 9775 else
f8eba3c6 9776 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9777 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9778 }
c3f6f71d 9779}
c906108c 9780
c906108c 9781
c3f6f71d 9782/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9783 inserted as a breakpoint. If it can't throw an error. */
c906108c 9784
b9362cc7 9785static void
23e7acfb 9786breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9787{
9788 int i;
cc59ec59 9789
c3f6f71d 9790 for (i = 0; i < sals->nelts; i++)
ee53e872 9791 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9792}
9793
7a697b8d
SS
9794/* Fast tracepoints may have restrictions on valid locations. For
9795 instance, a fast tracepoint using a jump instead of a trap will
9796 likely have to overwrite more bytes than a trap would, and so can
9797 only be placed where the instruction is longer than the jump, or a
9798 multi-instruction sequence does not have a jump into the middle of
9799 it, etc. */
9800
9801static void
9802check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9803 struct symtabs_and_lines *sals)
9804{
9805 int i, rslt;
9806 struct symtab_and_line *sal;
9807 char *msg;
9808 struct cleanup *old_chain;
9809
9810 for (i = 0; i < sals->nelts; i++)
9811 {
f8eba3c6
TT
9812 struct gdbarch *sarch;
9813
7a697b8d
SS
9814 sal = &sals->sals[i];
9815
f8eba3c6
TT
9816 sarch = get_sal_arch (*sal);
9817 /* We fall back to GDBARCH if there is no architecture
9818 associated with SAL. */
9819 if (sarch == NULL)
9820 sarch = gdbarch;
9821 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9822 NULL, &msg);
9823 old_chain = make_cleanup (xfree, msg);
9824
9825 if (!rslt)
9826 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9827 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9828
9829 do_cleanups (old_chain);
9830 }
9831}
9832
af4908ba
KS
9833/* Issue an invalid thread ID error. */
9834
9835static void ATTRIBUTE_NORETURN
9836invalid_thread_id_error (int id)
9837{
9838 error (_("Unknown thread %d."), id);
9839}
9840
018d34a4
VP
9841/* Given TOK, a string specification of condition and thread, as
9842 accepted by the 'break' command, extract the condition
9843 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9844 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9845 If no condition is found, *COND_STRING is set to NULL.
9846 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9847
9848static void
bbc13ae3 9849find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9850 char **cond_string, int *thread, int *task,
9851 char **rest)
018d34a4
VP
9852{
9853 *cond_string = NULL;
9854 *thread = -1;
ed1d1739
KS
9855 *task = 0;
9856 *rest = NULL;
9857
018d34a4
VP
9858 while (tok && *tok)
9859 {
bbc13ae3 9860 const char *end_tok;
018d34a4 9861 int toklen;
bbc13ae3
KS
9862 const char *cond_start = NULL;
9863 const char *cond_end = NULL;
cc59ec59 9864
bbc13ae3 9865 tok = skip_spaces_const (tok);
e7e0cddf
SS
9866
9867 if ((*tok == '"' || *tok == ',') && rest)
9868 {
9869 *rest = savestring (tok, strlen (tok));
9870 return;
9871 }
9872
bbc13ae3 9873 end_tok = skip_to_space_const (tok);
d634f2de 9874
018d34a4 9875 toklen = end_tok - tok;
d634f2de 9876
018d34a4
VP
9877 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9878 {
f7545552
TT
9879 struct expression *expr;
9880
018d34a4 9881 tok = cond_start = end_tok + 1;
1bb9788d 9882 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9883 xfree (expr);
018d34a4 9884 cond_end = tok;
d634f2de 9885 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9886 }
9887 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9888 {
9889 char *tmptok;
d634f2de 9890
018d34a4 9891 tok = end_tok + 1;
bbc13ae3 9892 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9893 if (tok == tmptok)
9894 error (_("Junk after thread keyword."));
9895 if (!valid_thread_id (*thread))
af4908ba 9896 invalid_thread_id_error (*thread);
bbc13ae3 9897 tok = tmptok;
018d34a4 9898 }
4a306c9a
JB
9899 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9900 {
9901 char *tmptok;
9902
9903 tok = end_tok + 1;
bbc13ae3 9904 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9905 if (tok == tmptok)
9906 error (_("Junk after task keyword."));
9907 if (!valid_task_id (*task))
b6199126 9908 error (_("Unknown task %d."), *task);
bbc13ae3 9909 tok = tmptok;
4a306c9a 9910 }
e7e0cddf
SS
9911 else if (rest)
9912 {
9913 *rest = savestring (tok, strlen (tok));
ccab2054 9914 return;
e7e0cddf 9915 }
018d34a4
VP
9916 else
9917 error (_("Junk at end of arguments."));
9918 }
9919}
9920
0fb4aa4b
PA
9921/* Decode a static tracepoint marker spec. */
9922
9923static struct symtabs_and_lines
9924decode_static_tracepoint_spec (char **arg_p)
9925{
9926 VEC(static_tracepoint_marker_p) *markers = NULL;
9927 struct symtabs_and_lines sals;
0fb4aa4b
PA
9928 struct cleanup *old_chain;
9929 char *p = &(*arg_p)[3];
9930 char *endp;
9931 char *marker_str;
9932 int i;
9933
e9cafbcc 9934 p = skip_spaces (p);
0fb4aa4b 9935
e9cafbcc 9936 endp = skip_to_space (p);
0fb4aa4b
PA
9937
9938 marker_str = savestring (p, endp - p);
9939 old_chain = make_cleanup (xfree, marker_str);
9940
9941 markers = target_static_tracepoint_markers_by_strid (marker_str);
9942 if (VEC_empty(static_tracepoint_marker_p, markers))
9943 error (_("No known static tracepoint marker named %s"), marker_str);
9944
9945 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9946 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9947
9948 for (i = 0; i < sals.nelts; i++)
9949 {
9950 struct static_tracepoint_marker *marker;
9951
9952 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9953
9954 init_sal (&sals.sals[i]);
9955
9956 sals.sals[i] = find_pc_line (marker->address, 0);
9957 sals.sals[i].pc = marker->address;
9958
9959 release_static_tracepoint_marker (marker);
9960 }
9961
9962 do_cleanups (old_chain);
9963
9964 *arg_p = endp;
9965 return sals;
9966}
9967
fd9b8c24
PA
9968/* Set a breakpoint. This function is shared between CLI and MI
9969 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9970 modes of operations, selected by the PARSE_ARG parameter. If
9971 non-zero, the function will parse ARG, extracting location,
9972 condition, thread and extra string. Otherwise, ARG is just the
9973 breakpoint's location, with condition, thread, and extra string
9974 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9975 If INTERNAL is non-zero, the breakpoint number will be allocated
9976 from the internal breakpoint count. Returns true if any breakpoint
9977 was created; false otherwise. */
0101ce28 9978
8cdf0e15
VP
9979int
9980create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9981 char *arg, char *cond_string,
9982 int thread, char *extra_string,
f6de8ec2 9983 int parse_arg,
0fb4aa4b 9984 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9985 int ignore_count,
9986 enum auto_boolean pending_break_support,
c0a91b2b 9987 const struct breakpoint_ops *ops,
44f238bb
PA
9988 int from_tty, int enabled, int internal,
9989 unsigned flags)
c3f6f71d 9990{
b78a6381 9991 volatile struct gdb_exception e;
f8eba3c6 9992 char *copy_arg = NULL;
c3f6f71d 9993 char *addr_start = arg;
7efd8fc2 9994 struct linespec_result canonical;
c3f6f71d 9995 struct cleanup *old_chain;
80c99de1 9996 struct cleanup *bkpt_chain = NULL;
0101ce28 9997 int pending = 0;
4a306c9a 9998 int task = 0;
86b17b60 9999 int prev_bkpt_count = breakpoint_count;
c3f6f71d 10000
348d480f
PA
10001 gdb_assert (ops != NULL);
10002
7efd8fc2 10003 init_linespec_result (&canonical);
c3f6f71d 10004
b78a6381
TT
10005 TRY_CATCH (e, RETURN_MASK_ALL)
10006 {
983af33b
SDJ
10007 ops->create_sals_from_address (&arg, &canonical, type_wanted,
10008 addr_start, &copy_arg);
b78a6381 10009 }
0101ce28
JJ
10010
10011 /* If caller is interested in rc value from parse, set value. */
05ff989b 10012 switch (e.reason)
0101ce28 10013 {
983af33b
SDJ
10014 case GDB_NO_ERROR:
10015 if (VEC_empty (linespec_sals, canonical.sals))
10016 return 0;
10017 break;
05ff989b
AC
10018 case RETURN_ERROR:
10019 switch (e.error)
0101ce28 10020 {
05ff989b 10021 case NOT_FOUND_ERROR:
0101ce28 10022
05ff989b
AC
10023 /* If pending breakpoint support is turned off, throw
10024 error. */
fa8d40ab
JJ
10025
10026 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
10027 throw_exception (e);
10028
10029 exception_print (gdb_stderr, e);
fa8d40ab 10030
05ff989b
AC
10031 /* If pending breakpoint support is auto query and the user
10032 selects no, then simply return the error code. */
059fb39f 10033 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
10034 && !nquery (_("Make %s pending on future shared library load? "),
10035 bptype_string (type_wanted)))
fd9b8c24 10036 return 0;
fa8d40ab 10037
05ff989b
AC
10038 /* At this point, either the user was queried about setting
10039 a pending breakpoint and selected yes, or pending
10040 breakpoint behavior is on and thus a pending breakpoint
10041 is defaulted on behalf of the user. */
f8eba3c6
TT
10042 {
10043 struct linespec_sals lsal;
10044
10045 copy_arg = xstrdup (addr_start);
10046 lsal.canonical = xstrdup (copy_arg);
10047 lsal.sals.nelts = 1;
10048 lsal.sals.sals = XNEW (struct symtab_and_line);
10049 init_sal (&lsal.sals.sals[0]);
10050 pending = 1;
10051 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
10052 }
05ff989b
AC
10053 break;
10054 default:
98deb0da 10055 throw_exception (e);
0101ce28 10056 }
2abae994 10057 break;
05ff989b 10058 default:
983af33b 10059 throw_exception (e);
0101ce28 10060 }
c3f6f71d 10061
4a64f543 10062 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 10063 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 10064
c3f6f71d
JM
10065 /* ----------------------------- SNIP -----------------------------
10066 Anything added to the cleanup chain beyond this point is assumed
10067 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
10068 then the memory is not reclaimed. */
10069 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 10070
c3f6f71d
JM
10071 /* Resolve all line numbers to PC's and verify that the addresses
10072 are ok for the target. */
0101ce28 10073 if (!pending)
f8eba3c6
TT
10074 {
10075 int ix;
10076 struct linespec_sals *iter;
10077
10078 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10079 breakpoint_sals_to_pc (&iter->sals);
10080 }
c3f6f71d 10081
7a697b8d 10082 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 10083 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
10084 {
10085 int ix;
10086 struct linespec_sals *iter;
10087
10088 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10089 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10090 }
7a697b8d 10091
c3f6f71d
JM
10092 /* Verify that condition can be parsed, before setting any
10093 breakpoints. Allocate a separate condition expression for each
4a64f543 10094 breakpoint. */
0101ce28 10095 if (!pending)
c3f6f71d 10096 {
f6de8ec2 10097 if (parse_arg)
72b2ff0e 10098 {
0878d0fa 10099 char *rest;
52d361e1
YQ
10100 struct linespec_sals *lsal;
10101
10102 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10103
0878d0fa
YQ
10104 /* Here we only parse 'arg' to separate condition
10105 from thread number, so parsing in context of first
10106 sal is OK. When setting the breakpoint we'll
10107 re-parse it in context of each sal. */
10108
10109 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10110 &thread, &task, &rest);
10111 if (cond_string)
10112 make_cleanup (xfree, cond_string);
10113 if (rest)
10114 make_cleanup (xfree, rest);
10115 if (rest)
10116 extra_string = rest;
72b2ff0e 10117 }
2f069f6f 10118 else
72b2ff0e 10119 {
0878d0fa
YQ
10120 if (*arg != '\0')
10121 error (_("Garbage '%s' at end of location"), arg);
10122
10123 /* Create a private copy of condition string. */
10124 if (cond_string)
10125 {
10126 cond_string = xstrdup (cond_string);
10127 make_cleanup (xfree, cond_string);
10128 }
10129 /* Create a private copy of any extra string. */
10130 if (extra_string)
10131 {
10132 extra_string = xstrdup (extra_string);
10133 make_cleanup (xfree, extra_string);
10134 }
72b2ff0e 10135 }
0fb4aa4b 10136
52d361e1 10137 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 10138 cond_string, extra_string, type_wanted,
d9b3f62e
PA
10139 tempflag ? disp_del : disp_donttouch,
10140 thread, task, ignore_count, ops,
44f238bb 10141 from_tty, enabled, internal, flags);
c906108c 10142 }
0101ce28
JJ
10143 else
10144 {
0101ce28
JJ
10145 struct breakpoint *b;
10146
0101ce28
JJ
10147 make_cleanup (xfree, copy_arg);
10148
bfccc43c
YQ
10149 if (is_tracepoint_type (type_wanted))
10150 {
10151 struct tracepoint *t;
10152
10153 t = XCNEW (struct tracepoint);
10154 b = &t->base;
10155 }
10156 else
10157 b = XNEW (struct breakpoint);
10158
10159 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10160
f8eba3c6 10161 b->addr_string = copy_arg;
f6de8ec2 10162 if (parse_arg)
e12c7713
MK
10163 b->cond_string = NULL;
10164 else
10165 {
10166 /* Create a private copy of condition string. */
10167 if (cond_string)
10168 {
10169 cond_string = xstrdup (cond_string);
10170 make_cleanup (xfree, cond_string);
10171 }
10172 b->cond_string = cond_string;
10173 }
e7e0cddf 10174 b->extra_string = NULL;
0101ce28 10175 b->ignore_count = ignore_count;
0101ce28 10176 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 10177 b->condition_not_parsed = 1;
41447f92 10178 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
10179 if ((type_wanted != bp_breakpoint
10180 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 10181 b->pspace = current_program_space;
8bea4e01 10182
bfccc43c 10183 install_breakpoint (internal, b, 0);
0101ce28
JJ
10184 }
10185
f8eba3c6 10186 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 10187 {
3e43a32a
MS
10188 warning (_("Multiple breakpoints were set.\nUse the "
10189 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10190 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10191 }
10192
80c99de1
PA
10193 /* That's it. Discard the cleanups for data inserted into the
10194 breakpoint. */
10195 discard_cleanups (bkpt_chain);
10196 /* But cleanup everything else. */
c3f6f71d 10197 do_cleanups (old_chain);
217dc9e2 10198
80c99de1 10199 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 10200 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
10201
10202 return 1;
c3f6f71d 10203}
c906108c 10204
348d480f 10205/* Set a breakpoint.
72b2ff0e
VP
10206 ARG is a string describing breakpoint address,
10207 condition, and thread.
10208 FLAG specifies if a breakpoint is hardware on,
10209 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10210 and BP_TEMPFLAG. */
348d480f 10211
98deb0da 10212static void
72b2ff0e 10213break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10214{
72b2ff0e 10215 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10216 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10217 ? bp_hardware_breakpoint
10218 : bp_breakpoint);
55aa24fb
SDJ
10219 struct breakpoint_ops *ops;
10220 const char *arg_cp = arg;
10221
10222 /* Matching breakpoints on probes. */
10223 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10224 ops = &bkpt_probe_breakpoint_ops;
10225 else
10226 ops = &bkpt_breakpoint_ops;
c3f6f71d 10227
8cdf0e15
VP
10228 create_breakpoint (get_current_arch (),
10229 arg,
e7e0cddf 10230 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10231 tempflag, type_wanted,
8cdf0e15
VP
10232 0 /* Ignore count */,
10233 pending_break_support,
55aa24fb 10234 ops,
8cdf0e15 10235 from_tty,
84f4c1fe 10236 1 /* enabled */,
44f238bb
PA
10237 0 /* internal */,
10238 0);
c906108c
SS
10239}
10240
c906108c
SS
10241/* Helper function for break_command_1 and disassemble_command. */
10242
10243void
fba45db2 10244resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10245{
10246 CORE_ADDR pc;
10247
10248 if (sal->pc == 0 && sal->symtab != NULL)
10249 {
10250 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10251 error (_("No line %d in file \"%s\"."),
05cba821 10252 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10253 sal->pc = pc;
6a048695 10254
4a64f543
MS
10255 /* If this SAL corresponds to a breakpoint inserted using a line
10256 number, then skip the function prologue if necessary. */
6a048695 10257 if (sal->explicit_line)
059acae7 10258 skip_prologue_sal (sal);
c906108c
SS
10259 }
10260
10261 if (sal->section == 0 && sal->symtab != NULL)
10262 {
346d1dfe 10263 const struct blockvector *bv;
3977b71f 10264 const struct block *b;
c5aa993b 10265 struct symbol *sym;
c906108c 10266
801e3a5b 10267 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
10268 if (bv != NULL)
10269 {
7f0df278 10270 sym = block_linkage_function (b);
c906108c
SS
10271 if (sym != NULL)
10272 {
10273 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 10274 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
10275 }
10276 else
10277 {
4a64f543
MS
10278 /* It really is worthwhile to have the section, so we'll
10279 just have to look harder. This case can be executed
10280 if we have line numbers but no functions (as can
10281 happen in assembly source). */
c906108c 10282
7cbd4a93 10283 struct bound_minimal_symbol msym;
6c95b8df
PA
10284 struct cleanup *old_chain = save_current_space_and_thread ();
10285
10286 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10287
10288 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10289 if (msym.minsym)
efd66ac6 10290 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10291
10292 do_cleanups (old_chain);
c906108c
SS
10293 }
10294 }
10295 }
10296}
10297
10298void
fba45db2 10299break_command (char *arg, int from_tty)
c906108c 10300{
db107f19 10301 break_command_1 (arg, 0, from_tty);
c906108c
SS
10302}
10303
c906108c 10304void
fba45db2 10305tbreak_command (char *arg, int from_tty)
c906108c 10306{
db107f19 10307 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10308}
10309
c906108c 10310static void
fba45db2 10311hbreak_command (char *arg, int from_tty)
c906108c 10312{
db107f19 10313 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10314}
10315
10316static void
fba45db2 10317thbreak_command (char *arg, int from_tty)
c906108c 10318{
db107f19 10319 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10320}
10321
10322static void
fba45db2 10323stop_command (char *arg, int from_tty)
c906108c 10324{
a3f17187 10325 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10326Usage: stop in <function | address>\n\
a3f17187 10327 stop at <line>\n"));
c906108c
SS
10328}
10329
10330static void
fba45db2 10331stopin_command (char *arg, int from_tty)
c906108c
SS
10332{
10333 int badInput = 0;
10334
c5aa993b 10335 if (arg == (char *) NULL)
c906108c
SS
10336 badInput = 1;
10337 else if (*arg != '*')
10338 {
10339 char *argptr = arg;
10340 int hasColon = 0;
10341
4a64f543 10342 /* Look for a ':'. If this is a line number specification, then
53a5351d 10343 say it is bad, otherwise, it should be an address or
4a64f543 10344 function/method name. */
c906108c 10345 while (*argptr && !hasColon)
c5aa993b
JM
10346 {
10347 hasColon = (*argptr == ':');
10348 argptr++;
10349 }
c906108c
SS
10350
10351 if (hasColon)
c5aa993b 10352 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10353 else
c5aa993b 10354 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10355 }
10356
10357 if (badInput)
a3f17187 10358 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10359 else
db107f19 10360 break_command_1 (arg, 0, from_tty);
c906108c
SS
10361}
10362
10363static void
fba45db2 10364stopat_command (char *arg, int from_tty)
c906108c
SS
10365{
10366 int badInput = 0;
10367
c5aa993b 10368 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10369 badInput = 1;
10370 else
10371 {
10372 char *argptr = arg;
10373 int hasColon = 0;
10374
4a64f543
MS
10375 /* Look for a ':'. If there is a '::' then get out, otherwise
10376 it is probably a line number. */
c906108c 10377 while (*argptr && !hasColon)
c5aa993b
JM
10378 {
10379 hasColon = (*argptr == ':');
10380 argptr++;
10381 }
c906108c
SS
10382
10383 if (hasColon)
c5aa993b 10384 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10385 else
c5aa993b 10386 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10387 }
10388
10389 if (badInput)
a3f17187 10390 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10391 else
db107f19 10392 break_command_1 (arg, 0, from_tty);
c906108c
SS
10393}
10394
e7e0cddf
SS
10395/* The dynamic printf command is mostly like a regular breakpoint, but
10396 with a prewired command list consisting of a single output command,
10397 built from extra arguments supplied on the dprintf command
10398 line. */
10399
da821c7b 10400static void
e7e0cddf
SS
10401dprintf_command (char *arg, int from_tty)
10402{
10403 create_breakpoint (get_current_arch (),
10404 arg,
10405 NULL, 0, NULL, 1 /* parse arg */,
10406 0, bp_dprintf,
10407 0 /* Ignore count */,
10408 pending_break_support,
10409 &dprintf_breakpoint_ops,
10410 from_tty,
10411 1 /* enabled */,
10412 0 /* internal */,
10413 0);
10414}
10415
d3ce09f5
SS
10416static void
10417agent_printf_command (char *arg, int from_tty)
10418{
10419 error (_("May only run agent-printf on the target"));
10420}
10421
f1310107
TJB
10422/* Implement the "breakpoint_hit" breakpoint_ops method for
10423 ranged breakpoints. */
10424
10425static int
10426breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10427 struct address_space *aspace,
09ac7c10
TT
10428 CORE_ADDR bp_addr,
10429 const struct target_waitstatus *ws)
f1310107 10430{
09ac7c10 10431 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10432 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10433 return 0;
10434
f1310107
TJB
10435 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10436 bl->length, aspace, bp_addr);
10437}
10438
10439/* Implement the "resources_needed" breakpoint_ops method for
10440 ranged breakpoints. */
10441
10442static int
10443resources_needed_ranged_breakpoint (const struct bp_location *bl)
10444{
10445 return target_ranged_break_num_registers ();
10446}
10447
10448/* Implement the "print_it" breakpoint_ops method for
10449 ranged breakpoints. */
10450
10451static enum print_stop_action
348d480f 10452print_it_ranged_breakpoint (bpstat bs)
f1310107 10453{
348d480f 10454 struct breakpoint *b = bs->breakpoint_at;
f1310107 10455 struct bp_location *bl = b->loc;
79a45e25 10456 struct ui_out *uiout = current_uiout;
f1310107
TJB
10457
10458 gdb_assert (b->type == bp_hardware_breakpoint);
10459
10460 /* Ranged breakpoints have only one location. */
10461 gdb_assert (bl && bl->next == NULL);
10462
10463 annotate_breakpoint (b->number);
10464 if (b->disposition == disp_del)
10465 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10466 else
10467 ui_out_text (uiout, "\nRanged breakpoint ");
10468 if (ui_out_is_mi_like_p (uiout))
10469 {
10470 ui_out_field_string (uiout, "reason",
10471 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10472 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10473 }
10474 ui_out_field_int (uiout, "bkptno", b->number);
10475 ui_out_text (uiout, ", ");
10476
10477 return PRINT_SRC_AND_LOC;
10478}
10479
10480/* Implement the "print_one" breakpoint_ops method for
10481 ranged breakpoints. */
10482
10483static void
10484print_one_ranged_breakpoint (struct breakpoint *b,
10485 struct bp_location **last_loc)
10486{
10487 struct bp_location *bl = b->loc;
10488 struct value_print_options opts;
79a45e25 10489 struct ui_out *uiout = current_uiout;
f1310107
TJB
10490
10491 /* Ranged breakpoints have only one location. */
10492 gdb_assert (bl && bl->next == NULL);
10493
10494 get_user_print_options (&opts);
10495
10496 if (opts.addressprint)
10497 /* We don't print the address range here, it will be printed later
10498 by print_one_detail_ranged_breakpoint. */
10499 ui_out_field_skip (uiout, "addr");
10500 annotate_field (5);
10501 print_breakpoint_location (b, bl);
10502 *last_loc = bl;
10503}
10504
10505/* Implement the "print_one_detail" breakpoint_ops method for
10506 ranged breakpoints. */
10507
10508static void
10509print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10510 struct ui_out *uiout)
10511{
10512 CORE_ADDR address_start, address_end;
10513 struct bp_location *bl = b->loc;
f99d8bf4
PA
10514 struct ui_file *stb = mem_fileopen ();
10515 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10516
10517 gdb_assert (bl);
10518
10519 address_start = bl->address;
10520 address_end = address_start + bl->length - 1;
10521
10522 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10523 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10524 print_core_address (bl->gdbarch, address_start),
10525 print_core_address (bl->gdbarch, address_end));
10526 ui_out_field_stream (uiout, "addr", stb);
10527 ui_out_text (uiout, "\n");
10528
10529 do_cleanups (cleanup);
10530}
10531
10532/* Implement the "print_mention" breakpoint_ops method for
10533 ranged breakpoints. */
10534
10535static void
10536print_mention_ranged_breakpoint (struct breakpoint *b)
10537{
10538 struct bp_location *bl = b->loc;
79a45e25 10539 struct ui_out *uiout = current_uiout;
f1310107
TJB
10540
10541 gdb_assert (bl);
10542 gdb_assert (b->type == bp_hardware_breakpoint);
10543
10544 if (ui_out_is_mi_like_p (uiout))
10545 return;
10546
10547 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10548 b->number, paddress (bl->gdbarch, bl->address),
10549 paddress (bl->gdbarch, bl->address + bl->length - 1));
10550}
10551
10552/* Implement the "print_recreate" breakpoint_ops method for
10553 ranged breakpoints. */
10554
10555static void
10556print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10557{
10558 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10559 b->addr_string_range_end);
d9b3f62e 10560 print_recreate_thread (b, fp);
f1310107
TJB
10561}
10562
10563/* The breakpoint_ops structure to be used in ranged breakpoints. */
10564
2060206e 10565static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10566
10567/* Find the address where the end of the breakpoint range should be
10568 placed, given the SAL of the end of the range. This is so that if
10569 the user provides a line number, the end of the range is set to the
10570 last instruction of the given line. */
10571
10572static CORE_ADDR
10573find_breakpoint_range_end (struct symtab_and_line sal)
10574{
10575 CORE_ADDR end;
10576
10577 /* If the user provided a PC value, use it. Otherwise,
10578 find the address of the end of the given location. */
10579 if (sal.explicit_pc)
10580 end = sal.pc;
10581 else
10582 {
10583 int ret;
10584 CORE_ADDR start;
10585
10586 ret = find_line_pc_range (sal, &start, &end);
10587 if (!ret)
10588 error (_("Could not find location of the end of the range."));
10589
10590 /* find_line_pc_range returns the start of the next line. */
10591 end--;
10592 }
10593
10594 return end;
10595}
10596
10597/* Implement the "break-range" CLI command. */
10598
10599static void
10600break_range_command (char *arg, int from_tty)
10601{
10602 char *arg_start, *addr_string_start, *addr_string_end;
10603 struct linespec_result canonical_start, canonical_end;
10604 int bp_count, can_use_bp, length;
10605 CORE_ADDR end;
10606 struct breakpoint *b;
10607 struct symtab_and_line sal_start, sal_end;
f1310107 10608 struct cleanup *cleanup_bkpt;
f8eba3c6 10609 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10610
10611 /* We don't support software ranged breakpoints. */
10612 if (target_ranged_break_num_registers () < 0)
10613 error (_("This target does not support hardware ranged breakpoints."));
10614
10615 bp_count = hw_breakpoint_used_count ();
10616 bp_count += target_ranged_break_num_registers ();
10617 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10618 bp_count, 0);
10619 if (can_use_bp < 0)
10620 error (_("Hardware breakpoints used exceeds limit."));
10621
f8eba3c6 10622 arg = skip_spaces (arg);
f1310107
TJB
10623 if (arg == NULL || arg[0] == '\0')
10624 error(_("No address range specified."));
10625
f1310107
TJB
10626 init_linespec_result (&canonical_start);
10627
f8eba3c6
TT
10628 arg_start = arg;
10629 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10630
f8eba3c6 10631 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10632
10633 if (arg[0] != ',')
10634 error (_("Too few arguments."));
f8eba3c6 10635 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10636 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10637
10638 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10639
10640 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10641 || lsal_start->sals.nelts != 1)
f1310107
TJB
10642 error (_("Cannot create a ranged breakpoint with multiple locations."));
10643
f8eba3c6
TT
10644 sal_start = lsal_start->sals.sals[0];
10645 addr_string_start = savestring (arg_start, arg - arg_start);
10646 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10647
10648 arg++; /* Skip the comma. */
f8eba3c6 10649 arg = skip_spaces (arg);
f1310107
TJB
10650
10651 /* Parse the end location. */
10652
f1310107
TJB
10653 init_linespec_result (&canonical_end);
10654 arg_start = arg;
10655
f8eba3c6 10656 /* We call decode_line_full directly here instead of using
f1310107
TJB
10657 parse_breakpoint_sals because we need to specify the start location's
10658 symtab and line as the default symtab and line for the end of the
10659 range. This makes it possible to have ranges like "foo.c:27, +14",
10660 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10661 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10662 sal_start.symtab, sal_start.line,
10663 &canonical_end, NULL, NULL);
10664
10665 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10666
f8eba3c6 10667 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10668 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10669
10670 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10671 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10672 || lsal_end->sals.nelts != 1)
f1310107
TJB
10673 error (_("Cannot create a ranged breakpoint with multiple locations."));
10674
f8eba3c6
TT
10675 sal_end = lsal_end->sals.sals[0];
10676 addr_string_end = savestring (arg_start, arg - arg_start);
10677 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10678
10679 end = find_breakpoint_range_end (sal_end);
10680 if (sal_start.pc > end)
177b42fe 10681 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10682
10683 length = end - sal_start.pc + 1;
10684 if (length < 0)
10685 /* Length overflowed. */
10686 error (_("Address range too large."));
10687 else if (length == 1)
10688 {
10689 /* This range is simple enough to be handled by
10690 the `hbreak' command. */
10691 hbreak_command (addr_string_start, 1);
10692
10693 do_cleanups (cleanup_bkpt);
10694
10695 return;
10696 }
10697
10698 /* Now set up the breakpoint. */
10699 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10700 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10701 set_breakpoint_count (breakpoint_count + 1);
10702 b->number = breakpoint_count;
10703 b->disposition = disp_donttouch;
f8eba3c6
TT
10704 b->addr_string = xstrdup (addr_string_start);
10705 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10706 b->loc->length = length;
10707
f8eba3c6 10708 do_cleanups (cleanup_bkpt);
f1310107
TJB
10709
10710 mention (b);
8d3788bd 10711 observer_notify_breakpoint_created (b);
44702360 10712 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10713}
10714
4a64f543
MS
10715/* Return non-zero if EXP is verified as constant. Returned zero
10716 means EXP is variable. Also the constant detection may fail for
10717 some constant expressions and in such case still falsely return
10718 zero. */
2e6e3d9c 10719
65d79d4b
SDJ
10720static int
10721watchpoint_exp_is_const (const struct expression *exp)
10722{
10723 int i = exp->nelts;
10724
10725 while (i > 0)
10726 {
10727 int oplenp, argsp;
10728
10729 /* We are only interested in the descriptor of each element. */
10730 operator_length (exp, i, &oplenp, &argsp);
10731 i -= oplenp;
10732
10733 switch (exp->elts[i].opcode)
10734 {
10735 case BINOP_ADD:
10736 case BINOP_SUB:
10737 case BINOP_MUL:
10738 case BINOP_DIV:
10739 case BINOP_REM:
10740 case BINOP_MOD:
10741 case BINOP_LSH:
10742 case BINOP_RSH:
10743 case BINOP_LOGICAL_AND:
10744 case BINOP_LOGICAL_OR:
10745 case BINOP_BITWISE_AND:
10746 case BINOP_BITWISE_IOR:
10747 case BINOP_BITWISE_XOR:
10748 case BINOP_EQUAL:
10749 case BINOP_NOTEQUAL:
10750 case BINOP_LESS:
10751 case BINOP_GTR:
10752 case BINOP_LEQ:
10753 case BINOP_GEQ:
10754 case BINOP_REPEAT:
10755 case BINOP_COMMA:
10756 case BINOP_EXP:
10757 case BINOP_MIN:
10758 case BINOP_MAX:
10759 case BINOP_INTDIV:
10760 case BINOP_CONCAT:
65d79d4b
SDJ
10761 case TERNOP_COND:
10762 case TERNOP_SLICE:
65d79d4b
SDJ
10763
10764 case OP_LONG:
10765 case OP_DOUBLE:
10766 case OP_DECFLOAT:
10767 case OP_LAST:
10768 case OP_COMPLEX:
10769 case OP_STRING:
65d79d4b
SDJ
10770 case OP_ARRAY:
10771 case OP_TYPE:
608b4967
TT
10772 case OP_TYPEOF:
10773 case OP_DECLTYPE:
6e72ca20 10774 case OP_TYPEID:
65d79d4b
SDJ
10775 case OP_NAME:
10776 case OP_OBJC_NSSTRING:
10777
10778 case UNOP_NEG:
10779 case UNOP_LOGICAL_NOT:
10780 case UNOP_COMPLEMENT:
10781 case UNOP_ADDR:
10782 case UNOP_HIGH:
aeaa2474 10783 case UNOP_CAST:
9eaf6705
TT
10784
10785 case UNOP_CAST_TYPE:
10786 case UNOP_REINTERPRET_CAST:
10787 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10788 /* Unary, binary and ternary operators: We have to check
10789 their operands. If they are constant, then so is the
10790 result of that operation. For instance, if A and B are
10791 determined to be constants, then so is "A + B".
10792
10793 UNOP_IND is one exception to the rule above, because the
10794 value of *ADDR is not necessarily a constant, even when
10795 ADDR is. */
65d79d4b
SDJ
10796 break;
10797
10798 case OP_VAR_VALUE:
10799 /* Check whether the associated symbol is a constant.
4a64f543 10800
65d79d4b 10801 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10802 possible that a buggy compiler could mark a variable as
10803 constant even when it is not, and TYPE_CONST would return
10804 true in this case, while SYMBOL_CLASS wouldn't.
10805
10806 We also have to check for function symbols because they
10807 are always constant. */
65d79d4b
SDJ
10808 {
10809 struct symbol *s = exp->elts[i + 2].symbol;
10810
10811 if (SYMBOL_CLASS (s) != LOC_BLOCK
10812 && SYMBOL_CLASS (s) != LOC_CONST
10813 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10814 return 0;
10815 break;
10816 }
10817
10818 /* The default action is to return 0 because we are using
10819 the optimistic approach here: If we don't know something,
10820 then it is not a constant. */
10821 default:
10822 return 0;
10823 }
10824 }
10825
10826 return 1;
10827}
10828
3a5c3e22
PA
10829/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10830
10831static void
10832dtor_watchpoint (struct breakpoint *self)
10833{
10834 struct watchpoint *w = (struct watchpoint *) self;
10835
10836 xfree (w->cond_exp);
10837 xfree (w->exp);
10838 xfree (w->exp_string);
10839 xfree (w->exp_string_reparse);
10840 value_free (w->val);
10841
10842 base_breakpoint_ops.dtor (self);
10843}
10844
348d480f
PA
10845/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10846
10847static void
10848re_set_watchpoint (struct breakpoint *b)
10849{
3a5c3e22
PA
10850 struct watchpoint *w = (struct watchpoint *) b;
10851
348d480f
PA
10852 /* Watchpoint can be either on expression using entirely global
10853 variables, or it can be on local variables.
10854
10855 Watchpoints of the first kind are never auto-deleted, and even
10856 persist across program restarts. Since they can use variables
10857 from shared libraries, we need to reparse expression as libraries
10858 are loaded and unloaded.
10859
10860 Watchpoints on local variables can also change meaning as result
10861 of solib event. For example, if a watchpoint uses both a local
10862 and a global variables in expression, it's a local watchpoint,
10863 but unloading of a shared library will make the expression
10864 invalid. This is not a very common use case, but we still
10865 re-evaluate expression, to avoid surprises to the user.
10866
10867 Note that for local watchpoints, we re-evaluate it only if
10868 watchpoints frame id is still valid. If it's not, it means the
10869 watchpoint is out of scope and will be deleted soon. In fact,
10870 I'm not sure we'll ever be called in this case.
10871
10872 If a local watchpoint's frame id is still valid, then
3a5c3e22 10873 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10874
3a5c3e22
PA
10875 Don't do anything about disabled watchpoints, since they will be
10876 reevaluated again when enabled. */
10877 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10878}
10879
77b06cd7
TJB
10880/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10881
10882static int
10883insert_watchpoint (struct bp_location *bl)
10884{
3a5c3e22
PA
10885 struct watchpoint *w = (struct watchpoint *) bl->owner;
10886 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10887
10888 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10889 w->cond_exp);
77b06cd7
TJB
10890}
10891
10892/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10893
10894static int
10895remove_watchpoint (struct bp_location *bl)
10896{
3a5c3e22
PA
10897 struct watchpoint *w = (struct watchpoint *) bl->owner;
10898 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10899
10900 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10901 w->cond_exp);
e09342b5
TJB
10902}
10903
e09342b5 10904static int
348d480f 10905breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10906 struct address_space *aspace, CORE_ADDR bp_addr,
10907 const struct target_waitstatus *ws)
e09342b5 10908{
348d480f 10909 struct breakpoint *b = bl->owner;
3a5c3e22 10910 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10911
348d480f
PA
10912 /* Continuable hardware watchpoints are treated as non-existent if the
10913 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10914 some data address). Otherwise gdb won't stop on a break instruction
10915 in the code (not from a breakpoint) when a hardware watchpoint has
10916 been defined. Also skip watchpoints which we know did not trigger
10917 (did not match the data address). */
10918 if (is_hardware_watchpoint (b)
3a5c3e22 10919 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10920 return 0;
9c06b0b4 10921
348d480f 10922 return 1;
9c06b0b4
TJB
10923}
10924
348d480f
PA
10925static void
10926check_status_watchpoint (bpstat bs)
9c06b0b4 10927{
348d480f 10928 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10929
348d480f 10930 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10931}
10932
10933/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10934 hardware watchpoints. */
9c06b0b4
TJB
10935
10936static int
348d480f 10937resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10938{
3a5c3e22
PA
10939 struct watchpoint *w = (struct watchpoint *) bl->owner;
10940 int length = w->exact? 1 : bl->length;
348d480f
PA
10941
10942 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10943}
10944
10945/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10946 hardware watchpoints. */
9c06b0b4
TJB
10947
10948static int
348d480f 10949works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10950{
efa80663
PA
10951 /* Read and access watchpoints only work with hardware support. */
10952 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10953}
10954
9c06b0b4 10955static enum print_stop_action
348d480f 10956print_it_watchpoint (bpstat bs)
9c06b0b4 10957{
348d480f
PA
10958 struct cleanup *old_chain;
10959 struct breakpoint *b;
f99d8bf4 10960 struct ui_file *stb;
348d480f 10961 enum print_stop_action result;
3a5c3e22 10962 struct watchpoint *w;
79a45e25 10963 struct ui_out *uiout = current_uiout;
348d480f
PA
10964
10965 gdb_assert (bs->bp_location_at != NULL);
10966
348d480f 10967 b = bs->breakpoint_at;
3a5c3e22 10968 w = (struct watchpoint *) b;
348d480f 10969
f99d8bf4
PA
10970 stb = mem_fileopen ();
10971 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10972
10973 switch (b->type)
10974 {
348d480f 10975 case bp_watchpoint:
9c06b0b4
TJB
10976 case bp_hardware_watchpoint:
10977 annotate_watchpoint (b->number);
10978 if (ui_out_is_mi_like_p (uiout))
10979 ui_out_field_string
10980 (uiout, "reason",
10981 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10982 mention (b);
10983 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10984 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10985 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10986 ui_out_field_stream (uiout, "old", stb);
10987 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10988 watchpoint_value_print (w->val, stb);
348d480f
PA
10989 ui_out_field_stream (uiout, "new", stb);
10990 ui_out_text (uiout, "\n");
10991 /* More than one watchpoint may have been triggered. */
10992 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10993 break;
10994
10995 case bp_read_watchpoint:
10996 if (ui_out_is_mi_like_p (uiout))
10997 ui_out_field_string
10998 (uiout, "reason",
10999 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
11000 mention (b);
11001 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11002 ui_out_text (uiout, "\nValue = ");
f99d8bf4 11003 watchpoint_value_print (w->val, stb);
348d480f
PA
11004 ui_out_field_stream (uiout, "value", stb);
11005 ui_out_text (uiout, "\n");
11006 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11007 break;
11008
11009 case bp_access_watchpoint:
348d480f
PA
11010 if (bs->old_val != NULL)
11011 {
11012 annotate_watchpoint (b->number);
11013 if (ui_out_is_mi_like_p (uiout))
11014 ui_out_field_string
11015 (uiout, "reason",
11016 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11017 mention (b);
11018 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11019 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 11020 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
11021 ui_out_field_stream (uiout, "old", stb);
11022 ui_out_text (uiout, "\nNew value = ");
11023 }
11024 else
11025 {
11026 mention (b);
11027 if (ui_out_is_mi_like_p (uiout))
11028 ui_out_field_string
11029 (uiout, "reason",
11030 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11031 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11032 ui_out_text (uiout, "\nValue = ");
11033 }
f99d8bf4 11034 watchpoint_value_print (w->val, stb);
348d480f
PA
11035 ui_out_field_stream (uiout, "new", stb);
11036 ui_out_text (uiout, "\n");
11037 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11038 break;
11039 default:
348d480f 11040 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11041 }
11042
348d480f
PA
11043 do_cleanups (old_chain);
11044 return result;
11045}
11046
11047/* Implement the "print_mention" breakpoint_ops method for hardware
11048 watchpoints. */
11049
11050static void
11051print_mention_watchpoint (struct breakpoint *b)
11052{
11053 struct cleanup *ui_out_chain;
3a5c3e22 11054 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11055 struct ui_out *uiout = current_uiout;
348d480f
PA
11056
11057 switch (b->type)
11058 {
11059 case bp_watchpoint:
11060 ui_out_text (uiout, "Watchpoint ");
11061 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11062 break;
11063 case bp_hardware_watchpoint:
11064 ui_out_text (uiout, "Hardware watchpoint ");
11065 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11066 break;
11067 case bp_read_watchpoint:
11068 ui_out_text (uiout, "Hardware read watchpoint ");
11069 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11070 break;
11071 case bp_access_watchpoint:
11072 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11073 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11074 break;
11075 default:
11076 internal_error (__FILE__, __LINE__,
11077 _("Invalid hardware watchpoint type."));
11078 }
11079
11080 ui_out_field_int (uiout, "number", b->number);
11081 ui_out_text (uiout, ": ");
3a5c3e22 11082 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
11083 do_cleanups (ui_out_chain);
11084}
11085
11086/* Implement the "print_recreate" breakpoint_ops method for
11087 watchpoints. */
11088
11089static void
11090print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11091{
3a5c3e22
PA
11092 struct watchpoint *w = (struct watchpoint *) b;
11093
348d480f
PA
11094 switch (b->type)
11095 {
11096 case bp_watchpoint:
11097 case bp_hardware_watchpoint:
11098 fprintf_unfiltered (fp, "watch");
11099 break;
11100 case bp_read_watchpoint:
11101 fprintf_unfiltered (fp, "rwatch");
11102 break;
11103 case bp_access_watchpoint:
11104 fprintf_unfiltered (fp, "awatch");
11105 break;
11106 default:
11107 internal_error (__FILE__, __LINE__,
11108 _("Invalid watchpoint type."));
11109 }
11110
3a5c3e22 11111 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 11112 print_recreate_thread (b, fp);
348d480f
PA
11113}
11114
427cd150
TT
11115/* Implement the "explains_signal" breakpoint_ops method for
11116 watchpoints. */
11117
47591c29 11118static int
427cd150
TT
11119explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11120{
11121 /* A software watchpoint cannot cause a signal other than
11122 GDB_SIGNAL_TRAP. */
11123 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 11124 return 0;
427cd150 11125
47591c29 11126 return 1;
427cd150
TT
11127}
11128
348d480f
PA
11129/* The breakpoint_ops structure to be used in hardware watchpoints. */
11130
2060206e 11131static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
11132
11133/* Implement the "insert" breakpoint_ops method for
11134 masked hardware watchpoints. */
11135
11136static int
11137insert_masked_watchpoint (struct bp_location *bl)
11138{
3a5c3e22
PA
11139 struct watchpoint *w = (struct watchpoint *) bl->owner;
11140
11141 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11142 bl->watchpoint_type);
11143}
11144
11145/* Implement the "remove" breakpoint_ops method for
11146 masked hardware watchpoints. */
11147
11148static int
11149remove_masked_watchpoint (struct bp_location *bl)
11150{
3a5c3e22
PA
11151 struct watchpoint *w = (struct watchpoint *) bl->owner;
11152
11153 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11154 bl->watchpoint_type);
11155}
11156
11157/* Implement the "resources_needed" breakpoint_ops method for
11158 masked hardware watchpoints. */
11159
11160static int
11161resources_needed_masked_watchpoint (const struct bp_location *bl)
11162{
3a5c3e22
PA
11163 struct watchpoint *w = (struct watchpoint *) bl->owner;
11164
11165 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
11166}
11167
11168/* Implement the "works_in_software_mode" breakpoint_ops method for
11169 masked hardware watchpoints. */
11170
11171static int
11172works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11173{
11174 return 0;
11175}
11176
11177/* Implement the "print_it" breakpoint_ops method for
11178 masked hardware watchpoints. */
11179
11180static enum print_stop_action
11181print_it_masked_watchpoint (bpstat bs)
11182{
11183 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11184 struct ui_out *uiout = current_uiout;
348d480f
PA
11185
11186 /* Masked watchpoints have only one location. */
11187 gdb_assert (b->loc && b->loc->next == NULL);
11188
11189 switch (b->type)
11190 {
11191 case bp_hardware_watchpoint:
11192 annotate_watchpoint (b->number);
11193 if (ui_out_is_mi_like_p (uiout))
11194 ui_out_field_string
11195 (uiout, "reason",
11196 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11197 break;
11198
11199 case bp_read_watchpoint:
11200 if (ui_out_is_mi_like_p (uiout))
11201 ui_out_field_string
11202 (uiout, "reason",
11203 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11204 break;
11205
11206 case bp_access_watchpoint:
11207 if (ui_out_is_mi_like_p (uiout))
11208 ui_out_field_string
11209 (uiout, "reason",
11210 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11211 break;
11212 default:
11213 internal_error (__FILE__, __LINE__,
11214 _("Invalid hardware watchpoint type."));
11215 }
11216
11217 mention (b);
9c06b0b4
TJB
11218 ui_out_text (uiout, _("\n\
11219Check the underlying instruction at PC for the memory\n\
11220address and value which triggered this watchpoint.\n"));
11221 ui_out_text (uiout, "\n");
11222
11223 /* More than one watchpoint may have been triggered. */
11224 return PRINT_UNKNOWN;
11225}
11226
11227/* Implement the "print_one_detail" breakpoint_ops method for
11228 masked hardware watchpoints. */
11229
11230static void
11231print_one_detail_masked_watchpoint (const struct breakpoint *b,
11232 struct ui_out *uiout)
11233{
3a5c3e22
PA
11234 struct watchpoint *w = (struct watchpoint *) b;
11235
9c06b0b4
TJB
11236 /* Masked watchpoints have only one location. */
11237 gdb_assert (b->loc && b->loc->next == NULL);
11238
11239 ui_out_text (uiout, "\tmask ");
3a5c3e22 11240 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11241 ui_out_text (uiout, "\n");
11242}
11243
11244/* Implement the "print_mention" breakpoint_ops method for
11245 masked hardware watchpoints. */
11246
11247static void
11248print_mention_masked_watchpoint (struct breakpoint *b)
11249{
3a5c3e22 11250 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11251 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11252 struct cleanup *ui_out_chain;
11253
11254 switch (b->type)
11255 {
11256 case bp_hardware_watchpoint:
11257 ui_out_text (uiout, "Masked hardware watchpoint ");
11258 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11259 break;
11260 case bp_read_watchpoint:
11261 ui_out_text (uiout, "Masked hardware read watchpoint ");
11262 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11263 break;
11264 case bp_access_watchpoint:
11265 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11266 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11267 break;
11268 default:
11269 internal_error (__FILE__, __LINE__,
11270 _("Invalid hardware watchpoint type."));
11271 }
11272
11273 ui_out_field_int (uiout, "number", b->number);
11274 ui_out_text (uiout, ": ");
3a5c3e22 11275 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11276 do_cleanups (ui_out_chain);
11277}
11278
11279/* Implement the "print_recreate" breakpoint_ops method for
11280 masked hardware watchpoints. */
11281
11282static void
11283print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11284{
3a5c3e22 11285 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11286 char tmp[40];
11287
11288 switch (b->type)
11289 {
11290 case bp_hardware_watchpoint:
11291 fprintf_unfiltered (fp, "watch");
11292 break;
11293 case bp_read_watchpoint:
11294 fprintf_unfiltered (fp, "rwatch");
11295 break;
11296 case bp_access_watchpoint:
11297 fprintf_unfiltered (fp, "awatch");
11298 break;
11299 default:
11300 internal_error (__FILE__, __LINE__,
11301 _("Invalid hardware watchpoint type."));
11302 }
11303
3a5c3e22
PA
11304 sprintf_vma (tmp, w->hw_wp_mask);
11305 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11306 print_recreate_thread (b, fp);
9c06b0b4
TJB
11307}
11308
11309/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11310
2060206e 11311static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11312
11313/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11314
11315static int
11316is_masked_watchpoint (const struct breakpoint *b)
11317{
11318 return b->ops == &masked_watchpoint_breakpoint_ops;
11319}
11320
53a5351d
JM
11321/* accessflag: hw_write: watch write,
11322 hw_read: watch read,
11323 hw_access: watch access (read or write) */
c906108c 11324static void
bbc13ae3 11325watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11326 int just_location, int internal)
c906108c 11327{
a9634178 11328 volatile struct gdb_exception e;
d983da9c 11329 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11330 struct expression *exp;
270140bd 11331 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11332 struct value *val, *mark, *result;
bb9d5f81 11333 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11334 struct frame_info *frame;
bbc13ae3
KS
11335 const char *exp_start = NULL;
11336 const char *exp_end = NULL;
11337 const char *tok, *end_tok;
9c06b0b4 11338 int toklen = -1;
bbc13ae3
KS
11339 const char *cond_start = NULL;
11340 const char *cond_end = NULL;
c906108c 11341 enum bptype bp_type;
37e4754d 11342 int thread = -1;
0cf6dd15 11343 int pc = 0;
9c06b0b4
TJB
11344 /* Flag to indicate whether we are going to use masks for
11345 the hardware watchpoint. */
11346 int use_mask = 0;
11347 CORE_ADDR mask = 0;
3a5c3e22 11348 struct watchpoint *w;
bbc13ae3
KS
11349 char *expression;
11350 struct cleanup *back_to;
c906108c 11351
37e4754d
LM
11352 /* Make sure that we actually have parameters to parse. */
11353 if (arg != NULL && arg[0] != '\0')
11354 {
bbc13ae3
KS
11355 const char *value_start;
11356
11357 exp_end = arg + strlen (arg);
37e4754d 11358
9c06b0b4
TJB
11359 /* Look for "parameter value" pairs at the end
11360 of the arguments string. */
bbc13ae3 11361 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11362 {
11363 /* Skip whitespace at the end of the argument list. */
11364 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11365 tok--;
11366
11367 /* Find the beginning of the last token.
11368 This is the value of the parameter. */
11369 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11370 tok--;
11371 value_start = tok + 1;
11372
11373 /* Skip whitespace. */
11374 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11375 tok--;
11376
11377 end_tok = tok;
11378
11379 /* Find the beginning of the second to last token.
11380 This is the parameter itself. */
11381 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11382 tok--;
11383 tok++;
11384 toklen = end_tok - tok + 1;
11385
11386 if (toklen == 6 && !strncmp (tok, "thread", 6))
11387 {
11388 /* At this point we've found a "thread" token, which means
11389 the user is trying to set a watchpoint that triggers
11390 only in a specific thread. */
11391 char *endp;
37e4754d 11392
9c06b0b4
TJB
11393 if (thread != -1)
11394 error(_("You can specify only one thread."));
37e4754d 11395
9c06b0b4
TJB
11396 /* Extract the thread ID from the next token. */
11397 thread = strtol (value_start, &endp, 0);
37e4754d 11398
9c06b0b4
TJB
11399 /* Check if the user provided a valid numeric value for the
11400 thread ID. */
11401 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11402 error (_("Invalid thread ID specification %s."), value_start);
11403
11404 /* Check if the thread actually exists. */
11405 if (!valid_thread_id (thread))
af4908ba 11406 invalid_thread_id_error (thread);
9c06b0b4
TJB
11407 }
11408 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11409 {
11410 /* We've found a "mask" token, which means the user wants to
11411 create a hardware watchpoint that is going to have the mask
11412 facility. */
11413 struct value *mask_value, *mark;
37e4754d 11414
9c06b0b4
TJB
11415 if (use_mask)
11416 error(_("You can specify only one mask."));
37e4754d 11417
9c06b0b4 11418 use_mask = just_location = 1;
37e4754d 11419
9c06b0b4
TJB
11420 mark = value_mark ();
11421 mask_value = parse_to_comma_and_eval (&value_start);
11422 mask = value_as_address (mask_value);
11423 value_free_to_mark (mark);
11424 }
11425 else
11426 /* We didn't recognize what we found. We should stop here. */
11427 break;
37e4754d 11428
9c06b0b4
TJB
11429 /* Truncate the string and get rid of the "parameter value" pair before
11430 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11431 exp_end = tok;
9c06b0b4 11432 }
37e4754d 11433 }
bbc13ae3
KS
11434 else
11435 exp_end = arg;
37e4754d 11436
bbc13ae3
KS
11437 /* Parse the rest of the arguments. From here on out, everything
11438 is in terms of a newly allocated string instead of the original
11439 ARG. */
c906108c 11440 innermost_block = NULL;
bbc13ae3
KS
11441 expression = savestring (arg, exp_end - arg);
11442 back_to = make_cleanup (xfree, expression);
11443 exp_start = arg = expression;
1bb9788d 11444 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11445 exp_end = arg;
fa8a61dc
TT
11446 /* Remove trailing whitespace from the expression before saving it.
11447 This makes the eventual display of the expression string a bit
11448 prettier. */
11449 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11450 --exp_end;
11451
65d79d4b
SDJ
11452 /* Checking if the expression is not constant. */
11453 if (watchpoint_exp_is_const (exp))
11454 {
11455 int len;
11456
11457 len = exp_end - exp_start;
11458 while (len > 0 && isspace (exp_start[len - 1]))
11459 len--;
11460 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11461 }
11462
c906108c
SS
11463 exp_valid_block = innermost_block;
11464 mark = value_mark ();
3a1115a0 11465 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11466
bb9d5f81
PP
11467 if (val != NULL && just_location)
11468 {
11469 saved_bitpos = value_bitpos (val);
11470 saved_bitsize = value_bitsize (val);
11471 }
11472
06a64a0b
TT
11473 if (just_location)
11474 {
9c06b0b4
TJB
11475 int ret;
11476
06a64a0b 11477 exp_valid_block = NULL;
a1442452 11478 val = value_addr (result);
06a64a0b
TT
11479 release_value (val);
11480 value_free_to_mark (mark);
9c06b0b4
TJB
11481
11482 if (use_mask)
11483 {
11484 ret = target_masked_watch_num_registers (value_as_address (val),
11485 mask);
11486 if (ret == -1)
11487 error (_("This target does not support masked watchpoints."));
11488 else if (ret == -2)
11489 error (_("Invalid mask or memory region."));
11490 }
06a64a0b
TT
11491 }
11492 else if (val != NULL)
fa4727a6 11493 release_value (val);
c906108c 11494
bbc13ae3
KS
11495 tok = skip_spaces_const (arg);
11496 end_tok = skip_to_space_const (tok);
c906108c
SS
11497
11498 toklen = end_tok - tok;
11499 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11500 {
2d134ed3
PA
11501 struct expression *cond;
11502
60e1c644 11503 innermost_block = NULL;
c906108c 11504 tok = cond_start = end_tok + 1;
1bb9788d 11505 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11506
11507 /* The watchpoint expression may not be local, but the condition
11508 may still be. E.g.: `watch global if local > 0'. */
11509 cond_exp_valid_block = innermost_block;
11510
2d134ed3 11511 xfree (cond);
c906108c
SS
11512 cond_end = tok;
11513 }
11514 if (*tok)
8a3fe4f8 11515 error (_("Junk at end of command."));
c906108c 11516
d983da9c 11517 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11518
11519 /* If the expression is "local", then set up a "watchpoint scope"
11520 breakpoint at the point where we've left the scope of the watchpoint
11521 expression. Create the scope breakpoint before the watchpoint, so
11522 that we will encounter it first in bpstat_stop_status. */
60e1c644 11523 if (exp_valid_block && frame)
d983da9c 11524 {
edb3359d
DJ
11525 if (frame_id_p (frame_unwind_caller_id (frame)))
11526 {
11527 scope_breakpoint
a6d9a66e
UW
11528 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11529 frame_unwind_caller_pc (frame),
06edf0c0
PA
11530 bp_watchpoint_scope,
11531 &momentary_breakpoint_ops);
d983da9c 11532
edb3359d 11533 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11534
edb3359d
DJ
11535 /* Automatically delete the breakpoint when it hits. */
11536 scope_breakpoint->disposition = disp_del;
d983da9c 11537
edb3359d
DJ
11538 /* Only break in the proper frame (help with recursion). */
11539 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11540
edb3359d 11541 /* Set the address at which we will stop. */
a6d9a66e
UW
11542 scope_breakpoint->loc->gdbarch
11543 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11544 scope_breakpoint->loc->requested_address
11545 = frame_unwind_caller_pc (frame);
11546 scope_breakpoint->loc->address
a6d9a66e
UW
11547 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11548 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11549 scope_breakpoint->type);
11550 }
d983da9c
DJ
11551 }
11552
e8369a73
AB
11553 /* Now set up the breakpoint. We create all watchpoints as hardware
11554 watchpoints here even if hardware watchpoints are turned off, a call
11555 to update_watchpoint later in this function will cause the type to
11556 drop back to bp_watchpoint (software watchpoint) if required. */
11557
11558 if (accessflag == hw_read)
11559 bp_type = bp_read_watchpoint;
11560 else if (accessflag == hw_access)
11561 bp_type = bp_access_watchpoint;
11562 else
11563 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11564
11565 w = XCNEW (struct watchpoint);
11566 b = &w->base;
348d480f 11567 if (use_mask)
3a5c3e22
PA
11568 init_raw_breakpoint_without_location (b, NULL, bp_type,
11569 &masked_watchpoint_breakpoint_ops);
348d480f 11570 else
3a5c3e22
PA
11571 init_raw_breakpoint_without_location (b, NULL, bp_type,
11572 &watchpoint_breakpoint_ops);
37e4754d 11573 b->thread = thread;
b5de0fa7 11574 b->disposition = disp_donttouch;
348d480f 11575 b->pspace = current_program_space;
3a5c3e22
PA
11576 w->exp = exp;
11577 w->exp_valid_block = exp_valid_block;
11578 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11579 if (just_location)
11580 {
11581 struct type *t = value_type (val);
11582 CORE_ADDR addr = value_as_address (val);
11583 char *name;
11584
11585 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11586 name = type_to_string (t);
11587
3a5c3e22 11588 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11589 core_addr_to_string (addr));
06a64a0b
TT
11590 xfree (name);
11591
3a5c3e22 11592 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11593 (int) (exp_end - exp_start), exp_start);
11594
06a64a0b
TT
11595 /* The above expression is in C. */
11596 b->language = language_c;
11597 }
11598 else
3a5c3e22 11599 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11600
11601 if (use_mask)
11602 {
3a5c3e22 11603 w->hw_wp_mask = mask;
9c06b0b4
TJB
11604 }
11605 else
11606 {
3a5c3e22 11607 w->val = val;
bb9d5f81
PP
11608 w->val_bitpos = saved_bitpos;
11609 w->val_bitsize = saved_bitsize;
3a5c3e22 11610 w->val_valid = 1;
9c06b0b4 11611 }
77b06cd7 11612
c906108c
SS
11613 if (cond_start)
11614 b->cond_string = savestring (cond_start, cond_end - cond_start);
11615 else
11616 b->cond_string = 0;
c5aa993b 11617
c906108c 11618 if (frame)
f6bc2008 11619 {
3a5c3e22
PA
11620 w->watchpoint_frame = get_frame_id (frame);
11621 w->watchpoint_thread = inferior_ptid;
f6bc2008 11622 }
c906108c 11623 else
f6bc2008 11624 {
3a5c3e22
PA
11625 w->watchpoint_frame = null_frame_id;
11626 w->watchpoint_thread = null_ptid;
f6bc2008 11627 }
c906108c 11628
d983da9c 11629 if (scope_breakpoint != NULL)
c906108c 11630 {
d983da9c
DJ
11631 /* The scope breakpoint is related to the watchpoint. We will
11632 need to act on them together. */
11633 b->related_breakpoint = scope_breakpoint;
11634 scope_breakpoint->related_breakpoint = b;
c906108c 11635 }
d983da9c 11636
06a64a0b
TT
11637 if (!just_location)
11638 value_free_to_mark (mark);
2d134ed3 11639
a9634178
TJB
11640 TRY_CATCH (e, RETURN_MASK_ALL)
11641 {
11642 /* Finally update the new watchpoint. This creates the locations
11643 that should be inserted. */
3a5c3e22 11644 update_watchpoint (w, 1);
a9634178
TJB
11645 }
11646 if (e.reason < 0)
11647 {
11648 delete_breakpoint (b);
11649 throw_exception (e);
11650 }
11651
3ea46bff 11652 install_breakpoint (internal, b, 1);
bbc13ae3 11653 do_cleanups (back_to);
c906108c
SS
11654}
11655
e09342b5 11656/* Return count of debug registers needed to watch the given expression.
e09342b5 11657 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11658
c906108c 11659static int
a9634178 11660can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11661{
11662 int found_memory_cnt = 0;
2e70b7b9 11663 struct value *head = v;
c906108c
SS
11664
11665 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11666 if (!can_use_hw_watchpoints)
c906108c 11667 return 0;
c5aa993b 11668
5c44784c
JM
11669 /* Make sure that the value of the expression depends only upon
11670 memory contents, and values computed from them within GDB. If we
11671 find any register references or function calls, we can't use a
11672 hardware watchpoint.
11673
11674 The idea here is that evaluating an expression generates a series
11675 of values, one holding the value of every subexpression. (The
11676 expression a*b+c has five subexpressions: a, b, a*b, c, and
11677 a*b+c.) GDB's values hold almost enough information to establish
11678 the criteria given above --- they identify memory lvalues,
11679 register lvalues, computed values, etcetera. So we can evaluate
11680 the expression, and then scan the chain of values that leaves
11681 behind to decide whether we can detect any possible change to the
11682 expression's final value using only hardware watchpoints.
11683
11684 However, I don't think that the values returned by inferior
11685 function calls are special in any way. So this function may not
11686 notice that an expression involving an inferior function call
11687 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11688 for (; v; v = value_next (v))
c906108c 11689 {
5c44784c 11690 if (VALUE_LVAL (v) == lval_memory)
c906108c 11691 {
8464be76
DJ
11692 if (v != head && value_lazy (v))
11693 /* A lazy memory lvalue in the chain is one that GDB never
11694 needed to fetch; we either just used its address (e.g.,
11695 `a' in `a.b') or we never needed it at all (e.g., `a'
11696 in `a,b'). This doesn't apply to HEAD; if that is
11697 lazy then it was not readable, but watch it anyway. */
5c44784c 11698 ;
53a5351d 11699 else
5c44784c
JM
11700 {
11701 /* Ahh, memory we actually used! Check if we can cover
11702 it with hardware watchpoints. */
df407dfe 11703 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11704
11705 /* We only watch structs and arrays if user asked for it
11706 explicitly, never if they just happen to appear in a
11707 middle of some value chain. */
11708 if (v == head
11709 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11710 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11711 {
42ae5230 11712 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11713 int len;
11714 int num_regs;
11715
a9634178 11716 len = (target_exact_watchpoints
e09342b5
TJB
11717 && is_scalar_type_recursive (vtype))?
11718 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11719
e09342b5
TJB
11720 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11721 if (!num_regs)
2e70b7b9
MS
11722 return 0;
11723 else
e09342b5 11724 found_memory_cnt += num_regs;
2e70b7b9 11725 }
5c44784c 11726 }
c5aa993b 11727 }
5086187c
AC
11728 else if (VALUE_LVAL (v) != not_lval
11729 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11730 return 0; /* These are values from the history (e.g., $1). */
5086187c 11731 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11732 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11733 }
11734
11735 /* The expression itself looks suitable for using a hardware
11736 watchpoint, but give the target machine a chance to reject it. */
11737 return found_memory_cnt;
11738}
11739
8b93c638 11740void
84f4c1fe 11741watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11742{
84f4c1fe 11743 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11744}
11745
06a64a0b
TT
11746/* A helper function that looks for the "-location" argument and then
11747 calls watch_command_1. */
11748
11749static void
11750watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11751{
11752 int just_location = 0;
11753
11754 if (arg
11755 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11756 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11757 {
e9cafbcc 11758 arg = skip_spaces (arg);
06a64a0b
TT
11759 just_location = 1;
11760 }
11761
84f4c1fe 11762 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11763}
8926118c 11764
c5aa993b 11765static void
fba45db2 11766watch_command (char *arg, int from_tty)
c906108c 11767{
06a64a0b 11768 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11769}
11770
8b93c638 11771void
84f4c1fe 11772rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11773{
84f4c1fe 11774 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11775}
8926118c 11776
c5aa993b 11777static void
fba45db2 11778rwatch_command (char *arg, int from_tty)
c906108c 11779{
06a64a0b 11780 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11781}
11782
8b93c638 11783void
84f4c1fe 11784awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11785{
84f4c1fe 11786 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11787}
8926118c 11788
c5aa993b 11789static void
fba45db2 11790awatch_command (char *arg, int from_tty)
c906108c 11791{
06a64a0b 11792 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11793}
c906108c 11794\f
c5aa993b 11795
43ff13b4 11796/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11797 because it uses the mechanisms of breakpoints. */
11798
bfec99b2
PA
11799struct until_break_command_continuation_args
11800{
11801 struct breakpoint *breakpoint;
11802 struct breakpoint *breakpoint2;
186c406b 11803 int thread_num;
bfec99b2
PA
11804};
11805
43ff13b4 11806/* This function is called by fetch_inferior_event via the
4a64f543 11807 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11808 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11809 command. */
c2c6d25f 11810static void
fa4cd53f 11811until_break_command_continuation (void *arg, int err)
43ff13b4 11812{
bfec99b2
PA
11813 struct until_break_command_continuation_args *a = arg;
11814
11815 delete_breakpoint (a->breakpoint);
11816 if (a->breakpoint2)
11817 delete_breakpoint (a->breakpoint2);
186c406b 11818 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11819}
11820
c906108c 11821void
ae66c1fc 11822until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11823{
11824 struct symtabs_and_lines sals;
11825 struct symtab_and_line sal;
8556afb4
PA
11826 struct frame_info *frame;
11827 struct gdbarch *frame_gdbarch;
11828 struct frame_id stack_frame_id;
11829 struct frame_id caller_frame_id;
c906108c 11830 struct breakpoint *breakpoint;
f107f563 11831 struct breakpoint *breakpoint2 = NULL;
c906108c 11832 struct cleanup *old_chain;
186c406b
TT
11833 int thread;
11834 struct thread_info *tp;
c906108c 11835
70509625 11836 clear_proceed_status (0);
c906108c
SS
11837
11838 /* Set a breakpoint where the user wants it and at return from
4a64f543 11839 this function. */
c5aa993b 11840
1bfeeb0f 11841 if (last_displayed_sal_is_valid ())
f8eba3c6 11842 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11843 get_last_displayed_symtab (),
f8eba3c6 11844 get_last_displayed_line ());
c906108c 11845 else
f8eba3c6
TT
11846 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11847 (struct symtab *) NULL, 0);
c5aa993b 11848
c906108c 11849 if (sals.nelts != 1)
8a3fe4f8 11850 error (_("Couldn't get information on specified line."));
c5aa993b 11851
c906108c 11852 sal = sals.sals[0];
4a64f543 11853 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11854
c906108c 11855 if (*arg)
8a3fe4f8 11856 error (_("Junk at end of arguments."));
c5aa993b 11857
c906108c 11858 resolve_sal_pc (&sal);
c5aa993b 11859
186c406b
TT
11860 tp = inferior_thread ();
11861 thread = tp->num;
11862
883bc8d1
PA
11863 old_chain = make_cleanup (null_cleanup, NULL);
11864
8556afb4
PA
11865 /* Note linespec handling above invalidates the frame chain.
11866 Installing a breakpoint also invalidates the frame chain (as it
11867 may need to switch threads), so do any frame handling before
11868 that. */
11869
11870 frame = get_selected_frame (NULL);
11871 frame_gdbarch = get_frame_arch (frame);
11872 stack_frame_id = get_stack_frame_id (frame);
11873 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11874
ae66c1fc
EZ
11875 /* Keep within the current frame, or in frames called by the current
11876 one. */
edb3359d 11877
883bc8d1 11878 if (frame_id_p (caller_frame_id))
c906108c 11879 {
883bc8d1
PA
11880 struct symtab_and_line sal2;
11881
11882 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11883 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11884 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11885 sal2,
11886 caller_frame_id,
f107f563
VP
11887 bp_until);
11888 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11889
883bc8d1 11890 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11891 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11892 }
c5aa993b 11893
c70a6932
JK
11894 /* set_momentary_breakpoint could invalidate FRAME. */
11895 frame = NULL;
11896
883bc8d1
PA
11897 if (anywhere)
11898 /* If the user told us to continue until a specified location,
11899 we don't specify a frame at which we need to stop. */
11900 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11901 null_frame_id, bp_until);
11902 else
11903 /* Otherwise, specify the selected frame, because we want to stop
11904 only at the very same frame. */
11905 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11906 stack_frame_id, bp_until);
11907 make_cleanup_delete_breakpoint (breakpoint);
11908
a493e3e2 11909 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11910
4a64f543
MS
11911 /* If we are running asynchronously, and proceed call above has
11912 actually managed to start the target, arrange for breakpoints to
11913 be deleted when the target stops. Otherwise, we're already
11914 stopped and delete breakpoints via cleanup chain. */
f107f563 11915
8ea051c5 11916 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11917 {
bfec99b2
PA
11918 struct until_break_command_continuation_args *args;
11919 args = xmalloc (sizeof (*args));
f107f563 11920
bfec99b2
PA
11921 args->breakpoint = breakpoint;
11922 args->breakpoint2 = breakpoint2;
186c406b 11923 args->thread_num = thread;
f107f563
VP
11924
11925 discard_cleanups (old_chain);
95e54da7
PA
11926 add_continuation (inferior_thread (),
11927 until_break_command_continuation, args,
604ead4a 11928 xfree);
f107f563
VP
11929 }
11930 else
c5aa993b 11931 do_cleanups (old_chain);
c906108c 11932}
ae66c1fc 11933
c906108c
SS
11934/* This function attempts to parse an optional "if <cond>" clause
11935 from the arg string. If one is not found, it returns NULL.
c5aa993b 11936
c906108c
SS
11937 Else, it returns a pointer to the condition string. (It does not
11938 attempt to evaluate the string against a particular block.) And,
11939 it updates arg to point to the first character following the parsed
4a64f543 11940 if clause in the arg string. */
53a5351d 11941
916703c0 11942char *
fba45db2 11943ep_parse_optional_if_clause (char **arg)
c906108c 11944{
c5aa993b
JM
11945 char *cond_string;
11946
11947 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11948 return NULL;
c5aa993b 11949
4a64f543 11950 /* Skip the "if" keyword. */
c906108c 11951 (*arg) += 2;
c5aa993b 11952
c906108c 11953 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11954 condition string. */
e9cafbcc 11955 *arg = skip_spaces (*arg);
c906108c 11956 cond_string = *arg;
c5aa993b 11957
4a64f543
MS
11958 /* Assume that the condition occupies the remainder of the arg
11959 string. */
c906108c 11960 (*arg) += strlen (cond_string);
c5aa993b 11961
c906108c
SS
11962 return cond_string;
11963}
c5aa993b 11964
c906108c
SS
11965/* Commands to deal with catching events, such as signals, exceptions,
11966 process start/exit, etc. */
c5aa993b
JM
11967
11968typedef enum
11969{
44feb3ce
TT
11970 catch_fork_temporary, catch_vfork_temporary,
11971 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11972}
11973catch_fork_kind;
11974
c906108c 11975static void
cc59ec59
MS
11976catch_fork_command_1 (char *arg, int from_tty,
11977 struct cmd_list_element *command)
c906108c 11978{
a6d9a66e 11979 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11980 char *cond_string = NULL;
44feb3ce
TT
11981 catch_fork_kind fork_kind;
11982 int tempflag;
11983
11984 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11985 tempflag = (fork_kind == catch_fork_temporary
11986 || fork_kind == catch_vfork_temporary);
c5aa993b 11987
44feb3ce
TT
11988 if (!arg)
11989 arg = "";
e9cafbcc 11990 arg = skip_spaces (arg);
c5aa993b 11991
c906108c 11992 /* The allowed syntax is:
c5aa993b
JM
11993 catch [v]fork
11994 catch [v]fork if <cond>
11995
4a64f543 11996 First, check if there's an if clause. */
c906108c 11997 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11998
c906108c 11999 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12000 error (_("Junk at end of arguments."));
c5aa993b 12001
c906108c 12002 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 12003 and enable reporting of such events. */
c5aa993b
JM
12004 switch (fork_kind)
12005 {
44feb3ce
TT
12006 case catch_fork_temporary:
12007 case catch_fork_permanent:
a6d9a66e 12008 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12009 &catch_fork_breakpoint_ops);
c906108c 12010 break;
44feb3ce
TT
12011 case catch_vfork_temporary:
12012 case catch_vfork_permanent:
a6d9a66e 12013 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12014 &catch_vfork_breakpoint_ops);
c906108c 12015 break;
c5aa993b 12016 default:
8a3fe4f8 12017 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 12018 break;
c5aa993b 12019 }
c906108c
SS
12020}
12021
12022static void
cc59ec59
MS
12023catch_exec_command_1 (char *arg, int from_tty,
12024 struct cmd_list_element *command)
c906108c 12025{
b4d90040 12026 struct exec_catchpoint *c;
a6d9a66e 12027 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 12028 int tempflag;
c5aa993b 12029 char *cond_string = NULL;
c906108c 12030
44feb3ce
TT
12031 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12032
12033 if (!arg)
12034 arg = "";
e9cafbcc 12035 arg = skip_spaces (arg);
c906108c
SS
12036
12037 /* The allowed syntax is:
c5aa993b
JM
12038 catch exec
12039 catch exec if <cond>
c906108c 12040
4a64f543 12041 First, check if there's an if clause. */
c906108c
SS
12042 cond_string = ep_parse_optional_if_clause (&arg);
12043
12044 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12045 error (_("Junk at end of arguments."));
c906108c 12046
b4d90040
PA
12047 c = XNEW (struct exec_catchpoint);
12048 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
12049 &catch_exec_breakpoint_ops);
12050 c->exec_pathname = NULL;
12051
3ea46bff 12052 install_breakpoint (0, &c->base, 1);
c906108c 12053}
c5aa993b 12054
9ac4176b 12055void
28010a5d
PA
12056init_ada_exception_breakpoint (struct breakpoint *b,
12057 struct gdbarch *gdbarch,
12058 struct symtab_and_line sal,
12059 char *addr_string,
c0a91b2b 12060 const struct breakpoint_ops *ops,
28010a5d 12061 int tempflag,
349774ef 12062 int enabled,
28010a5d 12063 int from_tty)
f7f9143b 12064{
f7f9143b
JB
12065 if (from_tty)
12066 {
5af949e3
UW
12067 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12068 if (!loc_gdbarch)
12069 loc_gdbarch = gdbarch;
12070
6c95b8df
PA
12071 describe_other_breakpoints (loc_gdbarch,
12072 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
12073 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12074 version for exception catchpoints, because two catchpoints
12075 used for different exception names will use the same address.
12076 In this case, a "breakpoint ... also set at..." warning is
4a64f543 12077 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 12078 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
12079 the user what type of catchpoint it is. The above is good
12080 enough for now, though. */
12081 }
12082
28010a5d 12083 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 12084
349774ef 12085 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 12086 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
12087 b->addr_string = addr_string;
12088 b->language = language_ada;
f7f9143b
JB
12089}
12090
a96d9b2e
SDJ
12091/* Splits the argument using space as delimiter. Returns an xmalloc'd
12092 filter list, or NULL if no filtering is required. */
12093static VEC(int) *
12094catch_syscall_split_args (char *arg)
12095{
12096 VEC(int) *result = NULL;
29d0bb3d 12097 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
12098
12099 while (*arg != '\0')
12100 {
12101 int i, syscall_number;
12102 char *endptr;
12103 char cur_name[128];
12104 struct syscall s;
12105
12106 /* Skip whitespace. */
529480d0 12107 arg = skip_spaces (arg);
a96d9b2e
SDJ
12108
12109 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12110 cur_name[i] = arg[i];
12111 cur_name[i] = '\0';
12112 arg += i;
12113
12114 /* Check if the user provided a syscall name or a number. */
12115 syscall_number = (int) strtol (cur_name, &endptr, 0);
12116 if (*endptr == '\0')
bccd0dd2 12117 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
12118 else
12119 {
12120 /* We have a name. Let's check if it's valid and convert it
12121 to a number. */
12122 get_syscall_by_name (cur_name, &s);
12123
12124 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
12125 /* Here we have to issue an error instead of a warning,
12126 because GDB cannot do anything useful if there's no
12127 syscall number to be caught. */
a96d9b2e
SDJ
12128 error (_("Unknown syscall name '%s'."), cur_name);
12129 }
12130
12131 /* Ok, it's valid. */
12132 VEC_safe_push (int, result, s.number);
12133 }
12134
12135 discard_cleanups (cleanup);
12136 return result;
12137}
12138
12139/* Implement the "catch syscall" command. */
12140
12141static void
cc59ec59
MS
12142catch_syscall_command_1 (char *arg, int from_tty,
12143 struct cmd_list_element *command)
a96d9b2e
SDJ
12144{
12145 int tempflag;
12146 VEC(int) *filter;
12147 struct syscall s;
12148 struct gdbarch *gdbarch = get_current_arch ();
12149
12150 /* Checking if the feature if supported. */
12151 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12152 error (_("The feature 'catch syscall' is not supported on \
ea666128 12153this architecture yet."));
a96d9b2e
SDJ
12154
12155 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12156
e9cafbcc 12157 arg = skip_spaces (arg);
a96d9b2e
SDJ
12158
12159 /* We need to do this first "dummy" translation in order
12160 to get the syscall XML file loaded or, most important,
12161 to display a warning to the user if there's no XML file
12162 for his/her architecture. */
12163 get_syscall_by_number (0, &s);
12164
12165 /* The allowed syntax is:
12166 catch syscall
12167 catch syscall <name | number> [<name | number> ... <name | number>]
12168
12169 Let's check if there's a syscall name. */
12170
12171 if (arg != NULL)
12172 filter = catch_syscall_split_args (arg);
12173 else
12174 filter = NULL;
12175
12176 create_syscall_event_catchpoint (tempflag, filter,
12177 &catch_syscall_breakpoint_ops);
12178}
12179
c906108c 12180static void
fba45db2 12181catch_command (char *arg, int from_tty)
c906108c 12182{
44feb3ce 12183 error (_("Catch requires an event name."));
c906108c
SS
12184}
12185\f
12186
12187static void
fba45db2 12188tcatch_command (char *arg, int from_tty)
c906108c 12189{
44feb3ce 12190 error (_("Catch requires an event name."));
c906108c
SS
12191}
12192
8a2c437b
TT
12193/* A qsort comparison function that sorts breakpoints in order. */
12194
12195static int
12196compare_breakpoints (const void *a, const void *b)
12197{
12198 const breakpoint_p *ba = a;
12199 uintptr_t ua = (uintptr_t) *ba;
12200 const breakpoint_p *bb = b;
12201 uintptr_t ub = (uintptr_t) *bb;
12202
12203 if ((*ba)->number < (*bb)->number)
12204 return -1;
12205 else if ((*ba)->number > (*bb)->number)
12206 return 1;
12207
12208 /* Now sort by address, in case we see, e..g, two breakpoints with
12209 the number 0. */
12210 if (ua < ub)
12211 return -1;
94b0e70d 12212 return ua > ub ? 1 : 0;
8a2c437b
TT
12213}
12214
80f8a6eb 12215/* Delete breakpoints by address or line. */
c906108c
SS
12216
12217static void
fba45db2 12218clear_command (char *arg, int from_tty)
c906108c 12219{
8a2c437b 12220 struct breakpoint *b, *prev;
d6e956e5
VP
12221 VEC(breakpoint_p) *found = 0;
12222 int ix;
c906108c
SS
12223 int default_match;
12224 struct symtabs_and_lines sals;
12225 struct symtab_and_line sal;
c906108c 12226 int i;
8a2c437b 12227 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12228
12229 if (arg)
12230 {
39cf75f7
DE
12231 sals = decode_line_with_current_source (arg,
12232 (DECODE_LINE_FUNFIRSTLINE
12233 | DECODE_LINE_LIST_MODE));
cf4ded82 12234 make_cleanup (xfree, sals.sals);
c906108c
SS
12235 default_match = 0;
12236 }
12237 else
12238 {
c5aa993b 12239 sals.sals = (struct symtab_and_line *)
c906108c 12240 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12241 make_cleanup (xfree, sals.sals);
4a64f543 12242 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12243
12244 /* Set sal's line, symtab, pc, and pspace to the values
12245 corresponding to the last call to print_frame_info. If the
12246 codepoint is not valid, this will set all the fields to 0. */
12247 get_last_displayed_sal (&sal);
c906108c 12248 if (sal.symtab == 0)
8a3fe4f8 12249 error (_("No source file specified."));
c906108c
SS
12250
12251 sals.sals[0] = sal;
12252 sals.nelts = 1;
12253
12254 default_match = 1;
12255 }
12256
4a64f543
MS
12257 /* We don't call resolve_sal_pc here. That's not as bad as it
12258 seems, because all existing breakpoints typically have both
12259 file/line and pc set. So, if clear is given file/line, we can
12260 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12261
12262 We only support clearing given the address explicitly
12263 present in breakpoint table. Say, we've set breakpoint
4a64f543 12264 at file:line. There were several PC values for that file:line,
ed0616c6 12265 due to optimization, all in one block.
4a64f543
MS
12266
12267 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12268 PC corresponding to the same file:line, the breakpoint won't
12269 be cleared. We probably can still clear the breakpoint, but
12270 since the other PC value is never presented to user, user
12271 can only find it by guessing, and it does not seem important
12272 to support that. */
12273
4a64f543
MS
12274 /* For each line spec given, delete bps which correspond to it. Do
12275 it in two passes, solely to preserve the current behavior that
12276 from_tty is forced true if we delete more than one
12277 breakpoint. */
c906108c 12278
80f8a6eb 12279 found = NULL;
8a2c437b 12280 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12281 for (i = 0; i < sals.nelts; i++)
12282 {
05cba821
JK
12283 const char *sal_fullname;
12284
c906108c 12285 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12286 If line given (pc == 0), clear all bpts on specified line.
12287 If defaulting, clear all bpts on default line
c906108c 12288 or at default pc.
c5aa993b
JM
12289
12290 defaulting sal.pc != 0 tests to do
12291
12292 0 1 pc
12293 1 1 pc _and_ line
12294 0 0 line
12295 1 0 <can't happen> */
c906108c
SS
12296
12297 sal = sals.sals[i];
05cba821
JK
12298 sal_fullname = (sal.symtab == NULL
12299 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12300
4a64f543 12301 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12302 ALL_BREAKPOINTS (b)
c5aa993b 12303 {
0d381245 12304 int match = 0;
4a64f543 12305 /* Are we going to delete b? */
cc60f2e3 12306 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12307 {
12308 struct bp_location *loc = b->loc;
12309 for (; loc; loc = loc->next)
12310 {
f8eba3c6
TT
12311 /* If the user specified file:line, don't allow a PC
12312 match. This matches historical gdb behavior. */
12313 int pc_match = (!sal.explicit_line
12314 && sal.pc
12315 && (loc->pspace == sal.pspace)
12316 && (loc->address == sal.pc)
12317 && (!section_is_overlay (loc->section)
12318 || loc->section == sal.section));
4aac40c8
TT
12319 int line_match = 0;
12320
12321 if ((default_match || sal.explicit_line)
2f202fde 12322 && loc->symtab != NULL
05cba821 12323 && sal_fullname != NULL
4aac40c8 12324 && sal.pspace == loc->pspace
05cba821
JK
12325 && loc->line_number == sal.line
12326 && filename_cmp (symtab_to_fullname (loc->symtab),
12327 sal_fullname) == 0)
12328 line_match = 1;
4aac40c8 12329
0d381245
VP
12330 if (pc_match || line_match)
12331 {
12332 match = 1;
12333 break;
12334 }
12335 }
12336 }
12337
12338 if (match)
d6e956e5 12339 VEC_safe_push(breakpoint_p, found, b);
c906108c 12340 }
80f8a6eb 12341 }
8a2c437b 12342
80f8a6eb 12343 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12344 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12345 {
12346 if (arg)
8a3fe4f8 12347 error (_("No breakpoint at %s."), arg);
80f8a6eb 12348 else
8a3fe4f8 12349 error (_("No breakpoint at this line."));
80f8a6eb 12350 }
c906108c 12351
8a2c437b
TT
12352 /* Remove duplicates from the vec. */
12353 qsort (VEC_address (breakpoint_p, found),
12354 VEC_length (breakpoint_p, found),
12355 sizeof (breakpoint_p),
12356 compare_breakpoints);
12357 prev = VEC_index (breakpoint_p, found, 0);
12358 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12359 {
12360 if (b == prev)
12361 {
12362 VEC_ordered_remove (breakpoint_p, found, ix);
12363 --ix;
12364 }
12365 }
12366
d6e956e5 12367 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12368 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12369 if (from_tty)
a3f17187 12370 {
d6e956e5 12371 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12372 printf_unfiltered (_("Deleted breakpoint "));
12373 else
12374 printf_unfiltered (_("Deleted breakpoints "));
12375 }
d6e956e5
VP
12376
12377 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12378 {
c5aa993b 12379 if (from_tty)
d6e956e5
VP
12380 printf_unfiltered ("%d ", b->number);
12381 delete_breakpoint (b);
c906108c 12382 }
80f8a6eb
MS
12383 if (from_tty)
12384 putchar_unfiltered ('\n');
8a2c437b
TT
12385
12386 do_cleanups (cleanups);
c906108c
SS
12387}
12388\f
12389/* Delete breakpoint in BS if they are `delete' breakpoints and
12390 all breakpoints that are marked for deletion, whether hit or not.
12391 This is called after any breakpoint is hit, or after errors. */
12392
12393void
fba45db2 12394breakpoint_auto_delete (bpstat bs)
c906108c 12395{
35df4500 12396 struct breakpoint *b, *b_tmp;
c906108c
SS
12397
12398 for (; bs; bs = bs->next)
f431efe5
PA
12399 if (bs->breakpoint_at
12400 && bs->breakpoint_at->disposition == disp_del
c906108c 12401 && bs->stop)
f431efe5 12402 delete_breakpoint (bs->breakpoint_at);
c906108c 12403
35df4500 12404 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12405 {
b5de0fa7 12406 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12407 delete_breakpoint (b);
12408 }
c906108c
SS
12409}
12410
4a64f543
MS
12411/* A comparison function for bp_location AP and BP being interfaced to
12412 qsort. Sort elements primarily by their ADDRESS (no matter what
12413 does breakpoint_address_is_meaningful say for its OWNER),
12414 secondarily by ordering first bp_permanent OWNERed elements and
12415 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12416 qsort being an unstable algorithm. */
876fa593
JK
12417
12418static int
494cfb0f 12419bp_location_compare (const void *ap, const void *bp)
876fa593 12420{
494cfb0f
JK
12421 struct bp_location *a = *(void **) ap;
12422 struct bp_location *b = *(void **) bp;
2bdf28a0 12423 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
12424 int a_perm = a->owner->enable_state == bp_permanent;
12425 int b_perm = b->owner->enable_state == bp_permanent;
12426
12427 if (a->address != b->address)
12428 return (a->address > b->address) - (a->address < b->address);
12429
dea2aa5f
LM
12430 /* Sort locations at the same address by their pspace number, keeping
12431 locations of the same inferior (in a multi-inferior environment)
12432 grouped. */
12433
12434 if (a->pspace->num != b->pspace->num)
12435 return ((a->pspace->num > b->pspace->num)
12436 - (a->pspace->num < b->pspace->num));
12437
876fa593
JK
12438 /* Sort permanent breakpoints first. */
12439 if (a_perm != b_perm)
12440 return (a_perm < b_perm) - (a_perm > b_perm);
12441
c56a97f9
JK
12442 /* Make the internal GDB representation stable across GDB runs
12443 where A and B memory inside GDB can differ. Breakpoint locations of
12444 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12445
12446 if (a->owner->number != b->owner->number)
c56a97f9
JK
12447 return ((a->owner->number > b->owner->number)
12448 - (a->owner->number < b->owner->number));
876fa593
JK
12449
12450 return (a > b) - (a < b);
12451}
12452
876fa593 12453/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12454 bp_location_shadow_len_after_address_max according to the current
12455 content of the bp_location array. */
f7545552
TT
12456
12457static void
876fa593 12458bp_location_target_extensions_update (void)
f7545552 12459{
876fa593
JK
12460 struct bp_location *bl, **blp_tmp;
12461
12462 bp_location_placed_address_before_address_max = 0;
12463 bp_location_shadow_len_after_address_max = 0;
12464
12465 ALL_BP_LOCATIONS (bl, blp_tmp)
12466 {
12467 CORE_ADDR start, end, addr;
12468
12469 if (!bp_location_has_shadow (bl))
12470 continue;
12471
12472 start = bl->target_info.placed_address;
12473 end = start + bl->target_info.shadow_len;
12474
12475 gdb_assert (bl->address >= start);
12476 addr = bl->address - start;
12477 if (addr > bp_location_placed_address_before_address_max)
12478 bp_location_placed_address_before_address_max = addr;
12479
12480 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12481
12482 gdb_assert (bl->address < end);
12483 addr = end - bl->address;
12484 if (addr > bp_location_shadow_len_after_address_max)
12485 bp_location_shadow_len_after_address_max = addr;
12486 }
f7545552
TT
12487}
12488
1e4d1764
YQ
12489/* Download tracepoint locations if they haven't been. */
12490
12491static void
12492download_tracepoint_locations (void)
12493{
7ed2c994 12494 struct breakpoint *b;
1e4d1764
YQ
12495 struct cleanup *old_chain;
12496
12497 if (!target_can_download_tracepoint ())
12498 return;
12499
12500 old_chain = save_current_space_and_thread ();
12501
7ed2c994 12502 ALL_TRACEPOINTS (b)
1e4d1764 12503 {
7ed2c994 12504 struct bp_location *bl;
1e4d1764 12505 struct tracepoint *t;
f2a8bc8a 12506 int bp_location_downloaded = 0;
1e4d1764 12507
7ed2c994 12508 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12509 ? !may_insert_fast_tracepoints
12510 : !may_insert_tracepoints))
12511 continue;
12512
7ed2c994
YQ
12513 for (bl = b->loc; bl; bl = bl->next)
12514 {
12515 /* In tracepoint, locations are _never_ duplicated, so
12516 should_be_inserted is equivalent to
12517 unduplicated_should_be_inserted. */
12518 if (!should_be_inserted (bl) || bl->inserted)
12519 continue;
1e4d1764 12520
7ed2c994 12521 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12522
7ed2c994 12523 target_download_tracepoint (bl);
1e4d1764 12524
7ed2c994 12525 bl->inserted = 1;
f2a8bc8a 12526 bp_location_downloaded = 1;
7ed2c994
YQ
12527 }
12528 t = (struct tracepoint *) b;
12529 t->number_on_target = b->number;
f2a8bc8a
YQ
12530 if (bp_location_downloaded)
12531 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12532 }
12533
12534 do_cleanups (old_chain);
12535}
12536
934709f0
PW
12537/* Swap the insertion/duplication state between two locations. */
12538
12539static void
12540swap_insertion (struct bp_location *left, struct bp_location *right)
12541{
12542 const int left_inserted = left->inserted;
12543 const int left_duplicate = left->duplicate;
b775012e 12544 const int left_needs_update = left->needs_update;
934709f0
PW
12545 const struct bp_target_info left_target_info = left->target_info;
12546
1e4d1764
YQ
12547 /* Locations of tracepoints can never be duplicated. */
12548 if (is_tracepoint (left->owner))
12549 gdb_assert (!left->duplicate);
12550 if (is_tracepoint (right->owner))
12551 gdb_assert (!right->duplicate);
12552
934709f0
PW
12553 left->inserted = right->inserted;
12554 left->duplicate = right->duplicate;
b775012e 12555 left->needs_update = right->needs_update;
934709f0
PW
12556 left->target_info = right->target_info;
12557 right->inserted = left_inserted;
12558 right->duplicate = left_duplicate;
b775012e 12559 right->needs_update = left_needs_update;
934709f0
PW
12560 right->target_info = left_target_info;
12561}
12562
b775012e
LM
12563/* Force the re-insertion of the locations at ADDRESS. This is called
12564 once a new/deleted/modified duplicate location is found and we are evaluating
12565 conditions on the target's side. Such conditions need to be updated on
12566 the target. */
12567
12568static void
12569force_breakpoint_reinsertion (struct bp_location *bl)
12570{
12571 struct bp_location **locp = NULL, **loc2p;
12572 struct bp_location *loc;
12573 CORE_ADDR address = 0;
12574 int pspace_num;
12575
12576 address = bl->address;
12577 pspace_num = bl->pspace->num;
12578
12579 /* This is only meaningful if the target is
12580 evaluating conditions and if the user has
12581 opted for condition evaluation on the target's
12582 side. */
12583 if (gdb_evaluates_breakpoint_condition_p ()
12584 || !target_supports_evaluation_of_breakpoint_conditions ())
12585 return;
12586
12587 /* Flag all breakpoint locations with this address and
12588 the same program space as the location
12589 as "its condition has changed". We need to
12590 update the conditions on the target's side. */
12591 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12592 {
12593 loc = *loc2p;
12594
12595 if (!is_breakpoint (loc->owner)
12596 || pspace_num != loc->pspace->num)
12597 continue;
12598
12599 /* Flag the location appropriately. We use a different state to
12600 let everyone know that we already updated the set of locations
12601 with addr bl->address and program space bl->pspace. This is so
12602 we don't have to keep calling these functions just to mark locations
12603 that have already been marked. */
12604 loc->condition_changed = condition_updated;
12605
12606 /* Free the agent expression bytecode as well. We will compute
12607 it later on. */
12608 if (loc->cond_bytecode)
12609 {
12610 free_agent_expr (loc->cond_bytecode);
12611 loc->cond_bytecode = NULL;
12612 }
12613 }
12614}
44702360
PA
12615/* Called whether new breakpoints are created, or existing breakpoints
12616 deleted, to update the global location list and recompute which
12617 locations are duplicate of which.
b775012e 12618
04086b45
PA
12619 The INSERT_MODE flag determines whether locations may not, may, or
12620 shall be inserted now. See 'enum ugll_insert_mode' for more
12621 info. */
b60e7edf 12622
0d381245 12623static void
44702360 12624update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12625{
74960c60 12626 struct breakpoint *b;
876fa593 12627 struct bp_location **locp, *loc;
f7545552 12628 struct cleanup *cleanups;
b775012e
LM
12629 /* Last breakpoint location address that was marked for update. */
12630 CORE_ADDR last_addr = 0;
12631 /* Last breakpoint location program space that was marked for update. */
12632 int last_pspace_num = -1;
f7545552 12633
2d134ed3
PA
12634 /* Used in the duplicates detection below. When iterating over all
12635 bp_locations, points to the first bp_location of a given address.
12636 Breakpoints and watchpoints of different types are never
12637 duplicates of each other. Keep one pointer for each type of
12638 breakpoint/watchpoint, so we only need to loop over all locations
12639 once. */
12640 struct bp_location *bp_loc_first; /* breakpoint */
12641 struct bp_location *wp_loc_first; /* hardware watchpoint */
12642 struct bp_location *awp_loc_first; /* access watchpoint */
12643 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12644
4a64f543
MS
12645 /* Saved former bp_location array which we compare against the newly
12646 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12647 struct bp_location **old_location, **old_locp;
12648 unsigned old_location_count;
12649
12650 old_location = bp_location;
12651 old_location_count = bp_location_count;
12652 bp_location = NULL;
12653 bp_location_count = 0;
12654 cleanups = make_cleanup (xfree, old_location);
0d381245 12655
74960c60 12656 ALL_BREAKPOINTS (b)
876fa593
JK
12657 for (loc = b->loc; loc; loc = loc->next)
12658 bp_location_count++;
12659
12660 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12661 locp = bp_location;
12662 ALL_BREAKPOINTS (b)
12663 for (loc = b->loc; loc; loc = loc->next)
12664 *locp++ = loc;
12665 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12666 bp_location_compare);
876fa593
JK
12667
12668 bp_location_target_extensions_update ();
74960c60 12669
4a64f543
MS
12670 /* Identify bp_location instances that are no longer present in the
12671 new list, and therefore should be freed. Note that it's not
12672 necessary that those locations should be removed from inferior --
12673 if there's another location at the same address (previously
12674 marked as duplicate), we don't need to remove/insert the
12675 location.
876fa593 12676
4a64f543
MS
12677 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12678 and former bp_location array state respectively. */
876fa593
JK
12679
12680 locp = bp_location;
12681 for (old_locp = old_location; old_locp < old_location + old_location_count;
12682 old_locp++)
74960c60 12683 {
876fa593 12684 struct bp_location *old_loc = *old_locp;
c7d46a38 12685 struct bp_location **loc2p;
876fa593 12686
e5dd4106 12687 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12688 not, we have to free it. */
c7d46a38 12689 int found_object = 0;
20874c92
VP
12690 /* Tells if the location should remain inserted in the target. */
12691 int keep_in_target = 0;
12692 int removed = 0;
876fa593 12693
4a64f543
MS
12694 /* Skip LOCP entries which will definitely never be needed.
12695 Stop either at or being the one matching OLD_LOC. */
876fa593 12696 while (locp < bp_location + bp_location_count
c7d46a38 12697 && (*locp)->address < old_loc->address)
876fa593 12698 locp++;
c7d46a38
PA
12699
12700 for (loc2p = locp;
12701 (loc2p < bp_location + bp_location_count
12702 && (*loc2p)->address == old_loc->address);
12703 loc2p++)
12704 {
b775012e
LM
12705 /* Check if this is a new/duplicated location or a duplicated
12706 location that had its condition modified. If so, we want to send
12707 its condition to the target if evaluation of conditions is taking
12708 place there. */
12709 if ((*loc2p)->condition_changed == condition_modified
12710 && (last_addr != old_loc->address
12711 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12712 {
b775012e
LM
12713 force_breakpoint_reinsertion (*loc2p);
12714 last_pspace_num = old_loc->pspace->num;
c7d46a38 12715 }
b775012e
LM
12716
12717 if (*loc2p == old_loc)
12718 found_object = 1;
c7d46a38 12719 }
74960c60 12720
b775012e
LM
12721 /* We have already handled this address, update it so that we don't
12722 have to go through updates again. */
12723 last_addr = old_loc->address;
12724
12725 /* Target-side condition evaluation: Handle deleted locations. */
12726 if (!found_object)
12727 force_breakpoint_reinsertion (old_loc);
12728
4a64f543
MS
12729 /* If this location is no longer present, and inserted, look if
12730 there's maybe a new location at the same address. If so,
12731 mark that one inserted, and don't remove this one. This is
12732 needed so that we don't have a time window where a breakpoint
12733 at certain location is not inserted. */
74960c60 12734
876fa593 12735 if (old_loc->inserted)
0d381245 12736 {
4a64f543
MS
12737 /* If the location is inserted now, we might have to remove
12738 it. */
74960c60 12739
876fa593 12740 if (found_object && should_be_inserted (old_loc))
74960c60 12741 {
4a64f543
MS
12742 /* The location is still present in the location list,
12743 and still should be inserted. Don't do anything. */
20874c92 12744 keep_in_target = 1;
74960c60
VP
12745 }
12746 else
12747 {
b775012e
LM
12748 /* This location still exists, but it won't be kept in the
12749 target since it may have been disabled. We proceed to
12750 remove its target-side condition. */
12751
4a64f543
MS
12752 /* The location is either no longer present, or got
12753 disabled. See if there's another location at the
12754 same address, in which case we don't need to remove
12755 this one from the target. */
876fa593 12756
2bdf28a0 12757 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12758 if (breakpoint_address_is_meaningful (old_loc->owner))
12759 {
876fa593 12760 for (loc2p = locp;
c7d46a38
PA
12761 (loc2p < bp_location + bp_location_count
12762 && (*loc2p)->address == old_loc->address);
876fa593
JK
12763 loc2p++)
12764 {
12765 struct bp_location *loc2 = *loc2p;
12766
2d134ed3 12767 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12768 {
85d721b8
PA
12769 /* Read watchpoint locations are switched to
12770 access watchpoints, if the former are not
12771 supported, but the latter are. */
12772 if (is_hardware_watchpoint (old_loc->owner))
12773 {
12774 gdb_assert (is_hardware_watchpoint (loc2->owner));
12775 loc2->watchpoint_type = old_loc->watchpoint_type;
12776 }
12777
934709f0
PW
12778 /* loc2 is a duplicated location. We need to check
12779 if it should be inserted in case it will be
12780 unduplicated. */
12781 if (loc2 != old_loc
12782 && unduplicated_should_be_inserted (loc2))
c7d46a38 12783 {
934709f0 12784 swap_insertion (old_loc, loc2);
c7d46a38
PA
12785 keep_in_target = 1;
12786 break;
12787 }
876fa593
JK
12788 }
12789 }
12790 }
74960c60
VP
12791 }
12792
20874c92
VP
12793 if (!keep_in_target)
12794 {
876fa593 12795 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12796 {
4a64f543
MS
12797 /* This is just about all we can do. We could keep
12798 this location on the global list, and try to
12799 remove it next time, but there's no particular
12800 reason why we will succeed next time.
20874c92 12801
4a64f543
MS
12802 Note that at this point, old_loc->owner is still
12803 valid, as delete_breakpoint frees the breakpoint
12804 only after calling us. */
3e43a32a
MS
12805 printf_filtered (_("warning: Error removing "
12806 "breakpoint %d\n"),
876fa593 12807 old_loc->owner->number);
20874c92
VP
12808 }
12809 removed = 1;
12810 }
0d381245 12811 }
74960c60
VP
12812
12813 if (!found_object)
1c5cfe86 12814 {
db82e815
PA
12815 if (removed && non_stop
12816 && breakpoint_address_is_meaningful (old_loc->owner)
12817 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12818 {
db82e815
PA
12819 /* This location was removed from the target. In
12820 non-stop mode, a race condition is possible where
12821 we've removed a breakpoint, but stop events for that
12822 breakpoint are already queued and will arrive later.
12823 We apply an heuristic to be able to distinguish such
12824 SIGTRAPs from other random SIGTRAPs: we keep this
12825 breakpoint location for a bit, and will retire it
12826 after we see some number of events. The theory here
12827 is that reporting of events should, "on the average",
12828 be fair, so after a while we'll see events from all
12829 threads that have anything of interest, and no longer
12830 need to keep this breakpoint location around. We
12831 don't hold locations forever so to reduce chances of
12832 mistaking a non-breakpoint SIGTRAP for a breakpoint
12833 SIGTRAP.
12834
12835 The heuristic failing can be disastrous on
12836 decr_pc_after_break targets.
12837
12838 On decr_pc_after_break targets, like e.g., x86-linux,
12839 if we fail to recognize a late breakpoint SIGTRAP,
12840 because events_till_retirement has reached 0 too
12841 soon, we'll fail to do the PC adjustment, and report
12842 a random SIGTRAP to the user. When the user resumes
12843 the inferior, it will most likely immediately crash
2dec564e 12844 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12845 corrupted, because of being resumed e.g., in the
12846 middle of a multi-byte instruction, or skipped a
12847 one-byte instruction. This was actually seen happen
12848 on native x86-linux, and should be less rare on
12849 targets that do not support new thread events, like
12850 remote, due to the heuristic depending on
12851 thread_count.
12852
12853 Mistaking a random SIGTRAP for a breakpoint trap
12854 causes similar symptoms (PC adjustment applied when
12855 it shouldn't), but then again, playing with SIGTRAPs
12856 behind the debugger's back is asking for trouble.
12857
12858 Since hardware watchpoint traps are always
12859 distinguishable from other traps, so we don't need to
12860 apply keep hardware watchpoint moribund locations
12861 around. We simply always ignore hardware watchpoint
12862 traps we can no longer explain. */
12863
876fa593
JK
12864 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12865 old_loc->owner = NULL;
20874c92 12866
876fa593 12867 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12868 }
12869 else
f431efe5
PA
12870 {
12871 old_loc->owner = NULL;
12872 decref_bp_location (&old_loc);
12873 }
20874c92 12874 }
74960c60 12875 }
1c5cfe86 12876
348d480f
PA
12877 /* Rescan breakpoints at the same address and section, marking the
12878 first one as "first" and any others as "duplicates". This is so
12879 that the bpt instruction is only inserted once. If we have a
12880 permanent breakpoint at the same place as BPT, make that one the
12881 official one, and the rest as duplicates. Permanent breakpoints
12882 are sorted first for the same address.
12883
12884 Do the same for hardware watchpoints, but also considering the
12885 watchpoint's type (regular/access/read) and length. */
12886
12887 bp_loc_first = NULL;
12888 wp_loc_first = NULL;
12889 awp_loc_first = NULL;
12890 rwp_loc_first = NULL;
12891 ALL_BP_LOCATIONS (loc, locp)
12892 {
12893 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12894 non-NULL. */
348d480f 12895 struct bp_location **loc_first_p;
d3fbdd86 12896 b = loc->owner;
348d480f 12897
6f380991 12898 if (!unduplicated_should_be_inserted (loc)
348d480f 12899 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12900 /* Don't detect duplicate for tracepoint locations because they are
12901 never duplicated. See the comments in field `duplicate' of
12902 `struct bp_location'. */
348d480f 12903 || is_tracepoint (b))
b775012e
LM
12904 {
12905 /* Clear the condition modification flag. */
12906 loc->condition_changed = condition_unchanged;
12907 continue;
12908 }
348d480f
PA
12909
12910 /* Permanent breakpoint should always be inserted. */
12911 if (b->enable_state == bp_permanent && ! loc->inserted)
12912 internal_error (__FILE__, __LINE__,
12913 _("allegedly permanent breakpoint is not "
12914 "actually inserted"));
12915
12916 if (b->type == bp_hardware_watchpoint)
12917 loc_first_p = &wp_loc_first;
12918 else if (b->type == bp_read_watchpoint)
12919 loc_first_p = &rwp_loc_first;
12920 else if (b->type == bp_access_watchpoint)
12921 loc_first_p = &awp_loc_first;
12922 else
12923 loc_first_p = &bp_loc_first;
12924
12925 if (*loc_first_p == NULL
12926 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12927 || !breakpoint_locations_match (loc, *loc_first_p))
12928 {
12929 *loc_first_p = loc;
12930 loc->duplicate = 0;
b775012e
LM
12931
12932 if (is_breakpoint (loc->owner) && loc->condition_changed)
12933 {
12934 loc->needs_update = 1;
12935 /* Clear the condition modification flag. */
12936 loc->condition_changed = condition_unchanged;
12937 }
348d480f
PA
12938 continue;
12939 }
12940
934709f0
PW
12941
12942 /* This and the above ensure the invariant that the first location
12943 is not duplicated, and is the inserted one.
12944 All following are marked as duplicated, and are not inserted. */
12945 if (loc->inserted)
12946 swap_insertion (loc, *loc_first_p);
348d480f
PA
12947 loc->duplicate = 1;
12948
b775012e
LM
12949 /* Clear the condition modification flag. */
12950 loc->condition_changed = condition_unchanged;
12951
348d480f
PA
12952 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12953 && b->enable_state != bp_permanent)
12954 internal_error (__FILE__, __LINE__,
12955 _("another breakpoint was inserted on top of "
12956 "a permanent breakpoint"));
12957 }
12958
a25a5a45 12959 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12960 {
04086b45 12961 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12962 insert_breakpoint_locations ();
12963 else
12964 {
44702360
PA
12965 /* Even though the caller told us to not insert new
12966 locations, we may still need to update conditions on the
12967 target's side of breakpoints that were already inserted
12968 if the target is evaluating breakpoint conditions. We
b775012e
LM
12969 only update conditions for locations that are marked
12970 "needs_update". */
12971 update_inserted_breakpoint_locations ();
12972 }
12973 }
348d480f 12974
04086b45 12975 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12976 download_tracepoint_locations ();
12977
348d480f
PA
12978 do_cleanups (cleanups);
12979}
12980
12981void
12982breakpoint_retire_moribund (void)
12983{
12984 struct bp_location *loc;
12985 int ix;
12986
12987 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12988 if (--(loc->events_till_retirement) == 0)
12989 {
12990 decref_bp_location (&loc);
12991 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12992 --ix;
12993 }
12994}
12995
12996static void
44702360 12997update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12998{
bfd189b1 12999 volatile struct gdb_exception e;
348d480f
PA
13000
13001 TRY_CATCH (e, RETURN_MASK_ERROR)
44702360 13002 update_global_location_list (insert_mode);
348d480f
PA
13003}
13004
13005/* Clear BKP from a BPS. */
13006
13007static void
13008bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
13009{
13010 bpstat bs;
13011
13012 for (bs = bps; bs; bs = bs->next)
13013 if (bs->breakpoint_at == bpt)
13014 {
13015 bs->breakpoint_at = NULL;
13016 bs->old_val = NULL;
13017 /* bs->commands will be freed later. */
13018 }
13019}
13020
13021/* Callback for iterate_over_threads. */
13022static int
13023bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
13024{
13025 struct breakpoint *bpt = data;
13026
13027 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
13028 return 0;
13029}
13030
13031/* Helper for breakpoint and tracepoint breakpoint_ops->mention
13032 callbacks. */
13033
13034static void
13035say_where (struct breakpoint *b)
13036{
13037 struct value_print_options opts;
13038
13039 get_user_print_options (&opts);
13040
13041 /* i18n: cagney/2005-02-11: Below needs to be merged into a
13042 single string. */
13043 if (b->loc == NULL)
13044 {
13045 printf_filtered (_(" (%s) pending."), b->addr_string);
13046 }
13047 else
13048 {
2f202fde 13049 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
13050 {
13051 printf_filtered (" at ");
13052 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
13053 gdb_stdout);
13054 }
2f202fde 13055 if (b->loc->symtab != NULL)
f8eba3c6
TT
13056 {
13057 /* If there is a single location, we can print the location
13058 more nicely. */
13059 if (b->loc->next == NULL)
13060 printf_filtered (": file %s, line %d.",
05cba821
JK
13061 symtab_to_filename_for_display (b->loc->symtab),
13062 b->loc->line_number);
f8eba3c6
TT
13063 else
13064 /* This is not ideal, but each location may have a
13065 different file name, and this at least reflects the
13066 real situation somewhat. */
13067 printf_filtered (": %s.", b->addr_string);
13068 }
348d480f
PA
13069
13070 if (b->loc->next)
13071 {
13072 struct bp_location *loc = b->loc;
13073 int n = 0;
13074 for (; loc; loc = loc->next)
13075 ++n;
13076 printf_filtered (" (%d locations)", n);
13077 }
13078 }
13079}
13080
348d480f
PA
13081/* Default bp_location_ops methods. */
13082
13083static void
13084bp_location_dtor (struct bp_location *self)
13085{
13086 xfree (self->cond);
b775012e
LM
13087 if (self->cond_bytecode)
13088 free_agent_expr (self->cond_bytecode);
348d480f 13089 xfree (self->function_name);
8b4f3082
PA
13090
13091 VEC_free (agent_expr_p, self->target_info.conditions);
13092 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
13093}
13094
13095static const struct bp_location_ops bp_location_ops =
13096{
13097 bp_location_dtor
13098};
13099
2060206e
PA
13100/* Default breakpoint_ops methods all breakpoint_ops ultimately
13101 inherit from. */
348d480f 13102
2060206e
PA
13103static void
13104base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
13105{
13106 decref_counted_command_line (&self->commands);
13107 xfree (self->cond_string);
fb81d016 13108 xfree (self->extra_string);
348d480f 13109 xfree (self->addr_string);
f8eba3c6 13110 xfree (self->filter);
348d480f 13111 xfree (self->addr_string_range_end);
348d480f
PA
13112}
13113
2060206e
PA
13114static struct bp_location *
13115base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
13116{
13117 struct bp_location *loc;
13118
13119 loc = XNEW (struct bp_location);
13120 init_bp_location (loc, &bp_location_ops, self);
13121 return loc;
13122}
13123
2060206e
PA
13124static void
13125base_breakpoint_re_set (struct breakpoint *b)
13126{
13127 /* Nothing to re-set. */
13128}
13129
13130#define internal_error_pure_virtual_called() \
13131 gdb_assert_not_reached ("pure virtual function called")
13132
13133static int
13134base_breakpoint_insert_location (struct bp_location *bl)
13135{
13136 internal_error_pure_virtual_called ();
13137}
13138
13139static int
13140base_breakpoint_remove_location (struct bp_location *bl)
13141{
13142 internal_error_pure_virtual_called ();
13143}
13144
13145static int
13146base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13147 struct address_space *aspace,
09ac7c10
TT
13148 CORE_ADDR bp_addr,
13149 const struct target_waitstatus *ws)
2060206e
PA
13150{
13151 internal_error_pure_virtual_called ();
13152}
13153
13154static void
13155base_breakpoint_check_status (bpstat bs)
13156{
13157 /* Always stop. */
13158}
13159
13160/* A "works_in_software_mode" breakpoint_ops method that just internal
13161 errors. */
13162
13163static int
13164base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13165{
13166 internal_error_pure_virtual_called ();
13167}
13168
13169/* A "resources_needed" breakpoint_ops method that just internal
13170 errors. */
13171
13172static int
13173base_breakpoint_resources_needed (const struct bp_location *bl)
13174{
13175 internal_error_pure_virtual_called ();
13176}
13177
13178static enum print_stop_action
13179base_breakpoint_print_it (bpstat bs)
13180{
13181 internal_error_pure_virtual_called ();
13182}
13183
13184static void
13185base_breakpoint_print_one_detail (const struct breakpoint *self,
13186 struct ui_out *uiout)
13187{
13188 /* nothing */
13189}
13190
13191static void
13192base_breakpoint_print_mention (struct breakpoint *b)
13193{
13194 internal_error_pure_virtual_called ();
13195}
13196
13197static void
13198base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13199{
13200 internal_error_pure_virtual_called ();
13201}
13202
983af33b
SDJ
13203static void
13204base_breakpoint_create_sals_from_address (char **arg,
13205 struct linespec_result *canonical,
13206 enum bptype type_wanted,
13207 char *addr_start,
13208 char **copy_arg)
13209{
13210 internal_error_pure_virtual_called ();
13211}
13212
13213static void
13214base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13215 struct linespec_result *c,
983af33b 13216 char *cond_string,
e7e0cddf 13217 char *extra_string,
983af33b
SDJ
13218 enum bptype type_wanted,
13219 enum bpdisp disposition,
13220 int thread,
13221 int task, int ignore_count,
13222 const struct breakpoint_ops *o,
13223 int from_tty, int enabled,
44f238bb 13224 int internal, unsigned flags)
983af33b
SDJ
13225{
13226 internal_error_pure_virtual_called ();
13227}
13228
13229static void
13230base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13231 struct symtabs_and_lines *sals)
13232{
13233 internal_error_pure_virtual_called ();
13234}
13235
ab04a2af
TT
13236/* The default 'explains_signal' method. */
13237
47591c29 13238static int
427cd150 13239base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13240{
47591c29 13241 return 1;
ab04a2af
TT
13242}
13243
9d6e6e84
HZ
13244/* The default "after_condition_true" method. */
13245
13246static void
13247base_breakpoint_after_condition_true (struct bpstats *bs)
13248{
13249 /* Nothing to do. */
13250}
13251
ab04a2af 13252struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13253{
13254 base_breakpoint_dtor,
13255 base_breakpoint_allocate_location,
13256 base_breakpoint_re_set,
13257 base_breakpoint_insert_location,
13258 base_breakpoint_remove_location,
13259 base_breakpoint_breakpoint_hit,
13260 base_breakpoint_check_status,
13261 base_breakpoint_resources_needed,
13262 base_breakpoint_works_in_software_mode,
13263 base_breakpoint_print_it,
13264 NULL,
13265 base_breakpoint_print_one_detail,
13266 base_breakpoint_print_mention,
983af33b
SDJ
13267 base_breakpoint_print_recreate,
13268 base_breakpoint_create_sals_from_address,
13269 base_breakpoint_create_breakpoints_sal,
13270 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13271 base_breakpoint_explains_signal,
13272 base_breakpoint_after_condition_true,
2060206e
PA
13273};
13274
13275/* Default breakpoint_ops methods. */
13276
13277static void
348d480f
PA
13278bkpt_re_set (struct breakpoint *b)
13279{
06edf0c0
PA
13280 /* FIXME: is this still reachable? */
13281 if (b->addr_string == NULL)
13282 {
13283 /* Anything without a string can't be re-set. */
348d480f 13284 delete_breakpoint (b);
06edf0c0 13285 return;
348d480f 13286 }
06edf0c0
PA
13287
13288 breakpoint_re_set_default (b);
348d480f
PA
13289}
13290
2060206e 13291static int
348d480f
PA
13292bkpt_insert_location (struct bp_location *bl)
13293{
13294 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13295 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13296 else
7c16b83e 13297 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13298}
13299
2060206e 13300static int
348d480f
PA
13301bkpt_remove_location (struct bp_location *bl)
13302{
13303 if (bl->loc_type == bp_loc_hardware_breakpoint)
13304 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13305 else
7c16b83e 13306 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13307}
13308
2060206e 13309static int
348d480f 13310bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13311 struct address_space *aspace, CORE_ADDR bp_addr,
13312 const struct target_waitstatus *ws)
348d480f 13313{
09ac7c10 13314 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13315 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13316 return 0;
13317
348d480f
PA
13318 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13319 aspace, bp_addr))
13320 return 0;
13321
13322 if (overlay_debugging /* unmapped overlay section */
13323 && section_is_overlay (bl->section)
13324 && !section_is_mapped (bl->section))
13325 return 0;
13326
13327 return 1;
13328}
13329
cd1608cc
PA
13330static int
13331dprintf_breakpoint_hit (const struct bp_location *bl,
13332 struct address_space *aspace, CORE_ADDR bp_addr,
13333 const struct target_waitstatus *ws)
13334{
13335 if (dprintf_style == dprintf_style_agent
13336 && target_can_run_breakpoint_commands ())
13337 {
13338 /* An agent-style dprintf never causes a stop. If we see a trap
13339 for this address it must be for a breakpoint that happens to
13340 be set at the same address. */
13341 return 0;
13342 }
13343
13344 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13345}
13346
2060206e 13347static int
348d480f
PA
13348bkpt_resources_needed (const struct bp_location *bl)
13349{
13350 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13351
13352 return 1;
13353}
13354
2060206e 13355static enum print_stop_action
348d480f
PA
13356bkpt_print_it (bpstat bs)
13357{
348d480f
PA
13358 struct breakpoint *b;
13359 const struct bp_location *bl;
001c8c33 13360 int bp_temp;
79a45e25 13361 struct ui_out *uiout = current_uiout;
348d480f
PA
13362
13363 gdb_assert (bs->bp_location_at != NULL);
13364
13365 bl = bs->bp_location_at;
13366 b = bs->breakpoint_at;
13367
001c8c33
PA
13368 bp_temp = b->disposition == disp_del;
13369 if (bl->address != bl->requested_address)
13370 breakpoint_adjustment_warning (bl->requested_address,
13371 bl->address,
13372 b->number, 1);
13373 annotate_breakpoint (b->number);
13374 if (bp_temp)
13375 ui_out_text (uiout, "\nTemporary breakpoint ");
13376 else
13377 ui_out_text (uiout, "\nBreakpoint ");
13378 if (ui_out_is_mi_like_p (uiout))
348d480f 13379 {
001c8c33
PA
13380 ui_out_field_string (uiout, "reason",
13381 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13382 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13383 }
001c8c33
PA
13384 ui_out_field_int (uiout, "bkptno", b->number);
13385 ui_out_text (uiout, ", ");
06edf0c0 13386
001c8c33 13387 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13388}
13389
2060206e 13390static void
06edf0c0
PA
13391bkpt_print_mention (struct breakpoint *b)
13392{
79a45e25 13393 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13394 return;
13395
13396 switch (b->type)
13397 {
13398 case bp_breakpoint:
13399 case bp_gnu_ifunc_resolver:
13400 if (b->disposition == disp_del)
13401 printf_filtered (_("Temporary breakpoint"));
13402 else
13403 printf_filtered (_("Breakpoint"));
13404 printf_filtered (_(" %d"), b->number);
13405 if (b->type == bp_gnu_ifunc_resolver)
13406 printf_filtered (_(" at gnu-indirect-function resolver"));
13407 break;
13408 case bp_hardware_breakpoint:
13409 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13410 break;
e7e0cddf
SS
13411 case bp_dprintf:
13412 printf_filtered (_("Dprintf %d"), b->number);
13413 break;
06edf0c0
PA
13414 }
13415
13416 say_where (b);
13417}
13418
2060206e 13419static void
06edf0c0
PA
13420bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13421{
13422 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13423 fprintf_unfiltered (fp, "tbreak");
13424 else if (tp->type == bp_breakpoint)
13425 fprintf_unfiltered (fp, "break");
13426 else if (tp->type == bp_hardware_breakpoint
13427 && tp->disposition == disp_del)
13428 fprintf_unfiltered (fp, "thbreak");
13429 else if (tp->type == bp_hardware_breakpoint)
13430 fprintf_unfiltered (fp, "hbreak");
13431 else
13432 internal_error (__FILE__, __LINE__,
13433 _("unhandled breakpoint type %d"), (int) tp->type);
13434
2060206e 13435 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13436 print_recreate_thread (tp, fp);
06edf0c0
PA
13437}
13438
983af33b
SDJ
13439static void
13440bkpt_create_sals_from_address (char **arg,
13441 struct linespec_result *canonical,
13442 enum bptype type_wanted,
13443 char *addr_start, char **copy_arg)
13444{
13445 create_sals_from_address_default (arg, canonical, type_wanted,
13446 addr_start, copy_arg);
13447}
13448
13449static void
13450bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13451 struct linespec_result *canonical,
983af33b 13452 char *cond_string,
e7e0cddf 13453 char *extra_string,
983af33b
SDJ
13454 enum bptype type_wanted,
13455 enum bpdisp disposition,
13456 int thread,
13457 int task, int ignore_count,
13458 const struct breakpoint_ops *ops,
13459 int from_tty, int enabled,
44f238bb 13460 int internal, unsigned flags)
983af33b 13461{
023fa29b 13462 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13463 cond_string, extra_string,
13464 type_wanted,
983af33b
SDJ
13465 disposition, thread, task,
13466 ignore_count, ops, from_tty,
44f238bb 13467 enabled, internal, flags);
983af33b
SDJ
13468}
13469
13470static void
13471bkpt_decode_linespec (struct breakpoint *b, char **s,
13472 struct symtabs_and_lines *sals)
13473{
13474 decode_linespec_default (b, s, sals);
13475}
13476
06edf0c0
PA
13477/* Virtual table for internal breakpoints. */
13478
13479static void
13480internal_bkpt_re_set (struct breakpoint *b)
13481{
13482 switch (b->type)
13483 {
13484 /* Delete overlay event and longjmp master breakpoints; they
13485 will be reset later by breakpoint_re_set. */
13486 case bp_overlay_event:
13487 case bp_longjmp_master:
13488 case bp_std_terminate_master:
13489 case bp_exception_master:
13490 delete_breakpoint (b);
13491 break;
13492
13493 /* This breakpoint is special, it's set up when the inferior
13494 starts and we really don't want to touch it. */
13495 case bp_shlib_event:
13496
13497 /* Like bp_shlib_event, this breakpoint type is special. Once
13498 it is set up, we do not want to touch it. */
13499 case bp_thread_event:
13500 break;
13501 }
13502}
13503
13504static void
13505internal_bkpt_check_status (bpstat bs)
13506{
a9b3a50f
PA
13507 if (bs->breakpoint_at->type == bp_shlib_event)
13508 {
13509 /* If requested, stop when the dynamic linker notifies GDB of
13510 events. This allows the user to get control and place
13511 breakpoints in initializer routines for dynamically loaded
13512 objects (among other things). */
13513 bs->stop = stop_on_solib_events;
13514 bs->print = stop_on_solib_events;
13515 }
13516 else
13517 bs->stop = 0;
06edf0c0
PA
13518}
13519
13520static enum print_stop_action
13521internal_bkpt_print_it (bpstat bs)
13522{
06edf0c0 13523 struct breakpoint *b;
06edf0c0 13524
06edf0c0
PA
13525 b = bs->breakpoint_at;
13526
06edf0c0
PA
13527 switch (b->type)
13528 {
348d480f
PA
13529 case bp_shlib_event:
13530 /* Did we stop because the user set the stop_on_solib_events
13531 variable? (If so, we report this as a generic, "Stopped due
13532 to shlib event" message.) */
edcc5120 13533 print_solib_event (0);
348d480f
PA
13534 break;
13535
13536 case bp_thread_event:
13537 /* Not sure how we will get here.
13538 GDB should not stop for these breakpoints. */
13539 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13540 break;
13541
13542 case bp_overlay_event:
13543 /* By analogy with the thread event, GDB should not stop for these. */
13544 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13545 break;
13546
13547 case bp_longjmp_master:
13548 /* These should never be enabled. */
13549 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13550 break;
13551
13552 case bp_std_terminate_master:
13553 /* These should never be enabled. */
13554 printf_filtered (_("std::terminate Master Breakpoint: "
13555 "gdb should not stop!\n"));
348d480f
PA
13556 break;
13557
13558 case bp_exception_master:
13559 /* These should never be enabled. */
13560 printf_filtered (_("Exception Master Breakpoint: "
13561 "gdb should not stop!\n"));
06edf0c0
PA
13562 break;
13563 }
13564
001c8c33 13565 return PRINT_NOTHING;
06edf0c0
PA
13566}
13567
13568static void
13569internal_bkpt_print_mention (struct breakpoint *b)
13570{
13571 /* Nothing to mention. These breakpoints are internal. */
13572}
13573
06edf0c0
PA
13574/* Virtual table for momentary breakpoints */
13575
13576static void
13577momentary_bkpt_re_set (struct breakpoint *b)
13578{
13579 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13580 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13581 Otherwise these should have been blown away via the cleanup chain
13582 or by breakpoint_init_inferior when we rerun the executable. */
13583}
13584
13585static void
13586momentary_bkpt_check_status (bpstat bs)
13587{
13588 /* Nothing. The point of these breakpoints is causing a stop. */
13589}
13590
13591static enum print_stop_action
13592momentary_bkpt_print_it (bpstat bs)
13593{
79a45e25
PA
13594 struct ui_out *uiout = current_uiout;
13595
001c8c33 13596 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13597 {
001c8c33 13598 struct breakpoint *b = bs->breakpoint_at;
348d480f 13599
001c8c33
PA
13600 switch (b->type)
13601 {
13602 case bp_finish:
13603 ui_out_field_string
13604 (uiout, "reason",
13605 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13606 break;
348d480f 13607
001c8c33
PA
13608 case bp_until:
13609 ui_out_field_string
13610 (uiout, "reason",
13611 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13612 break;
13613 }
348d480f
PA
13614 }
13615
001c8c33 13616 return PRINT_UNKNOWN;
348d480f
PA
13617}
13618
06edf0c0
PA
13619static void
13620momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13621{
06edf0c0 13622 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13623}
13624
e2e4d78b
JK
13625/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13626
13627 It gets cleared already on the removal of the first one of such placed
13628 breakpoints. This is OK as they get all removed altogether. */
13629
13630static void
13631longjmp_bkpt_dtor (struct breakpoint *self)
13632{
13633 struct thread_info *tp = find_thread_id (self->thread);
13634
13635 if (tp)
13636 tp->initiating_frame = null_frame_id;
13637
13638 momentary_breakpoint_ops.dtor (self);
13639}
13640
55aa24fb
SDJ
13641/* Specific methods for probe breakpoints. */
13642
13643static int
13644bkpt_probe_insert_location (struct bp_location *bl)
13645{
13646 int v = bkpt_insert_location (bl);
13647
13648 if (v == 0)
13649 {
13650 /* The insertion was successful, now let's set the probe's semaphore
13651 if needed. */
0ea5cda8
SDJ
13652 if (bl->probe.probe->pops->set_semaphore != NULL)
13653 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13654 bl->probe.objfile,
13655 bl->gdbarch);
55aa24fb
SDJ
13656 }
13657
13658 return v;
13659}
13660
13661static int
13662bkpt_probe_remove_location (struct bp_location *bl)
13663{
13664 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13665 if (bl->probe.probe->pops->clear_semaphore != NULL)
13666 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13667 bl->probe.objfile,
13668 bl->gdbarch);
55aa24fb
SDJ
13669
13670 return bkpt_remove_location (bl);
13671}
13672
13673static void
13674bkpt_probe_create_sals_from_address (char **arg,
13675 struct linespec_result *canonical,
13676 enum bptype type_wanted,
13677 char *addr_start, char **copy_arg)
13678{
13679 struct linespec_sals lsal;
13680
13681 lsal.sals = parse_probes (arg, canonical);
13682
13683 *copy_arg = xstrdup (canonical->addr_string);
13684 lsal.canonical = xstrdup (*copy_arg);
13685
13686 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13687}
13688
13689static void
13690bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13691 struct symtabs_and_lines *sals)
13692{
13693 *sals = parse_probes (s, NULL);
13694 if (!sals->sals)
13695 error (_("probe not found"));
13696}
13697
348d480f 13698/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13699
348d480f
PA
13700static void
13701tracepoint_re_set (struct breakpoint *b)
13702{
13703 breakpoint_re_set_default (b);
13704}
876fa593 13705
348d480f
PA
13706static int
13707tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13708 struct address_space *aspace, CORE_ADDR bp_addr,
13709 const struct target_waitstatus *ws)
348d480f
PA
13710{
13711 /* By definition, the inferior does not report stops at
13712 tracepoints. */
13713 return 0;
74960c60
VP
13714}
13715
13716static void
348d480f
PA
13717tracepoint_print_one_detail (const struct breakpoint *self,
13718 struct ui_out *uiout)
74960c60 13719{
d9b3f62e
PA
13720 struct tracepoint *tp = (struct tracepoint *) self;
13721 if (tp->static_trace_marker_id)
348d480f
PA
13722 {
13723 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13724
348d480f
PA
13725 ui_out_text (uiout, "\tmarker id is ");
13726 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13727 tp->static_trace_marker_id);
348d480f
PA
13728 ui_out_text (uiout, "\n");
13729 }
0d381245
VP
13730}
13731
a474d7c2 13732static void
348d480f 13733tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13734{
79a45e25 13735 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13736 return;
cc59ec59 13737
348d480f
PA
13738 switch (b->type)
13739 {
13740 case bp_tracepoint:
13741 printf_filtered (_("Tracepoint"));
13742 printf_filtered (_(" %d"), b->number);
13743 break;
13744 case bp_fast_tracepoint:
13745 printf_filtered (_("Fast tracepoint"));
13746 printf_filtered (_(" %d"), b->number);
13747 break;
13748 case bp_static_tracepoint:
13749 printf_filtered (_("Static tracepoint"));
13750 printf_filtered (_(" %d"), b->number);
13751 break;
13752 default:
13753 internal_error (__FILE__, __LINE__,
13754 _("unhandled tracepoint type %d"), (int) b->type);
13755 }
13756
13757 say_where (b);
a474d7c2
PA
13758}
13759
348d480f 13760static void
d9b3f62e 13761tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13762{
d9b3f62e
PA
13763 struct tracepoint *tp = (struct tracepoint *) self;
13764
13765 if (self->type == bp_fast_tracepoint)
348d480f 13766 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13767 if (self->type == bp_static_tracepoint)
348d480f 13768 fprintf_unfiltered (fp, "strace");
d9b3f62e 13769 else if (self->type == bp_tracepoint)
348d480f
PA
13770 fprintf_unfiltered (fp, "trace");
13771 else
13772 internal_error (__FILE__, __LINE__,
d9b3f62e 13773 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13774
d9b3f62e
PA
13775 fprintf_unfiltered (fp, " %s", self->addr_string);
13776 print_recreate_thread (self, fp);
13777
13778 if (tp->pass_count)
13779 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13780}
13781
983af33b
SDJ
13782static void
13783tracepoint_create_sals_from_address (char **arg,
13784 struct linespec_result *canonical,
13785 enum bptype type_wanted,
13786 char *addr_start, char **copy_arg)
13787{
13788 create_sals_from_address_default (arg, canonical, type_wanted,
13789 addr_start, copy_arg);
13790}
13791
13792static void
13793tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13794 struct linespec_result *canonical,
983af33b 13795 char *cond_string,
e7e0cddf 13796 char *extra_string,
983af33b
SDJ
13797 enum bptype type_wanted,
13798 enum bpdisp disposition,
13799 int thread,
13800 int task, int ignore_count,
13801 const struct breakpoint_ops *ops,
13802 int from_tty, int enabled,
44f238bb 13803 int internal, unsigned flags)
983af33b 13804{
023fa29b 13805 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13806 cond_string, extra_string,
13807 type_wanted,
983af33b
SDJ
13808 disposition, thread, task,
13809 ignore_count, ops, from_tty,
44f238bb 13810 enabled, internal, flags);
983af33b
SDJ
13811}
13812
13813static void
13814tracepoint_decode_linespec (struct breakpoint *b, char **s,
13815 struct symtabs_and_lines *sals)
13816{
13817 decode_linespec_default (b, s, sals);
13818}
13819
2060206e 13820struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13821
55aa24fb
SDJ
13822/* The breakpoint_ops structure to be use on tracepoints placed in a
13823 static probe. */
13824
13825static void
13826tracepoint_probe_create_sals_from_address (char **arg,
13827 struct linespec_result *canonical,
13828 enum bptype type_wanted,
13829 char *addr_start, char **copy_arg)
13830{
13831 /* We use the same method for breakpoint on probes. */
13832 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13833 addr_start, copy_arg);
13834}
13835
13836static void
13837tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13838 struct symtabs_and_lines *sals)
13839{
13840 /* We use the same method for breakpoint on probes. */
13841 bkpt_probe_decode_linespec (b, s, sals);
13842}
13843
13844static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13845
5c2b4418
HZ
13846/* Dprintf breakpoint_ops methods. */
13847
13848static void
13849dprintf_re_set (struct breakpoint *b)
13850{
13851 breakpoint_re_set_default (b);
13852
13853 /* This breakpoint could have been pending, and be resolved now, and
13854 if so, we should now have the extra string. If we don't, the
13855 dprintf was malformed when created, but we couldn't tell because
13856 we can't extract the extra string until the location is
13857 resolved. */
13858 if (b->loc != NULL && b->extra_string == NULL)
13859 error (_("Format string required"));
13860
13861 /* 1 - connect to target 1, that can run breakpoint commands.
13862 2 - create a dprintf, which resolves fine.
13863 3 - disconnect from target 1
13864 4 - connect to target 2, that can NOT run breakpoint commands.
13865
13866 After steps #3/#4, you'll want the dprintf command list to
13867 be updated, because target 1 and 2 may well return different
13868 answers for target_can_run_breakpoint_commands().
13869 Given absence of finer grained resetting, we get to do
13870 it all the time. */
13871 if (b->extra_string != NULL)
13872 update_dprintf_command_list (b);
13873}
13874
2d9442cc
HZ
13875/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13876
13877static void
13878dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13879{
13880 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13881 tp->extra_string);
13882 print_recreate_thread (tp, fp);
13883}
13884
9d6e6e84
HZ
13885/* Implement the "after_condition_true" breakpoint_ops method for
13886 dprintf.
13887
13888 dprintf's are implemented with regular commands in their command
13889 list, but we run the commands here instead of before presenting the
13890 stop to the user, as dprintf's don't actually cause a stop. This
13891 also makes it so that the commands of multiple dprintfs at the same
13892 address are all handled. */
13893
13894static void
13895dprintf_after_condition_true (struct bpstats *bs)
13896{
13897 struct cleanup *old_chain;
13898 struct bpstats tmp_bs = { NULL };
13899 struct bpstats *tmp_bs_p = &tmp_bs;
13900
13901 /* dprintf's never cause a stop. This wasn't set in the
13902 check_status hook instead because that would make the dprintf's
13903 condition not be evaluated. */
13904 bs->stop = 0;
13905
13906 /* Run the command list here. Take ownership of it instead of
13907 copying. We never want these commands to run later in
13908 bpstat_do_actions, if a breakpoint that causes a stop happens to
13909 be set at same address as this dprintf, or even if running the
13910 commands here throws. */
13911 tmp_bs.commands = bs->commands;
13912 bs->commands = NULL;
13913 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13914
13915 bpstat_do_actions_1 (&tmp_bs_p);
13916
13917 /* 'tmp_bs.commands' will usually be NULL by now, but
13918 bpstat_do_actions_1 may return early without processing the whole
13919 list. */
13920 do_cleanups (old_chain);
13921}
13922
983af33b
SDJ
13923/* The breakpoint_ops structure to be used on static tracepoints with
13924 markers (`-m'). */
13925
13926static void
13927strace_marker_create_sals_from_address (char **arg,
13928 struct linespec_result *canonical,
13929 enum bptype type_wanted,
13930 char *addr_start, char **copy_arg)
13931{
13932 struct linespec_sals lsal;
13933
13934 lsal.sals = decode_static_tracepoint_spec (arg);
13935
13936 *copy_arg = savestring (addr_start, *arg - addr_start);
13937
13938 canonical->addr_string = xstrdup (*copy_arg);
13939 lsal.canonical = xstrdup (*copy_arg);
13940 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13941}
13942
13943static void
13944strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13945 struct linespec_result *canonical,
983af33b 13946 char *cond_string,
e7e0cddf 13947 char *extra_string,
983af33b
SDJ
13948 enum bptype type_wanted,
13949 enum bpdisp disposition,
13950 int thread,
13951 int task, int ignore_count,
13952 const struct breakpoint_ops *ops,
13953 int from_tty, int enabled,
44f238bb 13954 int internal, unsigned flags)
983af33b
SDJ
13955{
13956 int i;
52d361e1
YQ
13957 struct linespec_sals *lsal = VEC_index (linespec_sals,
13958 canonical->sals, 0);
983af33b
SDJ
13959
13960 /* If the user is creating a static tracepoint by marker id
13961 (strace -m MARKER_ID), then store the sals index, so that
13962 breakpoint_re_set can try to match up which of the newly
13963 found markers corresponds to this one, and, don't try to
13964 expand multiple locations for each sal, given than SALS
13965 already should contain all sals for MARKER_ID. */
13966
13967 for (i = 0; i < lsal->sals.nelts; ++i)
13968 {
13969 struct symtabs_and_lines expanded;
13970 struct tracepoint *tp;
13971 struct cleanup *old_chain;
13972 char *addr_string;
13973
13974 expanded.nelts = 1;
13975 expanded.sals = &lsal->sals.sals[i];
13976
13977 addr_string = xstrdup (canonical->addr_string);
13978 old_chain = make_cleanup (xfree, addr_string);
13979
13980 tp = XCNEW (struct tracepoint);
13981 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13982 addr_string, NULL,
e7e0cddf
SS
13983 cond_string, extra_string,
13984 type_wanted, disposition,
983af33b 13985 thread, task, ignore_count, ops,
44f238bb 13986 from_tty, enabled, internal, flags,
983af33b
SDJ
13987 canonical->special_display);
13988 /* Given that its possible to have multiple markers with
13989 the same string id, if the user is creating a static
13990 tracepoint by marker id ("strace -m MARKER_ID"), then
13991 store the sals index, so that breakpoint_re_set can
13992 try to match up which of the newly found markers
13993 corresponds to this one */
13994 tp->static_trace_marker_id_idx = i;
13995
13996 install_breakpoint (internal, &tp->base, 0);
13997
13998 discard_cleanups (old_chain);
13999 }
14000}
14001
14002static void
14003strace_marker_decode_linespec (struct breakpoint *b, char **s,
14004 struct symtabs_and_lines *sals)
14005{
14006 struct tracepoint *tp = (struct tracepoint *) b;
14007
14008 *sals = decode_static_tracepoint_spec (s);
14009 if (sals->nelts > tp->static_trace_marker_id_idx)
14010 {
14011 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
14012 sals->nelts = 1;
14013 }
14014 else
14015 error (_("marker %s not found"), tp->static_trace_marker_id);
14016}
14017
14018static struct breakpoint_ops strace_marker_breakpoint_ops;
14019
14020static int
14021strace_marker_p (struct breakpoint *b)
14022{
14023 return b->ops == &strace_marker_breakpoint_ops;
14024}
14025
53a5351d 14026/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 14027 structures. */
c906108c
SS
14028
14029void
fba45db2 14030delete_breakpoint (struct breakpoint *bpt)
c906108c 14031{
52f0bd74 14032 struct breakpoint *b;
c906108c 14033
8a3fe4f8 14034 gdb_assert (bpt != NULL);
c906108c 14035
4a64f543
MS
14036 /* Has this bp already been deleted? This can happen because
14037 multiple lists can hold pointers to bp's. bpstat lists are
14038 especial culprits.
14039
14040 One example of this happening is a watchpoint's scope bp. When
14041 the scope bp triggers, we notice that the watchpoint is out of
14042 scope, and delete it. We also delete its scope bp. But the
14043 scope bp is marked "auto-deleting", and is already on a bpstat.
14044 That bpstat is then checked for auto-deleting bp's, which are
14045 deleted.
14046
14047 A real solution to this problem might involve reference counts in
14048 bp's, and/or giving them pointers back to their referencing
14049 bpstat's, and teaching delete_breakpoint to only free a bp's
14050 storage when no more references were extent. A cheaper bandaid
14051 was chosen. */
c906108c
SS
14052 if (bpt->type == bp_none)
14053 return;
14054
4a64f543
MS
14055 /* At least avoid this stale reference until the reference counting
14056 of breakpoints gets resolved. */
d0fb5eae 14057 if (bpt->related_breakpoint != bpt)
e5a0a904 14058 {
d0fb5eae 14059 struct breakpoint *related;
3a5c3e22 14060 struct watchpoint *w;
d0fb5eae
JK
14061
14062 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 14063 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 14064 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
14065 w = (struct watchpoint *) bpt;
14066 else
14067 w = NULL;
14068 if (w != NULL)
14069 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
14070
14071 /* Unlink bpt from the bpt->related_breakpoint ring. */
14072 for (related = bpt; related->related_breakpoint != bpt;
14073 related = related->related_breakpoint);
14074 related->related_breakpoint = bpt->related_breakpoint;
14075 bpt->related_breakpoint = bpt;
e5a0a904
JK
14076 }
14077
a9634178
TJB
14078 /* watch_command_1 creates a watchpoint but only sets its number if
14079 update_watchpoint succeeds in creating its bp_locations. If there's
14080 a problem in that process, we'll be asked to delete the half-created
14081 watchpoint. In that case, don't announce the deletion. */
14082 if (bpt->number)
14083 observer_notify_breakpoint_deleted (bpt);
c906108c 14084
c906108c
SS
14085 if (breakpoint_chain == bpt)
14086 breakpoint_chain = bpt->next;
14087
c906108c
SS
14088 ALL_BREAKPOINTS (b)
14089 if (b->next == bpt)
c5aa993b
JM
14090 {
14091 b->next = bpt->next;
14092 break;
14093 }
c906108c 14094
f431efe5
PA
14095 /* Be sure no bpstat's are pointing at the breakpoint after it's
14096 been freed. */
14097 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 14098 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
14099 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14100 commands are associated with the bpstat; if we remove it here,
14101 then the later call to bpstat_do_actions (&stop_bpstat); in
14102 event-top.c won't do anything, and temporary breakpoints with
14103 commands won't work. */
14104
14105 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14106
4a64f543
MS
14107 /* Now that breakpoint is removed from breakpoint list, update the
14108 global location list. This will remove locations that used to
14109 belong to this breakpoint. Do this before freeing the breakpoint
14110 itself, since remove_breakpoint looks at location's owner. It
14111 might be better design to have location completely
14112 self-contained, but it's not the case now. */
44702360 14113 update_global_location_list (UGLL_DONT_INSERT);
74960c60 14114
348d480f 14115 bpt->ops->dtor (bpt);
4a64f543
MS
14116 /* On the chance that someone will soon try again to delete this
14117 same bp, we mark it as deleted before freeing its storage. */
c906108c 14118 bpt->type = bp_none;
b8c9b27d 14119 xfree (bpt);
c906108c
SS
14120}
14121
4d6140d9
AC
14122static void
14123do_delete_breakpoint_cleanup (void *b)
14124{
14125 delete_breakpoint (b);
14126}
14127
14128struct cleanup *
14129make_cleanup_delete_breakpoint (struct breakpoint *b)
14130{
14131 return make_cleanup (do_delete_breakpoint_cleanup, b);
14132}
14133
51be5b68
PA
14134/* Iterator function to call a user-provided callback function once
14135 for each of B and its related breakpoints. */
14136
14137static void
14138iterate_over_related_breakpoints (struct breakpoint *b,
14139 void (*function) (struct breakpoint *,
14140 void *),
14141 void *data)
14142{
14143 struct breakpoint *related;
14144
14145 related = b;
14146 do
14147 {
14148 struct breakpoint *next;
14149
14150 /* FUNCTION may delete RELATED. */
14151 next = related->related_breakpoint;
14152
14153 if (next == related)
14154 {
14155 /* RELATED is the last ring entry. */
14156 function (related, data);
14157
14158 /* FUNCTION may have deleted it, so we'd never reach back to
14159 B. There's nothing left to do anyway, so just break
14160 out. */
14161 break;
14162 }
14163 else
14164 function (related, data);
14165
14166 related = next;
14167 }
14168 while (related != b);
14169}
95a42b64
TT
14170
14171static void
14172do_delete_breakpoint (struct breakpoint *b, void *ignore)
14173{
14174 delete_breakpoint (b);
14175}
14176
51be5b68
PA
14177/* A callback for map_breakpoint_numbers that calls
14178 delete_breakpoint. */
14179
14180static void
14181do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14182{
14183 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14184}
14185
c906108c 14186void
fba45db2 14187delete_command (char *arg, int from_tty)
c906108c 14188{
35df4500 14189 struct breakpoint *b, *b_tmp;
c906108c 14190
ea9365bb
TT
14191 dont_repeat ();
14192
c906108c
SS
14193 if (arg == 0)
14194 {
14195 int breaks_to_delete = 0;
14196
46c6471b
PA
14197 /* Delete all breakpoints if no argument. Do not delete
14198 internal breakpoints, these have to be deleted with an
14199 explicit breakpoint number argument. */
c5aa993b 14200 ALL_BREAKPOINTS (b)
46c6471b 14201 if (user_breakpoint_p (b))
973d738b
DJ
14202 {
14203 breaks_to_delete = 1;
14204 break;
14205 }
c906108c
SS
14206
14207 /* Ask user only if there are some breakpoints to delete. */
14208 if (!from_tty
e2e0b3e5 14209 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14210 {
35df4500 14211 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14212 if (user_breakpoint_p (b))
c5aa993b 14213 delete_breakpoint (b);
c906108c
SS
14214 }
14215 }
14216 else
51be5b68 14217 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14218}
14219
0d381245
VP
14220static int
14221all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14222{
0d381245 14223 for (; loc; loc = loc->next)
8645ff69
UW
14224 if (!loc->shlib_disabled
14225 && !loc->pspace->executing_startup)
0d381245
VP
14226 return 0;
14227 return 1;
fe3f5fa8
VP
14228}
14229
776592bf
DE
14230/* Subroutine of update_breakpoint_locations to simplify it.
14231 Return non-zero if multiple fns in list LOC have the same name.
14232 Null names are ignored. */
14233
14234static int
14235ambiguous_names_p (struct bp_location *loc)
14236{
14237 struct bp_location *l;
14238 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14239 (int (*) (const void *,
14240 const void *)) streq,
776592bf
DE
14241 NULL, xcalloc, xfree);
14242
14243 for (l = loc; l != NULL; l = l->next)
14244 {
14245 const char **slot;
14246 const char *name = l->function_name;
14247
14248 /* Allow for some names to be NULL, ignore them. */
14249 if (name == NULL)
14250 continue;
14251
14252 slot = (const char **) htab_find_slot (htab, (const void *) name,
14253 INSERT);
4a64f543
MS
14254 /* NOTE: We can assume slot != NULL here because xcalloc never
14255 returns NULL. */
776592bf
DE
14256 if (*slot != NULL)
14257 {
14258 htab_delete (htab);
14259 return 1;
14260 }
14261 *slot = name;
14262 }
14263
14264 htab_delete (htab);
14265 return 0;
14266}
14267
0fb4aa4b
PA
14268/* When symbols change, it probably means the sources changed as well,
14269 and it might mean the static tracepoint markers are no longer at
14270 the same address or line numbers they used to be at last we
14271 checked. Losing your static tracepoints whenever you rebuild is
14272 undesirable. This function tries to resync/rematch gdb static
14273 tracepoints with the markers on the target, for static tracepoints
14274 that have not been set by marker id. Static tracepoint that have
14275 been set by marker id are reset by marker id in breakpoint_re_set.
14276 The heuristic is:
14277
14278 1) For a tracepoint set at a specific address, look for a marker at
14279 the old PC. If one is found there, assume to be the same marker.
14280 If the name / string id of the marker found is different from the
14281 previous known name, assume that means the user renamed the marker
14282 in the sources, and output a warning.
14283
14284 2) For a tracepoint set at a given line number, look for a marker
14285 at the new address of the old line number. If one is found there,
14286 assume to be the same marker. If the name / string id of the
14287 marker found is different from the previous known name, assume that
14288 means the user renamed the marker in the sources, and output a
14289 warning.
14290
14291 3) If a marker is no longer found at the same address or line, it
14292 may mean the marker no longer exists. But it may also just mean
14293 the code changed a bit. Maybe the user added a few lines of code
14294 that made the marker move up or down (in line number terms). Ask
14295 the target for info about the marker with the string id as we knew
14296 it. If found, update line number and address in the matching
14297 static tracepoint. This will get confused if there's more than one
14298 marker with the same ID (possible in UST, although unadvised
14299 precisely because it confuses tools). */
14300
14301static struct symtab_and_line
14302update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14303{
d9b3f62e 14304 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14305 struct static_tracepoint_marker marker;
14306 CORE_ADDR pc;
0fb4aa4b
PA
14307
14308 pc = sal.pc;
14309 if (sal.line)
14310 find_line_pc (sal.symtab, sal.line, &pc);
14311
14312 if (target_static_tracepoint_marker_at (pc, &marker))
14313 {
d9b3f62e 14314 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14315 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14316 b->number,
d9b3f62e 14317 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14318
d9b3f62e
PA
14319 xfree (tp->static_trace_marker_id);
14320 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14321 release_static_tracepoint_marker (&marker);
14322
14323 return sal;
14324 }
14325
14326 /* Old marker wasn't found on target at lineno. Try looking it up
14327 by string ID. */
14328 if (!sal.explicit_pc
14329 && sal.line != 0
14330 && sal.symtab != NULL
d9b3f62e 14331 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14332 {
14333 VEC(static_tracepoint_marker_p) *markers;
14334
14335 markers
d9b3f62e 14336 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14337
14338 if (!VEC_empty(static_tracepoint_marker_p, markers))
14339 {
80e1d417 14340 struct symtab_and_line sal2;
0fb4aa4b 14341 struct symbol *sym;
80e1d417 14342 struct static_tracepoint_marker *tpmarker;
79a45e25 14343 struct ui_out *uiout = current_uiout;
0fb4aa4b 14344
80e1d417 14345 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14346
d9b3f62e 14347 xfree (tp->static_trace_marker_id);
80e1d417 14348 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14349
14350 warning (_("marker for static tracepoint %d (%s) not "
14351 "found at previous line number"),
d9b3f62e 14352 b->number, tp->static_trace_marker_id);
0fb4aa4b 14353
80e1d417 14354 init_sal (&sal2);
0fb4aa4b 14355
80e1d417 14356 sal2.pc = tpmarker->address;
0fb4aa4b 14357
80e1d417
AS
14358 sal2 = find_pc_line (tpmarker->address, 0);
14359 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14360 ui_out_text (uiout, "Now in ");
14361 if (sym)
14362 {
14363 ui_out_field_string (uiout, "func",
14364 SYMBOL_PRINT_NAME (sym));
14365 ui_out_text (uiout, " at ");
14366 }
05cba821
JK
14367 ui_out_field_string (uiout, "file",
14368 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14369 ui_out_text (uiout, ":");
14370
14371 if (ui_out_is_mi_like_p (uiout))
14372 {
0b0865da 14373 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14374
f35a17b5 14375 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14376 }
14377
80e1d417 14378 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14379 ui_out_text (uiout, "\n");
14380
80e1d417 14381 b->loc->line_number = sal2.line;
2f202fde 14382 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14383
14384 xfree (b->addr_string);
14385 b->addr_string = xstrprintf ("%s:%d",
05cba821 14386 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14387 b->loc->line_number);
0fb4aa4b
PA
14388
14389 /* Might be nice to check if function changed, and warn if
14390 so. */
14391
80e1d417 14392 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14393 }
14394 }
14395 return sal;
14396}
14397
8d3788bd
VP
14398/* Returns 1 iff locations A and B are sufficiently same that
14399 we don't need to report breakpoint as changed. */
14400
14401static int
14402locations_are_equal (struct bp_location *a, struct bp_location *b)
14403{
14404 while (a && b)
14405 {
14406 if (a->address != b->address)
14407 return 0;
14408
14409 if (a->shlib_disabled != b->shlib_disabled)
14410 return 0;
14411
14412 if (a->enabled != b->enabled)
14413 return 0;
14414
14415 a = a->next;
14416 b = b->next;
14417 }
14418
14419 if ((a == NULL) != (b == NULL))
14420 return 0;
14421
14422 return 1;
14423}
14424
f1310107
TJB
14425/* Create new breakpoint locations for B (a hardware or software breakpoint)
14426 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14427 a ranged breakpoint. */
14428
0e30163f 14429void
0d381245 14430update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14431 struct symtabs_and_lines sals,
14432 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14433{
14434 int i;
0d381245
VP
14435 struct bp_location *existing_locations = b->loc;
14436
f8eba3c6
TT
14437 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14438 {
14439 /* Ranged breakpoints have only one start location and one end
14440 location. */
14441 b->enable_state = bp_disabled;
44702360 14442 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14443 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14444 "multiple locations found\n"),
14445 b->number);
14446 return;
14447 }
f1310107 14448
4a64f543
MS
14449 /* If there's no new locations, and all existing locations are
14450 pending, don't do anything. This optimizes the common case where
14451 all locations are in the same shared library, that was unloaded.
14452 We'd like to retain the location, so that when the library is
14453 loaded again, we don't loose the enabled/disabled status of the
14454 individual locations. */
0d381245 14455 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14456 return;
14457
fe3f5fa8
VP
14458 b->loc = NULL;
14459
0d381245 14460 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14461 {
f8eba3c6
TT
14462 struct bp_location *new_loc;
14463
14464 switch_to_program_space_and_thread (sals.sals[i].pspace);
14465
14466 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14467
0d381245
VP
14468 /* Reparse conditions, they might contain references to the
14469 old symtab. */
14470 if (b->cond_string != NULL)
14471 {
bbc13ae3 14472 const char *s;
bfd189b1 14473 volatile struct gdb_exception e;
fe3f5fa8 14474
0d381245
VP
14475 s = b->cond_string;
14476 TRY_CATCH (e, RETURN_MASK_ERROR)
14477 {
1bb9788d
TT
14478 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14479 block_for_pc (sals.sals[i].pc),
0d381245
VP
14480 0);
14481 }
14482 if (e.reason < 0)
14483 {
3e43a32a
MS
14484 warning (_("failed to reevaluate condition "
14485 "for breakpoint %d: %s"),
0d381245
VP
14486 b->number, e.message);
14487 new_loc->enabled = 0;
14488 }
14489 }
fe3f5fa8 14490
f1310107
TJB
14491 if (sals_end.nelts)
14492 {
14493 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14494
14495 new_loc->length = end - sals.sals[0].pc + 1;
14496 }
0d381245 14497 }
fe3f5fa8 14498
514f746b
AR
14499 /* Update locations of permanent breakpoints. */
14500 if (b->enable_state == bp_permanent)
14501 make_breakpoint_permanent (b);
14502
4a64f543
MS
14503 /* If possible, carry over 'disable' status from existing
14504 breakpoints. */
0d381245
VP
14505 {
14506 struct bp_location *e = existing_locations;
776592bf
DE
14507 /* If there are multiple breakpoints with the same function name,
14508 e.g. for inline functions, comparing function names won't work.
14509 Instead compare pc addresses; this is just a heuristic as things
14510 may have moved, but in practice it gives the correct answer
14511 often enough until a better solution is found. */
14512 int have_ambiguous_names = ambiguous_names_p (b->loc);
14513
0d381245
VP
14514 for (; e; e = e->next)
14515 {
14516 if (!e->enabled && e->function_name)
14517 {
14518 struct bp_location *l = b->loc;
776592bf
DE
14519 if (have_ambiguous_names)
14520 {
14521 for (; l; l = l->next)
f1310107 14522 if (breakpoint_locations_match (e, l))
776592bf
DE
14523 {
14524 l->enabled = 0;
14525 break;
14526 }
14527 }
14528 else
14529 {
14530 for (; l; l = l->next)
14531 if (l->function_name
14532 && strcmp (e->function_name, l->function_name) == 0)
14533 {
14534 l->enabled = 0;
14535 break;
14536 }
14537 }
0d381245
VP
14538 }
14539 }
14540 }
fe3f5fa8 14541
8d3788bd
VP
14542 if (!locations_are_equal (existing_locations, b->loc))
14543 observer_notify_breakpoint_modified (b);
14544
44702360 14545 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14546}
14547
ef23e705
TJB
14548/* Find the SaL locations corresponding to the given ADDR_STRING.
14549 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14550
14551static struct symtabs_and_lines
14552addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14553{
14554 char *s;
02d20e4a 14555 struct symtabs_and_lines sals = {0};
f8eba3c6 14556 volatile struct gdb_exception e;
ef23e705 14557
983af33b 14558 gdb_assert (b->ops != NULL);
ef23e705 14559 s = addr_string;
ef23e705
TJB
14560
14561 TRY_CATCH (e, RETURN_MASK_ERROR)
14562 {
983af33b 14563 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14564 }
14565 if (e.reason < 0)
14566 {
14567 int not_found_and_ok = 0;
14568 /* For pending breakpoints, it's expected that parsing will
14569 fail until the right shared library is loaded. User has
14570 already told to create pending breakpoints and don't need
14571 extra messages. If breakpoint is in bp_shlib_disabled
14572 state, then user already saw the message about that
14573 breakpoint being disabled, and don't want to see more
14574 errors. */
58438ac1 14575 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14576 && (b->condition_not_parsed
14577 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14578 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14579 || b->enable_state == bp_disabled))
14580 not_found_and_ok = 1;
14581
14582 if (!not_found_and_ok)
14583 {
14584 /* We surely don't want to warn about the same breakpoint
14585 10 times. One solution, implemented here, is disable
14586 the breakpoint on error. Another solution would be to
14587 have separate 'warning emitted' flag. Since this
14588 happens only when a binary has changed, I don't know
14589 which approach is better. */
14590 b->enable_state = bp_disabled;
14591 throw_exception (e);
14592 }
14593 }
14594
58438ac1 14595 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14596 {
f8eba3c6 14597 int i;
ef23e705 14598
f8eba3c6
TT
14599 for (i = 0; i < sals.nelts; ++i)
14600 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14601 if (b->condition_not_parsed && s && s[0])
14602 {
ed1d1739
KS
14603 char *cond_string, *extra_string;
14604 int thread, task;
ef23e705
TJB
14605
14606 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14607 &cond_string, &thread, &task,
14608 &extra_string);
ef23e705
TJB
14609 if (cond_string)
14610 b->cond_string = cond_string;
14611 b->thread = thread;
14612 b->task = task;
e7e0cddf
SS
14613 if (extra_string)
14614 b->extra_string = extra_string;
ef23e705
TJB
14615 b->condition_not_parsed = 0;
14616 }
14617
983af33b 14618 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14619 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14620
58438ac1
TT
14621 *found = 1;
14622 }
14623 else
14624 *found = 0;
ef23e705
TJB
14625
14626 return sals;
14627}
14628
348d480f
PA
14629/* The default re_set method, for typical hardware or software
14630 breakpoints. Reevaluate the breakpoint and recreate its
14631 locations. */
14632
14633static void
28010a5d 14634breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14635{
14636 int found;
f1310107 14637 struct symtabs_and_lines sals, sals_end;
ef23e705 14638 struct symtabs_and_lines expanded = {0};
f1310107 14639 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14640
14641 sals = addr_string_to_sals (b, b->addr_string, &found);
14642 if (found)
14643 {
14644 make_cleanup (xfree, sals.sals);
f8eba3c6 14645 expanded = sals;
ef23e705
TJB
14646 }
14647
f1310107
TJB
14648 if (b->addr_string_range_end)
14649 {
14650 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14651 if (found)
14652 {
14653 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14654 expanded_end = sals_end;
f1310107
TJB
14655 }
14656 }
14657
14658 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14659}
14660
983af33b
SDJ
14661/* Default method for creating SALs from an address string. It basically
14662 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14663
14664static void
14665create_sals_from_address_default (char **arg,
14666 struct linespec_result *canonical,
14667 enum bptype type_wanted,
14668 char *addr_start, char **copy_arg)
14669{
14670 parse_breakpoint_sals (arg, canonical);
14671}
14672
14673/* Call create_breakpoints_sal for the given arguments. This is the default
14674 function for the `create_breakpoints_sal' method of
14675 breakpoint_ops. */
14676
14677static void
14678create_breakpoints_sal_default (struct gdbarch *gdbarch,
14679 struct linespec_result *canonical,
983af33b 14680 char *cond_string,
e7e0cddf 14681 char *extra_string,
983af33b
SDJ
14682 enum bptype type_wanted,
14683 enum bpdisp disposition,
14684 int thread,
14685 int task, int ignore_count,
14686 const struct breakpoint_ops *ops,
14687 int from_tty, int enabled,
44f238bb 14688 int internal, unsigned flags)
983af33b
SDJ
14689{
14690 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14691 extra_string,
983af33b
SDJ
14692 type_wanted, disposition,
14693 thread, task, ignore_count, ops, from_tty,
44f238bb 14694 enabled, internal, flags);
983af33b
SDJ
14695}
14696
14697/* Decode the line represented by S by calling decode_line_full. This is the
14698 default function for the `decode_linespec' method of breakpoint_ops. */
14699
14700static void
14701decode_linespec_default (struct breakpoint *b, char **s,
14702 struct symtabs_and_lines *sals)
14703{
14704 struct linespec_result canonical;
14705
14706 init_linespec_result (&canonical);
14707 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14708 (struct symtab *) NULL, 0,
14709 &canonical, multiple_symbols_all,
14710 b->filter);
14711
14712 /* We should get 0 or 1 resulting SALs. */
14713 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14714
14715 if (VEC_length (linespec_sals, canonical.sals) > 0)
14716 {
14717 struct linespec_sals *lsal;
14718
14719 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14720 *sals = lsal->sals;
14721 /* Arrange it so the destructor does not free the
14722 contents. */
14723 lsal->sals.sals = NULL;
14724 }
14725
14726 destroy_linespec_result (&canonical);
14727}
14728
28010a5d
PA
14729/* Prepare the global context for a re-set of breakpoint B. */
14730
14731static struct cleanup *
14732prepare_re_set_context (struct breakpoint *b)
14733{
14734 struct cleanup *cleanups;
14735
14736 input_radix = b->input_radix;
14737 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14738 if (b->pspace != NULL)
14739 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14740 set_language (b->language);
14741
14742 return cleanups;
ef23e705
TJB
14743}
14744
c906108c
SS
14745/* Reset a breakpoint given it's struct breakpoint * BINT.
14746 The value we return ends up being the return value from catch_errors.
14747 Unused in this case. */
14748
14749static int
4efb68b1 14750breakpoint_re_set_one (void *bint)
c906108c 14751{
4a64f543 14752 /* Get past catch_errs. */
53a5351d 14753 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14754 struct cleanup *cleanups;
c906108c 14755
348d480f
PA
14756 cleanups = prepare_re_set_context (b);
14757 b->ops->re_set (b);
14758 do_cleanups (cleanups);
c906108c
SS
14759 return 0;
14760}
14761
69de3c6a 14762/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14763void
69de3c6a 14764breakpoint_re_set (void)
c906108c 14765{
35df4500 14766 struct breakpoint *b, *b_tmp;
c906108c
SS
14767 enum language save_language;
14768 int save_input_radix;
6c95b8df 14769 struct cleanup *old_chain;
c5aa993b 14770
c906108c
SS
14771 save_language = current_language->la_language;
14772 save_input_radix = input_radix;
6c95b8df
PA
14773 old_chain = save_current_program_space ();
14774
35df4500 14775 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14776 {
4a64f543 14777 /* Format possible error msg. */
fe3f5fa8 14778 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14779 b->number);
14780 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14781 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14782 do_cleanups (cleanups);
c5aa993b 14783 }
c906108c
SS
14784 set_language (save_language);
14785 input_radix = save_input_radix;
e62c965a 14786
0756c555 14787 jit_breakpoint_re_set ();
4efc6507 14788
6c95b8df
PA
14789 do_cleanups (old_chain);
14790
af02033e
PP
14791 create_overlay_event_breakpoint ();
14792 create_longjmp_master_breakpoint ();
14793 create_std_terminate_master_breakpoint ();
186c406b 14794 create_exception_master_breakpoint ();
c906108c
SS
14795}
14796\f
c906108c
SS
14797/* Reset the thread number of this breakpoint:
14798
14799 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14800 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14801void
fba45db2 14802breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14803{
14804 if (b->thread != -1)
14805 {
39f77062
KB
14806 if (in_thread_list (inferior_ptid))
14807 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14808
14809 /* We're being called after following a fork. The new fork is
14810 selected as current, and unless this was a vfork will have a
14811 different program space from the original thread. Reset that
14812 as well. */
14813 b->loc->pspace = current_program_space;
c906108c
SS
14814 }
14815}
14816
03ac34d5
MS
14817/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14818 If from_tty is nonzero, it prints a message to that effect,
14819 which ends with a period (no newline). */
14820
c906108c 14821void
fba45db2 14822set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14823{
52f0bd74 14824 struct breakpoint *b;
c906108c
SS
14825
14826 if (count < 0)
14827 count = 0;
14828
14829 ALL_BREAKPOINTS (b)
14830 if (b->number == bptnum)
c5aa993b 14831 {
d77f58be
SS
14832 if (is_tracepoint (b))
14833 {
14834 if (from_tty && count != 0)
14835 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14836 bptnum);
14837 return;
14838 }
14839
c5aa993b 14840 b->ignore_count = count;
221ea385
KS
14841 if (from_tty)
14842 {
14843 if (count == 0)
3e43a32a
MS
14844 printf_filtered (_("Will stop next time "
14845 "breakpoint %d is reached."),
221ea385
KS
14846 bptnum);
14847 else if (count == 1)
a3f17187 14848 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14849 bptnum);
14850 else
3e43a32a
MS
14851 printf_filtered (_("Will ignore next %d "
14852 "crossings of breakpoint %d."),
221ea385
KS
14853 count, bptnum);
14854 }
8d3788bd 14855 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14856 return;
14857 }
c906108c 14858
8a3fe4f8 14859 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14860}
14861
c906108c
SS
14862/* Command to set ignore-count of breakpoint N to COUNT. */
14863
14864static void
fba45db2 14865ignore_command (char *args, int from_tty)
c906108c
SS
14866{
14867 char *p = args;
52f0bd74 14868 int num;
c906108c
SS
14869
14870 if (p == 0)
e2e0b3e5 14871 error_no_arg (_("a breakpoint number"));
c5aa993b 14872
c906108c 14873 num = get_number (&p);
5c44784c 14874 if (num == 0)
8a3fe4f8 14875 error (_("bad breakpoint number: '%s'"), args);
c906108c 14876 if (*p == 0)
8a3fe4f8 14877 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14878
14879 set_ignore_count (num,
14880 longest_to_int (value_as_long (parse_and_eval (p))),
14881 from_tty);
221ea385
KS
14882 if (from_tty)
14883 printf_filtered ("\n");
c906108c
SS
14884}
14885\f
14886/* Call FUNCTION on each of the breakpoints
14887 whose numbers are given in ARGS. */
14888
14889static void
95a42b64
TT
14890map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14891 void *),
14892 void *data)
c906108c 14893{
52f0bd74
AC
14894 int num;
14895 struct breakpoint *b, *tmp;
11cf8741 14896 int match;
197f0a60 14897 struct get_number_or_range_state state;
c906108c 14898
197f0a60 14899 if (args == 0)
e2e0b3e5 14900 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14901
197f0a60
TT
14902 init_number_or_range (&state, args);
14903
14904 while (!state.finished)
c906108c 14905 {
e799154c 14906 const char *p = state.string;
197f0a60 14907
11cf8741 14908 match = 0;
c5aa993b 14909
197f0a60 14910 num = get_number_or_range (&state);
5c44784c 14911 if (num == 0)
c5aa993b 14912 {
8a3fe4f8 14913 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14914 }
14915 else
14916 {
14917 ALL_BREAKPOINTS_SAFE (b, tmp)
14918 if (b->number == num)
14919 {
11cf8741 14920 match = 1;
cdac0397 14921 function (b, data);
11cf8741 14922 break;
5c44784c 14923 }
11cf8741 14924 if (match == 0)
a3f17187 14925 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14926 }
c906108c
SS
14927 }
14928}
14929
0d381245
VP
14930static struct bp_location *
14931find_location_by_number (char *number)
14932{
14933 char *dot = strchr (number, '.');
14934 char *p1;
14935 int bp_num;
14936 int loc_num;
14937 struct breakpoint *b;
14938 struct bp_location *loc;
14939
14940 *dot = '\0';
14941
14942 p1 = number;
197f0a60 14943 bp_num = get_number (&p1);
0d381245
VP
14944 if (bp_num == 0)
14945 error (_("Bad breakpoint number '%s'"), number);
14946
14947 ALL_BREAKPOINTS (b)
14948 if (b->number == bp_num)
14949 {
14950 break;
14951 }
14952
14953 if (!b || b->number != bp_num)
14954 error (_("Bad breakpoint number '%s'"), number);
14955
14956 p1 = dot+1;
197f0a60 14957 loc_num = get_number (&p1);
0d381245
VP
14958 if (loc_num == 0)
14959 error (_("Bad breakpoint location number '%s'"), number);
14960
14961 --loc_num;
14962 loc = b->loc;
14963 for (;loc_num && loc; --loc_num, loc = loc->next)
14964 ;
14965 if (!loc)
14966 error (_("Bad breakpoint location number '%s'"), dot+1);
14967
14968 return loc;
14969}
14970
14971
1900040c
MS
14972/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14973 If from_tty is nonzero, it prints a message to that effect,
14974 which ends with a period (no newline). */
14975
c906108c 14976void
fba45db2 14977disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14978{
14979 /* Never disable a watchpoint scope breakpoint; we want to
14980 hit them when we leave scope so we can delete both the
14981 watchpoint and its scope breakpoint at that time. */
14982 if (bpt->type == bp_watchpoint_scope)
14983 return;
14984
c2c6d25f 14985 /* You can't disable permanent breakpoints. */
b5de0fa7 14986 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14987 return;
14988
b5de0fa7 14989 bpt->enable_state = bp_disabled;
c906108c 14990
b775012e
LM
14991 /* Mark breakpoint locations modified. */
14992 mark_breakpoint_modified (bpt);
14993
d248b706
KY
14994 if (target_supports_enable_disable_tracepoint ()
14995 && current_trace_status ()->running && is_tracepoint (bpt))
14996 {
14997 struct bp_location *location;
14998
14999 for (location = bpt->loc; location; location = location->next)
15000 target_disable_tracepoint (location);
15001 }
15002
44702360 15003 update_global_location_list (UGLL_DONT_INSERT);
c906108c 15004
8d3788bd 15005 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15006}
15007
51be5b68
PA
15008/* A callback for iterate_over_related_breakpoints. */
15009
15010static void
15011do_disable_breakpoint (struct breakpoint *b, void *ignore)
15012{
15013 disable_breakpoint (b);
15014}
15015
95a42b64
TT
15016/* A callback for map_breakpoint_numbers that calls
15017 disable_breakpoint. */
15018
15019static void
15020do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
15021{
51be5b68 15022 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
15023}
15024
c906108c 15025static void
fba45db2 15026disable_command (char *args, int from_tty)
c906108c 15027{
c906108c 15028 if (args == 0)
46c6471b
PA
15029 {
15030 struct breakpoint *bpt;
15031
15032 ALL_BREAKPOINTS (bpt)
15033 if (user_breakpoint_p (bpt))
15034 disable_breakpoint (bpt);
15035 }
9eaabc75 15036 else
0d381245 15037 {
9eaabc75
MW
15038 char *num = extract_arg (&args);
15039
15040 while (num)
d248b706 15041 {
9eaabc75 15042 if (strchr (num, '.'))
b775012e 15043 {
9eaabc75
MW
15044 struct bp_location *loc = find_location_by_number (num);
15045
15046 if (loc)
15047 {
15048 if (loc->enabled)
15049 {
15050 loc->enabled = 0;
15051 mark_breakpoint_location_modified (loc);
15052 }
15053 if (target_supports_enable_disable_tracepoint ()
15054 && current_trace_status ()->running && loc->owner
15055 && is_tracepoint (loc->owner))
15056 target_disable_tracepoint (loc);
15057 }
44702360 15058 update_global_location_list (UGLL_DONT_INSERT);
b775012e 15059 }
9eaabc75
MW
15060 else
15061 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15062 num = extract_arg (&args);
d248b706 15063 }
0d381245 15064 }
c906108c
SS
15065}
15066
15067static void
816338b5
SS
15068enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15069 int count)
c906108c 15070{
afe38095 15071 int target_resources_ok;
c906108c
SS
15072
15073 if (bpt->type == bp_hardware_breakpoint)
15074 {
15075 int i;
c5aa993b 15076 i = hw_breakpoint_used_count ();
53a5351d 15077 target_resources_ok =
d92524f1 15078 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 15079 i + 1, 0);
c906108c 15080 if (target_resources_ok == 0)
8a3fe4f8 15081 error (_("No hardware breakpoint support in the target."));
c906108c 15082 else if (target_resources_ok < 0)
8a3fe4f8 15083 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
15084 }
15085
cc60f2e3 15086 if (is_watchpoint (bpt))
c906108c 15087 {
d07205c2
JK
15088 /* Initialize it just to avoid a GCC false warning. */
15089 enum enable_state orig_enable_state = 0;
bfd189b1 15090 volatile struct gdb_exception e;
dde02812
ES
15091
15092 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 15093 {
3a5c3e22
PA
15094 struct watchpoint *w = (struct watchpoint *) bpt;
15095
1e718ff1
TJB
15096 orig_enable_state = bpt->enable_state;
15097 bpt->enable_state = bp_enabled;
3a5c3e22 15098 update_watchpoint (w, 1 /* reparse */);
c906108c 15099 }
dde02812 15100 if (e.reason < 0)
c5aa993b 15101 {
1e718ff1 15102 bpt->enable_state = orig_enable_state;
dde02812
ES
15103 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15104 bpt->number);
15105 return;
c5aa993b 15106 }
c906108c 15107 }
0101ce28 15108
b4c291bb
KH
15109 if (bpt->enable_state != bp_permanent)
15110 bpt->enable_state = bp_enabled;
d248b706 15111
b775012e
LM
15112 bpt->enable_state = bp_enabled;
15113
15114 /* Mark breakpoint locations modified. */
15115 mark_breakpoint_modified (bpt);
15116
d248b706
KY
15117 if (target_supports_enable_disable_tracepoint ()
15118 && current_trace_status ()->running && is_tracepoint (bpt))
15119 {
15120 struct bp_location *location;
15121
15122 for (location = bpt->loc; location; location = location->next)
15123 target_enable_tracepoint (location);
15124 }
15125
b4c291bb 15126 bpt->disposition = disposition;
816338b5 15127 bpt->enable_count = count;
44702360 15128 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 15129
8d3788bd 15130 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15131}
15132
fe3f5fa8 15133
c906108c 15134void
fba45db2 15135enable_breakpoint (struct breakpoint *bpt)
c906108c 15136{
816338b5 15137 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15138}
15139
15140static void
15141do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15142{
15143 enable_breakpoint (bpt);
c906108c
SS
15144}
15145
95a42b64
TT
15146/* A callback for map_breakpoint_numbers that calls
15147 enable_breakpoint. */
15148
15149static void
15150do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15151{
51be5b68 15152 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15153}
15154
c906108c
SS
15155/* The enable command enables the specified breakpoints (or all defined
15156 breakpoints) so they once again become (or continue to be) effective
1272ad14 15157 in stopping the inferior. */
c906108c 15158
c906108c 15159static void
fba45db2 15160enable_command (char *args, int from_tty)
c906108c 15161{
c906108c 15162 if (args == 0)
46c6471b
PA
15163 {
15164 struct breakpoint *bpt;
15165
15166 ALL_BREAKPOINTS (bpt)
15167 if (user_breakpoint_p (bpt))
15168 enable_breakpoint (bpt);
15169 }
9eaabc75 15170 else
0d381245 15171 {
9eaabc75
MW
15172 char *num = extract_arg (&args);
15173
15174 while (num)
d248b706 15175 {
9eaabc75 15176 if (strchr (num, '.'))
b775012e 15177 {
9eaabc75
MW
15178 struct bp_location *loc = find_location_by_number (num);
15179
15180 if (loc)
15181 {
15182 if (!loc->enabled)
15183 {
15184 loc->enabled = 1;
15185 mark_breakpoint_location_modified (loc);
15186 }
15187 if (target_supports_enable_disable_tracepoint ()
15188 && current_trace_status ()->running && loc->owner
15189 && is_tracepoint (loc->owner))
15190 target_enable_tracepoint (loc);
15191 }
44702360 15192 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15193 }
9eaabc75
MW
15194 else
15195 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15196 num = extract_arg (&args);
d248b706 15197 }
0d381245 15198 }
c906108c
SS
15199}
15200
816338b5
SS
15201/* This struct packages up disposition data for application to multiple
15202 breakpoints. */
15203
15204struct disp_data
15205{
15206 enum bpdisp disp;
15207 int count;
15208};
15209
c906108c 15210static void
51be5b68
PA
15211do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15212{
816338b5 15213 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15214
816338b5 15215 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15216}
15217
15218static void
15219do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15220{
816338b5 15221 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15222
15223 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15224}
15225
c906108c 15226static void
fba45db2 15227enable_once_command (char *args, int from_tty)
c906108c 15228{
51be5b68 15229 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15230}
15231
816338b5
SS
15232static void
15233do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15234{
15235 struct disp_data disp = { disp_disable, *(int *) countptr };
15236
15237 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15238}
15239
15240static void
15241enable_count_command (char *args, int from_tty)
15242{
15243 int count = get_number (&args);
15244
15245 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15246}
15247
c906108c 15248static void
51be5b68 15249do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15250{
816338b5 15251 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15252
15253 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15254}
15255
c906108c 15256static void
fba45db2 15257enable_delete_command (char *args, int from_tty)
c906108c 15258{
51be5b68 15259 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15260}
15261\f
fa8d40ab
JJ
15262static void
15263set_breakpoint_cmd (char *args, int from_tty)
15264{
15265}
15266
15267static void
15268show_breakpoint_cmd (char *args, int from_tty)
15269{
15270}
15271
1f3b5d1b
PP
15272/* Invalidate last known value of any hardware watchpoint if
15273 the memory which that value represents has been written to by
15274 GDB itself. */
15275
15276static void
8de0566d
YQ
15277invalidate_bp_value_on_memory_change (struct inferior *inferior,
15278 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15279 const bfd_byte *data)
15280{
15281 struct breakpoint *bp;
15282
15283 ALL_BREAKPOINTS (bp)
15284 if (bp->enable_state == bp_enabled
3a5c3e22 15285 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15286 {
3a5c3e22 15287 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15288
3a5c3e22
PA
15289 if (wp->val_valid && wp->val)
15290 {
15291 struct bp_location *loc;
15292
15293 for (loc = bp->loc; loc != NULL; loc = loc->next)
15294 if (loc->loc_type == bp_loc_hardware_watchpoint
15295 && loc->address + loc->length > addr
15296 && addr + len > loc->address)
15297 {
15298 value_free (wp->val);
15299 wp->val = NULL;
15300 wp->val_valid = 0;
15301 }
15302 }
1f3b5d1b
PP
15303 }
15304}
15305
8181d85f
DJ
15306/* Create and insert a breakpoint for software single step. */
15307
15308void
6c95b8df 15309insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15310 struct address_space *aspace,
15311 CORE_ADDR next_pc)
8181d85f 15312{
7c16b83e
PA
15313 struct thread_info *tp = inferior_thread ();
15314 struct symtab_and_line sal;
15315 CORE_ADDR pc = next_pc;
8181d85f 15316
34b7e8a6
PA
15317 if (tp->control.single_step_breakpoints == NULL)
15318 {
15319 tp->control.single_step_breakpoints
15320 = new_single_step_breakpoint (tp->num, gdbarch);
15321 }
8181d85f 15322
7c16b83e
PA
15323 sal = find_pc_line (pc, 0);
15324 sal.pc = pc;
15325 sal.section = find_pc_overlay (pc);
15326 sal.explicit_pc = 1;
34b7e8a6 15327 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15328
7c16b83e 15329 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15330}
15331
34b7e8a6 15332/* See breakpoint.h. */
f02253f1
HZ
15333
15334int
7c16b83e
PA
15335breakpoint_has_location_inserted_here (struct breakpoint *bp,
15336 struct address_space *aspace,
15337 CORE_ADDR pc)
1aafd4da 15338{
7c16b83e 15339 struct bp_location *loc;
1aafd4da 15340
7c16b83e
PA
15341 for (loc = bp->loc; loc != NULL; loc = loc->next)
15342 if (loc->inserted
15343 && breakpoint_location_address_match (loc, aspace, pc))
15344 return 1;
1aafd4da 15345
7c16b83e 15346 return 0;
ef370185
JB
15347}
15348
15349/* Check whether a software single-step breakpoint is inserted at
15350 PC. */
15351
15352int
15353single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15354 CORE_ADDR pc)
15355{
34b7e8a6
PA
15356 struct breakpoint *bpt;
15357
15358 ALL_BREAKPOINTS (bpt)
15359 {
15360 if (bpt->type == bp_single_step
15361 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15362 return 1;
15363 }
15364 return 0;
1aafd4da
UW
15365}
15366
a96d9b2e
SDJ
15367/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15368 non-zero otherwise. */
15369static int
15370is_syscall_catchpoint_enabled (struct breakpoint *bp)
15371{
15372 if (syscall_catchpoint_p (bp)
15373 && bp->enable_state != bp_disabled
15374 && bp->enable_state != bp_call_disabled)
15375 return 1;
15376 else
15377 return 0;
15378}
15379
15380int
15381catch_syscall_enabled (void)
15382{
fa3064dd
YQ
15383 struct catch_syscall_inferior_data *inf_data
15384 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15385
fa3064dd 15386 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15387}
15388
15389int
15390catching_syscall_number (int syscall_number)
15391{
15392 struct breakpoint *bp;
15393
15394 ALL_BREAKPOINTS (bp)
15395 if (is_syscall_catchpoint_enabled (bp))
15396 {
be5c67c1
PA
15397 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15398
15399 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15400 {
15401 int i, iter;
15402 for (i = 0;
be5c67c1 15403 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15404 i++)
15405 if (syscall_number == iter)
15406 return 1;
15407 }
15408 else
15409 return 1;
15410 }
15411
15412 return 0;
15413}
15414
15415/* Complete syscall names. Used by "catch syscall". */
49c4e619 15416static VEC (char_ptr) *
a96d9b2e 15417catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15418 const char *text, const char *word)
a96d9b2e
SDJ
15419{
15420 const char **list = get_syscall_names ();
49c4e619 15421 VEC (char_ptr) *retlist
b45627a0 15422 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15423
c38eea1a
MS
15424 xfree (list);
15425 return retlist;
a96d9b2e
SDJ
15426}
15427
1042e4c0
SS
15428/* Tracepoint-specific operations. */
15429
15430/* Set tracepoint count to NUM. */
15431static void
15432set_tracepoint_count (int num)
15433{
15434 tracepoint_count = num;
4fa62494 15435 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15436}
15437
70221824 15438static void
1042e4c0
SS
15439trace_command (char *arg, int from_tty)
15440{
55aa24fb
SDJ
15441 struct breakpoint_ops *ops;
15442 const char *arg_cp = arg;
15443
15444 if (arg && probe_linespec_to_ops (&arg_cp))
15445 ops = &tracepoint_probe_breakpoint_ops;
15446 else
15447 ops = &tracepoint_breakpoint_ops;
15448
558a9d82
YQ
15449 create_breakpoint (get_current_arch (),
15450 arg,
15451 NULL, 0, NULL, 1 /* parse arg */,
15452 0 /* tempflag */,
15453 bp_tracepoint /* type_wanted */,
15454 0 /* Ignore count */,
15455 pending_break_support,
15456 ops,
15457 from_tty,
15458 1 /* enabled */,
15459 0 /* internal */, 0);
1042e4c0
SS
15460}
15461
70221824 15462static void
7a697b8d
SS
15463ftrace_command (char *arg, int from_tty)
15464{
558a9d82
YQ
15465 create_breakpoint (get_current_arch (),
15466 arg,
15467 NULL, 0, NULL, 1 /* parse arg */,
15468 0 /* tempflag */,
15469 bp_fast_tracepoint /* type_wanted */,
15470 0 /* Ignore count */,
15471 pending_break_support,
15472 &tracepoint_breakpoint_ops,
15473 from_tty,
15474 1 /* enabled */,
15475 0 /* internal */, 0);
0fb4aa4b
PA
15476}
15477
15478/* strace command implementation. Creates a static tracepoint. */
15479
70221824 15480static void
0fb4aa4b
PA
15481strace_command (char *arg, int from_tty)
15482{
983af33b
SDJ
15483 struct breakpoint_ops *ops;
15484
15485 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15486 or with a normal static tracepoint. */
15487 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15488 ops = &strace_marker_breakpoint_ops;
15489 else
15490 ops = &tracepoint_breakpoint_ops;
15491
558a9d82
YQ
15492 create_breakpoint (get_current_arch (),
15493 arg,
15494 NULL, 0, NULL, 1 /* parse arg */,
15495 0 /* tempflag */,
15496 bp_static_tracepoint /* type_wanted */,
15497 0 /* Ignore count */,
15498 pending_break_support,
15499 ops,
15500 from_tty,
15501 1 /* enabled */,
15502 0 /* internal */, 0);
7a697b8d
SS
15503}
15504
409873ef
SS
15505/* Set up a fake reader function that gets command lines from a linked
15506 list that was acquired during tracepoint uploading. */
15507
15508static struct uploaded_tp *this_utp;
3149d8c1 15509static int next_cmd;
409873ef
SS
15510
15511static char *
15512read_uploaded_action (void)
15513{
15514 char *rslt;
15515
3149d8c1 15516 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15517
3149d8c1 15518 next_cmd++;
409873ef
SS
15519
15520 return rslt;
15521}
15522
00bf0b85
SS
15523/* Given information about a tracepoint as recorded on a target (which
15524 can be either a live system or a trace file), attempt to create an
15525 equivalent GDB tracepoint. This is not a reliable process, since
15526 the target does not necessarily have all the information used when
15527 the tracepoint was originally defined. */
15528
d9b3f62e 15529struct tracepoint *
00bf0b85 15530create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15531{
409873ef 15532 char *addr_str, small_buf[100];
d9b3f62e 15533 struct tracepoint *tp;
fd9b8c24 15534
409873ef
SS
15535 if (utp->at_string)
15536 addr_str = utp->at_string;
15537 else
15538 {
15539 /* In the absence of a source location, fall back to raw
15540 address. Since there is no way to confirm that the address
15541 means the same thing as when the trace was started, warn the
15542 user. */
3e43a32a
MS
15543 warning (_("Uploaded tracepoint %d has no "
15544 "source location, using raw address"),
409873ef 15545 utp->number);
8c042590 15546 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15547 addr_str = small_buf;
15548 }
15549
15550 /* There's not much we can do with a sequence of bytecodes. */
15551 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15552 warning (_("Uploaded tracepoint %d condition "
15553 "has no source form, ignoring it"),
409873ef 15554 utp->number);
d5551862 15555
8cdf0e15 15556 if (!create_breakpoint (get_current_arch (),
409873ef 15557 addr_str,
e7e0cddf
SS
15558 utp->cond_string, -1, NULL,
15559 0 /* parse cond/thread */,
8cdf0e15 15560 0 /* tempflag */,
0fb4aa4b 15561 utp->type /* type_wanted */,
8cdf0e15
VP
15562 0 /* Ignore count */,
15563 pending_break_support,
348d480f 15564 &tracepoint_breakpoint_ops,
8cdf0e15 15565 0 /* from_tty */,
84f4c1fe 15566 utp->enabled /* enabled */,
44f238bb
PA
15567 0 /* internal */,
15568 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15569 return NULL;
15570
409873ef 15571 /* Get the tracepoint we just created. */
fd9b8c24
PA
15572 tp = get_tracepoint (tracepoint_count);
15573 gdb_assert (tp != NULL);
d5551862 15574
00bf0b85
SS
15575 if (utp->pass > 0)
15576 {
8c042590
PM
15577 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15578 tp->base.number);
00bf0b85 15579
409873ef 15580 trace_pass_command (small_buf, 0);
00bf0b85
SS
15581 }
15582
409873ef
SS
15583 /* If we have uploaded versions of the original commands, set up a
15584 special-purpose "reader" function and call the usual command line
15585 reader, then pass the result to the breakpoint command-setting
15586 function. */
3149d8c1 15587 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15588 {
409873ef 15589 struct command_line *cmd_list;
00bf0b85 15590
409873ef 15591 this_utp = utp;
3149d8c1 15592 next_cmd = 0;
d5551862 15593
409873ef
SS
15594 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15595
d9b3f62e 15596 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15597 }
3149d8c1
SS
15598 else if (!VEC_empty (char_ptr, utp->actions)
15599 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15600 warning (_("Uploaded tracepoint %d actions "
15601 "have no source form, ignoring them"),
409873ef 15602 utp->number);
00bf0b85 15603
f196051f
SS
15604 /* Copy any status information that might be available. */
15605 tp->base.hit_count = utp->hit_count;
15606 tp->traceframe_usage = utp->traceframe_usage;
15607
00bf0b85 15608 return tp;
d9b3f62e 15609}
00bf0b85 15610
1042e4c0
SS
15611/* Print information on tracepoint number TPNUM_EXP, or all if
15612 omitted. */
15613
15614static void
e5a67952 15615tracepoints_info (char *args, int from_tty)
1042e4c0 15616{
79a45e25 15617 struct ui_out *uiout = current_uiout;
e5a67952 15618 int num_printed;
1042e4c0 15619
e5a67952 15620 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15621
15622 if (num_printed == 0)
1042e4c0 15623 {
e5a67952 15624 if (args == NULL || *args == '\0')
d77f58be
SS
15625 ui_out_message (uiout, 0, "No tracepoints.\n");
15626 else
e5a67952 15627 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15628 }
ad443146
SS
15629
15630 default_collect_info ();
1042e4c0
SS
15631}
15632
4a64f543 15633/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15634 Not supported by all targets. */
15635static void
15636enable_trace_command (char *args, int from_tty)
15637{
15638 enable_command (args, from_tty);
15639}
15640
4a64f543 15641/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15642 Not supported by all targets. */
15643static void
15644disable_trace_command (char *args, int from_tty)
15645{
15646 disable_command (args, from_tty);
15647}
15648
4a64f543 15649/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15650static void
15651delete_trace_command (char *arg, int from_tty)
15652{
35df4500 15653 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15654
15655 dont_repeat ();
15656
15657 if (arg == 0)
15658 {
15659 int breaks_to_delete = 0;
15660
15661 /* Delete all breakpoints if no argument.
15662 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15663 have to be deleted with an explicit breakpoint number
15664 argument. */
1042e4c0 15665 ALL_TRACEPOINTS (b)
46c6471b 15666 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15667 {
15668 breaks_to_delete = 1;
15669 break;
15670 }
1042e4c0
SS
15671
15672 /* Ask user only if there are some breakpoints to delete. */
15673 if (!from_tty
15674 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15675 {
35df4500 15676 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15677 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15678 delete_breakpoint (b);
1042e4c0
SS
15679 }
15680 }
15681 else
51be5b68 15682 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15683}
15684
197f0a60
TT
15685/* Helper function for trace_pass_command. */
15686
15687static void
d9b3f62e 15688trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15689{
d9b3f62e 15690 tp->pass_count = count;
6f6484cd 15691 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15692 if (from_tty)
15693 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15694 tp->base.number, count);
197f0a60
TT
15695}
15696
1042e4c0
SS
15697/* Set passcount for tracepoint.
15698
15699 First command argument is passcount, second is tracepoint number.
15700 If tracepoint number omitted, apply to most recently defined.
15701 Also accepts special argument "all". */
15702
15703static void
15704trace_pass_command (char *args, int from_tty)
15705{
d9b3f62e 15706 struct tracepoint *t1;
1042e4c0 15707 unsigned int count;
1042e4c0
SS
15708
15709 if (args == 0 || *args == 0)
3e43a32a
MS
15710 error (_("passcount command requires an "
15711 "argument (count + optional TP num)"));
1042e4c0 15712
4a64f543 15713 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15714
529480d0 15715 args = skip_spaces (args);
1042e4c0
SS
15716 if (*args && strncasecmp (args, "all", 3) == 0)
15717 {
d9b3f62e
PA
15718 struct breakpoint *b;
15719
1042e4c0 15720 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15721 if (*args)
15722 error (_("Junk at end of arguments."));
1042e4c0 15723
d9b3f62e 15724 ALL_TRACEPOINTS (b)
197f0a60 15725 {
d9b3f62e 15726 t1 = (struct tracepoint *) b;
197f0a60
TT
15727 trace_pass_set_count (t1, count, from_tty);
15728 }
15729 }
15730 else if (*args == '\0')
1042e4c0 15731 {
5fa1d40e 15732 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15733 if (t1)
197f0a60
TT
15734 trace_pass_set_count (t1, count, from_tty);
15735 }
15736 else
15737 {
15738 struct get_number_or_range_state state;
15739
15740 init_number_or_range (&state, args);
15741 while (!state.finished)
1042e4c0 15742 {
5fa1d40e 15743 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15744 if (t1)
15745 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15746 }
15747 }
1042e4c0
SS
15748}
15749
d9b3f62e 15750struct tracepoint *
1042e4c0
SS
15751get_tracepoint (int num)
15752{
15753 struct breakpoint *t;
15754
15755 ALL_TRACEPOINTS (t)
15756 if (t->number == num)
d9b3f62e 15757 return (struct tracepoint *) t;
1042e4c0
SS
15758
15759 return NULL;
15760}
15761
d5551862
SS
15762/* Find the tracepoint with the given target-side number (which may be
15763 different from the tracepoint number after disconnecting and
15764 reconnecting). */
15765
d9b3f62e 15766struct tracepoint *
d5551862
SS
15767get_tracepoint_by_number_on_target (int num)
15768{
d9b3f62e 15769 struct breakpoint *b;
d5551862 15770
d9b3f62e
PA
15771 ALL_TRACEPOINTS (b)
15772 {
15773 struct tracepoint *t = (struct tracepoint *) b;
15774
15775 if (t->number_on_target == num)
15776 return t;
15777 }
d5551862
SS
15778
15779 return NULL;
15780}
15781
1042e4c0 15782/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15783 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15784 If the argument is missing, the most recent tracepoint
15785 (tracepoint_count) is returned. */
15786
d9b3f62e 15787struct tracepoint *
197f0a60 15788get_tracepoint_by_number (char **arg,
5fa1d40e 15789 struct get_number_or_range_state *state)
1042e4c0 15790{
1042e4c0
SS
15791 struct breakpoint *t;
15792 int tpnum;
15793 char *instring = arg == NULL ? NULL : *arg;
15794
197f0a60
TT
15795 if (state)
15796 {
15797 gdb_assert (!state->finished);
15798 tpnum = get_number_or_range (state);
15799 }
15800 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15801 tpnum = tracepoint_count;
1042e4c0 15802 else
197f0a60 15803 tpnum = get_number (arg);
1042e4c0
SS
15804
15805 if (tpnum <= 0)
15806 {
15807 if (instring && *instring)
15808 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15809 instring);
15810 else
5fa1d40e 15811 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15812 return NULL;
15813 }
15814
15815 ALL_TRACEPOINTS (t)
15816 if (t->number == tpnum)
15817 {
d9b3f62e 15818 return (struct tracepoint *) t;
1042e4c0
SS
15819 }
15820
1042e4c0
SS
15821 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15822 return NULL;
15823}
15824
d9b3f62e
PA
15825void
15826print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15827{
15828 if (b->thread != -1)
15829 fprintf_unfiltered (fp, " thread %d", b->thread);
15830
15831 if (b->task != 0)
15832 fprintf_unfiltered (fp, " task %d", b->task);
15833
15834 fprintf_unfiltered (fp, "\n");
15835}
15836
6149aea9
PA
15837/* Save information on user settable breakpoints (watchpoints, etc) to
15838 a new script file named FILENAME. If FILTER is non-NULL, call it
15839 on each breakpoint and only include the ones for which it returns
15840 non-zero. */
15841
1042e4c0 15842static void
6149aea9
PA
15843save_breakpoints (char *filename, int from_tty,
15844 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15845{
15846 struct breakpoint *tp;
6149aea9 15847 int any = 0;
1042e4c0 15848 struct cleanup *cleanup;
a7bdde9e 15849 struct ui_file *fp;
6149aea9 15850 int extra_trace_bits = 0;
1042e4c0 15851
6149aea9
PA
15852 if (filename == 0 || *filename == 0)
15853 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15854
15855 /* See if we have anything to save. */
6149aea9 15856 ALL_BREAKPOINTS (tp)
1042e4c0 15857 {
6149aea9 15858 /* Skip internal and momentary breakpoints. */
09d682a4 15859 if (!user_breakpoint_p (tp))
6149aea9
PA
15860 continue;
15861
15862 /* If we have a filter, only save the breakpoints it accepts. */
15863 if (filter && !filter (tp))
15864 continue;
15865
15866 any = 1;
15867
15868 if (is_tracepoint (tp))
15869 {
15870 extra_trace_bits = 1;
15871
15872 /* We can stop searching. */
15873 break;
15874 }
1042e4c0 15875 }
6149aea9
PA
15876
15877 if (!any)
1042e4c0 15878 {
6149aea9 15879 warning (_("Nothing to save."));
1042e4c0
SS
15880 return;
15881 }
15882
c718be47
PA
15883 filename = tilde_expand (filename);
15884 cleanup = make_cleanup (xfree, filename);
15885 fp = gdb_fopen (filename, "w");
059fb39f 15886 if (!fp)
6149aea9
PA
15887 error (_("Unable to open file '%s' for saving (%s)"),
15888 filename, safe_strerror (errno));
a7bdde9e 15889 make_cleanup_ui_file_delete (fp);
8bf6485c 15890
6149aea9
PA
15891 if (extra_trace_bits)
15892 save_trace_state_variables (fp);
8bf6485c 15893
6149aea9 15894 ALL_BREAKPOINTS (tp)
1042e4c0 15895 {
6149aea9 15896 /* Skip internal and momentary breakpoints. */
09d682a4 15897 if (!user_breakpoint_p (tp))
6149aea9 15898 continue;
8bf6485c 15899
6149aea9
PA
15900 /* If we have a filter, only save the breakpoints it accepts. */
15901 if (filter && !filter (tp))
15902 continue;
15903
348d480f 15904 tp->ops->print_recreate (tp, fp);
1042e4c0 15905
6149aea9
PA
15906 /* Note, we can't rely on tp->number for anything, as we can't
15907 assume the recreated breakpoint numbers will match. Use $bpnum
15908 instead. */
15909
15910 if (tp->cond_string)
15911 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15912
15913 if (tp->ignore_count)
15914 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15915
2d9442cc 15916 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15917 {
a7bdde9e
VP
15918 volatile struct gdb_exception ex;
15919
6149aea9 15920 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15921
79a45e25 15922 ui_out_redirect (current_uiout, fp);
14dba4b4 15923 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15924 {
79a45e25 15925 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15926 }
79a45e25 15927 ui_out_redirect (current_uiout, NULL);
1042e4c0 15928
a7bdde9e
VP
15929 if (ex.reason < 0)
15930 throw_exception (ex);
1042e4c0 15931
a7bdde9e 15932 fprintf_unfiltered (fp, " end\n");
1042e4c0 15933 }
6149aea9
PA
15934
15935 if (tp->enable_state == bp_disabled)
99894e11 15936 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15937
15938 /* If this is a multi-location breakpoint, check if the locations
15939 should be individually disabled. Watchpoint locations are
15940 special, and not user visible. */
15941 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15942 {
15943 struct bp_location *loc;
15944 int n = 1;
15945
15946 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15947 if (!loc->enabled)
15948 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15949 }
1042e4c0 15950 }
8bf6485c 15951
6149aea9 15952 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15953 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15954
1042e4c0 15955 if (from_tty)
6149aea9 15956 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15957 do_cleanups (cleanup);
6149aea9
PA
15958}
15959
15960/* The `save breakpoints' command. */
15961
15962static void
15963save_breakpoints_command (char *args, int from_tty)
15964{
15965 save_breakpoints (args, from_tty, NULL);
15966}
15967
15968/* The `save tracepoints' command. */
15969
15970static void
15971save_tracepoints_command (char *args, int from_tty)
15972{
15973 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15974}
15975
15976/* Create a vector of all tracepoints. */
15977
15978VEC(breakpoint_p) *
eeae04df 15979all_tracepoints (void)
1042e4c0
SS
15980{
15981 VEC(breakpoint_p) *tp_vec = 0;
15982 struct breakpoint *tp;
15983
15984 ALL_TRACEPOINTS (tp)
15985 {
15986 VEC_safe_push (breakpoint_p, tp_vec, tp);
15987 }
15988
15989 return tp_vec;
15990}
15991
c906108c 15992\f
4a64f543
MS
15993/* This help string is used for the break, hbreak, tbreak and thbreak
15994 commands. It is defined as a macro to prevent duplication.
15995 COMMAND should be a string constant containing the name of the
15996 command. */
31e2b00f 15997#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15998command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15999PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16000probe point. Accepted values are `-probe' (for a generic, automatically\n\
16001guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
16002LOCATION may be a line number, function name, or \"*\" and an address.\n\
16003If a line number is specified, break at start of code for that line.\n\
16004If a function is specified, break at start of code for that function.\n\
16005If an address is specified, break at that exact address.\n\
dc10affe
PA
16006With no LOCATION, uses current execution address of the selected\n\
16007stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
16008\n\
16009THREADNUM is the number from \"info threads\".\n\
16010CONDITION is a boolean expression.\n\
16011\n\
d41c0fc8
PA
16012Multiple breakpoints at one place are permitted, and useful if their\n\
16013conditions are different.\n\
31e2b00f
AS
16014\n\
16015Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16016
44feb3ce
TT
16017/* List of subcommands for "catch". */
16018static struct cmd_list_element *catch_cmdlist;
16019
16020/* List of subcommands for "tcatch". */
16021static struct cmd_list_element *tcatch_cmdlist;
16022
9ac4176b 16023void
44feb3ce 16024add_catch_command (char *name, char *docstring,
82ae6c8d 16025 cmd_sfunc_ftype *sfunc,
625e8578 16026 completer_ftype *completer,
44feb3ce
TT
16027 void *user_data_catch,
16028 void *user_data_tcatch)
16029{
16030 struct cmd_list_element *command;
16031
16032 command = add_cmd (name, class_breakpoint, NULL, docstring,
16033 &catch_cmdlist);
16034 set_cmd_sfunc (command, sfunc);
16035 set_cmd_context (command, user_data_catch);
a96d9b2e 16036 set_cmd_completer (command, completer);
44feb3ce
TT
16037
16038 command = add_cmd (name, class_breakpoint, NULL, docstring,
16039 &tcatch_cmdlist);
16040 set_cmd_sfunc (command, sfunc);
16041 set_cmd_context (command, user_data_tcatch);
a96d9b2e 16042 set_cmd_completer (command, completer);
44feb3ce
TT
16043}
16044
6c95b8df 16045static void
a79b8f6e 16046clear_syscall_counts (struct inferior *inf)
6c95b8df 16047{
fa3064dd
YQ
16048 struct catch_syscall_inferior_data *inf_data
16049 = get_catch_syscall_inferior_data (inf);
16050
16051 inf_data->total_syscalls_count = 0;
16052 inf_data->any_syscall_count = 0;
16053 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
16054}
16055
6149aea9
PA
16056static void
16057save_command (char *arg, int from_tty)
16058{
3e43a32a
MS
16059 printf_unfiltered (_("\"save\" must be followed by "
16060 "the name of a save subcommand.\n"));
635c7e8a 16061 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
16062}
16063
84f4c1fe
PM
16064struct breakpoint *
16065iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16066 void *data)
16067{
35df4500 16068 struct breakpoint *b, *b_tmp;
84f4c1fe 16069
35df4500 16070 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
16071 {
16072 if ((*callback) (b, data))
16073 return b;
16074 }
16075
16076 return NULL;
16077}
16078
0574c78f
GB
16079/* Zero if any of the breakpoint's locations could be a location where
16080 functions have been inlined, nonzero otherwise. */
16081
16082static int
16083is_non_inline_function (struct breakpoint *b)
16084{
16085 /* The shared library event breakpoint is set on the address of a
16086 non-inline function. */
16087 if (b->type == bp_shlib_event)
16088 return 1;
16089
16090 return 0;
16091}
16092
16093/* Nonzero if the specified PC cannot be a location where functions
16094 have been inlined. */
16095
16096int
09ac7c10
TT
16097pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16098 const struct target_waitstatus *ws)
0574c78f
GB
16099{
16100 struct breakpoint *b;
16101 struct bp_location *bl;
16102
16103 ALL_BREAKPOINTS (b)
16104 {
16105 if (!is_non_inline_function (b))
16106 continue;
16107
16108 for (bl = b->loc; bl != NULL; bl = bl->next)
16109 {
16110 if (!bl->shlib_disabled
09ac7c10 16111 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16112 return 1;
16113 }
16114 }
16115
16116 return 0;
16117}
16118
2f202fde
JK
16119/* Remove any references to OBJFILE which is going to be freed. */
16120
16121void
16122breakpoint_free_objfile (struct objfile *objfile)
16123{
16124 struct bp_location **locp, *loc;
16125
16126 ALL_BP_LOCATIONS (loc, locp)
16127 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
16128 loc->symtab = NULL;
16129}
16130
2060206e
PA
16131void
16132initialize_breakpoint_ops (void)
16133{
16134 static int initialized = 0;
16135
16136 struct breakpoint_ops *ops;
16137
16138 if (initialized)
16139 return;
16140 initialized = 1;
16141
16142 /* The breakpoint_ops structure to be inherit by all kinds of
16143 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16144 internal and momentary breakpoints, etc.). */
16145 ops = &bkpt_base_breakpoint_ops;
16146 *ops = base_breakpoint_ops;
16147 ops->re_set = bkpt_re_set;
16148 ops->insert_location = bkpt_insert_location;
16149 ops->remove_location = bkpt_remove_location;
16150 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16151 ops->create_sals_from_address = bkpt_create_sals_from_address;
16152 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16153 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16154
16155 /* The breakpoint_ops structure to be used in regular breakpoints. */
16156 ops = &bkpt_breakpoint_ops;
16157 *ops = bkpt_base_breakpoint_ops;
16158 ops->re_set = bkpt_re_set;
16159 ops->resources_needed = bkpt_resources_needed;
16160 ops->print_it = bkpt_print_it;
16161 ops->print_mention = bkpt_print_mention;
16162 ops->print_recreate = bkpt_print_recreate;
16163
16164 /* Ranged breakpoints. */
16165 ops = &ranged_breakpoint_ops;
16166 *ops = bkpt_breakpoint_ops;
16167 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16168 ops->resources_needed = resources_needed_ranged_breakpoint;
16169 ops->print_it = print_it_ranged_breakpoint;
16170 ops->print_one = print_one_ranged_breakpoint;
16171 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16172 ops->print_mention = print_mention_ranged_breakpoint;
16173 ops->print_recreate = print_recreate_ranged_breakpoint;
16174
16175 /* Internal breakpoints. */
16176 ops = &internal_breakpoint_ops;
16177 *ops = bkpt_base_breakpoint_ops;
16178 ops->re_set = internal_bkpt_re_set;
16179 ops->check_status = internal_bkpt_check_status;
16180 ops->print_it = internal_bkpt_print_it;
16181 ops->print_mention = internal_bkpt_print_mention;
16182
16183 /* Momentary breakpoints. */
16184 ops = &momentary_breakpoint_ops;
16185 *ops = bkpt_base_breakpoint_ops;
16186 ops->re_set = momentary_bkpt_re_set;
16187 ops->check_status = momentary_bkpt_check_status;
16188 ops->print_it = momentary_bkpt_print_it;
16189 ops->print_mention = momentary_bkpt_print_mention;
16190
e2e4d78b
JK
16191 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16192 ops = &longjmp_breakpoint_ops;
16193 *ops = momentary_breakpoint_ops;
16194 ops->dtor = longjmp_bkpt_dtor;
16195
55aa24fb
SDJ
16196 /* Probe breakpoints. */
16197 ops = &bkpt_probe_breakpoint_ops;
16198 *ops = bkpt_breakpoint_ops;
16199 ops->insert_location = bkpt_probe_insert_location;
16200 ops->remove_location = bkpt_probe_remove_location;
16201 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16202 ops->decode_linespec = bkpt_probe_decode_linespec;
16203
2060206e
PA
16204 /* Watchpoints. */
16205 ops = &watchpoint_breakpoint_ops;
16206 *ops = base_breakpoint_ops;
3a5c3e22 16207 ops->dtor = dtor_watchpoint;
2060206e
PA
16208 ops->re_set = re_set_watchpoint;
16209 ops->insert_location = insert_watchpoint;
16210 ops->remove_location = remove_watchpoint;
16211 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16212 ops->check_status = check_status_watchpoint;
16213 ops->resources_needed = resources_needed_watchpoint;
16214 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16215 ops->print_it = print_it_watchpoint;
16216 ops->print_mention = print_mention_watchpoint;
16217 ops->print_recreate = print_recreate_watchpoint;
427cd150 16218 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16219
16220 /* Masked watchpoints. */
16221 ops = &masked_watchpoint_breakpoint_ops;
16222 *ops = watchpoint_breakpoint_ops;
16223 ops->insert_location = insert_masked_watchpoint;
16224 ops->remove_location = remove_masked_watchpoint;
16225 ops->resources_needed = resources_needed_masked_watchpoint;
16226 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16227 ops->print_it = print_it_masked_watchpoint;
16228 ops->print_one_detail = print_one_detail_masked_watchpoint;
16229 ops->print_mention = print_mention_masked_watchpoint;
16230 ops->print_recreate = print_recreate_masked_watchpoint;
16231
16232 /* Tracepoints. */
16233 ops = &tracepoint_breakpoint_ops;
16234 *ops = base_breakpoint_ops;
16235 ops->re_set = tracepoint_re_set;
16236 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16237 ops->print_one_detail = tracepoint_print_one_detail;
16238 ops->print_mention = tracepoint_print_mention;
16239 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16240 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16241 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16242 ops->decode_linespec = tracepoint_decode_linespec;
16243
55aa24fb
SDJ
16244 /* Probe tracepoints. */
16245 ops = &tracepoint_probe_breakpoint_ops;
16246 *ops = tracepoint_breakpoint_ops;
16247 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16248 ops->decode_linespec = tracepoint_probe_decode_linespec;
16249
983af33b
SDJ
16250 /* Static tracepoints with marker (`-m'). */
16251 ops = &strace_marker_breakpoint_ops;
16252 *ops = tracepoint_breakpoint_ops;
16253 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16254 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16255 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16256
16257 /* Fork catchpoints. */
16258 ops = &catch_fork_breakpoint_ops;
16259 *ops = base_breakpoint_ops;
16260 ops->insert_location = insert_catch_fork;
16261 ops->remove_location = remove_catch_fork;
16262 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16263 ops->print_it = print_it_catch_fork;
16264 ops->print_one = print_one_catch_fork;
16265 ops->print_mention = print_mention_catch_fork;
16266 ops->print_recreate = print_recreate_catch_fork;
16267
16268 /* Vfork catchpoints. */
16269 ops = &catch_vfork_breakpoint_ops;
16270 *ops = base_breakpoint_ops;
16271 ops->insert_location = insert_catch_vfork;
16272 ops->remove_location = remove_catch_vfork;
16273 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16274 ops->print_it = print_it_catch_vfork;
16275 ops->print_one = print_one_catch_vfork;
16276 ops->print_mention = print_mention_catch_vfork;
16277 ops->print_recreate = print_recreate_catch_vfork;
16278
16279 /* Exec catchpoints. */
16280 ops = &catch_exec_breakpoint_ops;
16281 *ops = base_breakpoint_ops;
16282 ops->dtor = dtor_catch_exec;
16283 ops->insert_location = insert_catch_exec;
16284 ops->remove_location = remove_catch_exec;
16285 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16286 ops->print_it = print_it_catch_exec;
16287 ops->print_one = print_one_catch_exec;
16288 ops->print_mention = print_mention_catch_exec;
16289 ops->print_recreate = print_recreate_catch_exec;
16290
16291 /* Syscall catchpoints. */
16292 ops = &catch_syscall_breakpoint_ops;
16293 *ops = base_breakpoint_ops;
16294 ops->dtor = dtor_catch_syscall;
16295 ops->insert_location = insert_catch_syscall;
16296 ops->remove_location = remove_catch_syscall;
16297 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16298 ops->print_it = print_it_catch_syscall;
16299 ops->print_one = print_one_catch_syscall;
16300 ops->print_mention = print_mention_catch_syscall;
16301 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16302
16303 /* Solib-related catchpoints. */
16304 ops = &catch_solib_breakpoint_ops;
16305 *ops = base_breakpoint_ops;
16306 ops->dtor = dtor_catch_solib;
16307 ops->insert_location = insert_catch_solib;
16308 ops->remove_location = remove_catch_solib;
16309 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16310 ops->check_status = check_status_catch_solib;
16311 ops->print_it = print_it_catch_solib;
16312 ops->print_one = print_one_catch_solib;
16313 ops->print_mention = print_mention_catch_solib;
16314 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16315
16316 ops = &dprintf_breakpoint_ops;
16317 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16318 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16319 ops->resources_needed = bkpt_resources_needed;
16320 ops->print_it = bkpt_print_it;
16321 ops->print_mention = bkpt_print_mention;
2d9442cc 16322 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16323 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16324 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16325}
16326
8bfd80db
YQ
16327/* Chain containing all defined "enable breakpoint" subcommands. */
16328
16329static struct cmd_list_element *enablebreaklist = NULL;
16330
c906108c 16331void
fba45db2 16332_initialize_breakpoint (void)
c906108c
SS
16333{
16334 struct cmd_list_element *c;
16335
2060206e
PA
16336 initialize_breakpoint_ops ();
16337
84acb35a 16338 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16339 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16340 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16341 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16342
55aa24fb
SDJ
16343 breakpoint_objfile_key
16344 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16345
fa3064dd 16346 catch_syscall_inferior_data
8e260fc0
TT
16347 = register_inferior_data_with_cleanup (NULL,
16348 catch_syscall_inferior_data_cleanup);
fa3064dd 16349
c906108c
SS
16350 breakpoint_chain = 0;
16351 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16352 before a breakpoint is set. */
16353 breakpoint_count = 0;
16354
1042e4c0
SS
16355 tracepoint_count = 0;
16356
1bedd215
AC
16357 add_com ("ignore", class_breakpoint, ignore_command, _("\
16358Set ignore-count of breakpoint number N to COUNT.\n\
16359Usage is `ignore N COUNT'."));
c906108c 16360 if (xdb_commands)
c5aa993b 16361 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16362
1bedd215
AC
16363 add_com ("commands", class_breakpoint, commands_command, _("\
16364Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16365Give breakpoint number as argument after \"commands\".\n\
16366With no argument, the targeted breakpoint is the last one set.\n\
16367The commands themselves follow starting on the next line.\n\
16368Type a line containing \"end\" to indicate the end of them.\n\
16369Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16370then no output is printed when it is hit, except what the commands print."));
c906108c 16371
d55637df 16372 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16373Specify breakpoint number N to break only if COND is true.\n\
c906108c 16374Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16375expression to be evaluated whenever breakpoint N is reached."));
d55637df 16376 set_cmd_completer (c, condition_completer);
c906108c 16377
1bedd215 16378 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16379Set a temporary breakpoint.\n\
c906108c
SS
16380Like \"break\" except the breakpoint is only temporary,\n\
16381so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16382by using \"enable delete\" on the breakpoint number.\n\
16383\n"
16384BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16385 set_cmd_completer (c, location_completer);
c94fdfd0 16386
1bedd215 16387 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16388Set a hardware assisted breakpoint.\n\
c906108c 16389Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16390some target hardware may not have this support.\n\
16391\n"
16392BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16393 set_cmd_completer (c, location_completer);
c906108c 16394
1bedd215 16395 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16396Set a temporary hardware assisted breakpoint.\n\
c906108c 16397Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16398so it will be deleted when hit.\n\
16399\n"
16400BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16401 set_cmd_completer (c, location_completer);
c906108c 16402
1bedd215
AC
16403 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16404Enable some breakpoints.\n\
c906108c
SS
16405Give breakpoint numbers (separated by spaces) as arguments.\n\
16406With no subcommand, breakpoints are enabled until you command otherwise.\n\
16407This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16408With a subcommand you can enable temporarily."),
c906108c
SS
16409 &enablelist, "enable ", 1, &cmdlist);
16410 if (xdb_commands)
1bedd215
AC
16411 add_com ("ab", class_breakpoint, enable_command, _("\
16412Enable some breakpoints.\n\
c906108c
SS
16413Give breakpoint numbers (separated by spaces) as arguments.\n\
16414With no subcommand, breakpoints are enabled until you command otherwise.\n\
16415This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16416With a subcommand you can enable temporarily."));
c906108c
SS
16417
16418 add_com_alias ("en", "enable", class_breakpoint, 1);
16419
84951ab5 16420 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16421Enable some breakpoints.\n\
c906108c
SS
16422Give breakpoint numbers (separated by spaces) as arguments.\n\
16423This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16424May be abbreviated to simply \"enable\".\n"),
c5aa993b 16425 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16426
1a966eab
AC
16427 add_cmd ("once", no_class, enable_once_command, _("\
16428Enable breakpoints for one hit. Give breakpoint numbers.\n\
16429If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16430 &enablebreaklist);
16431
1a966eab
AC
16432 add_cmd ("delete", no_class, enable_delete_command, _("\
16433Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16434If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16435 &enablebreaklist);
16436
816338b5
SS
16437 add_cmd ("count", no_class, enable_count_command, _("\
16438Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16439If a breakpoint is hit while enabled in this fashion,\n\
16440the count is decremented; when it reaches zero, the breakpoint is disabled."),
16441 &enablebreaklist);
16442
1a966eab
AC
16443 add_cmd ("delete", no_class, enable_delete_command, _("\
16444Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16445If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16446 &enablelist);
16447
1a966eab
AC
16448 add_cmd ("once", no_class, enable_once_command, _("\
16449Enable breakpoints for one hit. Give breakpoint numbers.\n\
16450If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16451 &enablelist);
16452
16453 add_cmd ("count", no_class, enable_count_command, _("\
16454Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16455If a breakpoint is hit while enabled in this fashion,\n\
16456the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16457 &enablelist);
16458
1bedd215
AC
16459 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16460Disable some breakpoints.\n\
c906108c
SS
16461Arguments are breakpoint numbers with spaces in between.\n\
16462To disable all breakpoints, give no argument.\n\
64b9b334 16463A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16464 &disablelist, "disable ", 1, &cmdlist);
16465 add_com_alias ("dis", "disable", class_breakpoint, 1);
16466 add_com_alias ("disa", "disable", class_breakpoint, 1);
16467 if (xdb_commands)
1bedd215
AC
16468 add_com ("sb", class_breakpoint, disable_command, _("\
16469Disable some breakpoints.\n\
c906108c
SS
16470Arguments are breakpoint numbers with spaces in between.\n\
16471To disable all breakpoints, give no argument.\n\
64b9b334 16472A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16473
1a966eab
AC
16474 add_cmd ("breakpoints", class_alias, disable_command, _("\
16475Disable some breakpoints.\n\
c906108c
SS
16476Arguments are breakpoint numbers with spaces in between.\n\
16477To disable all breakpoints, give no argument.\n\
64b9b334 16478A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16479This command may be abbreviated \"disable\"."),
c906108c
SS
16480 &disablelist);
16481
1bedd215
AC
16482 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16483Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16484Arguments are breakpoint numbers with spaces in between.\n\
16485To delete all breakpoints, give no argument.\n\
16486\n\
16487Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16488The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16489 &deletelist, "delete ", 1, &cmdlist);
16490 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16491 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16492 if (xdb_commands)
1bedd215
AC
16493 add_com ("db", class_breakpoint, delete_command, _("\
16494Delete some breakpoints.\n\
c906108c 16495Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16496To delete all breakpoints, give no argument.\n"));
c906108c 16497
1a966eab
AC
16498 add_cmd ("breakpoints", class_alias, delete_command, _("\
16499Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16500Arguments are breakpoint numbers with spaces in between.\n\
16501To delete all breakpoints, give no argument.\n\
1a966eab 16502This command may be abbreviated \"delete\"."),
c906108c
SS
16503 &deletelist);
16504
1bedd215
AC
16505 add_com ("clear", class_breakpoint, clear_command, _("\
16506Clear breakpoint at specified line or function.\n\
c906108c
SS
16507Argument may be line number, function name, or \"*\" and an address.\n\
16508If line number is specified, all breakpoints in that line are cleared.\n\
16509If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16510If an address is specified, breakpoints at that address are cleared.\n\
16511\n\
16512With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16513is executing in.\n\
16514\n\
1bedd215 16515See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16516 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16517
1bedd215 16518 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16519Set breakpoint at specified line or function.\n"
16520BREAK_ARGS_HELP ("break")));
5ba2abeb 16521 set_cmd_completer (c, location_completer);
c94fdfd0 16522
c906108c
SS
16523 add_com_alias ("b", "break", class_run, 1);
16524 add_com_alias ("br", "break", class_run, 1);
16525 add_com_alias ("bre", "break", class_run, 1);
16526 add_com_alias ("brea", "break", class_run, 1);
16527
7681d515
PM
16528 if (xdb_commands)
16529 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16530
16531 if (dbx_commands)
16532 {
1bedd215
AC
16533 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16534Break in function/address or break at a line in the current file."),
c5aa993b
JM
16535 &stoplist, "stop ", 1, &cmdlist);
16536 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16537 _("Break in function or address."), &stoplist);
c5aa993b 16538 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16539 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16540 add_com ("status", class_info, breakpoints_info, _("\
16541Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16542The \"Type\" column indicates one of:\n\
16543\tbreakpoint - normal breakpoint\n\
16544\twatchpoint - watchpoint\n\
16545The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16546the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16547breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16548address and file/line number respectively.\n\
16549\n\
16550Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16551are set to the address of the last breakpoint listed unless the command\n\
16552is prefixed with \"server \".\n\n\
c906108c 16553Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16554breakpoint set."));
c906108c
SS
16555 }
16556
1bedd215 16557 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16558Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16559The \"Type\" column indicates one of:\n\
16560\tbreakpoint - normal breakpoint\n\
16561\twatchpoint - watchpoint\n\
16562The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16563the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16564breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16565address and file/line number respectively.\n\
16566\n\
16567Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16568are set to the address of the last breakpoint listed unless the command\n\
16569is prefixed with \"server \".\n\n\
c906108c 16570Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16571breakpoint set."));
c906108c 16572
6b04bdb7
MS
16573 add_info_alias ("b", "breakpoints", 1);
16574
c906108c 16575 if (xdb_commands)
1bedd215
AC
16576 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16577Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16578The \"Type\" column indicates one of:\n\
16579\tbreakpoint - normal breakpoint\n\
16580\twatchpoint - watchpoint\n\
16581The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16582the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16583breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16584address and file/line number respectively.\n\
16585\n\
16586Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16587are set to the address of the last breakpoint listed unless the command\n\
16588is prefixed with \"server \".\n\n\
c906108c 16589Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16590breakpoint set."));
c906108c 16591
1a966eab
AC
16592 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16593Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16594The \"Type\" column indicates one of:\n\
16595\tbreakpoint - normal breakpoint\n\
16596\twatchpoint - watchpoint\n\
16597\tlongjmp - internal breakpoint used to step through longjmp()\n\
16598\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16599\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16600\tfinish - internal breakpoint used by the \"finish\" command\n\
16601The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
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\
1a966eab
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\
1a966eab 16610breakpoint set."),
c906108c
SS
16611 &maintenanceinfolist);
16612
44feb3ce
TT
16613 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16614Set catchpoints to catch events."),
16615 &catch_cmdlist, "catch ",
16616 0/*allow-unknown*/, &cmdlist);
16617
16618 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16619Set temporary catchpoints to catch events."),
16620 &tcatch_cmdlist, "tcatch ",
16621 0/*allow-unknown*/, &cmdlist);
16622
44feb3ce
TT
16623 add_catch_command ("fork", _("Catch calls to fork."),
16624 catch_fork_command_1,
a96d9b2e 16625 NULL,
44feb3ce
TT
16626 (void *) (uintptr_t) catch_fork_permanent,
16627 (void *) (uintptr_t) catch_fork_temporary);
16628 add_catch_command ("vfork", _("Catch calls to vfork."),
16629 catch_fork_command_1,
a96d9b2e 16630 NULL,
44feb3ce
TT
16631 (void *) (uintptr_t) catch_vfork_permanent,
16632 (void *) (uintptr_t) catch_vfork_temporary);
16633 add_catch_command ("exec", _("Catch calls to exec."),
16634 catch_exec_command_1,
a96d9b2e
SDJ
16635 NULL,
16636 CATCH_PERMANENT,
16637 CATCH_TEMPORARY);
edcc5120
TT
16638 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16639Usage: catch load [REGEX]\n\
16640If REGEX is given, only stop for libraries matching the regular expression."),
16641 catch_load_command_1,
16642 NULL,
16643 CATCH_PERMANENT,
16644 CATCH_TEMPORARY);
16645 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16646Usage: catch unload [REGEX]\n\
16647If REGEX is given, only stop for libraries matching the regular expression."),
16648 catch_unload_command_1,
16649 NULL,
16650 CATCH_PERMANENT,
16651 CATCH_TEMPORARY);
a96d9b2e
SDJ
16652 add_catch_command ("syscall", _("\
16653Catch system calls by their names and/or numbers.\n\
16654Arguments say which system calls to catch. If no arguments\n\
16655are given, every system call will be caught.\n\
16656Arguments, if given, should be one or more system call names\n\
16657(if your system supports that), or system call numbers."),
16658 catch_syscall_command_1,
16659 catch_syscall_completer,
44feb3ce
TT
16660 CATCH_PERMANENT,
16661 CATCH_TEMPORARY);
c5aa993b 16662
1bedd215
AC
16663 c = add_com ("watch", class_breakpoint, watch_command, _("\
16664Set a watchpoint for an expression.\n\
06a64a0b 16665Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16666A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16667an expression changes.\n\
16668If -l or -location is given, this evaluates EXPRESSION and watches\n\
16669the memory to which it refers."));
65d12d83 16670 set_cmd_completer (c, expression_completer);
c906108c 16671
1bedd215
AC
16672 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16673Set a read watchpoint for an expression.\n\
06a64a0b 16674Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16675A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16676an expression is read.\n\
16677If -l or -location is given, this evaluates EXPRESSION and watches\n\
16678the memory to which it refers."));
65d12d83 16679 set_cmd_completer (c, expression_completer);
c906108c 16680
1bedd215
AC
16681 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16682Set a watchpoint for an expression.\n\
06a64a0b 16683Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16684A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16685an expression is either read or written.\n\
16686If -l or -location is given, this evaluates EXPRESSION and watches\n\
16687the memory to which it refers."));
65d12d83 16688 set_cmd_completer (c, expression_completer);
c906108c 16689
d77f58be 16690 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16691Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16692
920d2a44
AC
16693 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16694 respond to changes - contrary to the description. */
85c07804
AC
16695 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16696 &can_use_hw_watchpoints, _("\
16697Set debugger's willingness to use watchpoint hardware."), _("\
16698Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16699If zero, gdb will not use hardware for new watchpoints, even if\n\
16700such is available. (However, any hardware watchpoints that were\n\
16701created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16702hardware.)"),
16703 NULL,
920d2a44 16704 show_can_use_hw_watchpoints,
85c07804 16705 &setlist, &showlist);
c906108c
SS
16706
16707 can_use_hw_watchpoints = 1;
fa8d40ab 16708
1042e4c0
SS
16709 /* Tracepoint manipulation commands. */
16710
16711 c = add_com ("trace", class_breakpoint, trace_command, _("\
16712Set a tracepoint at specified line or function.\n\
16713\n"
16714BREAK_ARGS_HELP ("trace") "\n\
16715Do \"help tracepoints\" for info on other tracepoint commands."));
16716 set_cmd_completer (c, location_completer);
16717
16718 add_com_alias ("tp", "trace", class_alias, 0);
16719 add_com_alias ("tr", "trace", class_alias, 1);
16720 add_com_alias ("tra", "trace", class_alias, 1);
16721 add_com_alias ("trac", "trace", class_alias, 1);
16722
7a697b8d
SS
16723 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16724Set a fast tracepoint at specified line or function.\n\
16725\n"
16726BREAK_ARGS_HELP ("ftrace") "\n\
16727Do \"help tracepoints\" for info on other tracepoint commands."));
16728 set_cmd_completer (c, location_completer);
16729
0fb4aa4b
PA
16730 c = add_com ("strace", class_breakpoint, strace_command, _("\
16731Set a static tracepoint at specified line, function or marker.\n\
16732\n\
16733strace [LOCATION] [if CONDITION]\n\
16734LOCATION may be a line number, function name, \"*\" and an address,\n\
16735or -m MARKER_ID.\n\
16736If a line number is specified, probe the marker at start of code\n\
16737for that line. If a function is specified, probe the marker at start\n\
16738of code for that function. If an address is specified, probe the marker\n\
16739at that exact address. If a marker id is specified, probe the marker\n\
16740with that name. With no LOCATION, uses current execution address of\n\
16741the selected stack frame.\n\
16742Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16743This collects arbitrary user data passed in the probe point call to the\n\
16744tracing library. You can inspect it when analyzing the trace buffer,\n\
16745by printing the $_sdata variable like any other convenience variable.\n\
16746\n\
16747CONDITION is a boolean expression.\n\
16748\n\
d41c0fc8
PA
16749Multiple tracepoints at one place are permitted, and useful if their\n\
16750conditions are different.\n\
0fb4aa4b
PA
16751\n\
16752Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16753Do \"help tracepoints\" for info on other tracepoint commands."));
16754 set_cmd_completer (c, location_completer);
16755
1042e4c0 16756 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16757Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16758Convenience variable \"$tpnum\" contains the number of the\n\
16759last tracepoint set."));
16760
16761 add_info_alias ("tp", "tracepoints", 1);
16762
16763 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16764Delete specified tracepoints.\n\
16765Arguments are tracepoint numbers, separated by spaces.\n\
16766No argument means delete all tracepoints."),
16767 &deletelist);
7e20dfcd 16768 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16769
16770 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16771Disable specified tracepoints.\n\
16772Arguments are tracepoint numbers, separated by spaces.\n\
16773No argument means disable all tracepoints."),
16774 &disablelist);
16775 deprecate_cmd (c, "disable");
16776
16777 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16778Enable specified tracepoints.\n\
16779Arguments are tracepoint numbers, separated by spaces.\n\
16780No argument means enable all tracepoints."),
16781 &enablelist);
16782 deprecate_cmd (c, "enable");
16783
16784 add_com ("passcount", class_trace, trace_pass_command, _("\
16785Set the passcount for a tracepoint.\n\
16786The trace will end when the tracepoint has been passed 'count' times.\n\
16787Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16788if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16789
6149aea9
PA
16790 add_prefix_cmd ("save", class_breakpoint, save_command,
16791 _("Save breakpoint definitions as a script."),
16792 &save_cmdlist, "save ",
16793 0/*allow-unknown*/, &cmdlist);
16794
16795 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16796Save current breakpoint definitions as a script.\n\
cce7e648 16797This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16798catchpoints, tracepoints). Use the 'source' command in another debug\n\
16799session to restore them."),
16800 &save_cmdlist);
16801 set_cmd_completer (c, filename_completer);
16802
16803 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16804Save current tracepoint definitions as a script.\n\
6149aea9
PA
16805Use the 'source' command in another debug session to restore them."),
16806 &save_cmdlist);
1042e4c0
SS
16807 set_cmd_completer (c, filename_completer);
16808
6149aea9
PA
16809 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16810 deprecate_cmd (c, "save tracepoints");
16811
1bedd215 16812 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16813Breakpoint specific settings\n\
16814Configure various breakpoint-specific variables such as\n\
1bedd215 16815pending breakpoint behavior"),
fa8d40ab
JJ
16816 &breakpoint_set_cmdlist, "set breakpoint ",
16817 0/*allow-unknown*/, &setlist);
1bedd215 16818 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16819Breakpoint specific settings\n\
16820Configure various breakpoint-specific variables such as\n\
1bedd215 16821pending breakpoint behavior"),
fa8d40ab
JJ
16822 &breakpoint_show_cmdlist, "show breakpoint ",
16823 0/*allow-unknown*/, &showlist);
16824
7915a72c
AC
16825 add_setshow_auto_boolean_cmd ("pending", no_class,
16826 &pending_break_support, _("\
16827Set debugger's behavior regarding pending breakpoints."), _("\
16828Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16829If on, an unrecognized breakpoint location will cause gdb to create a\n\
16830pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16831an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16832user-query to see if a pending breakpoint should be created."),
2c5b56ce 16833 NULL,
920d2a44 16834 show_pending_break_support,
6e1d7d6c
AC
16835 &breakpoint_set_cmdlist,
16836 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16837
16838 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16839
16840 add_setshow_boolean_cmd ("auto-hw", no_class,
16841 &automatic_hardware_breakpoints, _("\
16842Set automatic usage of hardware breakpoints."), _("\
16843Show automatic usage of hardware breakpoints."), _("\
16844If set, the debugger will automatically use hardware breakpoints for\n\
16845breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16846a warning will be emitted for such breakpoints."),
16847 NULL,
16848 show_automatic_hardware_breakpoints,
16849 &breakpoint_set_cmdlist,
16850 &breakpoint_show_cmdlist);
74960c60 16851
a25a5a45
PA
16852 add_setshow_boolean_cmd ("always-inserted", class_support,
16853 &always_inserted_mode, _("\
74960c60
VP
16854Set mode for inserting breakpoints."), _("\
16855Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16856When this mode is on, breakpoints are inserted immediately as soon as\n\
16857they're created, kept inserted even when execution stops, and removed\n\
16858only when the user deletes them. When this mode is off (the default),\n\
16859breakpoints are inserted only when execution continues, and removed\n\
16860when execution stops."),
72d0e2c5
YQ
16861 NULL,
16862 &show_always_inserted_mode,
16863 &breakpoint_set_cmdlist,
16864 &breakpoint_show_cmdlist);
f1310107 16865
b775012e
LM
16866 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16867 condition_evaluation_enums,
16868 &condition_evaluation_mode_1, _("\
16869Set mode of breakpoint condition evaluation."), _("\
16870Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16871When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16872evaluated on the host's side by GDB. When it is set to \"target\",\n\
16873breakpoint conditions will be downloaded to the target (if the target\n\
16874supports such feature) and conditions will be evaluated on the target's side.\n\
16875If this is set to \"auto\" (default), this will be automatically set to\n\
16876\"target\" if it supports condition evaluation, otherwise it will\n\
16877be set to \"gdb\""),
16878 &set_condition_evaluation_mode,
16879 &show_condition_evaluation_mode,
16880 &breakpoint_set_cmdlist,
16881 &breakpoint_show_cmdlist);
16882
f1310107
TJB
16883 add_com ("break-range", class_breakpoint, break_range_command, _("\
16884Set a breakpoint for an address range.\n\
16885break-range START-LOCATION, END-LOCATION\n\
16886where START-LOCATION and END-LOCATION can be one of the following:\n\
16887 LINENUM, for that line in the current file,\n\
16888 FILE:LINENUM, for that line in that file,\n\
16889 +OFFSET, for that number of lines after the current line\n\
16890 or the start of the range\n\
16891 FUNCTION, for the first line in that function,\n\
16892 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16893 *ADDRESS, for the instruction at that address.\n\
16894\n\
16895The breakpoint will stop execution of the inferior whenever it executes\n\
16896an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16897range (including START-LOCATION and END-LOCATION)."));
16898
e7e0cddf
SS
16899 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16900Set a dynamic printf at specified line or function.\n\
16901dprintf location,format string,arg1,arg2,...\n\
16902location may be a line number, function name, or \"*\" and an address.\n\
16903If a line number is specified, break at start of code for that line.\n\
0e4777df 16904If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16905 set_cmd_completer (c, location_completer);
16906
16907 add_setshow_enum_cmd ("dprintf-style", class_support,
16908 dprintf_style_enums, &dprintf_style, _("\
16909Set the style of usage for dynamic printf."), _("\
16910Show the style of usage for dynamic printf."), _("\
16911This setting chooses how GDB will do a dynamic printf.\n\
16912If the value is \"gdb\", then the printing is done by GDB to its own\n\
16913console, as with the \"printf\" command.\n\
16914If the value is \"call\", the print is done by calling a function in your\n\
16915program; by default printf(), but you can choose a different function or\n\
16916output stream by setting dprintf-function and dprintf-channel."),
16917 update_dprintf_commands, NULL,
16918 &setlist, &showlist);
16919
16920 dprintf_function = xstrdup ("printf");
16921 add_setshow_string_cmd ("dprintf-function", class_support,
16922 &dprintf_function, _("\
16923Set the function to use for dynamic printf"), _("\
16924Show the function to use for dynamic printf"), NULL,
16925 update_dprintf_commands, NULL,
16926 &setlist, &showlist);
16927
16928 dprintf_channel = xstrdup ("");
16929 add_setshow_string_cmd ("dprintf-channel", class_support,
16930 &dprintf_channel, _("\
16931Set the channel to use for dynamic printf"), _("\
16932Show the channel to use for dynamic printf"), NULL,
16933 update_dprintf_commands, NULL,
16934 &setlist, &showlist);
16935
d3ce09f5
SS
16936 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16937 &disconnected_dprintf, _("\
16938Set whether dprintf continues after GDB disconnects."), _("\
16939Show whether dprintf continues after GDB disconnects."), _("\
16940Use this to let dprintf commands continue to hit and produce output\n\
16941even if GDB disconnects or detaches from the target."),
16942 NULL,
16943 NULL,
16944 &setlist, &showlist);
16945
16946 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16947agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16948(target agent only) This is useful for formatted output in user-defined commands."));
16949
765dc015 16950 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16951
16952 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16953 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16954}
This page took 3.501826 seconds and 4 git commands to generate.