]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
Tell update_global_location_list to insert breakpoints
[thirdparty/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"
60250e8b 54#include "exceptions.h"
765dc015 55#include "memattr.h"
f7f9143b 56#include "ada-lang.h"
d1aa2f50 57#include "top.h"
79a45b7d 58#include "valprint.h"
4efc6507 59#include "jit.h"
a96d9b2e 60#include "xml-syscall.h"
65d79d4b 61#include "parser-defs.h"
55aa24fb
SDJ
62#include "gdb_regex.h"
63#include "probe.h"
e9cafbcc 64#include "cli/cli-utils.h"
be34f849 65#include "continuations.h"
1bfeeb0f
JL
66#include "stack.h"
67#include "skip.h"
b775012e 68#include "ax-gdb.h"
e2e4d78b 69#include "dummy-frame.h"
c906108c 70
d3ce09f5
SS
71#include "format.h"
72
1042e4c0
SS
73/* readline include files */
74#include "readline/readline.h"
75#include "readline/history.h"
76
77/* readline defines this. */
78#undef savestring
79
034dad6f 80#include "mi/mi-common.h"
6dddc817 81#include "extension.h"
104c1213 82
e7e8980f
YQ
83/* Enums for exception-handling support. */
84enum exception_event_kind
85{
86 EX_EVENT_THROW,
591f19e8 87 EX_EVENT_RETHROW,
e7e8980f
YQ
88 EX_EVENT_CATCH
89};
90
4a64f543 91/* Prototypes for local functions. */
c906108c 92
a14ed312 93static void enable_delete_command (char *, int);
c906108c 94
a14ed312 95static void enable_once_command (char *, int);
c906108c 96
816338b5
SS
97static void enable_count_command (char *, int);
98
a14ed312 99static void disable_command (char *, int);
c906108c 100
a14ed312 101static void enable_command (char *, int);
c906108c 102
95a42b64
TT
103static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
104 void *),
105 void *);
c906108c 106
a14ed312 107static void ignore_command (char *, int);
c906108c 108
4efb68b1 109static int breakpoint_re_set_one (void *);
c906108c 110
348d480f
PA
111static void breakpoint_re_set_default (struct breakpoint *);
112
983af33b
SDJ
113static void create_sals_from_address_default (char **,
114 struct linespec_result *,
115 enum bptype, char *,
116 char **);
117
118static void create_breakpoints_sal_default (struct gdbarch *,
119 struct linespec_result *,
e7e0cddf 120 char *, char *, enum bptype,
983af33b
SDJ
121 enum bpdisp, int, int,
122 int,
123 const struct breakpoint_ops *,
44f238bb 124 int, int, int, unsigned);
983af33b
SDJ
125
126static void decode_linespec_default (struct breakpoint *, char **,
127 struct symtabs_and_lines *);
128
a14ed312 129static void clear_command (char *, int);
c906108c 130
a14ed312 131static void catch_command (char *, int);
c906108c 132
a9634178 133static int can_use_hardware_watchpoint (struct value *);
c906108c 134
98deb0da 135static void break_command_1 (char *, int, int);
c906108c 136
a14ed312 137static void mention (struct breakpoint *);
c906108c 138
348d480f
PA
139static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
140 enum bptype,
c0a91b2b 141 const struct breakpoint_ops *);
3742cc8b
YQ
142static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
143 const struct symtab_and_line *);
144
4a64f543
MS
145/* This function is used in gdbtk sources and thus can not be made
146 static. */
63c252f8 147struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 148 struct symtab_and_line,
c0a91b2b
TT
149 enum bptype,
150 const struct breakpoint_ops *);
c906108c 151
06edf0c0
PA
152static struct breakpoint *
153 momentary_breakpoint_from_master (struct breakpoint *orig,
154 enum bptype type,
a1aa2221
LM
155 const struct breakpoint_ops *ops,
156 int loc_enabled);
06edf0c0 157
76897487
KB
158static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
159
a6d9a66e
UW
160static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
161 CORE_ADDR bpaddr,
88f7da05 162 enum bptype bptype);
76897487 163
6c95b8df
PA
164static void describe_other_breakpoints (struct gdbarch *,
165 struct program_space *, CORE_ADDR,
5af949e3 166 struct obj_section *, int);
c906108c 167
85d721b8
PA
168static int watchpoint_locations_match (struct bp_location *loc1,
169 struct bp_location *loc2);
170
f1310107
TJB
171static int breakpoint_location_address_match (struct bp_location *bl,
172 struct address_space *aspace,
173 CORE_ADDR addr);
174
a14ed312 175static void breakpoints_info (char *, int);
c906108c 176
d77f58be
SS
177static void watchpoints_info (char *, int);
178
e5a67952
MS
179static int breakpoint_1 (char *, int,
180 int (*) (const struct breakpoint *));
c906108c 181
4efb68b1 182static int breakpoint_cond_eval (void *);
c906108c 183
4efb68b1 184static void cleanup_executing_breakpoints (void *);
c906108c 185
a14ed312 186static void commands_command (char *, int);
c906108c 187
a14ed312 188static void condition_command (char *, int);
c906108c 189
c5aa993b
JM
190typedef enum
191 {
192 mark_inserted,
193 mark_uninserted
194 }
195insertion_state_t;
c906108c 196
0bde7532 197static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 198static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 199
e514a9d6 200static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 201
4efb68b1 202static int watchpoint_check (void *);
c906108c 203
a14ed312 204static void maintenance_info_breakpoints (char *, int);
c906108c 205
a14ed312 206static int hw_breakpoint_used_count (void);
c906108c 207
a1398e0c
PA
208static int hw_watchpoint_use_count (struct breakpoint *);
209
210static int hw_watchpoint_used_count_others (struct breakpoint *except,
211 enum bptype type,
212 int *other_type_used);
c906108c 213
a14ed312 214static void hbreak_command (char *, int);
c906108c 215
a14ed312 216static void thbreak_command (char *, int);
c906108c 217
816338b5
SS
218static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
219 int count);
c906108c 220
a14ed312 221static void stop_command (char *arg, int from_tty);
7a292a7a 222
a14ed312 223static void stopin_command (char *arg, int from_tty);
7a292a7a 224
a14ed312 225static void stopat_command (char *arg, int from_tty);
7a292a7a 226
a14ed312 227static void tcatch_command (char *arg, int from_tty);
7a292a7a 228
d03285ec
UW
229static void detach_single_step_breakpoints (void);
230
ef370185
JB
231static int find_single_step_breakpoint (struct address_space *aspace,
232 CORE_ADDR pc);
233
fe3f5fa8 234static void free_bp_location (struct bp_location *loc);
f431efe5
PA
235static void incref_bp_location (struct bp_location *loc);
236static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 237
39d61571 238static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 239
44702360
PA
240/* update_global_location_list's modes of operation wrt to whether to
241 insert locations now. */
242enum ugll_insert_mode
243{
244 /* Don't insert any breakpoint locations into the inferior, only
245 remove already-inserted locations that no longer should be
246 inserted. Functions that delete a breakpoint or breakpoints
247 should specify this mode, so that deleting a breakpoint doesn't
248 have the side effect of inserting the locations of other
249 breakpoints that are marked not-inserted, but should_be_inserted
250 returns true on them.
251
252 This behavior is useful is situations close to tear-down -- e.g.,
253 after an exec, while the target still has execution, but
254 breakpoint shadows of the previous executable image should *NOT*
255 be restored to the new image; or before detaching, where the
256 target still has execution and wants to delete breakpoints from
257 GDB's lists, and all breakpoints had already been removed from
258 the inferior. */
259 UGLL_DONT_INSERT,
260
261 /* May insert breakpoints if breakpoints_always_inserted_mode is
262 true. */
04086b45
PA
263 UGLL_MAY_INSERT,
264
265 /* Insert locations now, even if breakpoints_always_inserted_mode is
266 false. */
267 UGLL_INSERT
44702360
PA
268};
269
270static void update_global_location_list (enum ugll_insert_mode);
a5606eee 271
44702360 272static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 273
d77f58be 274static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
275
276static void insert_breakpoint_locations (void);
a5606eee 277
a96d9b2e
SDJ
278static int syscall_catchpoint_p (struct breakpoint *b);
279
1042e4c0
SS
280static void tracepoints_info (char *, int);
281
282static void delete_trace_command (char *, int);
283
284static void enable_trace_command (char *, int);
285
286static void disable_trace_command (char *, int);
287
288static void trace_pass_command (char *, int);
289
558a9d82
YQ
290static void set_tracepoint_count (int num);
291
9c06b0b4
TJB
292static int is_masked_watchpoint (const struct breakpoint *b);
293
b775012e
LM
294static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
295
983af33b
SDJ
296/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
297 otherwise. */
298
299static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 300
2060206e
PA
301/* The abstract base class all breakpoint_ops structures inherit
302 from. */
ab04a2af 303struct breakpoint_ops base_breakpoint_ops;
2060206e
PA
304
305/* The breakpoint_ops structure to be inherited by all breakpoint_ops
306 that are implemented on top of software or hardware breakpoints
307 (user breakpoints, internal and momentary breakpoints, etc.). */
308static struct breakpoint_ops bkpt_base_breakpoint_ops;
309
310/* Internal breakpoints class type. */
06edf0c0 311static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
312
313/* Momentary breakpoints class type. */
06edf0c0
PA
314static struct breakpoint_ops momentary_breakpoint_ops;
315
e2e4d78b
JK
316/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
317static struct breakpoint_ops longjmp_breakpoint_ops;
318
2060206e
PA
319/* The breakpoint_ops structure to be used in regular user created
320 breakpoints. */
321struct breakpoint_ops bkpt_breakpoint_ops;
322
55aa24fb
SDJ
323/* Breakpoints set on probes. */
324static struct breakpoint_ops bkpt_probe_breakpoint_ops;
325
e7e0cddf 326/* Dynamic printf class type. */
c5867ab6 327struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 328
9d497a19
PA
329/* One (or perhaps two) breakpoints used for software single
330 stepping. */
331
332static void *single_step_breakpoints[2];
333static struct gdbarch *single_step_gdbarch[2];
334
d3ce09f5
SS
335/* The style in which to perform a dynamic printf. This is a user
336 option because different output options have different tradeoffs;
337 if GDB does the printing, there is better error handling if there
338 is a problem with any of the arguments, but using an inferior
339 function lets you have special-purpose printers and sending of
340 output to the same place as compiled-in print functions. */
341
342static const char dprintf_style_gdb[] = "gdb";
343static const char dprintf_style_call[] = "call";
344static const char dprintf_style_agent[] = "agent";
345static const char *const dprintf_style_enums[] = {
346 dprintf_style_gdb,
347 dprintf_style_call,
348 dprintf_style_agent,
349 NULL
350};
351static const char *dprintf_style = dprintf_style_gdb;
352
353/* The function to use for dynamic printf if the preferred style is to
354 call into the inferior. The value is simply a string that is
355 copied into the command, so it can be anything that GDB can
356 evaluate to a callable address, not necessarily a function name. */
357
358static char *dprintf_function = "";
359
360/* The channel to use for dynamic printf if the preferred style is to
361 call into the inferior; if a nonempty string, it will be passed to
362 the call as the first argument, with the format string as the
363 second. As with the dprintf function, this can be anything that
364 GDB knows how to evaluate, so in addition to common choices like
365 "stderr", this could be an app-specific expression like
366 "mystreams[curlogger]". */
367
368static char *dprintf_channel = "";
369
370/* True if dprintf commands should continue to operate even if GDB
371 has disconnected. */
372static int disconnected_dprintf = 1;
373
5cea2a26
PA
374/* A reference-counted struct command_line. This lets multiple
375 breakpoints share a single command list. */
376struct counted_command_line
377{
378 /* The reference count. */
379 int refc;
380
381 /* The command list. */
382 struct command_line *commands;
383};
384
385struct command_line *
386breakpoint_commands (struct breakpoint *b)
387{
388 return b->commands ? b->commands->commands : NULL;
389}
3daf8fe5 390
f3b1572e
PA
391/* Flag indicating that a command has proceeded the inferior past the
392 current breakpoint. */
393
394static int breakpoint_proceeded;
395
956a9fb9 396const char *
2cec12e5
AR
397bpdisp_text (enum bpdisp disp)
398{
4a64f543
MS
399 /* NOTE: the following values are a part of MI protocol and
400 represent values of 'disp' field returned when inferior stops at
401 a breakpoint. */
bc043ef3 402 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 403
2cec12e5
AR
404 return bpdisps[(int) disp];
405}
c906108c 406
4a64f543 407/* Prototypes for exported functions. */
c906108c 408/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 409 if such is available. */
c906108c
SS
410static int can_use_hw_watchpoints;
411
920d2a44
AC
412static void
413show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
414 struct cmd_list_element *c,
415 const char *value)
416{
3e43a32a
MS
417 fprintf_filtered (file,
418 _("Debugger's willingness to use "
419 "watchpoint hardware is %s.\n"),
920d2a44
AC
420 value);
421}
422
fa8d40ab
JJ
423/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
424 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 425 for unrecognized breakpoint locations.
fa8d40ab
JJ
426 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
427static enum auto_boolean pending_break_support;
920d2a44
AC
428static void
429show_pending_break_support (struct ui_file *file, int from_tty,
430 struct cmd_list_element *c,
431 const char *value)
432{
3e43a32a
MS
433 fprintf_filtered (file,
434 _("Debugger's behavior regarding "
435 "pending breakpoints is %s.\n"),
920d2a44
AC
436 value);
437}
fa8d40ab 438
765dc015 439/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 440 set with "break" but falling in read-only memory.
765dc015
VP
441 If 0, gdb will warn about such breakpoints, but won't automatically
442 use hardware breakpoints. */
443static int automatic_hardware_breakpoints;
444static void
445show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
446 struct cmd_list_element *c,
447 const char *value)
448{
3e43a32a
MS
449 fprintf_filtered (file,
450 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
451 value);
452}
453
33e5cbd6
PA
454/* If on, gdb will keep breakpoints inserted even as inferior is
455 stopped, and immediately insert any new breakpoints. If off, gdb
456 will insert breakpoints into inferior only when resuming it, and
457 will remove breakpoints upon stop. If auto, GDB will behave as ON
458 if in non-stop mode, and as OFF if all-stop mode.*/
459
72d0e2c5
YQ
460static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
461
33e5cbd6 462static void
74960c60 463show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 464 struct cmd_list_element *c, const char *value)
74960c60 465{
72d0e2c5 466 if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
3e43a32a
MS
467 fprintf_filtered (file,
468 _("Always inserted breakpoint "
469 "mode is %s (currently %s).\n"),
33e5cbd6
PA
470 value,
471 breakpoints_always_inserted_mode () ? "on" : "off");
472 else
3e43a32a
MS
473 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
474 value);
74960c60
VP
475}
476
33e5cbd6
PA
477int
478breakpoints_always_inserted_mode (void)
479{
72d0e2c5
YQ
480 return (always_inserted_mode == AUTO_BOOLEAN_TRUE
481 || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
33e5cbd6 482}
765dc015 483
b775012e
LM
484static const char condition_evaluation_both[] = "host or target";
485
486/* Modes for breakpoint condition evaluation. */
487static const char condition_evaluation_auto[] = "auto";
488static const char condition_evaluation_host[] = "host";
489static const char condition_evaluation_target[] = "target";
490static const char *const condition_evaluation_enums[] = {
491 condition_evaluation_auto,
492 condition_evaluation_host,
493 condition_evaluation_target,
494 NULL
495};
496
497/* Global that holds the current mode for breakpoint condition evaluation. */
498static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
499
500/* Global that we use to display information to the user (gets its value from
501 condition_evaluation_mode_1. */
502static const char *condition_evaluation_mode = condition_evaluation_auto;
503
504/* Translate a condition evaluation mode MODE into either "host"
505 or "target". This is used mostly to translate from "auto" to the
506 real setting that is being used. It returns the translated
507 evaluation mode. */
508
509static const char *
510translate_condition_evaluation_mode (const char *mode)
511{
512 if (mode == condition_evaluation_auto)
513 {
514 if (target_supports_evaluation_of_breakpoint_conditions ())
515 return condition_evaluation_target;
516 else
517 return condition_evaluation_host;
518 }
519 else
520 return mode;
521}
522
523/* Discovers what condition_evaluation_auto translates to. */
524
525static const char *
526breakpoint_condition_evaluation_mode (void)
527{
528 return translate_condition_evaluation_mode (condition_evaluation_mode);
529}
530
531/* Return true if GDB should evaluate breakpoint conditions or false
532 otherwise. */
533
534static int
535gdb_evaluates_breakpoint_condition_p (void)
536{
537 const char *mode = breakpoint_condition_evaluation_mode ();
538
539 return (mode == condition_evaluation_host);
540}
541
a14ed312 542void _initialize_breakpoint (void);
c906108c 543
c906108c
SS
544/* Are we executing breakpoint commands? */
545static int executing_breakpoint_commands;
546
c02f5703
MS
547/* Are overlay event breakpoints enabled? */
548static int overlay_events_enabled;
549
e09342b5
TJB
550/* See description in breakpoint.h. */
551int target_exact_watchpoints = 0;
552
c906108c 553/* Walk the following statement or block through all breakpoints.
e5dd4106 554 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 555 current breakpoint. */
c906108c 556
5c44784c 557#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 558
5c44784c
JM
559#define ALL_BREAKPOINTS_SAFE(B,TMP) \
560 for (B = breakpoint_chain; \
561 B ? (TMP=B->next, 1): 0; \
562 B = TMP)
c906108c 563
4a64f543
MS
564/* Similar iterator for the low-level breakpoints. SAFE variant is
565 not provided so update_global_location_list must not be called
566 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 567
876fa593
JK
568#define ALL_BP_LOCATIONS(B,BP_TMP) \
569 for (BP_TMP = bp_location; \
570 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
571 BP_TMP++)
7cc221ef 572
b775012e
LM
573/* Iterates through locations with address ADDRESS for the currently selected
574 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
575 to where the loop should start from.
576 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
577 appropriate location to start with. */
578
579#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
580 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
581 BP_LOCP_TMP = BP_LOCP_START; \
582 BP_LOCP_START \
583 && (BP_LOCP_TMP < bp_location + bp_location_count \
584 && (*BP_LOCP_TMP)->address == ADDRESS); \
585 BP_LOCP_TMP++)
586
1042e4c0
SS
587/* Iterator for tracepoints only. */
588
589#define ALL_TRACEPOINTS(B) \
590 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 591 if (is_tracepoint (B))
1042e4c0 592
7cc221ef 593/* Chains of all breakpoints defined. */
c906108c
SS
594
595struct breakpoint *breakpoint_chain;
596
876fa593
JK
597/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
598
599static struct bp_location **bp_location;
600
601/* Number of elements of BP_LOCATION. */
602
603static unsigned bp_location_count;
604
4a64f543
MS
605/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
606 ADDRESS for the current elements of BP_LOCATION which get a valid
607 result from bp_location_has_shadow. You can use it for roughly
608 limiting the subrange of BP_LOCATION to scan for shadow bytes for
609 an address you need to read. */
876fa593
JK
610
611static CORE_ADDR bp_location_placed_address_before_address_max;
612
4a64f543
MS
613/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
614 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
615 BP_LOCATION which get a valid result from bp_location_has_shadow.
616 You can use it for roughly limiting the subrange of BP_LOCATION to
617 scan for shadow bytes for an address you need to read. */
876fa593
JK
618
619static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 620
4a64f543
MS
621/* The locations that no longer correspond to any breakpoint, unlinked
622 from bp_location array, but for which a hit may still be reported
623 by a target. */
20874c92
VP
624VEC(bp_location_p) *moribund_locations = NULL;
625
c906108c
SS
626/* Number of last breakpoint made. */
627
95a42b64
TT
628static int breakpoint_count;
629
86b17b60
PA
630/* The value of `breakpoint_count' before the last command that
631 created breakpoints. If the last (break-like) command created more
632 than one breakpoint, then the difference between BREAKPOINT_COUNT
633 and PREV_BREAKPOINT_COUNT is more than one. */
634static int prev_breakpoint_count;
c906108c 635
1042e4c0
SS
636/* Number of last tracepoint made. */
637
95a42b64 638static int tracepoint_count;
1042e4c0 639
6149aea9
PA
640static struct cmd_list_element *breakpoint_set_cmdlist;
641static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 642struct cmd_list_element *save_cmdlist;
6149aea9 643
468d015d
JJ
644/* Return whether a breakpoint is an active enabled breakpoint. */
645static int
646breakpoint_enabled (struct breakpoint *b)
647{
0d381245 648 return (b->enable_state == bp_enabled);
468d015d
JJ
649}
650
c906108c
SS
651/* Set breakpoint count to NUM. */
652
95a42b64 653static void
fba45db2 654set_breakpoint_count (int num)
c906108c 655{
86b17b60 656 prev_breakpoint_count = breakpoint_count;
c906108c 657 breakpoint_count = num;
4fa62494 658 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
659}
660
86b17b60
PA
661/* Used by `start_rbreak_breakpoints' below, to record the current
662 breakpoint count before "rbreak" creates any breakpoint. */
663static int rbreak_start_breakpoint_count;
664
95a42b64
TT
665/* Called at the start an "rbreak" command to record the first
666 breakpoint made. */
86b17b60 667
95a42b64
TT
668void
669start_rbreak_breakpoints (void)
670{
86b17b60 671 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
672}
673
674/* Called at the end of an "rbreak" command to record the last
675 breakpoint made. */
86b17b60 676
95a42b64
TT
677void
678end_rbreak_breakpoints (void)
679{
86b17b60 680 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
681}
682
4a64f543 683/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
684
685void
fba45db2 686clear_breakpoint_hit_counts (void)
c906108c
SS
687{
688 struct breakpoint *b;
689
690 ALL_BREAKPOINTS (b)
691 b->hit_count = 0;
692}
693
9add0f1b
TT
694/* Allocate a new counted_command_line with reference count of 1.
695 The new structure owns COMMANDS. */
696
697static struct counted_command_line *
698alloc_counted_command_line (struct command_line *commands)
699{
700 struct counted_command_line *result
701 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 702
9add0f1b
TT
703 result->refc = 1;
704 result->commands = commands;
705 return result;
706}
707
708/* Increment reference count. This does nothing if CMD is NULL. */
709
710static void
711incref_counted_command_line (struct counted_command_line *cmd)
712{
713 if (cmd)
714 ++cmd->refc;
715}
716
717/* Decrement reference count. If the reference count reaches 0,
718 destroy the counted_command_line. Sets *CMDP to NULL. This does
719 nothing if *CMDP is NULL. */
720
721static void
722decref_counted_command_line (struct counted_command_line **cmdp)
723{
724 if (*cmdp)
725 {
726 if (--(*cmdp)->refc == 0)
727 {
728 free_command_lines (&(*cmdp)->commands);
729 xfree (*cmdp);
730 }
731 *cmdp = NULL;
732 }
733}
734
735/* A cleanup function that calls decref_counted_command_line. */
736
737static void
738do_cleanup_counted_command_line (void *arg)
739{
740 decref_counted_command_line (arg);
741}
742
743/* Create a cleanup that calls decref_counted_command_line on the
744 argument. */
745
746static struct cleanup *
747make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
748{
749 return make_cleanup (do_cleanup_counted_command_line, cmdp);
750}
751
c906108c 752\f
48cb2d85
VP
753/* Return the breakpoint with the specified number, or NULL
754 if the number does not refer to an existing breakpoint. */
755
756struct breakpoint *
757get_breakpoint (int num)
758{
759 struct breakpoint *b;
760
761 ALL_BREAKPOINTS (b)
762 if (b->number == num)
763 return b;
764
765 return NULL;
766}
5c44784c 767
c906108c 768\f
adc36818 769
b775012e
LM
770/* Mark locations as "conditions have changed" in case the target supports
771 evaluating conditions on its side. */
772
773static void
774mark_breakpoint_modified (struct breakpoint *b)
775{
776 struct bp_location *loc;
777
778 /* This is only meaningful if the target is
779 evaluating conditions and if the user has
780 opted for condition evaluation on the target's
781 side. */
782 if (gdb_evaluates_breakpoint_condition_p ()
783 || !target_supports_evaluation_of_breakpoint_conditions ())
784 return;
785
786 if (!is_breakpoint (b))
787 return;
788
789 for (loc = b->loc; loc; loc = loc->next)
790 loc->condition_changed = condition_modified;
791}
792
793/* Mark location as "conditions have changed" in case the target supports
794 evaluating conditions on its side. */
795
796static void
797mark_breakpoint_location_modified (struct bp_location *loc)
798{
799 /* This is only meaningful if the target is
800 evaluating conditions and if the user has
801 opted for condition evaluation on the target's
802 side. */
803 if (gdb_evaluates_breakpoint_condition_p ()
804 || !target_supports_evaluation_of_breakpoint_conditions ())
805
806 return;
807
808 if (!is_breakpoint (loc->owner))
809 return;
810
811 loc->condition_changed = condition_modified;
812}
813
814/* Sets the condition-evaluation mode using the static global
815 condition_evaluation_mode. */
816
817static void
818set_condition_evaluation_mode (char *args, int from_tty,
819 struct cmd_list_element *c)
820{
b775012e
LM
821 const char *old_mode, *new_mode;
822
823 if ((condition_evaluation_mode_1 == condition_evaluation_target)
824 && !target_supports_evaluation_of_breakpoint_conditions ())
825 {
826 condition_evaluation_mode_1 = condition_evaluation_mode;
827 warning (_("Target does not support breakpoint condition evaluation.\n"
828 "Using host evaluation mode instead."));
829 return;
830 }
831
832 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
833 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
834
abf1152a
JK
835 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
836 settings was "auto". */
837 condition_evaluation_mode = condition_evaluation_mode_1;
838
b775012e
LM
839 /* Only update the mode if the user picked a different one. */
840 if (new_mode != old_mode)
841 {
842 struct bp_location *loc, **loc_tmp;
843 /* If the user switched to a different evaluation mode, we
844 need to synch the changes with the target as follows:
845
846 "host" -> "target": Send all (valid) conditions to the target.
847 "target" -> "host": Remove all the conditions from the target.
848 */
849
b775012e
LM
850 if (new_mode == condition_evaluation_target)
851 {
852 /* Mark everything modified and synch conditions with the
853 target. */
854 ALL_BP_LOCATIONS (loc, loc_tmp)
855 mark_breakpoint_location_modified (loc);
856 }
857 else
858 {
859 /* Manually mark non-duplicate locations to synch conditions
860 with the target. We do this to remove all the conditions the
861 target knows about. */
862 ALL_BP_LOCATIONS (loc, loc_tmp)
863 if (is_breakpoint (loc->owner) && loc->inserted)
864 loc->needs_update = 1;
865 }
866
867 /* Do the update. */
44702360 868 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
869 }
870
871 return;
872}
873
874/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
875 what "auto" is translating to. */
876
877static void
878show_condition_evaluation_mode (struct ui_file *file, int from_tty,
879 struct cmd_list_element *c, const char *value)
880{
881 if (condition_evaluation_mode == condition_evaluation_auto)
882 fprintf_filtered (file,
883 _("Breakpoint condition evaluation "
884 "mode is %s (currently %s).\n"),
885 value,
886 breakpoint_condition_evaluation_mode ());
887 else
888 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
889 value);
890}
891
892/* A comparison function for bp_location AP and BP that is used by
893 bsearch. This comparison function only cares about addresses, unlike
894 the more general bp_location_compare function. */
895
896static int
897bp_location_compare_addrs (const void *ap, const void *bp)
898{
899 struct bp_location *a = *(void **) ap;
900 struct bp_location *b = *(void **) bp;
901
902 if (a->address == b->address)
903 return 0;
904 else
905 return ((a->address > b->address) - (a->address < b->address));
906}
907
908/* Helper function to skip all bp_locations with addresses
909 less than ADDRESS. It returns the first bp_location that
910 is greater than or equal to ADDRESS. If none is found, just
911 return NULL. */
912
913static struct bp_location **
914get_first_locp_gte_addr (CORE_ADDR address)
915{
916 struct bp_location dummy_loc;
917 struct bp_location *dummy_locp = &dummy_loc;
918 struct bp_location **locp_found = NULL;
919
920 /* Initialize the dummy location's address field. */
921 memset (&dummy_loc, 0, sizeof (struct bp_location));
922 dummy_loc.address = address;
923
924 /* Find a close match to the first location at ADDRESS. */
925 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
926 sizeof (struct bp_location **),
927 bp_location_compare_addrs);
928
929 /* Nothing was found, nothing left to do. */
930 if (locp_found == NULL)
931 return NULL;
932
933 /* We may have found a location that is at ADDRESS but is not the first in the
934 location's list. Go backwards (if possible) and locate the first one. */
935 while ((locp_found - 1) >= bp_location
936 && (*(locp_found - 1))->address == address)
937 locp_found--;
938
939 return locp_found;
940}
941
adc36818
PM
942void
943set_breakpoint_condition (struct breakpoint *b, char *exp,
944 int from_tty)
945{
3a5c3e22
PA
946 xfree (b->cond_string);
947 b->cond_string = NULL;
adc36818 948
3a5c3e22 949 if (is_watchpoint (b))
adc36818 950 {
3a5c3e22
PA
951 struct watchpoint *w = (struct watchpoint *) b;
952
953 xfree (w->cond_exp);
954 w->cond_exp = NULL;
955 }
956 else
957 {
958 struct bp_location *loc;
959
960 for (loc = b->loc; loc; loc = loc->next)
961 {
962 xfree (loc->cond);
963 loc->cond = NULL;
b775012e
LM
964
965 /* No need to free the condition agent expression
966 bytecode (if we have one). We will handle this
967 when we go through update_global_location_list. */
3a5c3e22 968 }
adc36818 969 }
adc36818
PM
970
971 if (*exp == 0)
972 {
973 if (from_tty)
974 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
975 }
976 else
977 {
bbc13ae3 978 const char *arg = exp;
cc59ec59 979
adc36818
PM
980 /* I don't know if it matters whether this is the string the user
981 typed in or the decompiled expression. */
982 b->cond_string = xstrdup (arg);
983 b->condition_not_parsed = 0;
984
985 if (is_watchpoint (b))
986 {
3a5c3e22
PA
987 struct watchpoint *w = (struct watchpoint *) b;
988
adc36818
PM
989 innermost_block = NULL;
990 arg = exp;
1bb9788d 991 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
992 if (*arg)
993 error (_("Junk at end of expression"));
3a5c3e22 994 w->cond_exp_valid_block = innermost_block;
adc36818
PM
995 }
996 else
997 {
3a5c3e22
PA
998 struct bp_location *loc;
999
adc36818
PM
1000 for (loc = b->loc; loc; loc = loc->next)
1001 {
1002 arg = exp;
1003 loc->cond =
1bb9788d
TT
1004 parse_exp_1 (&arg, loc->address,
1005 block_for_pc (loc->address), 0);
adc36818
PM
1006 if (*arg)
1007 error (_("Junk at end of expression"));
1008 }
1009 }
1010 }
b775012e
LM
1011 mark_breakpoint_modified (b);
1012
8d3788bd 1013 observer_notify_breakpoint_modified (b);
adc36818
PM
1014}
1015
d55637df
TT
1016/* Completion for the "condition" command. */
1017
1018static VEC (char_ptr) *
6f937416
PA
1019condition_completer (struct cmd_list_element *cmd,
1020 const char *text, const char *word)
d55637df 1021{
6f937416 1022 const char *space;
d55637df 1023
6f937416
PA
1024 text = skip_spaces_const (text);
1025 space = skip_to_space_const (text);
d55637df
TT
1026 if (*space == '\0')
1027 {
1028 int len;
1029 struct breakpoint *b;
1030 VEC (char_ptr) *result = NULL;
1031
1032 if (text[0] == '$')
1033 {
1034 /* We don't support completion of history indices. */
1035 if (isdigit (text[1]))
1036 return NULL;
1037 return complete_internalvar (&text[1]);
1038 }
1039
1040 /* We're completing the breakpoint number. */
1041 len = strlen (text);
1042
1043 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1044 {
1045 char number[50];
1046
1047 xsnprintf (number, sizeof (number), "%d", b->number);
1048
1049 if (strncmp (number, text, len) == 0)
1050 VEC_safe_push (char_ptr, result, xstrdup (number));
1051 }
d55637df
TT
1052
1053 return result;
1054 }
1055
1056 /* We're completing the expression part. */
6f937416 1057 text = skip_spaces_const (space);
d55637df
TT
1058 return expression_completer (cmd, text, word);
1059}
1060
c906108c
SS
1061/* condition N EXP -- set break condition of breakpoint N to EXP. */
1062
1063static void
fba45db2 1064condition_command (char *arg, int from_tty)
c906108c 1065{
52f0bd74 1066 struct breakpoint *b;
c906108c 1067 char *p;
52f0bd74 1068 int bnum;
c906108c
SS
1069
1070 if (arg == 0)
e2e0b3e5 1071 error_no_arg (_("breakpoint number"));
c906108c
SS
1072
1073 p = arg;
1074 bnum = get_number (&p);
5c44784c 1075 if (bnum == 0)
8a3fe4f8 1076 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1077
1078 ALL_BREAKPOINTS (b)
1079 if (b->number == bnum)
2f069f6f 1080 {
6dddc817
DE
1081 /* Check if this breakpoint has a "stop" method implemented in an
1082 extension language. This method and conditions entered into GDB
1083 from the CLI are mutually exclusive. */
1084 const struct extension_language_defn *extlang
1085 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1086
1087 if (extlang != NULL)
1088 {
1089 error (_("Only one stop condition allowed. There is currently"
1090 " a %s stop condition defined for this breakpoint."),
1091 ext_lang_capitalized_name (extlang));
1092 }
2566ad2d 1093 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1094
1095 if (is_breakpoint (b))
44702360 1096 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1097
2f069f6f
JB
1098 return;
1099 }
c906108c 1100
8a3fe4f8 1101 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1102}
1103
a7bdde9e
VP
1104/* Check that COMMAND do not contain commands that are suitable
1105 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1106 Throw if any such commands is found. */
1107
a7bdde9e
VP
1108static void
1109check_no_tracepoint_commands (struct command_line *commands)
1110{
1111 struct command_line *c;
cc59ec59 1112
a7bdde9e
VP
1113 for (c = commands; c; c = c->next)
1114 {
1115 int i;
1116
1117 if (c->control_type == while_stepping_control)
3e43a32a
MS
1118 error (_("The 'while-stepping' command can "
1119 "only be used for tracepoints"));
a7bdde9e
VP
1120
1121 for (i = 0; i < c->body_count; ++i)
1122 check_no_tracepoint_commands ((c->body_list)[i]);
1123
1124 /* Not that command parsing removes leading whitespace and comment
4a64f543 1125 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1126 command directly. */
1127 if (strstr (c->line, "collect ") == c->line)
1128 error (_("The 'collect' command can only be used for tracepoints"));
1129
51661e93
VP
1130 if (strstr (c->line, "teval ") == c->line)
1131 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1132 }
1133}
1134
d77f58be
SS
1135/* Encapsulate tests for different types of tracepoints. */
1136
d9b3f62e
PA
1137static int
1138is_tracepoint_type (enum bptype type)
1139{
1140 return (type == bp_tracepoint
1141 || type == bp_fast_tracepoint
1142 || type == bp_static_tracepoint);
1143}
1144
a7bdde9e 1145int
d77f58be 1146is_tracepoint (const struct breakpoint *b)
a7bdde9e 1147{
d9b3f62e 1148 return is_tracepoint_type (b->type);
a7bdde9e 1149}
d9b3f62e 1150
e5dd4106 1151/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1152 breakpoint. This function will throw an exception if a problem is
1153 found. */
48cb2d85 1154
95a42b64
TT
1155static void
1156validate_commands_for_breakpoint (struct breakpoint *b,
1157 struct command_line *commands)
48cb2d85 1158{
d77f58be 1159 if (is_tracepoint (b))
a7bdde9e 1160 {
c9a6ce02 1161 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1162 struct command_line *c;
1163 struct command_line *while_stepping = 0;
c9a6ce02
PA
1164
1165 /* Reset the while-stepping step count. The previous commands
1166 might have included a while-stepping action, while the new
1167 ones might not. */
1168 t->step_count = 0;
1169
1170 /* We need to verify that each top-level element of commands is
1171 valid for tracepoints, that there's at most one
1172 while-stepping element, and that the while-stepping's body
1173 has valid tracing commands excluding nested while-stepping.
1174 We also need to validate the tracepoint action line in the
1175 context of the tracepoint --- validate_actionline actually
1176 has side effects, like setting the tracepoint's
1177 while-stepping STEP_COUNT, in addition to checking if the
1178 collect/teval actions parse and make sense in the
1179 tracepoint's context. */
a7bdde9e
VP
1180 for (c = commands; c; c = c->next)
1181 {
a7bdde9e
VP
1182 if (c->control_type == while_stepping_control)
1183 {
1184 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1185 error (_("The 'while-stepping' command "
1186 "cannot be used for fast tracepoint"));
0fb4aa4b 1187 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1188 error (_("The 'while-stepping' command "
1189 "cannot be used for static tracepoint"));
a7bdde9e
VP
1190
1191 if (while_stepping)
3e43a32a
MS
1192 error (_("The 'while-stepping' command "
1193 "can be used only once"));
a7bdde9e
VP
1194 else
1195 while_stepping = c;
1196 }
c9a6ce02
PA
1197
1198 validate_actionline (c->line, b);
a7bdde9e
VP
1199 }
1200 if (while_stepping)
1201 {
1202 struct command_line *c2;
1203
1204 gdb_assert (while_stepping->body_count == 1);
1205 c2 = while_stepping->body_list[0];
1206 for (; c2; c2 = c2->next)
1207 {
a7bdde9e
VP
1208 if (c2->control_type == while_stepping_control)
1209 error (_("The 'while-stepping' command cannot be nested"));
1210 }
1211 }
1212 }
1213 else
1214 {
1215 check_no_tracepoint_commands (commands);
1216 }
95a42b64
TT
1217}
1218
0fb4aa4b
PA
1219/* Return a vector of all the static tracepoints set at ADDR. The
1220 caller is responsible for releasing the vector. */
1221
1222VEC(breakpoint_p) *
1223static_tracepoints_here (CORE_ADDR addr)
1224{
1225 struct breakpoint *b;
1226 VEC(breakpoint_p) *found = 0;
1227 struct bp_location *loc;
1228
1229 ALL_BREAKPOINTS (b)
1230 if (b->type == bp_static_tracepoint)
1231 {
1232 for (loc = b->loc; loc; loc = loc->next)
1233 if (loc->address == addr)
1234 VEC_safe_push(breakpoint_p, found, b);
1235 }
1236
1237 return found;
1238}
1239
95a42b64 1240/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1241 validate that only allowed commands are included. */
95a42b64
TT
1242
1243void
4a64f543
MS
1244breakpoint_set_commands (struct breakpoint *b,
1245 struct command_line *commands)
95a42b64
TT
1246{
1247 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1248
9add0f1b
TT
1249 decref_counted_command_line (&b->commands);
1250 b->commands = alloc_counted_command_line (commands);
8d3788bd 1251 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1252}
1253
45a43567
TT
1254/* Set the internal `silent' flag on the breakpoint. Note that this
1255 is not the same as the "silent" that may appear in the breakpoint's
1256 commands. */
1257
1258void
1259breakpoint_set_silent (struct breakpoint *b, int silent)
1260{
1261 int old_silent = b->silent;
1262
1263 b->silent = silent;
1264 if (old_silent != silent)
8d3788bd 1265 observer_notify_breakpoint_modified (b);
45a43567
TT
1266}
1267
1268/* Set the thread for this breakpoint. If THREAD is -1, make the
1269 breakpoint work for any thread. */
1270
1271void
1272breakpoint_set_thread (struct breakpoint *b, int thread)
1273{
1274 int old_thread = b->thread;
1275
1276 b->thread = thread;
1277 if (old_thread != thread)
8d3788bd 1278 observer_notify_breakpoint_modified (b);
45a43567
TT
1279}
1280
1281/* Set the task for this breakpoint. If TASK is 0, make the
1282 breakpoint work for any task. */
1283
1284void
1285breakpoint_set_task (struct breakpoint *b, int task)
1286{
1287 int old_task = b->task;
1288
1289 b->task = task;
1290 if (old_task != task)
8d3788bd 1291 observer_notify_breakpoint_modified (b);
45a43567
TT
1292}
1293
95a42b64
TT
1294void
1295check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1296{
1297 struct breakpoint *b = closure;
cc59ec59 1298
6f937416 1299 validate_actionline (line, b);
a7bdde9e
VP
1300}
1301
95a42b64
TT
1302/* A structure used to pass information through
1303 map_breakpoint_numbers. */
1304
1305struct commands_info
1306{
1307 /* True if the command was typed at a tty. */
1308 int from_tty;
86b17b60
PA
1309
1310 /* The breakpoint range spec. */
1311 char *arg;
1312
95a42b64
TT
1313 /* Non-NULL if the body of the commands are being read from this
1314 already-parsed command. */
1315 struct command_line *control;
86b17b60 1316
95a42b64
TT
1317 /* The command lines read from the user, or NULL if they have not
1318 yet been read. */
1319 struct counted_command_line *cmd;
1320};
1321
1322/* A callback for map_breakpoint_numbers that sets the commands for
1323 commands_command. */
1324
c906108c 1325static void
95a42b64 1326do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1327{
95a42b64 1328 struct commands_info *info = data;
c906108c 1329
95a42b64
TT
1330 if (info->cmd == NULL)
1331 {
1332 struct command_line *l;
5c44784c 1333
95a42b64
TT
1334 if (info->control != NULL)
1335 l = copy_command_lines (info->control->body_list[0]);
1336 else
86b17b60
PA
1337 {
1338 struct cleanup *old_chain;
1339 char *str;
c5aa993b 1340
3e43a32a
MS
1341 str = xstrprintf (_("Type commands for breakpoint(s) "
1342 "%s, one per line."),
86b17b60
PA
1343 info->arg);
1344
1345 old_chain = make_cleanup (xfree, str);
1346
1347 l = read_command_lines (str,
1348 info->from_tty, 1,
d77f58be 1349 (is_tracepoint (b)
86b17b60
PA
1350 ? check_tracepoint_command : 0),
1351 b);
1352
1353 do_cleanups (old_chain);
1354 }
a7bdde9e 1355
95a42b64
TT
1356 info->cmd = alloc_counted_command_line (l);
1357 }
1358
1359 /* If a breakpoint was on the list more than once, we don't need to
1360 do anything. */
1361 if (b->commands != info->cmd)
1362 {
1363 validate_commands_for_breakpoint (b, info->cmd->commands);
1364 incref_counted_command_line (info->cmd);
1365 decref_counted_command_line (&b->commands);
1366 b->commands = info->cmd;
8d3788bd 1367 observer_notify_breakpoint_modified (b);
c5aa993b 1368 }
95a42b64
TT
1369}
1370
1371static void
4a64f543
MS
1372commands_command_1 (char *arg, int from_tty,
1373 struct command_line *control)
95a42b64
TT
1374{
1375 struct cleanup *cleanups;
1376 struct commands_info info;
1377
1378 info.from_tty = from_tty;
1379 info.control = control;
1380 info.cmd = NULL;
1381 /* If we read command lines from the user, then `info' will hold an
1382 extra reference to the commands that we must clean up. */
1383 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1384
1385 if (arg == NULL || !*arg)
1386 {
86b17b60 1387 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1388 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1389 breakpoint_count);
95a42b64
TT
1390 else if (breakpoint_count > 0)
1391 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1392 else
1393 {
1394 /* So that we don't try to free the incoming non-NULL
1395 argument in the cleanup below. Mapping breakpoint
1396 numbers will fail in this case. */
1397 arg = NULL;
1398 }
95a42b64 1399 }
9766ced4
SS
1400 else
1401 /* The command loop has some static state, so we need to preserve
1402 our argument. */
1403 arg = xstrdup (arg);
86b17b60
PA
1404
1405 if (arg != NULL)
1406 make_cleanup (xfree, arg);
1407
1408 info.arg = arg;
95a42b64
TT
1409
1410 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1411
1412 if (info.cmd == NULL)
1413 error (_("No breakpoints specified."));
1414
1415 do_cleanups (cleanups);
1416}
1417
1418static void
1419commands_command (char *arg, int from_tty)
1420{
1421 commands_command_1 (arg, from_tty, NULL);
c906108c 1422}
40c03ae8
EZ
1423
1424/* Like commands_command, but instead of reading the commands from
1425 input stream, takes them from an already parsed command structure.
1426
1427 This is used by cli-script.c to DTRT with breakpoint commands
1428 that are part of if and while bodies. */
1429enum command_control_type
1430commands_from_control_command (char *arg, struct command_line *cmd)
1431{
95a42b64
TT
1432 commands_command_1 (arg, 0, cmd);
1433 return simple_control;
40c03ae8 1434}
876fa593
JK
1435
1436/* Return non-zero if BL->TARGET_INFO contains valid information. */
1437
1438static int
1439bp_location_has_shadow (struct bp_location *bl)
1440{
1441 if (bl->loc_type != bp_loc_software_breakpoint)
1442 return 0;
1443 if (!bl->inserted)
1444 return 0;
1445 if (bl->target_info.shadow_len == 0)
e5dd4106 1446 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1447 return 0;
1448 return 1;
1449}
1450
9d497a19
PA
1451/* Update BUF, which is LEN bytes read from the target address
1452 MEMADDR, by replacing a memory breakpoint with its shadowed
1453 contents.
1454
1455 If READBUF is not NULL, this buffer must not overlap with the of
1456 the breakpoint location's shadow_contents buffer. Otherwise, a
1457 failed assertion internal error will be raised. */
1458
1459static void
1460one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1461 const gdb_byte *writebuf_org,
1462 ULONGEST memaddr, LONGEST len,
1463 struct bp_target_info *target_info,
1464 struct gdbarch *gdbarch)
1465{
1466 /* Now do full processing of the found relevant range of elements. */
1467 CORE_ADDR bp_addr = 0;
1468 int bp_size = 0;
1469 int bptoffset = 0;
1470
1471 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1472 current_program_space->aspace, 0))
1473 {
1474 /* The breakpoint is inserted in a different address space. */
1475 return;
1476 }
1477
1478 /* Addresses and length of the part of the breakpoint that
1479 we need to copy. */
1480 bp_addr = target_info->placed_address;
1481 bp_size = target_info->shadow_len;
1482
1483 if (bp_addr + bp_size <= memaddr)
1484 {
1485 /* The breakpoint is entirely before the chunk of memory we are
1486 reading. */
1487 return;
1488 }
1489
1490 if (bp_addr >= memaddr + len)
1491 {
1492 /* The breakpoint is entirely after the chunk of memory we are
1493 reading. */
1494 return;
1495 }
1496
1497 /* Offset within shadow_contents. */
1498 if (bp_addr < memaddr)
1499 {
1500 /* Only copy the second part of the breakpoint. */
1501 bp_size -= memaddr - bp_addr;
1502 bptoffset = memaddr - bp_addr;
1503 bp_addr = memaddr;
1504 }
1505
1506 if (bp_addr + bp_size > memaddr + len)
1507 {
1508 /* Only copy the first part of the breakpoint. */
1509 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1510 }
1511
1512 if (readbuf != NULL)
1513 {
1514 /* Verify that the readbuf buffer does not overlap with the
1515 shadow_contents buffer. */
1516 gdb_assert (target_info->shadow_contents >= readbuf + len
1517 || readbuf >= (target_info->shadow_contents
1518 + target_info->shadow_len));
1519
1520 /* Update the read buffer with this inserted breakpoint's
1521 shadow. */
1522 memcpy (readbuf + bp_addr - memaddr,
1523 target_info->shadow_contents + bptoffset, bp_size);
1524 }
1525 else
1526 {
1527 const unsigned char *bp;
1528 CORE_ADDR placed_address = target_info->placed_address;
1529 int placed_size = target_info->placed_size;
1530
1531 /* Update the shadow with what we want to write to memory. */
1532 memcpy (target_info->shadow_contents + bptoffset,
1533 writebuf_org + bp_addr - memaddr, bp_size);
1534
1535 /* Determine appropriate breakpoint contents and size for this
1536 address. */
1537 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1538
1539 /* Update the final write buffer with this inserted
1540 breakpoint's INSN. */
1541 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1542 }
1543}
1544
8defab1a 1545/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1546 by replacing any memory breakpoints with their shadowed contents.
1547
35c63cd8
JB
1548 If READBUF is not NULL, this buffer must not overlap with any of
1549 the breakpoint location's shadow_contents buffers. Otherwise,
1550 a failed assertion internal error will be raised.
1551
876fa593 1552 The range of shadowed area by each bp_location is:
35df4500
TJB
1553 bl->address - bp_location_placed_address_before_address_max
1554 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1555 The range we were requested to resolve shadows for is:
1556 memaddr ... memaddr + len
1557 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1558 memaddr + len <= (bl->address
1559 - bp_location_placed_address_before_address_max)
876fa593 1560 and:
35df4500 1561 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1562
8defab1a 1563void
f0ba3972
PA
1564breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1565 const gdb_byte *writebuf_org,
1566 ULONGEST memaddr, LONGEST len)
c906108c 1567{
4a64f543
MS
1568 /* Left boundary, right boundary and median element of our binary
1569 search. */
876fa593 1570 unsigned bc_l, bc_r, bc;
9d497a19 1571 size_t i;
876fa593 1572
4a64f543
MS
1573 /* Find BC_L which is a leftmost element which may affect BUF
1574 content. It is safe to report lower value but a failure to
1575 report higher one. */
876fa593
JK
1576
1577 bc_l = 0;
1578 bc_r = bp_location_count;
1579 while (bc_l + 1 < bc_r)
1580 {
35df4500 1581 struct bp_location *bl;
876fa593
JK
1582
1583 bc = (bc_l + bc_r) / 2;
35df4500 1584 bl = bp_location[bc];
876fa593 1585
4a64f543
MS
1586 /* Check first BL->ADDRESS will not overflow due to the added
1587 constant. Then advance the left boundary only if we are sure
1588 the BC element can in no way affect the BUF content (MEMADDR
1589 to MEMADDR + LEN range).
876fa593 1590
4a64f543
MS
1591 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1592 offset so that we cannot miss a breakpoint with its shadow
1593 range tail still reaching MEMADDR. */
c5aa993b 1594
35df4500
TJB
1595 if ((bl->address + bp_location_shadow_len_after_address_max
1596 >= bl->address)
1597 && (bl->address + bp_location_shadow_len_after_address_max
1598 <= memaddr))
876fa593
JK
1599 bc_l = bc;
1600 else
1601 bc_r = bc;
1602 }
1603
128070bb
PA
1604 /* Due to the binary search above, we need to make sure we pick the
1605 first location that's at BC_L's address. E.g., if there are
1606 multiple locations at the same address, BC_L may end up pointing
1607 at a duplicate location, and miss the "master"/"inserted"
1608 location. Say, given locations L1, L2 and L3 at addresses A and
1609 B:
1610
1611 L1@A, L2@A, L3@B, ...
1612
1613 BC_L could end up pointing at location L2, while the "master"
1614 location could be L1. Since the `loc->inserted' flag is only set
1615 on "master" locations, we'd forget to restore the shadow of L1
1616 and L2. */
1617 while (bc_l > 0
1618 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1619 bc_l--;
1620
876fa593
JK
1621 /* Now do full processing of the found relevant range of elements. */
1622
1623 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1624 {
35df4500 1625 struct bp_location *bl = bp_location[bc];
876fa593
JK
1626 CORE_ADDR bp_addr = 0;
1627 int bp_size = 0;
1628 int bptoffset = 0;
1629
35df4500
TJB
1630 /* bp_location array has BL->OWNER always non-NULL. */
1631 if (bl->owner->type == bp_none)
8a3fe4f8 1632 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1633 bl->owner->number);
ffce0d52 1634
e5dd4106 1635 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1636 content. */
1637
35df4500
TJB
1638 if (bl->address >= bp_location_placed_address_before_address_max
1639 && memaddr + len <= (bl->address
1640 - bp_location_placed_address_before_address_max))
876fa593
JK
1641 break;
1642
35df4500 1643 if (!bp_location_has_shadow (bl))
c5aa993b 1644 continue;
6c95b8df 1645
9d497a19
PA
1646 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1647 memaddr, len, &bl->target_info, bl->gdbarch);
1648 }
c5aa993b 1649
9d497a19
PA
1650 /* Now process single-step breakpoints. These are not found in the
1651 bp_location array. */
1652 for (i = 0; i < 2; i++)
1653 {
1654 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
c5aa993b 1655
9d497a19
PA
1656 if (bp_tgt != NULL)
1657 {
1658 struct gdbarch *gdbarch = single_step_gdbarch[i];
c5aa993b 1659
9d497a19
PA
1660 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1661 memaddr, len, bp_tgt, gdbarch);
1662 }
1663 }
c906108c 1664}
9d497a19 1665
c906108c 1666\f
c5aa993b 1667
b775012e
LM
1668/* Return true if BPT is either a software breakpoint or a hardware
1669 breakpoint. */
1670
1671int
1672is_breakpoint (const struct breakpoint *bpt)
1673{
1674 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1675 || bpt->type == bp_hardware_breakpoint
1676 || bpt->type == bp_dprintf);
b775012e
LM
1677}
1678
60e1c644
PA
1679/* Return true if BPT is of any hardware watchpoint kind. */
1680
a5606eee 1681static int
d77f58be 1682is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1683{
1684 return (bpt->type == bp_hardware_watchpoint
1685 || bpt->type == bp_read_watchpoint
1686 || bpt->type == bp_access_watchpoint);
1687}
7270d8f2 1688
60e1c644
PA
1689/* Return true if BPT is of any watchpoint kind, hardware or
1690 software. */
1691
3a5c3e22 1692int
d77f58be 1693is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1694{
1695 return (is_hardware_watchpoint (bpt)
1696 || bpt->type == bp_watchpoint);
1697}
1698
3a5c3e22
PA
1699/* Returns true if the current thread and its running state are safe
1700 to evaluate or update watchpoint B. Watchpoints on local
1701 expressions need to be evaluated in the context of the thread that
1702 was current when the watchpoint was created, and, that thread needs
1703 to be stopped to be able to select the correct frame context.
1704 Watchpoints on global expressions can be evaluated on any thread,
1705 and in any state. It is presently left to the target allowing
1706 memory accesses when threads are running. */
f6bc2008
PA
1707
1708static int
3a5c3e22 1709watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1710{
d0d8b0c6
JK
1711 return (b->base.pspace == current_program_space
1712 && (ptid_equal (b->watchpoint_thread, null_ptid)
1713 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1714 && !is_executing (inferior_ptid))));
f6bc2008
PA
1715}
1716
d0fb5eae
JK
1717/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1718 associated bp_watchpoint_scope breakpoint. */
1719
1720static void
3a5c3e22 1721watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1722{
3a5c3e22 1723 struct breakpoint *b = &w->base;
d0fb5eae
JK
1724
1725 if (b->related_breakpoint != b)
1726 {
1727 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1728 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1729 b->related_breakpoint->disposition = disp_del_at_next_stop;
1730 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1731 b->related_breakpoint = b;
1732 }
1733 b->disposition = disp_del_at_next_stop;
1734}
1735
bb9d5f81
PP
1736/* Extract a bitfield value from value VAL using the bit parameters contained in
1737 watchpoint W. */
1738
1739static struct value *
1740extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1741{
1742 struct value *bit_val;
1743
1744 if (val == NULL)
1745 return NULL;
1746
1747 bit_val = allocate_value (value_type (val));
1748
1749 unpack_value_bitfield (bit_val,
1750 w->val_bitpos,
1751 w->val_bitsize,
1752 value_contents_for_printing (val),
1753 value_offset (val),
1754 val);
1755
1756 return bit_val;
1757}
1758
567e1b4e
JB
1759/* Assuming that B is a watchpoint:
1760 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1761 - Evaluate expression and store the result in B->val
567e1b4e
JB
1762 - Evaluate the condition if there is one, and store the result
1763 in b->loc->cond.
a5606eee
VP
1764 - Update the list of values that must be watched in B->loc.
1765
4a64f543
MS
1766 If the watchpoint disposition is disp_del_at_next_stop, then do
1767 nothing. If this is local watchpoint that is out of scope, delete
1768 it.
1769
1770 Even with `set breakpoint always-inserted on' the watchpoints are
1771 removed + inserted on each stop here. Normal breakpoints must
1772 never be removed because they might be missed by a running thread
1773 when debugging in non-stop mode. On the other hand, hardware
1774 watchpoints (is_hardware_watchpoint; processed here) are specific
1775 to each LWP since they are stored in each LWP's hardware debug
1776 registers. Therefore, such LWP must be stopped first in order to
1777 be able to modify its hardware watchpoints.
1778
1779 Hardware watchpoints must be reset exactly once after being
1780 presented to the user. It cannot be done sooner, because it would
1781 reset the data used to present the watchpoint hit to the user. And
1782 it must not be done later because it could display the same single
1783 watchpoint hit during multiple GDB stops. Note that the latter is
1784 relevant only to the hardware watchpoint types bp_read_watchpoint
1785 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1786 not user-visible - its hit is suppressed if the memory content has
1787 not changed.
1788
1789 The following constraints influence the location where we can reset
1790 hardware watchpoints:
1791
1792 * target_stopped_by_watchpoint and target_stopped_data_address are
1793 called several times when GDB stops.
1794
1795 [linux]
1796 * Multiple hardware watchpoints can be hit at the same time,
1797 causing GDB to stop. GDB only presents one hardware watchpoint
1798 hit at a time as the reason for stopping, and all the other hits
1799 are presented later, one after the other, each time the user
1800 requests the execution to be resumed. Execution is not resumed
1801 for the threads still having pending hit event stored in
1802 LWP_INFO->STATUS. While the watchpoint is already removed from
1803 the inferior on the first stop the thread hit event is kept being
1804 reported from its cached value by linux_nat_stopped_data_address
1805 until the real thread resume happens after the watchpoint gets
1806 presented and thus its LWP_INFO->STATUS gets reset.
1807
1808 Therefore the hardware watchpoint hit can get safely reset on the
1809 watchpoint removal from inferior. */
a79d3c27 1810
b40ce68a 1811static void
3a5c3e22 1812update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1813{
a5606eee 1814 int within_current_scope;
a5606eee 1815 struct frame_id saved_frame_id;
66076460 1816 int frame_saved;
a5606eee 1817
f6bc2008
PA
1818 /* If this is a local watchpoint, we only want to check if the
1819 watchpoint frame is in scope if the current thread is the thread
1820 that was used to create the watchpoint. */
1821 if (!watchpoint_in_thread_scope (b))
1822 return;
1823
3a5c3e22 1824 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1825 return;
1826
66076460 1827 frame_saved = 0;
a5606eee
VP
1828
1829 /* Determine if the watchpoint is within scope. */
1830 if (b->exp_valid_block == NULL)
1831 within_current_scope = 1;
1832 else
1833 {
b5db5dfc
UW
1834 struct frame_info *fi = get_current_frame ();
1835 struct gdbarch *frame_arch = get_frame_arch (fi);
1836 CORE_ADDR frame_pc = get_frame_pc (fi);
1837
1838 /* If we're in a function epilogue, unwinding may not work
1839 properly, so do not attempt to recreate locations at this
1840 point. See similar comments in watchpoint_check. */
1841 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1842 return;
66076460
DJ
1843
1844 /* Save the current frame's ID so we can restore it after
1845 evaluating the watchpoint expression on its own frame. */
1846 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1847 took a frame parameter, so that we didn't have to change the
1848 selected frame. */
1849 frame_saved = 1;
1850 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1851
a5606eee
VP
1852 fi = frame_find_by_id (b->watchpoint_frame);
1853 within_current_scope = (fi != NULL);
1854 if (within_current_scope)
1855 select_frame (fi);
1856 }
1857
b5db5dfc
UW
1858 /* We don't free locations. They are stored in the bp_location array
1859 and update_global_location_list will eventually delete them and
1860 remove breakpoints if needed. */
3a5c3e22 1861 b->base.loc = NULL;
b5db5dfc 1862
a5606eee
VP
1863 if (within_current_scope && reparse)
1864 {
bbc13ae3 1865 const char *s;
d63d0675 1866
a5606eee
VP
1867 if (b->exp)
1868 {
1869 xfree (b->exp);
1870 b->exp = NULL;
1871 }
d63d0675 1872 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1873 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1874 /* If the meaning of expression itself changed, the old value is
1875 no longer relevant. We don't want to report a watchpoint hit
1876 to the user when the old value and the new value may actually
1877 be completely different objects. */
1878 value_free (b->val);
fa4727a6
DJ
1879 b->val = NULL;
1880 b->val_valid = 0;
60e1c644
PA
1881
1882 /* Note that unlike with breakpoints, the watchpoint's condition
1883 expression is stored in the breakpoint object, not in the
1884 locations (re)created below. */
3a5c3e22 1885 if (b->base.cond_string != NULL)
60e1c644
PA
1886 {
1887 if (b->cond_exp != NULL)
1888 {
1889 xfree (b->cond_exp);
1890 b->cond_exp = NULL;
1891 }
1892
3a5c3e22 1893 s = b->base.cond_string;
1bb9788d 1894 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1895 }
a5606eee 1896 }
a5606eee
VP
1897
1898 /* If we failed to parse the expression, for example because
1899 it refers to a global variable in a not-yet-loaded shared library,
1900 don't try to insert watchpoint. We don't automatically delete
1901 such watchpoint, though, since failure to parse expression
1902 is different from out-of-scope watchpoint. */
e8369a73 1903 if (!target_has_execution)
2d134ed3
PA
1904 {
1905 /* Without execution, memory can't change. No use to try and
1906 set watchpoint locations. The watchpoint will be reset when
1907 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1908 if (!can_use_hw_watchpoints)
1909 {
1910 if (b->base.ops->works_in_software_mode (&b->base))
1911 b->base.type = bp_watchpoint;
1912 else
638aa5a1
AB
1913 error (_("Can't set read/access watchpoint when "
1914 "hardware watchpoints are disabled."));
e8369a73 1915 }
2d134ed3
PA
1916 }
1917 else if (within_current_scope && b->exp)
a5606eee 1918 {
0cf6dd15 1919 int pc = 0;
fa4727a6 1920 struct value *val_chain, *v, *result, *next;
2d134ed3 1921 struct program_space *frame_pspace;
a5606eee 1922
3a1115a0 1923 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1924
a5606eee
VP
1925 /* Avoid setting b->val if it's already set. The meaning of
1926 b->val is 'the last value' user saw, and we should update
1927 it only if we reported that last value to user. As it
9c06b0b4
TJB
1928 happens, the code that reports it updates b->val directly.
1929 We don't keep track of the memory value for masked
1930 watchpoints. */
3a5c3e22 1931 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1932 {
bb9d5f81
PP
1933 if (b->val_bitsize != 0)
1934 {
1935 v = extract_bitfield_from_watchpoint_value (b, v);
1936 if (v != NULL)
1937 release_value (v);
1938 }
fa4727a6
DJ
1939 b->val = v;
1940 b->val_valid = 1;
1941 }
a5606eee 1942
2d134ed3
PA
1943 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1944
a5606eee 1945 /* Look at each value on the value chain. */
9fa40276 1946 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1947 {
1948 /* If it's a memory location, and GDB actually needed
1949 its contents to evaluate the expression, then we
fa4727a6
DJ
1950 must watch it. If the first value returned is
1951 still lazy, that means an error occurred reading it;
1952 watch it anyway in case it becomes readable. */
a5606eee 1953 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1954 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1955 {
1956 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1957
a5606eee
VP
1958 /* We only watch structs and arrays if user asked
1959 for it explicitly, never if they just happen to
1960 appear in the middle of some value chain. */
fa4727a6 1961 if (v == result
a5606eee
VP
1962 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1963 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1964 {
1965 CORE_ADDR addr;
744a8059 1966 int type;
a5606eee 1967 struct bp_location *loc, **tmp;
bb9d5f81
PP
1968 int bitpos = 0, bitsize = 0;
1969
1970 if (value_bitsize (v) != 0)
1971 {
1972 /* Extract the bit parameters out from the bitfield
1973 sub-expression. */
1974 bitpos = value_bitpos (v);
1975 bitsize = value_bitsize (v);
1976 }
1977 else if (v == result && b->val_bitsize != 0)
1978 {
1979 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1980 lvalue whose bit parameters are saved in the fields
1981 VAL_BITPOS and VAL_BITSIZE. */
1982 bitpos = b->val_bitpos;
1983 bitsize = b->val_bitsize;
1984 }
a5606eee 1985
42ae5230 1986 addr = value_address (v);
bb9d5f81
PP
1987 if (bitsize != 0)
1988 {
1989 /* Skip the bytes that don't contain the bitfield. */
1990 addr += bitpos / 8;
1991 }
1992
a5606eee 1993 type = hw_write;
3a5c3e22 1994 if (b->base.type == bp_read_watchpoint)
a5606eee 1995 type = hw_read;
3a5c3e22 1996 else if (b->base.type == bp_access_watchpoint)
a5606eee 1997 type = hw_access;
3a5c3e22
PA
1998
1999 loc = allocate_bp_location (&b->base);
2000 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2001 ;
2002 *tmp = loc;
a6d9a66e 2003 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2004
2005 loc->pspace = frame_pspace;
a5606eee 2006 loc->address = addr;
bb9d5f81
PP
2007
2008 if (bitsize != 0)
2009 {
2010 /* Just cover the bytes that make up the bitfield. */
2011 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2012 }
2013 else
2014 loc->length = TYPE_LENGTH (value_type (v));
2015
a5606eee
VP
2016 loc->watchpoint_type = type;
2017 }
2018 }
9fa40276
TJB
2019 }
2020
2021 /* Change the type of breakpoint between hardware assisted or
2022 an ordinary watchpoint depending on the hardware support
2023 and free hardware slots. REPARSE is set when the inferior
2024 is started. */
a9634178 2025 if (reparse)
9fa40276 2026 {
e09342b5 2027 int reg_cnt;
9fa40276
TJB
2028 enum bp_loc_type loc_type;
2029 struct bp_location *bl;
a5606eee 2030
a9634178 2031 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2032
2033 if (reg_cnt)
9fa40276
TJB
2034 {
2035 int i, target_resources_ok, other_type_used;
a1398e0c 2036 enum bptype type;
9fa40276 2037
a9634178
TJB
2038 /* Use an exact watchpoint when there's only one memory region to be
2039 watched, and only one debug register is needed to watch it. */
2040 b->exact = target_exact_watchpoints && reg_cnt == 1;
2041
9fa40276 2042 /* We need to determine how many resources are already
e09342b5
TJB
2043 used for all other hardware watchpoints plus this one
2044 to see if we still have enough resources to also fit
a1398e0c
PA
2045 this watchpoint in as well. */
2046
2047 /* If this is a software watchpoint, we try to turn it
2048 to a hardware one -- count resources as if B was of
2049 hardware watchpoint type. */
2050 type = b->base.type;
2051 if (type == bp_watchpoint)
2052 type = bp_hardware_watchpoint;
2053
2054 /* This watchpoint may or may not have been placed on
2055 the list yet at this point (it won't be in the list
2056 if we're trying to create it for the first time,
2057 through watch_command), so always account for it
2058 manually. */
2059
2060 /* Count resources used by all watchpoints except B. */
2061 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2062
2063 /* Add in the resources needed for B. */
2064 i += hw_watchpoint_use_count (&b->base);
2065
2066 target_resources_ok
2067 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2068 if (target_resources_ok <= 0)
a9634178 2069 {
3a5c3e22 2070 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2071
2072 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2073 error (_("Target does not support this type of "
2074 "hardware watchpoint."));
9c06b0b4
TJB
2075 else if (target_resources_ok < 0 && !sw_mode)
2076 error (_("There are not enough available hardware "
2077 "resources for this watchpoint."));
a1398e0c
PA
2078
2079 /* Downgrade to software watchpoint. */
2080 b->base.type = bp_watchpoint;
2081 }
2082 else
2083 {
2084 /* If this was a software watchpoint, we've just
2085 found we have enough resources to turn it to a
2086 hardware watchpoint. Otherwise, this is a
2087 nop. */
2088 b->base.type = type;
a9634178 2089 }
9fa40276 2090 }
3a5c3e22 2091 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2092 {
2093 if (!can_use_hw_watchpoints)
2094 error (_("Can't set read/access watchpoint when "
2095 "hardware watchpoints are disabled."));
2096 else
2097 error (_("Expression cannot be implemented with "
2098 "read/access watchpoint."));
2099 }
9fa40276 2100 else
3a5c3e22 2101 b->base.type = bp_watchpoint;
9fa40276 2102
3a5c3e22 2103 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2104 : bp_loc_hardware_watchpoint);
3a5c3e22 2105 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2106 bl->loc_type = loc_type;
2107 }
2108
2109 for (v = val_chain; v; v = next)
2110 {
a5606eee
VP
2111 next = value_next (v);
2112 if (v != b->val)
2113 value_free (v);
2114 }
2115
c7437ca6
PA
2116 /* If a software watchpoint is not watching any memory, then the
2117 above left it without any location set up. But,
2118 bpstat_stop_status requires a location to be able to report
2119 stops, so make sure there's at least a dummy one. */
3a5c3e22 2120 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2121 {
3a5c3e22
PA
2122 struct breakpoint *base = &b->base;
2123 base->loc = allocate_bp_location (base);
2124 base->loc->pspace = frame_pspace;
2125 base->loc->address = -1;
2126 base->loc->length = -1;
2127 base->loc->watchpoint_type = -1;
c7437ca6 2128 }
a5606eee
VP
2129 }
2130 else if (!within_current_scope)
7270d8f2 2131 {
ac74f770
MS
2132 printf_filtered (_("\
2133Watchpoint %d deleted because the program has left the block\n\
2134in which its expression is valid.\n"),
3a5c3e22 2135 b->base.number);
d0fb5eae 2136 watchpoint_del_at_next_stop (b);
7270d8f2 2137 }
a5606eee
VP
2138
2139 /* Restore the selected frame. */
66076460
DJ
2140 if (frame_saved)
2141 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2142}
2143
a5606eee 2144
74960c60 2145/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2146 inserted in the inferior. We don't differentiate the type of BL's owner
2147 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2148 breakpoint_ops is not defined, because in insert_bp_location,
2149 tracepoint's insert_location will not be called. */
74960c60 2150static int
35df4500 2151should_be_inserted (struct bp_location *bl)
74960c60 2152{
35df4500 2153 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2154 return 0;
2155
35df4500 2156 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2157 return 0;
2158
35df4500 2159 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2160 return 0;
2161
f8eba3c6
TT
2162 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2163 return 0;
2164
56710373
PA
2165 /* This is set for example, when we're attached to the parent of a
2166 vfork, and have detached from the child. The child is running
2167 free, and we expect it to do an exec or exit, at which point the
2168 OS makes the parent schedulable again (and the target reports
2169 that the vfork is done). Until the child is done with the shared
2170 memory region, do not insert breakpoints in the parent, otherwise
2171 the child could still trip on the parent's breakpoints. Since
2172 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2173 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2174 return 0;
2175
31e77af2
PA
2176 /* Don't insert a breakpoint if we're trying to step past its
2177 location. */
2178 if ((bl->loc_type == bp_loc_software_breakpoint
2179 || bl->loc_type == bp_loc_hardware_breakpoint)
2180 && stepping_past_instruction_at (bl->pspace->aspace,
2181 bl->address))
2182 return 0;
2183
74960c60
VP
2184 return 1;
2185}
2186
934709f0
PW
2187/* Same as should_be_inserted but does the check assuming
2188 that the location is not duplicated. */
2189
2190static int
2191unduplicated_should_be_inserted (struct bp_location *bl)
2192{
2193 int result;
2194 const int save_duplicate = bl->duplicate;
2195
2196 bl->duplicate = 0;
2197 result = should_be_inserted (bl);
2198 bl->duplicate = save_duplicate;
2199 return result;
2200}
2201
b775012e
LM
2202/* Parses a conditional described by an expression COND into an
2203 agent expression bytecode suitable for evaluation
2204 by the bytecode interpreter. Return NULL if there was
2205 any error during parsing. */
2206
2207static struct agent_expr *
2208parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2209{
2210 struct agent_expr *aexpr = NULL;
b775012e
LM
2211 volatile struct gdb_exception ex;
2212
2213 if (!cond)
2214 return NULL;
2215
2216 /* We don't want to stop processing, so catch any errors
2217 that may show up. */
2218 TRY_CATCH (ex, RETURN_MASK_ERROR)
2219 {
2220 aexpr = gen_eval_for_expr (scope, cond);
2221 }
2222
2223 if (ex.reason < 0)
2224 {
2225 /* If we got here, it means the condition could not be parsed to a valid
2226 bytecode expression and thus can't be evaluated on the target's side.
2227 It's no use iterating through the conditions. */
2228 return NULL;
2229 }
2230
2231 /* We have a valid agent expression. */
2232 return aexpr;
2233}
2234
2235/* Based on location BL, create a list of breakpoint conditions to be
2236 passed on to the target. If we have duplicated locations with different
2237 conditions, we will add such conditions to the list. The idea is that the
2238 target will evaluate the list of conditions and will only notify GDB when
2239 one of them is true. */
2240
2241static void
2242build_target_condition_list (struct bp_location *bl)
2243{
2244 struct bp_location **locp = NULL, **loc2p;
2245 int null_condition_or_parse_error = 0;
2246 int modified = bl->needs_update;
2247 struct bp_location *loc;
2248
8b4f3082
PA
2249 /* Release conditions left over from a previous insert. */
2250 VEC_free (agent_expr_p, bl->target_info.conditions);
2251
b775012e
LM
2252 /* This is only meaningful if the target is
2253 evaluating conditions and if the user has
2254 opted for condition evaluation on the target's
2255 side. */
2256 if (gdb_evaluates_breakpoint_condition_p ()
2257 || !target_supports_evaluation_of_breakpoint_conditions ())
2258 return;
2259
2260 /* Do a first pass to check for locations with no assigned
2261 conditions or conditions that fail to parse to a valid agent expression
2262 bytecode. If any of these happen, then it's no use to send conditions
2263 to the target since this location will always trigger and generate a
2264 response back to GDB. */
2265 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2266 {
2267 loc = (*loc2p);
2268 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2269 {
2270 if (modified)
2271 {
2272 struct agent_expr *aexpr;
2273
2274 /* Re-parse the conditions since something changed. In that
2275 case we already freed the condition bytecodes (see
2276 force_breakpoint_reinsertion). We just
2277 need to parse the condition to bytecodes again. */
2278 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2279 loc->cond_bytecode = aexpr;
2280
2281 /* Check if we managed to parse the conditional expression
2282 correctly. If not, we will not send this condition
2283 to the target. */
2284 if (aexpr)
2285 continue;
2286 }
2287
2288 /* If we have a NULL bytecode expression, it means something
2289 went wrong or we have a null condition expression. */
2290 if (!loc->cond_bytecode)
2291 {
2292 null_condition_or_parse_error = 1;
2293 break;
2294 }
2295 }
2296 }
2297
2298 /* If any of these happened, it means we will have to evaluate the conditions
2299 for the location's address on gdb's side. It is no use keeping bytecodes
2300 for all the other duplicate locations, thus we free all of them here.
2301
2302 This is so we have a finer control over which locations' conditions are
2303 being evaluated by GDB or the remote stub. */
2304 if (null_condition_or_parse_error)
2305 {
2306 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2307 {
2308 loc = (*loc2p);
2309 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2310 {
2311 /* Only go as far as the first NULL bytecode is
2312 located. */
2313 if (!loc->cond_bytecode)
2314 return;
2315
2316 free_agent_expr (loc->cond_bytecode);
2317 loc->cond_bytecode = NULL;
2318 }
2319 }
2320 }
2321
2322 /* No NULL conditions or failed bytecode generation. Build a condition list
2323 for this location's address. */
2324 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2325 {
2326 loc = (*loc2p);
2327 if (loc->cond
2328 && is_breakpoint (loc->owner)
2329 && loc->pspace->num == bl->pspace->num
2330 && loc->owner->enable_state == bp_enabled
2331 && loc->enabled)
2332 /* Add the condition to the vector. This will be used later to send the
2333 conditions to the target. */
2334 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2335 loc->cond_bytecode);
2336 }
2337
2338 return;
2339}
2340
d3ce09f5
SS
2341/* Parses a command described by string CMD into an agent expression
2342 bytecode suitable for evaluation by the bytecode interpreter.
2343 Return NULL if there was any error during parsing. */
2344
2345static struct agent_expr *
2346parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2347{
2348 struct cleanup *old_cleanups = 0;
2349 struct expression *expr, **argvec;
2350 struct agent_expr *aexpr = NULL;
d3ce09f5 2351 volatile struct gdb_exception ex;
bbc13ae3
KS
2352 const char *cmdrest;
2353 const char *format_start, *format_end;
d3ce09f5
SS
2354 struct format_piece *fpieces;
2355 int nargs;
2356 struct gdbarch *gdbarch = get_current_arch ();
2357
2358 if (!cmd)
2359 return NULL;
2360
2361 cmdrest = cmd;
2362
2363 if (*cmdrest == ',')
2364 ++cmdrest;
bbc13ae3 2365 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2366
2367 if (*cmdrest++ != '"')
2368 error (_("No format string following the location"));
2369
2370 format_start = cmdrest;
2371
2372 fpieces = parse_format_string (&cmdrest);
2373
2374 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2375
2376 format_end = cmdrest;
2377
2378 if (*cmdrest++ != '"')
2379 error (_("Bad format string, non-terminated '\"'."));
2380
bbc13ae3 2381 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2382
2383 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2384 error (_("Invalid argument syntax"));
2385
2386 if (*cmdrest == ',')
2387 cmdrest++;
bbc13ae3 2388 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2389
2390 /* For each argument, make an expression. */
2391
2392 argvec = (struct expression **) alloca (strlen (cmd)
2393 * sizeof (struct expression *));
2394
2395 nargs = 0;
2396 while (*cmdrest != '\0')
2397 {
bbc13ae3 2398 const char *cmd1;
d3ce09f5
SS
2399
2400 cmd1 = cmdrest;
2401 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2402 argvec[nargs++] = expr;
2403 cmdrest = cmd1;
2404 if (*cmdrest == ',')
2405 ++cmdrest;
2406 }
2407
2408 /* We don't want to stop processing, so catch any errors
2409 that may show up. */
2410 TRY_CATCH (ex, RETURN_MASK_ERROR)
2411 {
2412 aexpr = gen_printf (scope, gdbarch, 0, 0,
2413 format_start, format_end - format_start,
2414 fpieces, nargs, argvec);
2415 }
2416
752eb8b4
TT
2417 do_cleanups (old_cleanups);
2418
d3ce09f5
SS
2419 if (ex.reason < 0)
2420 {
2421 /* If we got here, it means the command could not be parsed to a valid
2422 bytecode expression and thus can't be evaluated on the target's side.
2423 It's no use iterating through the other commands. */
2424 return NULL;
2425 }
2426
d3ce09f5
SS
2427 /* We have a valid agent expression, return it. */
2428 return aexpr;
2429}
2430
2431/* Based on location BL, create a list of breakpoint commands to be
2432 passed on to the target. If we have duplicated locations with
2433 different commands, we will add any such to the list. */
2434
2435static void
2436build_target_command_list (struct bp_location *bl)
2437{
2438 struct bp_location **locp = NULL, **loc2p;
2439 int null_command_or_parse_error = 0;
2440 int modified = bl->needs_update;
2441 struct bp_location *loc;
2442
8b4f3082
PA
2443 /* Release commands left over from a previous insert. */
2444 VEC_free (agent_expr_p, bl->target_info.tcommands);
2445
41fac0cf 2446 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2447 return;
2448
41fac0cf
PA
2449 /* For now, limit to agent-style dprintf breakpoints. */
2450 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2451 return;
2452
41fac0cf
PA
2453 /* For now, if we have any duplicate location that isn't a dprintf,
2454 don't install the target-side commands, as that would make the
2455 breakpoint not be reported to the core, and we'd lose
2456 control. */
2457 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2458 {
2459 loc = (*loc2p);
2460 if (is_breakpoint (loc->owner)
2461 && loc->pspace->num == bl->pspace->num
2462 && loc->owner->type != bp_dprintf)
2463 return;
2464 }
2465
d3ce09f5
SS
2466 /* Do a first pass to check for locations with no assigned
2467 conditions or conditions that fail to parse to a valid agent expression
2468 bytecode. If any of these happen, then it's no use to send conditions
2469 to the target since this location will always trigger and generate a
2470 response back to GDB. */
2471 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2472 {
2473 loc = (*loc2p);
2474 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2475 {
2476 if (modified)
2477 {
2478 struct agent_expr *aexpr;
2479
2480 /* Re-parse the commands since something changed. In that
2481 case we already freed the command bytecodes (see
2482 force_breakpoint_reinsertion). We just
2483 need to parse the command to bytecodes again. */
2484 aexpr = parse_cmd_to_aexpr (bl->address,
2485 loc->owner->extra_string);
2486 loc->cmd_bytecode = aexpr;
2487
2488 if (!aexpr)
2489 continue;
2490 }
2491
2492 /* If we have a NULL bytecode expression, it means something
2493 went wrong or we have a null command expression. */
2494 if (!loc->cmd_bytecode)
2495 {
2496 null_command_or_parse_error = 1;
2497 break;
2498 }
2499 }
2500 }
2501
2502 /* If anything failed, then we're not doing target-side commands,
2503 and so clean up. */
2504 if (null_command_or_parse_error)
2505 {
2506 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2507 {
2508 loc = (*loc2p);
2509 if (is_breakpoint (loc->owner)
2510 && loc->pspace->num == bl->pspace->num)
2511 {
2512 /* Only go as far as the first NULL bytecode is
2513 located. */
40fb6c5e 2514 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2515 return;
2516
40fb6c5e
HZ
2517 free_agent_expr (loc->cmd_bytecode);
2518 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2519 }
2520 }
2521 }
2522
2523 /* No NULL commands or failed bytecode generation. Build a command list
2524 for this location's address. */
2525 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2526 {
2527 loc = (*loc2p);
2528 if (loc->owner->extra_string
2529 && is_breakpoint (loc->owner)
2530 && loc->pspace->num == bl->pspace->num
2531 && loc->owner->enable_state == bp_enabled
2532 && loc->enabled)
2533 /* Add the command to the vector. This will be used later
2534 to send the commands to the target. */
2535 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2536 loc->cmd_bytecode);
2537 }
2538
2539 bl->target_info.persist = 0;
2540 /* Maybe flag this location as persistent. */
2541 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2542 bl->target_info.persist = 1;
2543}
2544
35df4500
TJB
2545/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2546 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2547 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2548 Returns 0 for success, 1 if the bp_location type is not supported or
2549 -1 for failure.
879bfdc2 2550
4a64f543
MS
2551 NOTE drow/2003-09-09: This routine could be broken down to an
2552 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2553static int
35df4500 2554insert_bp_location (struct bp_location *bl,
26bb91f3 2555 struct ui_file *tmp_error_stream,
3fbb6ffa 2556 int *disabled_breaks,
dd61ec5c
MW
2557 int *hw_breakpoint_error,
2558 int *hw_bp_error_explained_already)
879bfdc2 2559{
0000e5cc
PA
2560 enum errors bp_err = GDB_NO_ERROR;
2561 const char *bp_err_message = NULL;
c90a6fb7 2562 volatile struct gdb_exception e;
879bfdc2 2563
b775012e 2564 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2565 return 0;
2566
35c63cd8
JB
2567 /* Note we don't initialize bl->target_info, as that wipes out
2568 the breakpoint location's shadow_contents if the breakpoint
2569 is still inserted at that location. This in turn breaks
2570 target_read_memory which depends on these buffers when
2571 a memory read is requested at the breakpoint location:
2572 Once the target_info has been wiped, we fail to see that
2573 we have a breakpoint inserted at that address and thus
2574 read the breakpoint instead of returning the data saved in
2575 the breakpoint location's shadow contents. */
35df4500
TJB
2576 bl->target_info.placed_address = bl->address;
2577 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2578 bl->target_info.length = bl->length;
8181d85f 2579
b775012e
LM
2580 /* When working with target-side conditions, we must pass all the conditions
2581 for the same breakpoint address down to the target since GDB will not
2582 insert those locations. With a list of breakpoint conditions, the target
2583 can decide when to stop and notify GDB. */
2584
2585 if (is_breakpoint (bl->owner))
2586 {
2587 build_target_condition_list (bl);
d3ce09f5
SS
2588 build_target_command_list (bl);
2589 /* Reset the modification marker. */
b775012e
LM
2590 bl->needs_update = 0;
2591 }
2592
35df4500
TJB
2593 if (bl->loc_type == bp_loc_software_breakpoint
2594 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2595 {
35df4500 2596 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2597 {
2598 /* If the explicitly specified breakpoint type
2599 is not hardware breakpoint, check the memory map to see
2600 if the breakpoint address is in read only memory or not.
4a64f543 2601
765dc015
VP
2602 Two important cases are:
2603 - location type is not hardware breakpoint, memory
2604 is readonly. We change the type of the location to
2605 hardware breakpoint.
4a64f543
MS
2606 - location type is hardware breakpoint, memory is
2607 read-write. This means we've previously made the
2608 location hardware one, but then the memory map changed,
2609 so we undo.
765dc015 2610
4a64f543
MS
2611 When breakpoints are removed, remove_breakpoints will use
2612 location types we've just set here, the only possible
2613 problem is that memory map has changed during running
2614 program, but it's not going to work anyway with current
2615 gdb. */
765dc015 2616 struct mem_region *mr
35df4500 2617 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2618
2619 if (mr)
2620 {
2621 if (automatic_hardware_breakpoints)
2622 {
765dc015
VP
2623 enum bp_loc_type new_type;
2624
2625 if (mr->attrib.mode != MEM_RW)
2626 new_type = bp_loc_hardware_breakpoint;
2627 else
2628 new_type = bp_loc_software_breakpoint;
2629
35df4500 2630 if (new_type != bl->loc_type)
765dc015
VP
2631 {
2632 static int said = 0;
cc59ec59 2633
35df4500 2634 bl->loc_type = new_type;
765dc015
VP
2635 if (!said)
2636 {
3e43a32a
MS
2637 fprintf_filtered (gdb_stdout,
2638 _("Note: automatically using "
2639 "hardware breakpoints for "
2640 "read-only addresses.\n"));
765dc015
VP
2641 said = 1;
2642 }
2643 }
2644 }
35df4500 2645 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2646 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2647 warning (_("cannot set software breakpoint "
2648 "at readonly address %s"),
35df4500 2649 paddress (bl->gdbarch, bl->address));
765dc015
VP
2650 }
2651 }
2652
879bfdc2
DJ
2653 /* First check to see if we have to handle an overlay. */
2654 if (overlay_debugging == ovly_off
35df4500
TJB
2655 || bl->section == NULL
2656 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2657 {
2658 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2659 TRY_CATCH (e, RETURN_MASK_ALL)
2660 {
0000e5cc
PA
2661 int val;
2662
dd61ec5c 2663 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2664 if (val)
2665 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2666 }
2667 if (e.reason < 0)
2668 {
0000e5cc
PA
2669 bp_err = e.error;
2670 bp_err_message = e.message;
dd61ec5c 2671 }
879bfdc2
DJ
2672 }
2673 else
2674 {
4a64f543 2675 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2676 Shall we set a breakpoint at the LMA? */
2677 if (!overlay_events_enabled)
2678 {
2679 /* Yes -- overlay event support is not active,
2680 so we must try to set a breakpoint at the LMA.
2681 This will not work for a hardware breakpoint. */
35df4500 2682 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2683 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2684 bl->owner->number);
879bfdc2
DJ
2685 else
2686 {
35df4500
TJB
2687 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2688 bl->section);
879bfdc2 2689 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2690 bl->overlay_target_info = bl->target_info;
2691 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2692
2693 /* No overlay handling: just set the breakpoint. */
2694 TRY_CATCH (e, RETURN_MASK_ALL)
2695 {
2696 int val;
2697
2698 val = target_insert_breakpoint (bl->gdbarch,
2699 &bl->overlay_target_info);
2700 if (val)
2701 bp_err = GENERIC_ERROR;
2702 }
2703 if (e.reason < 0)
2704 {
2705 bp_err = e.error;
2706 bp_err_message = e.message;
2707 }
2708
2709 if (bp_err != GDB_NO_ERROR)
99361f52 2710 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2711 "Overlay breakpoint %d "
2712 "failed: in ROM?\n",
35df4500 2713 bl->owner->number);
879bfdc2
DJ
2714 }
2715 }
2716 /* Shall we set a breakpoint at the VMA? */
35df4500 2717 if (section_is_mapped (bl->section))
879bfdc2
DJ
2718 {
2719 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2720 TRY_CATCH (e, RETURN_MASK_ALL)
2721 {
0000e5cc
PA
2722 int val;
2723
dd61ec5c 2724 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2725 if (val)
2726 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2727 }
2728 if (e.reason < 0)
2729 {
0000e5cc
PA
2730 bp_err = e.error;
2731 bp_err_message = e.message;
dd61ec5c 2732 }
879bfdc2
DJ
2733 }
2734 else
2735 {
2736 /* No. This breakpoint will not be inserted.
2737 No error, but do not mark the bp as 'inserted'. */
2738 return 0;
2739 }
2740 }
2741
0000e5cc 2742 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2743 {
2744 /* Can't set the breakpoint. */
0000e5cc
PA
2745
2746 /* In some cases, we might not be able to insert a
2747 breakpoint in a shared library that has already been
2748 removed, but we have not yet processed the shlib unload
2749 event. Unfortunately, some targets that implement
076855f9
PA
2750 breakpoint insertion themselves can't tell why the
2751 breakpoint insertion failed (e.g., the remote target
2752 doesn't define error codes), so we must treat generic
2753 errors as memory errors. */
0000e5cc 2754 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2755 && bl->loc_type == bp_loc_software_breakpoint
08351840 2756 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2757 || shared_objfile_contains_address_p (bl->pspace,
2758 bl->address)))
879bfdc2 2759 {
4a64f543 2760 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2761 bl->shlib_disabled = 1;
8d3788bd 2762 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2763 if (!*disabled_breaks)
2764 {
2765 fprintf_unfiltered (tmp_error_stream,
2766 "Cannot insert breakpoint %d.\n",
2767 bl->owner->number);
2768 fprintf_unfiltered (tmp_error_stream,
2769 "Temporarily disabling shared "
2770 "library breakpoints:\n");
2771 }
2772 *disabled_breaks = 1;
879bfdc2 2773 fprintf_unfiltered (tmp_error_stream,
35df4500 2774 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2775 return 0;
879bfdc2
DJ
2776 }
2777 else
879bfdc2 2778 {
35df4500 2779 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2780 {
0000e5cc
PA
2781 *hw_breakpoint_error = 1;
2782 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2783 fprintf_unfiltered (tmp_error_stream,
2784 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2785 bl->owner->number, bp_err_message ? ":" : ".\n");
2786 if (bp_err_message != NULL)
2787 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2788 }
2789 else
2790 {
0000e5cc
PA
2791 if (bp_err_message == NULL)
2792 {
2793 char *message
2794 = memory_error_message (TARGET_XFER_E_IO,
2795 bl->gdbarch, bl->address);
2796 struct cleanup *old_chain = make_cleanup (xfree, message);
2797
2798 fprintf_unfiltered (tmp_error_stream,
2799 "Cannot insert breakpoint %d.\n"
2800 "%s\n",
2801 bl->owner->number, message);
2802 do_cleanups (old_chain);
2803 }
2804 else
2805 {
2806 fprintf_unfiltered (tmp_error_stream,
2807 "Cannot insert breakpoint %d: %s\n",
2808 bl->owner->number,
2809 bp_err_message);
2810 }
879bfdc2 2811 }
0000e5cc 2812 return 1;
879bfdc2
DJ
2813
2814 }
2815 }
2816 else
35df4500 2817 bl->inserted = 1;
879bfdc2 2818
0000e5cc 2819 return 0;
879bfdc2
DJ
2820 }
2821
35df4500 2822 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2823 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2824 watchpoints. It's not clear that it's necessary... */
35df4500 2825 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2826 {
0000e5cc
PA
2827 int val;
2828
77b06cd7
TJB
2829 gdb_assert (bl->owner->ops != NULL
2830 && bl->owner->ops->insert_location != NULL);
2831
2832 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2833
2834 /* If trying to set a read-watchpoint, and it turns out it's not
2835 supported, try emulating one with an access watchpoint. */
35df4500 2836 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2837 {
2838 struct bp_location *loc, **loc_temp;
2839
2840 /* But don't try to insert it, if there's already another
2841 hw_access location that would be considered a duplicate
2842 of this one. */
2843 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2844 if (loc != bl
85d721b8 2845 && loc->watchpoint_type == hw_access
35df4500 2846 && watchpoint_locations_match (bl, loc))
85d721b8 2847 {
35df4500
TJB
2848 bl->duplicate = 1;
2849 bl->inserted = 1;
2850 bl->target_info = loc->target_info;
2851 bl->watchpoint_type = hw_access;
85d721b8
PA
2852 val = 0;
2853 break;
2854 }
2855
2856 if (val == 1)
2857 {
77b06cd7
TJB
2858 bl->watchpoint_type = hw_access;
2859 val = bl->owner->ops->insert_location (bl);
2860
2861 if (val)
2862 /* Back to the original value. */
2863 bl->watchpoint_type = hw_read;
85d721b8
PA
2864 }
2865 }
2866
35df4500 2867 bl->inserted = (val == 0);
879bfdc2
DJ
2868 }
2869
35df4500 2870 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2871 {
0000e5cc
PA
2872 int val;
2873
77b06cd7
TJB
2874 gdb_assert (bl->owner->ops != NULL
2875 && bl->owner->ops->insert_location != NULL);
2876
2877 val = bl->owner->ops->insert_location (bl);
2878 if (val)
2879 {
2880 bl->owner->enable_state = bp_disabled;
2881
2882 if (val == 1)
2883 warning (_("\
2884Error inserting catchpoint %d: Your system does not support this type\n\
2885of catchpoint."), bl->owner->number);
2886 else
2887 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2888 }
2889
2890 bl->inserted = (val == 0);
1640b821
DJ
2891
2892 /* We've already printed an error message if there was a problem
2893 inserting this catchpoint, and we've disabled the catchpoint,
2894 so just return success. */
2895 return 0;
879bfdc2
DJ
2896 }
2897
2898 return 0;
2899}
2900
6c95b8df
PA
2901/* This function is called when program space PSPACE is about to be
2902 deleted. It takes care of updating breakpoints to not reference
2903 PSPACE anymore. */
2904
2905void
2906breakpoint_program_space_exit (struct program_space *pspace)
2907{
2908 struct breakpoint *b, *b_temp;
876fa593 2909 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2910
2911 /* Remove any breakpoint that was set through this program space. */
2912 ALL_BREAKPOINTS_SAFE (b, b_temp)
2913 {
2914 if (b->pspace == pspace)
2915 delete_breakpoint (b);
2916 }
2917
2918 /* Breakpoints set through other program spaces could have locations
2919 bound to PSPACE as well. Remove those. */
876fa593 2920 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2921 {
2922 struct bp_location *tmp;
2923
2924 if (loc->pspace == pspace)
2925 {
2bdf28a0 2926 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2927 if (loc->owner->loc == loc)
2928 loc->owner->loc = loc->next;
2929 else
2930 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2931 if (tmp->next == loc)
2932 {
2933 tmp->next = loc->next;
2934 break;
2935 }
2936 }
2937 }
2938
2939 /* Now update the global location list to permanently delete the
2940 removed locations above. */
44702360 2941 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2942}
2943
74960c60
VP
2944/* Make sure all breakpoints are inserted in inferior.
2945 Throws exception on any error.
2946 A breakpoint that is already inserted won't be inserted
2947 again, so calling this function twice is safe. */
2948void
2949insert_breakpoints (void)
2950{
2951 struct breakpoint *bpt;
2952
2953 ALL_BREAKPOINTS (bpt)
2954 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2955 {
2956 struct watchpoint *w = (struct watchpoint *) bpt;
2957
2958 update_watchpoint (w, 0 /* don't reparse. */);
2959 }
74960c60 2960
04086b45
PA
2961 /* Updating watchpoints creates new locations, so update the global
2962 location list. Explicitly tell ugll to insert locations and
2963 ignore breakpoints_always_inserted_mode. */
2964 update_global_location_list (UGLL_INSERT);
74960c60
VP
2965}
2966
20388dd6
YQ
2967/* Invoke CALLBACK for each of bp_location. */
2968
2969void
2970iterate_over_bp_locations (walk_bp_location_callback callback)
2971{
2972 struct bp_location *loc, **loc_tmp;
2973
2974 ALL_BP_LOCATIONS (loc, loc_tmp)
2975 {
2976 callback (loc, NULL);
2977 }
2978}
2979
b775012e
LM
2980/* This is used when we need to synch breakpoint conditions between GDB and the
2981 target. It is the case with deleting and disabling of breakpoints when using
2982 always-inserted mode. */
2983
2984static void
2985update_inserted_breakpoint_locations (void)
2986{
2987 struct bp_location *bl, **blp_tmp;
2988 int error_flag = 0;
2989 int val = 0;
2990 int disabled_breaks = 0;
2991 int hw_breakpoint_error = 0;
dd61ec5c 2992 int hw_bp_details_reported = 0;
b775012e
LM
2993
2994 struct ui_file *tmp_error_stream = mem_fileopen ();
2995 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2996
2997 /* Explicitly mark the warning -- this will only be printed if
2998 there was an error. */
2999 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3000
3001 save_current_space_and_thread ();
3002
3003 ALL_BP_LOCATIONS (bl, blp_tmp)
3004 {
3005 /* We only want to update software breakpoints and hardware
3006 breakpoints. */
3007 if (!is_breakpoint (bl->owner))
3008 continue;
3009
3010 /* We only want to update locations that are already inserted
3011 and need updating. This is to avoid unwanted insertion during
3012 deletion of breakpoints. */
3013 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3014 continue;
3015
3016 switch_to_program_space_and_thread (bl->pspace);
3017
3018 /* For targets that support global breakpoints, there's no need
3019 to select an inferior to insert breakpoint to. In fact, even
3020 if we aren't attached to any process yet, we should still
3021 insert breakpoints. */
f5656ead 3022 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3023 && ptid_equal (inferior_ptid, null_ptid))
3024 continue;
3025
3026 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3027 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3028 if (val)
3029 error_flag = val;
3030 }
3031
3032 if (error_flag)
3033 {
3034 target_terminal_ours_for_output ();
3035 error_stream (tmp_error_stream);
3036 }
3037
3038 do_cleanups (cleanups);
3039}
3040
c30eee59 3041/* Used when starting or continuing the program. */
c906108c 3042
74960c60
VP
3043static void
3044insert_breakpoint_locations (void)
c906108c 3045{
a5606eee 3046 struct breakpoint *bpt;
35df4500 3047 struct bp_location *bl, **blp_tmp;
eacd795a 3048 int error_flag = 0;
c906108c 3049 int val = 0;
3fbb6ffa 3050 int disabled_breaks = 0;
81d0cc19 3051 int hw_breakpoint_error = 0;
dd61ec5c 3052 int hw_bp_error_explained_already = 0;
c906108c 3053
81d0cc19 3054 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3055 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3056
81d0cc19
GS
3057 /* Explicitly mark the warning -- this will only be printed if
3058 there was an error. */
3059 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3060
3061 save_current_space_and_thread ();
3062
35df4500 3063 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3064 {
b775012e 3065 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3066 continue;
3067
4a64f543
MS
3068 /* There is no point inserting thread-specific breakpoints if
3069 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3070 has BL->OWNER always non-NULL. */
35df4500
TJB
3071 if (bl->owner->thread != -1
3072 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3073 continue;
3074
35df4500 3075 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3076
3077 /* For targets that support global breakpoints, there's no need
3078 to select an inferior to insert breakpoint to. In fact, even
3079 if we aren't attached to any process yet, we should still
3080 insert breakpoints. */
f5656ead 3081 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3082 && ptid_equal (inferior_ptid, null_ptid))
3083 continue;
3084
3fbb6ffa 3085 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3086 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3087 if (val)
eacd795a 3088 error_flag = val;
879bfdc2 3089 }
c906108c 3090
4a64f543
MS
3091 /* If we failed to insert all locations of a watchpoint, remove
3092 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3093 ALL_BREAKPOINTS (bpt)
3094 {
3095 int some_failed = 0;
3096 struct bp_location *loc;
3097
3098 if (!is_hardware_watchpoint (bpt))
3099 continue;
3100
d6b74ac4 3101 if (!breakpoint_enabled (bpt))
a5606eee 3102 continue;
74960c60
VP
3103
3104 if (bpt->disposition == disp_del_at_next_stop)
3105 continue;
a5606eee
VP
3106
3107 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3108 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3109 {
3110 some_failed = 1;
3111 break;
3112 }
3113 if (some_failed)
3114 {
3115 for (loc = bpt->loc; loc; loc = loc->next)
3116 if (loc->inserted)
3117 remove_breakpoint (loc, mark_uninserted);
3118
3119 hw_breakpoint_error = 1;
3120 fprintf_unfiltered (tmp_error_stream,
3121 "Could not insert hardware watchpoint %d.\n",
3122 bpt->number);
eacd795a 3123 error_flag = -1;
a5606eee
VP
3124 }
3125 }
3126
eacd795a 3127 if (error_flag)
81d0cc19
GS
3128 {
3129 /* If a hardware breakpoint or watchpoint was inserted, add a
3130 message about possibly exhausted resources. */
dd61ec5c 3131 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3132 {
c6510018
MS
3133 fprintf_unfiltered (tmp_error_stream,
3134 "Could not insert hardware breakpoints:\n\
3135You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3136 }
81d0cc19
GS
3137 target_terminal_ours_for_output ();
3138 error_stream (tmp_error_stream);
3139 }
f7545552
TT
3140
3141 do_cleanups (cleanups);
c906108c
SS
3142}
3143
c30eee59
TJB
3144/* Used when the program stops.
3145 Returns zero if successful, or non-zero if there was a problem
3146 removing a breakpoint location. */
3147
c906108c 3148int
fba45db2 3149remove_breakpoints (void)
c906108c 3150{
35df4500 3151 struct bp_location *bl, **blp_tmp;
3a1bae8e 3152 int val = 0;
c906108c 3153
35df4500 3154 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3155 {
1e4d1764 3156 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3157 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3158 }
3a1bae8e 3159 return val;
c906108c
SS
3160}
3161
49fa26b0
PA
3162/* When a thread exits, remove breakpoints that are related to
3163 that thread. */
3164
3165static void
3166remove_threaded_breakpoints (struct thread_info *tp, int silent)
3167{
3168 struct breakpoint *b, *b_tmp;
3169
3170 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3171 {
96181529 3172 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3173 {
3174 b->disposition = disp_del_at_next_stop;
3175
3176 printf_filtered (_("\
46ecd527 3177Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3178 b->number, tp->num);
3179
3180 /* Hide it from the user. */
3181 b->number = 0;
3182 }
3183 }
3184}
3185
6c95b8df
PA
3186/* Remove breakpoints of process PID. */
3187
3188int
3189remove_breakpoints_pid (int pid)
3190{
35df4500 3191 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3192 int val;
3193 struct inferior *inf = find_inferior_pid (pid);
3194
35df4500 3195 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3196 {
35df4500 3197 if (bl->pspace != inf->pspace)
6c95b8df
PA
3198 continue;
3199
d3ce09f5
SS
3200 if (bl->owner->type == bp_dprintf)
3201 continue;
3202
35df4500 3203 if (bl->inserted)
6c95b8df 3204 {
35df4500 3205 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3206 if (val != 0)
3207 return val;
3208 }
3209 }
3210 return 0;
3211}
3212
c906108c 3213int
fba45db2 3214reattach_breakpoints (int pid)
c906108c 3215{
6c95b8df 3216 struct cleanup *old_chain;
35df4500 3217 struct bp_location *bl, **blp_tmp;
c906108c 3218 int val;
86b887df 3219 struct ui_file *tmp_error_stream;
dd61ec5c 3220 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3221 struct inferior *inf;
3222 struct thread_info *tp;
3223
3224 tp = any_live_thread_of_process (pid);
3225 if (tp == NULL)
3226 return 1;
3227
3228 inf = find_inferior_pid (pid);
3229 old_chain = save_inferior_ptid ();
3230
3231 inferior_ptid = tp->ptid;
a4954f26 3232
86b887df 3233 tmp_error_stream = mem_fileopen ();
a4954f26 3234 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3235
35df4500 3236 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3237 {
35df4500 3238 if (bl->pspace != inf->pspace)
6c95b8df
PA
3239 continue;
3240
35df4500 3241 if (bl->inserted)
c5aa993b 3242 {
35df4500 3243 bl->inserted = 0;
dd61ec5c 3244 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3245 if (val != 0)
3246 {
ce696e05 3247 do_cleanups (old_chain);
c5aa993b
JM
3248 return val;
3249 }
3250 }
3251 }
ce696e05 3252 do_cleanups (old_chain);
c906108c
SS
3253 return 0;
3254}
3255
e58b0e63
PA
3256static int internal_breakpoint_number = -1;
3257
84f4c1fe
PM
3258/* Set the breakpoint number of B, depending on the value of INTERNAL.
3259 If INTERNAL is non-zero, the breakpoint number will be populated
3260 from internal_breakpoint_number and that variable decremented.
e5dd4106 3261 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3262 breakpoint_count and that value incremented. Internal breakpoints
3263 do not set the internal var bpnum. */
3264static void
3265set_breakpoint_number (int internal, struct breakpoint *b)
3266{
3267 if (internal)
3268 b->number = internal_breakpoint_number--;
3269 else
3270 {
3271 set_breakpoint_count (breakpoint_count + 1);
3272 b->number = breakpoint_count;
3273 }
3274}
3275
e62c965a 3276static struct breakpoint *
a6d9a66e 3277create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3278 CORE_ADDR address, enum bptype type,
c0a91b2b 3279 const struct breakpoint_ops *ops)
e62c965a 3280{
e62c965a
PP
3281 struct symtab_and_line sal;
3282 struct breakpoint *b;
3283
4a64f543 3284 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3285
3286 sal.pc = address;
3287 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3288 sal.pspace = current_program_space;
e62c965a 3289
06edf0c0 3290 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3291 b->number = internal_breakpoint_number--;
3292 b->disposition = disp_donttouch;
3293
3294 return b;
3295}
3296
17450429
PP
3297static const char *const longjmp_names[] =
3298 {
3299 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3300 };
3301#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3302
3303/* Per-objfile data private to breakpoint.c. */
3304struct breakpoint_objfile_data
3305{
3306 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3307 struct bound_minimal_symbol overlay_msym;
17450429
PP
3308
3309 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3310 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3311
28106bc2
SDJ
3312 /* True if we have looked for longjmp probes. */
3313 int longjmp_searched;
3314
3315 /* SystemTap probe points for longjmp (if any). */
3316 VEC (probe_p) *longjmp_probes;
3317
17450429 3318 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3319 struct bound_minimal_symbol terminate_msym;
17450429
PP
3320
3321 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3322 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3323
3324 /* True if we have looked for exception probes. */
3325 int exception_searched;
3326
3327 /* SystemTap probe points for unwinding (if any). */
3328 VEC (probe_p) *exception_probes;
17450429
PP
3329};
3330
3331static const struct objfile_data *breakpoint_objfile_key;
3332
3333/* Minimal symbol not found sentinel. */
3334static struct minimal_symbol msym_not_found;
3335
3336/* Returns TRUE if MSYM point to the "not found" sentinel. */
3337
3338static int
3339msym_not_found_p (const struct minimal_symbol *msym)
3340{
3341 return msym == &msym_not_found;
3342}
3343
3344/* Return per-objfile data needed by breakpoint.c.
3345 Allocate the data if necessary. */
3346
3347static struct breakpoint_objfile_data *
3348get_breakpoint_objfile_data (struct objfile *objfile)
3349{
3350 struct breakpoint_objfile_data *bp_objfile_data;
3351
3352 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3353 if (bp_objfile_data == NULL)
3354 {
3355 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3356 sizeof (*bp_objfile_data));
3357
3358 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3359 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3360 }
3361 return bp_objfile_data;
3362}
3363
28106bc2
SDJ
3364static void
3365free_breakpoint_probes (struct objfile *obj, void *data)
3366{
3367 struct breakpoint_objfile_data *bp_objfile_data = data;
3368
3369 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3370 VEC_free (probe_p, bp_objfile_data->exception_probes);
3371}
3372
e62c965a 3373static void
af02033e 3374create_overlay_event_breakpoint (void)
e62c965a 3375{
69de3c6a 3376 struct objfile *objfile;
af02033e 3377 const char *const func_name = "_ovly_debug_event";
e62c965a 3378
69de3c6a
PP
3379 ALL_OBJFILES (objfile)
3380 {
3381 struct breakpoint *b;
17450429
PP
3382 struct breakpoint_objfile_data *bp_objfile_data;
3383 CORE_ADDR addr;
69de3c6a 3384
17450429
PP
3385 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3386
3b7344d5 3387 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3388 continue;
3389
3b7344d5 3390 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3391 {
3b7344d5 3392 struct bound_minimal_symbol m;
17450429
PP
3393
3394 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3395 if (m.minsym == NULL)
17450429
PP
3396 {
3397 /* Avoid future lookups in this objfile. */
3b7344d5 3398 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3399 continue;
3400 }
3401 bp_objfile_data->overlay_msym = m;
3402 }
e62c965a 3403
77e371c0 3404 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3405 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3406 bp_overlay_event,
3407 &internal_breakpoint_ops);
69de3c6a 3408 b->addr_string = xstrdup (func_name);
e62c965a 3409
69de3c6a
PP
3410 if (overlay_debugging == ovly_auto)
3411 {
3412 b->enable_state = bp_enabled;
3413 overlay_events_enabled = 1;
3414 }
3415 else
3416 {
3417 b->enable_state = bp_disabled;
3418 overlay_events_enabled = 0;
3419 }
e62c965a 3420 }
44702360 3421 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3422}
3423
0fd8e87f 3424static void
af02033e 3425create_longjmp_master_breakpoint (void)
0fd8e87f 3426{
6c95b8df 3427 struct program_space *pspace;
6c95b8df
PA
3428 struct cleanup *old_chain;
3429
3430 old_chain = save_current_program_space ();
0fd8e87f 3431
6c95b8df 3432 ALL_PSPACES (pspace)
af02033e
PP
3433 {
3434 struct objfile *objfile;
3435
3436 set_current_program_space (pspace);
3437
3438 ALL_OBJFILES (objfile)
0fd8e87f 3439 {
af02033e
PP
3440 int i;
3441 struct gdbarch *gdbarch;
17450429 3442 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3443
af02033e 3444 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3445
17450429
PP
3446 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3447
28106bc2
SDJ
3448 if (!bp_objfile_data->longjmp_searched)
3449 {
25f9533e
SDJ
3450 VEC (probe_p) *ret;
3451
3452 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3453 if (ret != NULL)
3454 {
3455 /* We are only interested in checking one element. */
3456 struct probe *p = VEC_index (probe_p, ret, 0);
3457
3458 if (!can_evaluate_probe_arguments (p))
3459 {
3460 /* We cannot use the probe interface here, because it does
3461 not know how to evaluate arguments. */
3462 VEC_free (probe_p, ret);
3463 ret = NULL;
3464 }
3465 }
3466 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3467 bp_objfile_data->longjmp_searched = 1;
3468 }
3469
3470 if (bp_objfile_data->longjmp_probes != NULL)
3471 {
3472 int i;
3473 struct probe *probe;
3474 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3475
3476 for (i = 0;
3477 VEC_iterate (probe_p,
3478 bp_objfile_data->longjmp_probes,
3479 i, probe);
3480 ++i)
3481 {
3482 struct breakpoint *b;
3483
729662a5
TT
3484 b = create_internal_breakpoint (gdbarch,
3485 get_probe_address (probe,
3486 objfile),
28106bc2
SDJ
3487 bp_longjmp_master,
3488 &internal_breakpoint_ops);
3489 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3490 b->enable_state = bp_disabled;
3491 }
3492
3493 continue;
3494 }
3495
0569175e
TSD
3496 if (!gdbarch_get_longjmp_target_p (gdbarch))
3497 continue;
3498
17450429 3499 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3500 {
3501 struct breakpoint *b;
af02033e 3502 const char *func_name;
17450429 3503 CORE_ADDR addr;
6c95b8df 3504
3b7344d5 3505 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3506 continue;
0fd8e87f 3507
17450429 3508 func_name = longjmp_names[i];
3b7344d5 3509 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3510 {
3b7344d5 3511 struct bound_minimal_symbol m;
17450429
PP
3512
3513 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3514 if (m.minsym == NULL)
17450429
PP
3515 {
3516 /* Prevent future lookups in this objfile. */
3b7344d5 3517 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3518 continue;
3519 }
3520 bp_objfile_data->longjmp_msym[i] = m;
3521 }
3522
77e371c0 3523 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3524 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3525 &internal_breakpoint_ops);
af02033e
PP
3526 b->addr_string = xstrdup (func_name);
3527 b->enable_state = bp_disabled;
3528 }
0fd8e87f 3529 }
af02033e 3530 }
44702360 3531 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3532
3533 do_cleanups (old_chain);
0fd8e87f
UW
3534}
3535
af02033e 3536/* Create a master std::terminate breakpoint. */
aa7d318d 3537static void
af02033e 3538create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3539{
3540 struct program_space *pspace;
aa7d318d 3541 struct cleanup *old_chain;
af02033e 3542 const char *const func_name = "std::terminate()";
aa7d318d
TT
3543
3544 old_chain = save_current_program_space ();
3545
3546 ALL_PSPACES (pspace)
17450429
PP
3547 {
3548 struct objfile *objfile;
3549 CORE_ADDR addr;
3550
3551 set_current_program_space (pspace);
3552
aa7d318d
TT
3553 ALL_OBJFILES (objfile)
3554 {
3555 struct breakpoint *b;
17450429 3556 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3557
17450429 3558 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3559
3b7344d5 3560 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3561 continue;
3562
3b7344d5 3563 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3564 {
3b7344d5 3565 struct bound_minimal_symbol m;
17450429
PP
3566
3567 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3568 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3569 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3570 {
3571 /* Prevent future lookups in this objfile. */
3b7344d5 3572 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3573 continue;
3574 }
3575 bp_objfile_data->terminate_msym = m;
3576 }
aa7d318d 3577
77e371c0 3578 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3579 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3580 bp_std_terminate_master,
3581 &internal_breakpoint_ops);
aa7d318d
TT
3582 b->addr_string = xstrdup (func_name);
3583 b->enable_state = bp_disabled;
3584 }
17450429
PP
3585 }
3586
44702360 3587 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3588
3589 do_cleanups (old_chain);
3590}
3591
186c406b
TT
3592/* Install a master breakpoint on the unwinder's debug hook. */
3593
70221824 3594static void
186c406b
TT
3595create_exception_master_breakpoint (void)
3596{
3597 struct objfile *objfile;
17450429 3598 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3599
3600 ALL_OBJFILES (objfile)
3601 {
17450429
PP
3602 struct breakpoint *b;
3603 struct gdbarch *gdbarch;
3604 struct breakpoint_objfile_data *bp_objfile_data;
3605 CORE_ADDR addr;
3606
3607 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3608
28106bc2
SDJ
3609 /* We prefer the SystemTap probe point if it exists. */
3610 if (!bp_objfile_data->exception_searched)
3611 {
25f9533e
SDJ
3612 VEC (probe_p) *ret;
3613
3614 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3615
3616 if (ret != NULL)
3617 {
3618 /* We are only interested in checking one element. */
3619 struct probe *p = VEC_index (probe_p, ret, 0);
3620
3621 if (!can_evaluate_probe_arguments (p))
3622 {
3623 /* We cannot use the probe interface here, because it does
3624 not know how to evaluate arguments. */
3625 VEC_free (probe_p, ret);
3626 ret = NULL;
3627 }
3628 }
3629 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3630 bp_objfile_data->exception_searched = 1;
3631 }
3632
3633 if (bp_objfile_data->exception_probes != NULL)
3634 {
3635 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3636 int i;
3637 struct probe *probe;
3638
3639 for (i = 0;
3640 VEC_iterate (probe_p,
3641 bp_objfile_data->exception_probes,
3642 i, probe);
3643 ++i)
3644 {
3645 struct breakpoint *b;
3646
729662a5
TT
3647 b = create_internal_breakpoint (gdbarch,
3648 get_probe_address (probe,
3649 objfile),
28106bc2
SDJ
3650 bp_exception_master,
3651 &internal_breakpoint_ops);
3652 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3653 b->enable_state = bp_disabled;
3654 }
3655
3656 continue;
3657 }
3658
3659 /* Otherwise, try the hook function. */
3660
3b7344d5 3661 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3662 continue;
3663
3664 gdbarch = get_objfile_arch (objfile);
186c406b 3665
3b7344d5 3666 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3667 {
3b7344d5 3668 struct bound_minimal_symbol debug_hook;
186c406b 3669
17450429 3670 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3671 if (debug_hook.minsym == NULL)
17450429 3672 {
3b7344d5 3673 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3674 continue;
3675 }
3676
3677 bp_objfile_data->exception_msym = debug_hook;
186c406b 3678 }
17450429 3679
77e371c0 3680 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3681 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3682 &current_target);
06edf0c0
PA
3683 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3684 &internal_breakpoint_ops);
17450429
PP
3685 b->addr_string = xstrdup (func_name);
3686 b->enable_state = bp_disabled;
186c406b
TT
3687 }
3688
44702360 3689 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3690}
3691
c906108c 3692void
fba45db2 3693update_breakpoints_after_exec (void)
c906108c 3694{
35df4500 3695 struct breakpoint *b, *b_tmp;
876fa593 3696 struct bp_location *bploc, **bplocp_tmp;
c906108c 3697
25b22b0a
PA
3698 /* We're about to delete breakpoints from GDB's lists. If the
3699 INSERTED flag is true, GDB will try to lift the breakpoints by
3700 writing the breakpoints' "shadow contents" back into memory. The
3701 "shadow contents" are NOT valid after an exec, so GDB should not
3702 do that. Instead, the target is responsible from marking
3703 breakpoints out as soon as it detects an exec. We don't do that
3704 here instead, because there may be other attempts to delete
3705 breakpoints after detecting an exec and before reaching here. */
876fa593 3706 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3707 if (bploc->pspace == current_program_space)
3708 gdb_assert (!bploc->inserted);
c906108c 3709
35df4500 3710 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3711 {
6c95b8df
PA
3712 if (b->pspace != current_program_space)
3713 continue;
3714
4a64f543 3715 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3716 if (b->type == bp_shlib_event)
3717 {
3718 delete_breakpoint (b);
3719 continue;
3720 }
c906108c 3721
4a64f543 3722 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3723 if (b->type == bp_jit_event)
3724 {
3725 delete_breakpoint (b);
3726 continue;
3727 }
3728
1900040c 3729 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3730 as must overlay event and longjmp master breakpoints. */
3731 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3732 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3733 || b->type == bp_exception_master)
c4093a6a
JM
3734 {
3735 delete_breakpoint (b);
3736 continue;
3737 }
3738
4a64f543 3739 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3740 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3741 {
3742 delete_breakpoint (b);
3743 continue;
3744 }
3745
611c83ae
PA
3746 /* Longjmp and longjmp-resume breakpoints are also meaningless
3747 after an exec. */
186c406b 3748 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3749 || b->type == bp_longjmp_call_dummy
186c406b 3750 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3751 {
3752 delete_breakpoint (b);
3753 continue;
3754 }
3755
ce78b96d
JB
3756 if (b->type == bp_catchpoint)
3757 {
3758 /* For now, none of the bp_catchpoint breakpoints need to
3759 do anything at this point. In the future, if some of
3760 the catchpoints need to something, we will need to add
3761 a new method, and call this method from here. */
3762 continue;
3763 }
3764
c5aa993b
JM
3765 /* bp_finish is a special case. The only way we ought to be able
3766 to see one of these when an exec() has happened, is if the user
3767 caught a vfork, and then said "finish". Ordinarily a finish just
3768 carries them to the call-site of the current callee, by setting
3769 a temporary bp there and resuming. But in this case, the finish
3770 will carry them entirely through the vfork & exec.
3771
3772 We don't want to allow a bp_finish to remain inserted now. But
3773 we can't safely delete it, 'cause finish_command has a handle to
3774 the bp on a bpstat, and will later want to delete it. There's a
3775 chance (and I've seen it happen) that if we delete the bp_finish
3776 here, that its storage will get reused by the time finish_command
3777 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3778 We really must allow finish_command to delete a bp_finish.
3779
e5dd4106 3780 In the absence of a general solution for the "how do we know
53a5351d
JM
3781 it's safe to delete something others may have handles to?"
3782 problem, what we'll do here is just uninsert the bp_finish, and
3783 let finish_command delete it.
3784
3785 (We know the bp_finish is "doomed" in the sense that it's
3786 momentary, and will be deleted as soon as finish_command sees
3787 the inferior stopped. So it doesn't matter that the bp's
3788 address is probably bogus in the new a.out, unlike e.g., the
3789 solib breakpoints.) */
c5aa993b 3790
c5aa993b
JM
3791 if (b->type == bp_finish)
3792 {
3793 continue;
3794 }
3795
3796 /* Without a symbolic address, we have little hope of the
3797 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3798 a.out. */
c5aa993b
JM
3799 if (b->addr_string == NULL)
3800 {
3801 delete_breakpoint (b);
3802 continue;
3803 }
c5aa993b 3804 }
1900040c 3805 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3806 create_overlay_event_breakpoint ();
3807 create_longjmp_master_breakpoint ();
3808 create_std_terminate_master_breakpoint ();
186c406b 3809 create_exception_master_breakpoint ();
c906108c
SS
3810}
3811
3812int
d80ee84f 3813detach_breakpoints (ptid_t ptid)
c906108c 3814{
35df4500 3815 struct bp_location *bl, **blp_tmp;
3a1bae8e 3816 int val = 0;
ce696e05 3817 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3818 struct inferior *inf = current_inferior ();
c5aa993b 3819
dfd4cc63 3820 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3821 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3822
6c95b8df 3823 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3824 inferior_ptid = ptid;
35df4500 3825 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3826 {
35df4500 3827 if (bl->pspace != inf->pspace)
6c95b8df
PA
3828 continue;
3829
bd9673a4
PW
3830 /* This function must physically remove breakpoints locations
3831 from the specified ptid, without modifying the breakpoint
3832 package's state. Locations of type bp_loc_other are only
3833 maintained at GDB side. So, there is no need to remove
3834 these bp_loc_other locations. Moreover, removing these
3835 would modify the breakpoint package's state. */
3836 if (bl->loc_type == bp_loc_other)
3837 continue;
3838
35df4500
TJB
3839 if (bl->inserted)
3840 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3841 }
d03285ec
UW
3842
3843 /* Detach single-step breakpoints as well. */
3844 detach_single_step_breakpoints ();
3845
ce696e05 3846 do_cleanups (old_chain);
3a1bae8e 3847 return val;
c906108c
SS
3848}
3849
35df4500 3850/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3851 Note that this is used to detach breakpoints from a child fork.
3852 When we get here, the child isn't in the inferior list, and neither
3853 do we have objects to represent its address space --- we should
35df4500 3854 *not* look at bl->pspace->aspace here. */
6c95b8df 3855
c906108c 3856static int
35df4500 3857remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3858{
3859 int val;
c5aa993b 3860
35df4500
TJB
3861 /* BL is never in moribund_locations by our callers. */
3862 gdb_assert (bl->owner != NULL);
2bdf28a0 3863
35df4500 3864 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3865 /* Permanent breakpoints cannot be inserted or removed. */
3866 return 0;
3867
74960c60
VP
3868 /* The type of none suggests that owner is actually deleted.
3869 This should not ever happen. */
35df4500 3870 gdb_assert (bl->owner->type != bp_none);
0bde7532 3871
35df4500
TJB
3872 if (bl->loc_type == bp_loc_software_breakpoint
3873 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3874 {
c02f5703
MS
3875 /* "Normal" instruction breakpoint: either the standard
3876 trap-instruction bp (bp_breakpoint), or a
3877 bp_hardware_breakpoint. */
3878
3879 /* First check to see if we have to handle an overlay. */
3880 if (overlay_debugging == ovly_off
35df4500
TJB
3881 || bl->section == NULL
3882 || !(section_is_overlay (bl->section)))
c02f5703
MS
3883 {
3884 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3885
3886 /* If we're trying to uninsert a memory breakpoint that we
3887 know is set in a dynamic object that is marked
3888 shlib_disabled, then either the dynamic object was
3889 removed with "remove-symbol-file" or with
3890 "nosharedlibrary". In the former case, we don't know
3891 whether another dynamic object might have loaded over the
3892 breakpoint's address -- the user might well let us know
3893 about it next with add-symbol-file (the whole point of
d03de421 3894 add-symbol-file is letting the user manually maintain a
08351840
PA
3895 list of dynamically loaded objects). If we have the
3896 breakpoint's shadow memory, that is, this is a software
3897 breakpoint managed by GDB, check whether the breakpoint
3898 is still inserted in memory, to avoid overwriting wrong
3899 code with stale saved shadow contents. Note that HW
3900 breakpoints don't have shadow memory, as they're
3901 implemented using a mechanism that is not dependent on
3902 being able to modify the target's memory, and as such
3903 they should always be removed. */
3904 if (bl->shlib_disabled
3905 && bl->target_info.shadow_len != 0
3906 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3907 val = 0;
3908 else
3909 val = bl->owner->ops->remove_location (bl);
c02f5703 3910 }
c906108c
SS
3911 else
3912 {
4a64f543 3913 /* This breakpoint is in an overlay section.
c02f5703
MS
3914 Did we set a breakpoint at the LMA? */
3915 if (!overlay_events_enabled)
3916 {
3917 /* Yes -- overlay event support is not active, so we
3918 should have set a breakpoint at the LMA. Remove it.
3919 */
c02f5703
MS
3920 /* Ignore any failures: if the LMA is in ROM, we will
3921 have already warned when we failed to insert it. */
35df4500
TJB
3922 if (bl->loc_type == bp_loc_hardware_breakpoint)
3923 target_remove_hw_breakpoint (bl->gdbarch,
3924 &bl->overlay_target_info);
c02f5703 3925 else
35df4500
TJB
3926 target_remove_breakpoint (bl->gdbarch,
3927 &bl->overlay_target_info);
c02f5703
MS
3928 }
3929 /* Did we set a breakpoint at the VMA?
3930 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3931 if (bl->inserted)
c906108c 3932 {
c02f5703
MS
3933 /* Yes -- remove it. Previously we did not bother to
3934 remove the breakpoint if the section had been
3935 unmapped, but let's not rely on that being safe. We
3936 don't know what the overlay manager might do. */
aa67235e
UW
3937
3938 /* However, we should remove *software* breakpoints only
3939 if the section is still mapped, or else we overwrite
3940 wrong code with the saved shadow contents. */
348d480f
PA
3941 if (bl->loc_type == bp_loc_hardware_breakpoint
3942 || section_is_mapped (bl->section))
3943 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3944 else
3945 val = 0;
c906108c 3946 }
c02f5703
MS
3947 else
3948 {
3949 /* No -- not inserted, so no need to remove. No error. */
3950 val = 0;
3951 }
c906108c 3952 }
879d1e6b 3953
08351840
PA
3954 /* In some cases, we might not be able to remove a breakpoint in
3955 a shared library that has already been removed, but we have
3956 not yet processed the shlib unload event. Similarly for an
3957 unloaded add-symbol-file object - the user might not yet have
3958 had the chance to remove-symbol-file it. shlib_disabled will
3959 be set if the library/object has already been removed, but
3960 the breakpoint hasn't been uninserted yet, e.g., after
3961 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3962 always-inserted mode. */
076855f9 3963 if (val
08351840
PA
3964 && (bl->loc_type == bp_loc_software_breakpoint
3965 && (bl->shlib_disabled
3966 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3967 || shared_objfile_contains_address_p (bl->pspace,
3968 bl->address))))
879d1e6b
UW
3969 val = 0;
3970
c906108c
SS
3971 if (val)
3972 return val;
35df4500 3973 bl->inserted = (is == mark_inserted);
c906108c 3974 }
35df4500 3975 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3976 {
77b06cd7
TJB
3977 gdb_assert (bl->owner->ops != NULL
3978 && bl->owner->ops->remove_location != NULL);
3979
35df4500 3980 bl->inserted = (is == mark_inserted);
77b06cd7 3981 bl->owner->ops->remove_location (bl);
2e70b7b9 3982
c906108c 3983 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3984 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3985 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3986 bl->owner->number);
c906108c 3987 }
35df4500
TJB
3988 else if (bl->owner->type == bp_catchpoint
3989 && breakpoint_enabled (bl->owner)
3990 && !bl->duplicate)
ce78b96d 3991 {
77b06cd7
TJB
3992 gdb_assert (bl->owner->ops != NULL
3993 && bl->owner->ops->remove_location != NULL);
ce78b96d 3994
77b06cd7 3995 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3996 if (val)
3997 return val;
77b06cd7 3998
35df4500 3999 bl->inserted = (is == mark_inserted);
ce78b96d 4000 }
c906108c
SS
4001
4002 return 0;
4003}
4004
6c95b8df 4005static int
35df4500 4006remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4007{
4008 int ret;
4009 struct cleanup *old_chain;
4010
35df4500
TJB
4011 /* BL is never in moribund_locations by our callers. */
4012 gdb_assert (bl->owner != NULL);
2bdf28a0 4013
35df4500 4014 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
4015 /* Permanent breakpoints cannot be inserted or removed. */
4016 return 0;
4017
4018 /* The type of none suggests that owner is actually deleted.
4019 This should not ever happen. */
35df4500 4020 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4021
4022 old_chain = save_current_space_and_thread ();
4023
35df4500 4024 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4025
35df4500 4026 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4027
4028 do_cleanups (old_chain);
4029 return ret;
4030}
4031
c906108c
SS
4032/* Clear the "inserted" flag in all breakpoints. */
4033
25b22b0a 4034void
fba45db2 4035mark_breakpoints_out (void)
c906108c 4036{
35df4500 4037 struct bp_location *bl, **blp_tmp;
c906108c 4038
35df4500
TJB
4039 ALL_BP_LOCATIONS (bl, blp_tmp)
4040 if (bl->pspace == current_program_space)
4041 bl->inserted = 0;
c906108c
SS
4042}
4043
53a5351d
JM
4044/* Clear the "inserted" flag in all breakpoints and delete any
4045 breakpoints which should go away between runs of the program.
c906108c
SS
4046
4047 Plus other such housekeeping that has to be done for breakpoints
4048 between runs.
4049
53a5351d
JM
4050 Note: this function gets called at the end of a run (by
4051 generic_mourn_inferior) and when a run begins (by
4a64f543 4052 init_wait_for_inferior). */
c906108c
SS
4053
4054
4055
4056void
fba45db2 4057breakpoint_init_inferior (enum inf_context context)
c906108c 4058{
35df4500
TJB
4059 struct breakpoint *b, *b_tmp;
4060 struct bp_location *bl, **blp_tmp;
1c5cfe86 4061 int ix;
6c95b8df 4062 struct program_space *pspace = current_program_space;
c906108c 4063
50c71eaf
PA
4064 /* If breakpoint locations are shared across processes, then there's
4065 nothing to do. */
f5656ead 4066 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4067 return;
4068
35df4500 4069 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 4070 {
35df4500
TJB
4071 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4072 if (bl->pspace == pspace
4073 && bl->owner->enable_state != bp_permanent)
4074 bl->inserted = 0;
6c95b8df 4075 }
075f6582 4076
35df4500 4077 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4078 {
6c95b8df
PA
4079 if (b->loc && b->loc->pspace != pspace)
4080 continue;
4081
c5aa993b
JM
4082 switch (b->type)
4083 {
4084 case bp_call_dummy:
e2e4d78b 4085 case bp_longjmp_call_dummy:
c906108c 4086
c5aa993b 4087 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4088 cause problems when the inferior is rerun, so we better get
4089 rid of it. */
4090
4091 case bp_watchpoint_scope:
4092
4093 /* Also get rid of scope breakpoints. */
4094
4095 case bp_shlib_event:
4096
4097 /* Also remove solib event breakpoints. Their addresses may
4098 have changed since the last time we ran the program.
4099 Actually we may now be debugging against different target;
4100 and so the solib backend that installed this breakpoint may
4101 not be used in by the target. E.g.,
4102
4103 (gdb) file prog-linux
4104 (gdb) run # native linux target
4105 ...
4106 (gdb) kill
4107 (gdb) file prog-win.exe
4108 (gdb) tar rem :9999 # remote Windows gdbserver.
4109 */
c906108c 4110
f59f708a
PA
4111 case bp_step_resume:
4112
4113 /* Also remove step-resume breakpoints. */
4114
c5aa993b
JM
4115 delete_breakpoint (b);
4116 break;
c906108c 4117
c5aa993b
JM
4118 case bp_watchpoint:
4119 case bp_hardware_watchpoint:
4120 case bp_read_watchpoint:
4121 case bp_access_watchpoint:
3a5c3e22
PA
4122 {
4123 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4124
3a5c3e22
PA
4125 /* Likewise for watchpoints on local expressions. */
4126 if (w->exp_valid_block != NULL)
4127 delete_breakpoint (b);
4128 else if (context == inf_starting)
4129 {
4130 /* Reset val field to force reread of starting value in
4131 insert_breakpoints. */
4132 if (w->val)
4133 value_free (w->val);
4134 w->val = NULL;
4135 w->val_valid = 0;
c860120c 4136 }
3a5c3e22 4137 }
c5aa993b
JM
4138 break;
4139 default:
c5aa993b
JM
4140 break;
4141 }
4142 }
1c5cfe86
PA
4143
4144 /* Get rid of the moribund locations. */
35df4500
TJB
4145 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4146 decref_bp_location (&bl);
1c5cfe86 4147 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4148}
4149
6c95b8df
PA
4150/* These functions concern about actual breakpoints inserted in the
4151 target --- to e.g. check if we need to do decr_pc adjustment or if
4152 we need to hop over the bkpt --- so we check for address space
4153 match, not program space. */
4154
c2c6d25f
JM
4155/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4156 exists at PC. It returns ordinary_breakpoint_here if it's an
4157 ordinary breakpoint, or permanent_breakpoint_here if it's a
4158 permanent breakpoint.
4159 - When continuing from a location with an ordinary breakpoint, we
4160 actually single step once before calling insert_breakpoints.
e5dd4106 4161 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4162 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4163 the target, to advance the PC past the breakpoint. */
c906108c 4164
c2c6d25f 4165enum breakpoint_here
6c95b8df 4166breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4167{
35df4500 4168 struct bp_location *bl, **blp_tmp;
c2c6d25f 4169 int any_breakpoint_here = 0;
c906108c 4170
35df4500 4171 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4172 {
35df4500
TJB
4173 if (bl->loc_type != bp_loc_software_breakpoint
4174 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4175 continue;
4176
f1310107 4177 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
4178 if ((breakpoint_enabled (bl->owner)
4179 || bl->owner->enable_state == bp_permanent)
f1310107 4180 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4181 {
4182 if (overlay_debugging
35df4500
TJB
4183 && section_is_overlay (bl->section)
4184 && !section_is_mapped (bl->section))
075f6582 4185 continue; /* unmapped overlay -- can't be a match */
35df4500 4186 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
4187 return permanent_breakpoint_here;
4188 else
4189 any_breakpoint_here = 1;
4190 }
4191 }
c906108c 4192
c2c6d25f 4193 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4194}
4195
1c5cfe86
PA
4196/* Return true if there's a moribund breakpoint at PC. */
4197
4198int
6c95b8df 4199moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4200{
4201 struct bp_location *loc;
4202 int ix;
4203
4204 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4205 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4206 return 1;
4207
4208 return 0;
4209}
c2c6d25f 4210
c36b740a 4211/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
4212 inserted using regular breakpoint_chain / bp_location array
4213 mechanism. This does not check for single-step breakpoints, which
4214 are inserted and removed using direct target manipulation. */
c906108c
SS
4215
4216int
4a64f543
MS
4217regular_breakpoint_inserted_here_p (struct address_space *aspace,
4218 CORE_ADDR pc)
c906108c 4219{
35df4500 4220 struct bp_location *bl, **blp_tmp;
c906108c 4221
35df4500 4222 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4223 {
35df4500
TJB
4224 if (bl->loc_type != bp_loc_software_breakpoint
4225 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4226 continue;
4227
35df4500 4228 if (bl->inserted
f1310107 4229 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4230 {
4231 if (overlay_debugging
35df4500
TJB
4232 && section_is_overlay (bl->section)
4233 && !section_is_mapped (bl->section))
075f6582
DJ
4234 continue; /* unmapped overlay -- can't be a match */
4235 else
4236 return 1;
4237 }
c5aa993b 4238 }
c36b740a
VP
4239 return 0;
4240}
4241
4242/* Returns non-zero iff there's either regular breakpoint
4243 or a single step breakpoint inserted at PC. */
4244
4245int
6c95b8df 4246breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 4247{
6c95b8df 4248 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 4249 return 1;
c906108c 4250
6c95b8df 4251 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4252 return 1;
4253
c906108c
SS
4254 return 0;
4255}
4256
ef370185
JB
4257/* Ignoring deprecated raw breakpoints, return non-zero iff there is a
4258 software breakpoint inserted at PC. */
4fa8626c 4259
ef370185
JB
4260static struct bp_location *
4261find_non_raw_software_breakpoint_inserted_here (struct address_space *aspace,
4262 CORE_ADDR pc)
4fa8626c 4263{
35df4500 4264 struct bp_location *bl, **blp_tmp;
4fa8626c 4265
35df4500 4266 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 4267 {
35df4500 4268 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4269 continue;
4270
35df4500
TJB
4271 if (bl->inserted
4272 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 4273 aspace, pc))
4fa8626c
DJ
4274 {
4275 if (overlay_debugging
35df4500
TJB
4276 && section_is_overlay (bl->section)
4277 && !section_is_mapped (bl->section))
4fa8626c
DJ
4278 continue; /* unmapped overlay -- can't be a match */
4279 else
ef370185 4280 return bl;
4fa8626c
DJ
4281 }
4282 }
4283
ef370185
JB
4284 return NULL;
4285}
4286
4287/* This function returns non-zero iff there is a software breakpoint
4288 inserted at PC. */
4289
4290int
4291software_breakpoint_inserted_here_p (struct address_space *aspace,
4292 CORE_ADDR pc)
4293{
4294 if (find_non_raw_software_breakpoint_inserted_here (aspace, pc) != NULL)
4295 return 1;
4296
1aafd4da 4297 /* Also check for software single-step breakpoints. */
6c95b8df 4298 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4299 return 1;
4300
4fa8626c
DJ
4301 return 0;
4302}
4303
9093389c
PA
4304int
4305hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4306 CORE_ADDR addr, ULONGEST len)
4307{
4308 struct breakpoint *bpt;
4309
4310 ALL_BREAKPOINTS (bpt)
4311 {
4312 struct bp_location *loc;
4313
4314 if (bpt->type != bp_hardware_watchpoint
4315 && bpt->type != bp_access_watchpoint)
4316 continue;
4317
4318 if (!breakpoint_enabled (bpt))
4319 continue;
4320
4321 for (loc = bpt->loc; loc; loc = loc->next)
4322 if (loc->pspace->aspace == aspace && loc->inserted)
4323 {
4324 CORE_ADDR l, h;
4325
4326 /* Check for intersection. */
4327 l = max (loc->address, addr);
4328 h = min (loc->address + loc->length, addr + len);
4329 if (l < h)
4330 return 1;
4331 }
4332 }
4333 return 0;
4334}
4335
075f6582
DJ
4336/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4337 PC is valid for process/thread PTID. */
c906108c
SS
4338
4339int
6c95b8df
PA
4340breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4341 ptid_t ptid)
c906108c 4342{
35df4500 4343 struct bp_location *bl, **blp_tmp;
4a306c9a 4344 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4345 int thread = -1;
4a306c9a 4346 int task = 0;
a6f1cd96 4347
35df4500 4348 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4349 {
35df4500
TJB
4350 if (bl->loc_type != bp_loc_software_breakpoint
4351 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4352 continue;
4353
35df4500
TJB
4354 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4355 if (!breakpoint_enabled (bl->owner)
4356 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4357 continue;
4358
f1310107 4359 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4360 continue;
4361
35df4500 4362 if (bl->owner->thread != -1)
075f6582 4363 {
a6f1cd96
JB
4364 /* This is a thread-specific breakpoint. Check that ptid
4365 matches that thread. If thread hasn't been computed yet,
4366 it is now time to do so. */
4367 if (thread == -1)
4368 thread = pid_to_thread_id (ptid);
35df4500 4369 if (bl->owner->thread != thread)
a6f1cd96 4370 continue;
075f6582 4371 }
a6f1cd96 4372
35df4500 4373 if (bl->owner->task != 0)
4a306c9a
JB
4374 {
4375 /* This is a task-specific breakpoint. Check that ptid
4376 matches that task. If task hasn't been computed yet,
4377 it is now time to do so. */
4378 if (task == 0)
4379 task = ada_get_task_number (ptid);
35df4500 4380 if (bl->owner->task != task)
4a306c9a
JB
4381 continue;
4382 }
4383
a6f1cd96 4384 if (overlay_debugging
35df4500
TJB
4385 && section_is_overlay (bl->section)
4386 && !section_is_mapped (bl->section))
a6f1cd96
JB
4387 continue; /* unmapped overlay -- can't be a match */
4388
4389 return 1;
c5aa993b 4390 }
c906108c
SS
4391
4392 return 0;
4393}
c906108c 4394\f
c5aa993b 4395
c906108c
SS
4396/* bpstat stuff. External routines' interfaces are documented
4397 in breakpoint.h. */
4398
4399int
c326b90e 4400is_catchpoint (struct breakpoint *ep)
c906108c 4401{
533be4dd 4402 return (ep->type == bp_catchpoint);
c906108c
SS
4403}
4404
f431efe5
PA
4405/* Frees any storage that is part of a bpstat. Does not walk the
4406 'next' chain. */
4407
4408static void
198757a8
VP
4409bpstat_free (bpstat bs)
4410{
4411 if (bs->old_val != NULL)
4412 value_free (bs->old_val);
9add0f1b 4413 decref_counted_command_line (&bs->commands);
f431efe5 4414 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4415 xfree (bs);
4416}
4417
c906108c
SS
4418/* Clear a bpstat so that it says we are not at any breakpoint.
4419 Also free any storage that is part of a bpstat. */
4420
4421void
fba45db2 4422bpstat_clear (bpstat *bsp)
c906108c
SS
4423{
4424 bpstat p;
4425 bpstat q;
4426
4427 if (bsp == 0)
4428 return;
4429 p = *bsp;
4430 while (p != NULL)
4431 {
4432 q = p->next;
198757a8 4433 bpstat_free (p);
c906108c
SS
4434 p = q;
4435 }
4436 *bsp = NULL;
4437}
4438
4439/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4440 is part of the bpstat is copied as well. */
4441
4442bpstat
fba45db2 4443bpstat_copy (bpstat bs)
c906108c
SS
4444{
4445 bpstat p = NULL;
4446 bpstat tmp;
4447 bpstat retval = NULL;
4448
4449 if (bs == NULL)
4450 return bs;
4451
4452 for (; bs != NULL; bs = bs->next)
4453 {
4454 tmp = (bpstat) xmalloc (sizeof (*tmp));
4455 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4456 incref_counted_command_line (tmp->commands);
f431efe5 4457 incref_bp_location (tmp->bp_location_at);
31cc81e9 4458 if (bs->old_val != NULL)
3c3185ac
JK
4459 {
4460 tmp->old_val = value_copy (bs->old_val);
4461 release_value (tmp->old_val);
4462 }
31cc81e9 4463
c906108c
SS
4464 if (p == NULL)
4465 /* This is the first thing in the chain. */
4466 retval = tmp;
4467 else
4468 p->next = tmp;
4469 p = tmp;
4470 }
4471 p->next = NULL;
4472 return retval;
4473}
4474
4a64f543 4475/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4476
4477bpstat
fba45db2 4478bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4479{
c5aa993b
JM
4480 if (bsp == NULL)
4481 return NULL;
c906108c 4482
c5aa993b
JM
4483 for (; bsp != NULL; bsp = bsp->next)
4484 {
f431efe5 4485 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4486 return bsp;
4487 }
c906108c
SS
4488 return NULL;
4489}
4490
ab04a2af
TT
4491/* See breakpoint.h. */
4492
47591c29 4493int
427cd150 4494bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4495{
ab04a2af
TT
4496 for (; bsp != NULL; bsp = bsp->next)
4497 {
427cd150
TT
4498 if (bsp->breakpoint_at == NULL)
4499 {
4500 /* A moribund location can never explain a signal other than
4501 GDB_SIGNAL_TRAP. */
4502 if (sig == GDB_SIGNAL_TRAP)
47591c29 4503 return 1;
427cd150
TT
4504 }
4505 else
47591c29
PA
4506 {
4507 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4508 sig))
4509 return 1;
4510 }
ab04a2af
TT
4511 }
4512
47591c29 4513 return 0;
ab04a2af
TT
4514}
4515
4a64f543
MS
4516/* Put in *NUM the breakpoint number of the first breakpoint we are
4517 stopped at. *BSP upon return is a bpstat which points to the
4518 remaining breakpoints stopped at (but which is not guaranteed to be
4519 good for anything but further calls to bpstat_num).
4520
8671a17b
PA
4521 Return 0 if passed a bpstat which does not indicate any breakpoints.
4522 Return -1 if stopped at a breakpoint that has been deleted since
4523 we set it.
4524 Return 1 otherwise. */
c906108c
SS
4525
4526int
8671a17b 4527bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4528{
4529 struct breakpoint *b;
4530
4531 if ((*bsp) == NULL)
4532 return 0; /* No more breakpoint values */
8671a17b 4533
4a64f543
MS
4534 /* We assume we'll never have several bpstats that correspond to a
4535 single breakpoint -- otherwise, this function might return the
4536 same number more than once and this will look ugly. */
f431efe5 4537 b = (*bsp)->breakpoint_at;
8671a17b
PA
4538 *bsp = (*bsp)->next;
4539 if (b == NULL)
4540 return -1; /* breakpoint that's been deleted since */
4541
4542 *num = b->number; /* We have its number */
4543 return 1;
c906108c
SS
4544}
4545
e93ca019 4546/* See breakpoint.h. */
c906108c
SS
4547
4548void
e93ca019 4549bpstat_clear_actions (void)
c906108c 4550{
e93ca019
JK
4551 struct thread_info *tp;
4552 bpstat bs;
4553
4554 if (ptid_equal (inferior_ptid, null_ptid))
4555 return;
4556
4557 tp = find_thread_ptid (inferior_ptid);
4558 if (tp == NULL)
4559 return;
4560
4561 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4562 {
9add0f1b 4563 decref_counted_command_line (&bs->commands);
abf85f46 4564
c906108c
SS
4565 if (bs->old_val != NULL)
4566 {
4567 value_free (bs->old_val);
4568 bs->old_val = NULL;
4569 }
4570 }
4571}
4572
f3b1572e
PA
4573/* Called when a command is about to proceed the inferior. */
4574
4575static void
4576breakpoint_about_to_proceed (void)
4577{
4578 if (!ptid_equal (inferior_ptid, null_ptid))
4579 {
4580 struct thread_info *tp = inferior_thread ();
4581
4582 /* Allow inferior function calls in breakpoint commands to not
4583 interrupt the command list. When the call finishes
4584 successfully, the inferior will be standing at the same
4585 breakpoint as if nothing happened. */
16c381f0 4586 if (tp->control.in_infcall)
f3b1572e
PA
4587 return;
4588 }
4589
4590 breakpoint_proceeded = 1;
4591}
4592
4a64f543
MS
4593/* Stub for cleaning up our state if we error-out of a breakpoint
4594 command. */
c906108c 4595static void
4efb68b1 4596cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4597{
4598 executing_breakpoint_commands = 0;
4599}
4600
abf85f46
JK
4601/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4602 or its equivalent. */
4603
4604static int
4605command_line_is_silent (struct command_line *cmd)
4606{
4607 return cmd && (strcmp ("silent", cmd->line) == 0
4608 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4609}
4610
4a64f543
MS
4611/* Execute all the commands associated with all the breakpoints at
4612 this location. Any of these commands could cause the process to
4613 proceed beyond this point, etc. We look out for such changes by
4614 checking the global "breakpoint_proceeded" after each command.
c906108c 4615
347bddb7
PA
4616 Returns true if a breakpoint command resumed the inferior. In that
4617 case, it is the caller's responsibility to recall it again with the
4618 bpstat of the current thread. */
4619
4620static int
4621bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4622{
4623 bpstat bs;
4624 struct cleanup *old_chain;
347bddb7 4625 int again = 0;
c906108c
SS
4626
4627 /* Avoid endless recursion if a `source' command is contained
4628 in bs->commands. */
4629 if (executing_breakpoint_commands)
347bddb7 4630 return 0;
c906108c
SS
4631
4632 executing_breakpoint_commands = 1;
4633 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4634
cf6c5ffb
TT
4635 prevent_dont_repeat ();
4636
4a64f543 4637 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4638 bs = *bsp;
4639
4640 breakpoint_proceeded = 0;
4641 for (; bs != NULL; bs = bs->next)
4642 {
9add0f1b 4643 struct counted_command_line *ccmd;
6c50ab1c
JB
4644 struct command_line *cmd;
4645 struct cleanup *this_cmd_tree_chain;
4646
4647 /* Take ownership of the BSP's command tree, if it has one.
4648
4649 The command tree could legitimately contain commands like
4650 'step' and 'next', which call clear_proceed_status, which
4651 frees stop_bpstat's command tree. To make sure this doesn't
4652 free the tree we're executing out from under us, we need to
4653 take ownership of the tree ourselves. Since a given bpstat's
4654 commands are only executed once, we don't need to copy it; we
4655 can clear the pointer in the bpstat, and make sure we free
4656 the tree when we're done. */
9add0f1b
TT
4657 ccmd = bs->commands;
4658 bs->commands = NULL;
abf85f46
JK
4659 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4660 cmd = ccmd ? ccmd->commands : NULL;
4661 if (command_line_is_silent (cmd))
4662 {
4663 /* The action has been already done by bpstat_stop_status. */
4664 cmd = cmd->next;
4665 }
6c50ab1c 4666
c906108c
SS
4667 while (cmd != NULL)
4668 {
4669 execute_control_command (cmd);
4670
4671 if (breakpoint_proceeded)
4672 break;
4673 else
4674 cmd = cmd->next;
4675 }
6c50ab1c
JB
4676
4677 /* We can free this command tree now. */
4678 do_cleanups (this_cmd_tree_chain);
4679
c906108c 4680 if (breakpoint_proceeded)
32c1e744
VP
4681 {
4682 if (target_can_async_p ())
347bddb7
PA
4683 /* If we are in async mode, then the target might be still
4684 running, not stopped at any breakpoint, so nothing for
4685 us to do here -- just return to the event loop. */
4686 ;
32c1e744
VP
4687 else
4688 /* In sync mode, when execute_control_command returns
4689 we're already standing on the next breakpoint.
347bddb7
PA
4690 Breakpoint commands for that stop were not run, since
4691 execute_command does not run breakpoint commands --
4692 only command_line_handler does, but that one is not
4693 involved in execution of breakpoint commands. So, we
4694 can now execute breakpoint commands. It should be
4695 noted that making execute_command do bpstat actions is
4696 not an option -- in this case we'll have recursive
4697 invocation of bpstat for each breakpoint with a
4698 command, and can easily blow up GDB stack. Instead, we
4699 return true, which will trigger the caller to recall us
4700 with the new stop_bpstat. */
4701 again = 1;
4702 break;
32c1e744 4703 }
c906108c 4704 }
c2b8ed2c 4705 do_cleanups (old_chain);
347bddb7
PA
4706 return again;
4707}
4708
4709void
4710bpstat_do_actions (void)
4711{
353d1d73
JK
4712 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4713
347bddb7
PA
4714 /* Do any commands attached to breakpoint we are stopped at. */
4715 while (!ptid_equal (inferior_ptid, null_ptid)
4716 && target_has_execution
4717 && !is_exited (inferior_ptid)
4718 && !is_executing (inferior_ptid))
4719 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4720 and only return when it is stopped at the next breakpoint, we
4721 keep doing breakpoint actions until it returns false to
4722 indicate the inferior was not resumed. */
16c381f0 4723 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4724 break;
353d1d73
JK
4725
4726 discard_cleanups (cleanup_if_error);
c906108c
SS
4727}
4728
fa4727a6
DJ
4729/* Print out the (old or new) value associated with a watchpoint. */
4730
4731static void
4732watchpoint_value_print (struct value *val, struct ui_file *stream)
4733{
4734 if (val == NULL)
4735 fprintf_unfiltered (stream, _("<unreadable>"));
4736 else
79a45b7d
TT
4737 {
4738 struct value_print_options opts;
4739 get_user_print_options (&opts);
4740 value_print (val, stream, &opts);
4741 }
fa4727a6
DJ
4742}
4743
e514a9d6 4744/* Generic routine for printing messages indicating why we
4a64f543 4745 stopped. The behavior of this function depends on the value
e514a9d6
JM
4746 'print_it' in the bpstat structure. Under some circumstances we
4747 may decide not to print anything here and delegate the task to
4a64f543 4748 normal_stop(). */
e514a9d6
JM
4749
4750static enum print_stop_action
4751print_bp_stop_message (bpstat bs)
4752{
4753 switch (bs->print_it)
4754 {
4755 case print_it_noop:
4a64f543 4756 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4757 return PRINT_UNKNOWN;
4758 break;
4759
4760 case print_it_done:
4761 /* We still want to print the frame, but we already printed the
4a64f543 4762 relevant messages. */
e514a9d6
JM
4763 return PRINT_SRC_AND_LOC;
4764 break;
4765
4766 case print_it_normal:
4f8d1dc6 4767 {
f431efe5
PA
4768 struct breakpoint *b = bs->breakpoint_at;
4769
1a6a67de
TJB
4770 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4771 which has since been deleted. */
4772 if (b == NULL)
4773 return PRINT_UNKNOWN;
4774
348d480f
PA
4775 /* Normal case. Call the breakpoint's print_it method. */
4776 return b->ops->print_it (bs);
4f8d1dc6 4777 }
348d480f 4778 break;
3086aeae 4779
e514a9d6 4780 default:
8e65ff28 4781 internal_error (__FILE__, __LINE__,
e2e0b3e5 4782 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4783 break;
c906108c 4784 }
c906108c
SS
4785}
4786
edcc5120
TT
4787/* A helper function that prints a shared library stopped event. */
4788
4789static void
4790print_solib_event (int is_catchpoint)
4791{
4792 int any_deleted
4793 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4794 int any_added
4795 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4796
4797 if (!is_catchpoint)
4798 {
4799 if (any_added || any_deleted)
4800 ui_out_text (current_uiout,
4801 _("Stopped due to shared library event:\n"));
4802 else
4803 ui_out_text (current_uiout,
4804 _("Stopped due to shared library event (no "
4805 "libraries added or removed)\n"));
4806 }
4807
4808 if (ui_out_is_mi_like_p (current_uiout))
4809 ui_out_field_string (current_uiout, "reason",
4810 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4811
4812 if (any_deleted)
4813 {
4814 struct cleanup *cleanup;
4815 char *name;
4816 int ix;
4817
4818 ui_out_text (current_uiout, _(" Inferior unloaded "));
4819 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4820 "removed");
4821 for (ix = 0;
4822 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4823 ix, name);
4824 ++ix)
4825 {
4826 if (ix > 0)
4827 ui_out_text (current_uiout, " ");
4828 ui_out_field_string (current_uiout, "library", name);
4829 ui_out_text (current_uiout, "\n");
4830 }
4831
4832 do_cleanups (cleanup);
4833 }
4834
4835 if (any_added)
4836 {
4837 struct so_list *iter;
4838 int ix;
4839 struct cleanup *cleanup;
4840
4841 ui_out_text (current_uiout, _(" Inferior loaded "));
4842 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4843 "added");
4844 for (ix = 0;
4845 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4846 ix, iter);
4847 ++ix)
4848 {
4849 if (ix > 0)
4850 ui_out_text (current_uiout, " ");
4851 ui_out_field_string (current_uiout, "library", iter->so_name);
4852 ui_out_text (current_uiout, "\n");
4853 }
4854
4855 do_cleanups (cleanup);
4856 }
4857}
4858
e514a9d6
JM
4859/* Print a message indicating what happened. This is called from
4860 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4861 list - a list of the eventpoints that caused this stop. KIND is
4862 the target_waitkind for the stopping event. This
e514a9d6
JM
4863 routine calls the generic print routine for printing a message
4864 about reasons for stopping. This will print (for example) the
4865 "Breakpoint n," part of the output. The return value of this
4866 routine is one of:
c906108c 4867
4a64f543 4868 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4869 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4870 code to print the location. An example is
c5aa993b
JM
4871 "Breakpoint 1, " which should be followed by
4872 the location.
917317f4 4873 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4874 to also print the location part of the message.
4875 An example is the catch/throw messages, which
4a64f543 4876 don't require a location appended to the end.
917317f4 4877 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4878 further info to be printed. */
c906108c 4879
917317f4 4880enum print_stop_action
36dfb11c 4881bpstat_print (bpstat bs, int kind)
c906108c
SS
4882{
4883 int val;
c5aa993b 4884
c906108c 4885 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4886 (Currently all watchpoints go on the bpstat whether hit or not.
4887 That probably could (should) be changed, provided care is taken
c906108c 4888 with respect to bpstat_explains_signal). */
e514a9d6
JM
4889 for (; bs; bs = bs->next)
4890 {
4891 val = print_bp_stop_message (bs);
4892 if (val == PRINT_SRC_ONLY
4893 || val == PRINT_SRC_AND_LOC
4894 || val == PRINT_NOTHING)
4895 return val;
4896 }
c906108c 4897
36dfb11c
TT
4898 /* If we had hit a shared library event breakpoint,
4899 print_bp_stop_message would print out this message. If we hit an
4900 OS-level shared library event, do the same thing. */
4901 if (kind == TARGET_WAITKIND_LOADED)
4902 {
edcc5120 4903 print_solib_event (0);
36dfb11c
TT
4904 return PRINT_NOTHING;
4905 }
4906
e514a9d6 4907 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4908 with and nothing was printed. */
917317f4 4909 return PRINT_UNKNOWN;
c906108c
SS
4910}
4911
c42bd95a
DE
4912/* Evaluate the expression EXP and return 1 if value is zero.
4913 This returns the inverse of the condition because it is called
4914 from catch_errors which returns 0 if an exception happened, and if an
4915 exception happens we want execution to stop.
4a64f543 4916 The argument is a "struct expression *" that has been cast to a
c42bd95a 4917 "void *" to make it pass through catch_errors. */
c906108c
SS
4918
4919static int
4efb68b1 4920breakpoint_cond_eval (void *exp)
c906108c 4921{
278cd55f 4922 struct value *mark = value_mark ();
c5aa993b 4923 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4924
c906108c
SS
4925 value_free_to_mark (mark);
4926 return i;
4927}
4928
5760d0ab 4929/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4930
4931static bpstat
5760d0ab 4932bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4933{
4934 bpstat bs;
4935
4936 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4937 bs->next = NULL;
4938 **bs_link_pointer = bs;
4939 *bs_link_pointer = &bs->next;
f431efe5
PA
4940 bs->breakpoint_at = bl->owner;
4941 bs->bp_location_at = bl;
4942 incref_bp_location (bl);
c906108c
SS
4943 /* If the condition is false, etc., don't do the commands. */
4944 bs->commands = NULL;
4945 bs->old_val = NULL;
4946 bs->print_it = print_it_normal;
4947 return bs;
4948}
4949\f
d983da9c
DJ
4950/* The target has stopped with waitstatus WS. Check if any hardware
4951 watchpoints have triggered, according to the target. */
4952
4953int
4954watchpoints_triggered (struct target_waitstatus *ws)
4955{
d92524f1 4956 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4957 CORE_ADDR addr;
4958 struct breakpoint *b;
4959
4960 if (!stopped_by_watchpoint)
4961 {
4962 /* We were not stopped by a watchpoint. Mark all watchpoints
4963 as not triggered. */
4964 ALL_BREAKPOINTS (b)
cc60f2e3 4965 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4966 {
4967 struct watchpoint *w = (struct watchpoint *) b;
4968
4969 w->watchpoint_triggered = watch_triggered_no;
4970 }
d983da9c
DJ
4971
4972 return 0;
4973 }
4974
4975 if (!target_stopped_data_address (&current_target, &addr))
4976 {
4977 /* We were stopped by a watchpoint, but we don't know where.
4978 Mark all watchpoints as unknown. */
4979 ALL_BREAKPOINTS (b)
cc60f2e3 4980 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4981 {
4982 struct watchpoint *w = (struct watchpoint *) b;
4983
4984 w->watchpoint_triggered = watch_triggered_unknown;
4985 }
d983da9c 4986
3c4797ba 4987 return 1;
d983da9c
DJ
4988 }
4989
4990 /* The target could report the data address. Mark watchpoints
4991 affected by this data address as triggered, and all others as not
4992 triggered. */
4993
4994 ALL_BREAKPOINTS (b)
cc60f2e3 4995 if (is_hardware_watchpoint (b))
d983da9c 4996 {
3a5c3e22 4997 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4998 struct bp_location *loc;
d983da9c 4999
3a5c3e22 5000 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5001 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5002 {
3a5c3e22 5003 if (is_masked_watchpoint (b))
9c06b0b4 5004 {
3a5c3e22
PA
5005 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5006 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5007
5008 if (newaddr == start)
5009 {
3a5c3e22 5010 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5011 break;
5012 }
5013 }
5014 /* Exact match not required. Within range is sufficient. */
5015 else if (target_watchpoint_addr_within_range (&current_target,
5016 addr, loc->address,
5017 loc->length))
5018 {
3a5c3e22 5019 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5020 break;
5021 }
5022 }
d983da9c
DJ
5023 }
5024
5025 return 1;
5026}
5027
c906108c
SS
5028/* Possible return values for watchpoint_check (this can't be an enum
5029 because of check_errors). */
5030/* The watchpoint has been deleted. */
5031#define WP_DELETED 1
5032/* The value has changed. */
5033#define WP_VALUE_CHANGED 2
5034/* The value has not changed. */
5035#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5036/* Ignore this watchpoint, no matter if the value changed or not. */
5037#define WP_IGNORE 4
c906108c
SS
5038
5039#define BP_TEMPFLAG 1
5040#define BP_HARDWAREFLAG 2
5041
4a64f543
MS
5042/* Evaluate watchpoint condition expression and check if its value
5043 changed.
553e4c11
JB
5044
5045 P should be a pointer to struct bpstat, but is defined as a void *
5046 in order for this function to be usable with catch_errors. */
c906108c
SS
5047
5048static int
4efb68b1 5049watchpoint_check (void *p)
c906108c
SS
5050{
5051 bpstat bs = (bpstat) p;
3a5c3e22 5052 struct watchpoint *b;
c906108c
SS
5053 struct frame_info *fr;
5054 int within_current_scope;
5055
f431efe5 5056 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5057 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5058 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5059
f6bc2008
PA
5060 /* If this is a local watchpoint, we only want to check if the
5061 watchpoint frame is in scope if the current thread is the thread
5062 that was used to create the watchpoint. */
5063 if (!watchpoint_in_thread_scope (b))
60e1c644 5064 return WP_IGNORE;
f6bc2008 5065
c906108c
SS
5066 if (b->exp_valid_block == NULL)
5067 within_current_scope = 1;
5068 else
5069 {
edb3359d
DJ
5070 struct frame_info *frame = get_current_frame ();
5071 struct gdbarch *frame_arch = get_frame_arch (frame);
5072 CORE_ADDR frame_pc = get_frame_pc (frame);
5073
4a64f543
MS
5074 /* in_function_epilogue_p() returns a non-zero value if we're
5075 still in the function but the stack frame has already been
5076 invalidated. Since we can't rely on the values of local
5077 variables after the stack has been destroyed, we are treating
5078 the watchpoint in that state as `not changed' without further
5079 checking. Don't mark watchpoints as changed if the current
5080 frame is in an epilogue - even if they are in some other
5081 frame, our view of the stack is likely to be wrong and
5082 frame_find_by_id could error out. */
a0f49112 5083 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 5084 return WP_IGNORE;
a0f49112 5085
101dcfbe 5086 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5087 within_current_scope = (fr != NULL);
69fbadd5
DJ
5088
5089 /* If we've gotten confused in the unwinder, we might have
5090 returned a frame that can't describe this variable. */
edb3359d
DJ
5091 if (within_current_scope)
5092 {
5093 struct symbol *function;
5094
5095 function = get_frame_function (fr);
5096 if (function == NULL
5097 || !contained_in (b->exp_valid_block,
5098 SYMBOL_BLOCK_VALUE (function)))
5099 within_current_scope = 0;
5100 }
69fbadd5 5101
edb3359d 5102 if (within_current_scope)
c906108c
SS
5103 /* If we end up stopping, the current frame will get selected
5104 in normal_stop. So this call to select_frame won't affect
5105 the user. */
0f7d239c 5106 select_frame (fr);
c906108c 5107 }
c5aa993b 5108
c906108c
SS
5109 if (within_current_scope)
5110 {
4a64f543
MS
5111 /* We use value_{,free_to_}mark because it could be a *long*
5112 time before we return to the command level and call
5113 free_all_values. We can't call free_all_values because we
5114 might be in the middle of evaluating a function call. */
c906108c 5115
0cf6dd15 5116 int pc = 0;
9c06b0b4 5117 struct value *mark;
fa4727a6
DJ
5118 struct value *new_val;
5119
3a5c3e22 5120 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5121 /* Since we don't know the exact trigger address (from
5122 stopped_data_address), just tell the user we've triggered
5123 a mask watchpoint. */
5124 return WP_VALUE_CHANGED;
5125
5126 mark = value_mark ();
3a1115a0 5127 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5128
bb9d5f81
PP
5129 if (b->val_bitsize != 0)
5130 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5131
4a64f543
MS
5132 /* We use value_equal_contents instead of value_equal because
5133 the latter coerces an array to a pointer, thus comparing just
5134 the address of the array instead of its contents. This is
5135 not what we want. */
fa4727a6 5136 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5137 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5138 {
fa4727a6
DJ
5139 if (new_val != NULL)
5140 {
5141 release_value (new_val);
5142 value_free_to_mark (mark);
5143 }
c906108c
SS
5144 bs->old_val = b->val;
5145 b->val = new_val;
fa4727a6 5146 b->val_valid = 1;
c906108c
SS
5147 return WP_VALUE_CHANGED;
5148 }
5149 else
5150 {
60e1c644 5151 /* Nothing changed. */
c906108c 5152 value_free_to_mark (mark);
c906108c
SS
5153 return WP_VALUE_NOT_CHANGED;
5154 }
5155 }
5156 else
5157 {
79a45e25
PA
5158 struct ui_out *uiout = current_uiout;
5159
c906108c 5160 /* This seems like the only logical thing to do because
c5aa993b
JM
5161 if we temporarily ignored the watchpoint, then when
5162 we reenter the block in which it is valid it contains
5163 garbage (in the case of a function, it may have two
5164 garbage values, one before and one after the prologue).
5165 So we can't even detect the first assignment to it and
5166 watch after that (since the garbage may or may not equal
5167 the first value assigned). */
348d480f
PA
5168 /* We print all the stop information in
5169 breakpoint_ops->print_it, but in this case, by the time we
5170 call breakpoint_ops->print_it this bp will be deleted
5171 already. So we have no choice but print the information
5172 here. */
9dc5e2a9 5173 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5174 ui_out_field_string
5175 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5176 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5177 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5178 ui_out_text (uiout,
5179 " deleted because the program has left the block in\n\
8b93c638 5180which its expression is valid.\n");
4ce44c66 5181
cdac0397 5182 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5183 decref_counted_command_line (&b->base.commands);
d0fb5eae 5184 watchpoint_del_at_next_stop (b);
c906108c
SS
5185
5186 return WP_DELETED;
5187 }
5188}
5189
18a18393 5190/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5191 breakpoint location BL. This function does not check if we should
5192 stop, only if BL explains the stop. */
5193
18a18393 5194static int
6c95b8df 5195bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5196 struct address_space *aspace, CORE_ADDR bp_addr,
5197 const struct target_waitstatus *ws)
18a18393
VP
5198{
5199 struct breakpoint *b = bl->owner;
5200
348d480f 5201 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5202 gdb_assert (b != NULL);
5203
09ac7c10 5204 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5205}
5206
3a5c3e22
PA
5207/* Determine if the watched values have actually changed, and we
5208 should stop. If not, set BS->stop to 0. */
5209
18a18393
VP
5210static void
5211bpstat_check_watchpoint (bpstat bs)
5212{
2bdf28a0 5213 const struct bp_location *bl;
3a5c3e22 5214 struct watchpoint *b;
2bdf28a0
JK
5215
5216 /* BS is built for existing struct breakpoint. */
f431efe5 5217 bl = bs->bp_location_at;
2bdf28a0 5218 gdb_assert (bl != NULL);
3a5c3e22 5219 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5220 gdb_assert (b != NULL);
18a18393 5221
18a18393 5222 {
18a18393
VP
5223 int must_check_value = 0;
5224
3a5c3e22 5225 if (b->base.type == bp_watchpoint)
18a18393
VP
5226 /* For a software watchpoint, we must always check the
5227 watched value. */
5228 must_check_value = 1;
5229 else if (b->watchpoint_triggered == watch_triggered_yes)
5230 /* We have a hardware watchpoint (read, write, or access)
5231 and the target earlier reported an address watched by
5232 this watchpoint. */
5233 must_check_value = 1;
5234 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5235 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5236 /* We were stopped by a hardware watchpoint, but the target could
5237 not report the data address. We must check the watchpoint's
5238 value. Access and read watchpoints are out of luck; without
5239 a data address, we can't figure it out. */
5240 must_check_value = 1;
3a5c3e22 5241
18a18393
VP
5242 if (must_check_value)
5243 {
3e43a32a
MS
5244 char *message
5245 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5246 b->base.number);
18a18393
VP
5247 struct cleanup *cleanups = make_cleanup (xfree, message);
5248 int e = catch_errors (watchpoint_check, bs, message,
5249 RETURN_MASK_ALL);
5250 do_cleanups (cleanups);
5251 switch (e)
5252 {
5253 case WP_DELETED:
5254 /* We've already printed what needs to be printed. */
5255 bs->print_it = print_it_done;
5256 /* Stop. */
5257 break;
60e1c644
PA
5258 case WP_IGNORE:
5259 bs->print_it = print_it_noop;
5260 bs->stop = 0;
5261 break;
18a18393 5262 case WP_VALUE_CHANGED:
3a5c3e22 5263 if (b->base.type == bp_read_watchpoint)
18a18393 5264 {
85d721b8
PA
5265 /* There are two cases to consider here:
5266
4a64f543 5267 1. We're watching the triggered memory for reads.
85d721b8
PA
5268 In that case, trust the target, and always report
5269 the watchpoint hit to the user. Even though
5270 reads don't cause value changes, the value may
5271 have changed since the last time it was read, and
5272 since we're not trapping writes, we will not see
5273 those, and as such we should ignore our notion of
5274 old value.
5275
4a64f543 5276 2. We're watching the triggered memory for both
85d721b8
PA
5277 reads and writes. There are two ways this may
5278 happen:
5279
4a64f543 5280 2.1. This is a target that can't break on data
85d721b8
PA
5281 reads only, but can break on accesses (reads or
5282 writes), such as e.g., x86. We detect this case
5283 at the time we try to insert read watchpoints.
5284
4a64f543 5285 2.2. Otherwise, the target supports read
85d721b8
PA
5286 watchpoints, but, the user set an access or write
5287 watchpoint watching the same memory as this read
5288 watchpoint.
5289
5290 If we're watching memory writes as well as reads,
5291 ignore watchpoint hits when we find that the
5292 value hasn't changed, as reads don't cause
5293 changes. This still gives false positives when
5294 the program writes the same value to memory as
5295 what there was already in memory (we will confuse
5296 it for a read), but it's much better than
5297 nothing. */
5298
5299 int other_write_watchpoint = 0;
5300
5301 if (bl->watchpoint_type == hw_read)
5302 {
5303 struct breakpoint *other_b;
5304
5305 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5306 if (other_b->type == bp_hardware_watchpoint
5307 || other_b->type == bp_access_watchpoint)
85d721b8 5308 {
3a5c3e22
PA
5309 struct watchpoint *other_w =
5310 (struct watchpoint *) other_b;
5311
5312 if (other_w->watchpoint_triggered
5313 == watch_triggered_yes)
5314 {
5315 other_write_watchpoint = 1;
5316 break;
5317 }
85d721b8
PA
5318 }
5319 }
5320
5321 if (other_write_watchpoint
5322 || bl->watchpoint_type == hw_access)
5323 {
5324 /* We're watching the same memory for writes,
5325 and the value changed since the last time we
5326 updated it, so this trap must be for a write.
5327 Ignore it. */
5328 bs->print_it = print_it_noop;
5329 bs->stop = 0;
5330 }
18a18393
VP
5331 }
5332 break;
5333 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5334 if (b->base.type == bp_hardware_watchpoint
5335 || b->base.type == bp_watchpoint)
18a18393
VP
5336 {
5337 /* Don't stop: write watchpoints shouldn't fire if
5338 the value hasn't changed. */
5339 bs->print_it = print_it_noop;
5340 bs->stop = 0;
5341 }
5342 /* Stop. */
5343 break;
5344 default:
5345 /* Can't happen. */
5346 case 0:
5347 /* Error from catch_errors. */
3a5c3e22 5348 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5349 watchpoint_del_at_next_stop (b);
18a18393
VP
5350 /* We've already printed what needs to be printed. */
5351 bs->print_it = print_it_done;
5352 break;
5353 }
5354 }
5355 else /* must_check_value == 0 */
5356 {
5357 /* This is a case where some watchpoint(s) triggered, but
5358 not at the address of this watchpoint, or else no
5359 watchpoint triggered after all. So don't print
5360 anything for this watchpoint. */
5361 bs->print_it = print_it_noop;
5362 bs->stop = 0;
5363 }
5364 }
5365}
5366
7d4df6a4
DE
5367/* For breakpoints that are currently marked as telling gdb to stop,
5368 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5369 of breakpoint referred to by BS. If we should not stop for this
5370 breakpoint, set BS->stop to 0. */
f431efe5 5371
18a18393
VP
5372static void
5373bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5374{
2bdf28a0
JK
5375 const struct bp_location *bl;
5376 struct breakpoint *b;
7d4df6a4
DE
5377 int value_is_zero = 0;
5378 struct expression *cond;
5379
5380 gdb_assert (bs->stop);
2bdf28a0
JK
5381
5382 /* BS is built for existing struct breakpoint. */
f431efe5 5383 bl = bs->bp_location_at;
2bdf28a0 5384 gdb_assert (bl != NULL);
f431efe5 5385 b = bs->breakpoint_at;
2bdf28a0 5386 gdb_assert (b != NULL);
18a18393 5387
b775012e
LM
5388 /* Even if the target evaluated the condition on its end and notified GDB, we
5389 need to do so again since GDB does not know if we stopped due to a
5390 breakpoint or a single step breakpoint. */
5391
18a18393 5392 if (frame_id_p (b->frame_id)
edb3359d 5393 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5394 {
7d4df6a4
DE
5395 bs->stop = 0;
5396 return;
5397 }
60e1c644 5398
12ab52e9
PA
5399 /* If this is a thread/task-specific breakpoint, don't waste cpu
5400 evaluating the condition if this isn't the specified
5401 thread/task. */
5402 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5403 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5404
6c1b0f7b
DE
5405 {
5406 bs->stop = 0;
5407 return;
5408 }
5409
6dddc817
DE
5410 /* Evaluate extension language breakpoints that have a "stop" method
5411 implemented. */
5412 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5413
7d4df6a4
DE
5414 if (is_watchpoint (b))
5415 {
5416 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5417
7d4df6a4
DE
5418 cond = w->cond_exp;
5419 }
5420 else
5421 cond = bl->cond;
60e1c644 5422
7d4df6a4
DE
5423 if (cond && b->disposition != disp_del_at_next_stop)
5424 {
5425 int within_current_scope = 1;
5426 struct watchpoint * w;
60e1c644 5427
7d4df6a4
DE
5428 /* We use value_mark and value_free_to_mark because it could
5429 be a long time before we return to the command level and
5430 call free_all_values. We can't call free_all_values
5431 because we might be in the middle of evaluating a
5432 function call. */
5433 struct value *mark = value_mark ();
5434
5435 if (is_watchpoint (b))
5436 w = (struct watchpoint *) b;
5437 else
5438 w = NULL;
5439
5440 /* Need to select the frame, with all that implies so that
5441 the conditions will have the right context. Because we
5442 use the frame, we will not see an inlined function's
5443 variables when we arrive at a breakpoint at the start
5444 of the inlined function; the current frame will be the
5445 call site. */
5446 if (w == NULL || w->cond_exp_valid_block == NULL)
5447 select_frame (get_current_frame ());
5448 else
18a18393 5449 {
7d4df6a4
DE
5450 struct frame_info *frame;
5451
5452 /* For local watchpoint expressions, which particular
5453 instance of a local is being watched matters, so we
5454 keep track of the frame to evaluate the expression
5455 in. To evaluate the condition however, it doesn't
5456 really matter which instantiation of the function
5457 where the condition makes sense triggers the
5458 watchpoint. This allows an expression like "watch
5459 global if q > 10" set in `func', catch writes to
5460 global on all threads that call `func', or catch
5461 writes on all recursive calls of `func' by a single
5462 thread. We simply always evaluate the condition in
5463 the innermost frame that's executing where it makes
5464 sense to evaluate the condition. It seems
5465 intuitive. */
5466 frame = block_innermost_frame (w->cond_exp_valid_block);
5467 if (frame != NULL)
5468 select_frame (frame);
5469 else
5470 within_current_scope = 0;
18a18393 5471 }
7d4df6a4
DE
5472 if (within_current_scope)
5473 value_is_zero
5474 = catch_errors (breakpoint_cond_eval, cond,
5475 "Error in testing breakpoint condition:\n",
5476 RETURN_MASK_ALL);
5477 else
18a18393 5478 {
7d4df6a4
DE
5479 warning (_("Watchpoint condition cannot be tested "
5480 "in the current scope"));
5481 /* If we failed to set the right context for this
5482 watchpoint, unconditionally report it. */
5483 value_is_zero = 0;
18a18393 5484 }
7d4df6a4
DE
5485 /* FIXME-someday, should give breakpoint #. */
5486 value_free_to_mark (mark);
18a18393 5487 }
7d4df6a4
DE
5488
5489 if (cond && value_is_zero)
5490 {
5491 bs->stop = 0;
5492 }
7d4df6a4
DE
5493 else if (b->ignore_count > 0)
5494 {
5495 b->ignore_count--;
5496 bs->stop = 0;
5497 /* Increase the hit count even though we don't stop. */
5498 ++(b->hit_count);
5499 observer_notify_breakpoint_modified (b);
5500 }
18a18393
VP
5501}
5502
5503
9709f61c 5504/* Get a bpstat associated with having just stopped at address
d983da9c 5505 BP_ADDR in thread PTID.
c906108c 5506
d983da9c 5507 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5508 don't understand this stop. Result is a chain of bpstat's such
5509 that:
c906108c 5510
c5aa993b 5511 if we don't understand the stop, the result is a null pointer.
c906108c 5512
c5aa993b 5513 if we understand why we stopped, the result is not null.
c906108c 5514
c5aa993b
JM
5515 Each element of the chain refers to a particular breakpoint or
5516 watchpoint at which we have stopped. (We may have stopped for
5517 several reasons concurrently.)
c906108c 5518
c5aa993b
JM
5519 Each element of the chain has valid next, breakpoint_at,
5520 commands, FIXME??? fields. */
c906108c
SS
5521
5522bpstat
6c95b8df 5523bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5524 CORE_ADDR bp_addr, ptid_t ptid,
5525 const struct target_waitstatus *ws)
c906108c 5526{
0d381245 5527 struct breakpoint *b = NULL;
afe38095 5528 struct bp_location *bl;
20874c92 5529 struct bp_location *loc;
5760d0ab
JK
5530 /* First item of allocated bpstat's. */
5531 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5532 /* Pointer to the last thing in the chain currently. */
5760d0ab 5533 bpstat bs;
20874c92 5534 int ix;
429374b8 5535 int need_remove_insert;
f431efe5 5536 int removed_any;
c906108c 5537
f431efe5
PA
5538 /* First, build the bpstat chain with locations that explain a
5539 target stop, while being careful to not set the target running,
5540 as that may invalidate locations (in particular watchpoint
5541 locations are recreated). Resuming will happen here with
5542 breakpoint conditions or watchpoint expressions that include
5543 inferior function calls. */
c5aa993b 5544
429374b8
JK
5545 ALL_BREAKPOINTS (b)
5546 {
5547 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5548 continue;
a5606eee 5549
429374b8
JK
5550 for (bl = b->loc; bl != NULL; bl = bl->next)
5551 {
4a64f543
MS
5552 /* For hardware watchpoints, we look only at the first
5553 location. The watchpoint_check function will work on the
5554 entire expression, not the individual locations. For
5555 read watchpoints, the watchpoints_triggered function has
5556 checked all locations already. */
429374b8
JK
5557 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5558 break;
18a18393 5559
f6592439 5560 if (!bl->enabled || bl->shlib_disabled)
429374b8 5561 continue;
c5aa993b 5562
09ac7c10 5563 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5564 continue;
c5aa993b 5565
4a64f543
MS
5566 /* Come here if it's a watchpoint, or if the break address
5567 matches. */
c5aa993b 5568
4a64f543
MS
5569 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5570 explain stop. */
c5aa993b 5571
f431efe5
PA
5572 /* Assume we stop. Should we find a watchpoint that is not
5573 actually triggered, or if the condition of the breakpoint
5574 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5575 bs->stop = 1;
5576 bs->print = 1;
d983da9c 5577
f431efe5
PA
5578 /* If this is a scope breakpoint, mark the associated
5579 watchpoint as triggered so that we will handle the
5580 out-of-scope event. We'll get to the watchpoint next
5581 iteration. */
d0fb5eae 5582 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5583 {
5584 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5585
5586 w->watchpoint_triggered = watch_triggered_yes;
5587 }
f431efe5
PA
5588 }
5589 }
5590
5591 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5592 {
f1310107 5593 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5594 {
5760d0ab 5595 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5596 /* For hits of moribund locations, we should just proceed. */
5597 bs->stop = 0;
5598 bs->print = 0;
5599 bs->print_it = print_it_noop;
5600 }
5601 }
5602
edcc5120
TT
5603 /* A bit of special processing for shlib breakpoints. We need to
5604 process solib loading here, so that the lists of loaded and
5605 unloaded libraries are correct before we handle "catch load" and
5606 "catch unload". */
5607 for (bs = bs_head; bs != NULL; bs = bs->next)
5608 {
5d268276 5609 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5610 {
5611 handle_solib_event ();
5612 break;
5613 }
5614 }
5615
f431efe5
PA
5616 /* Now go through the locations that caused the target to stop, and
5617 check whether we're interested in reporting this stop to higher
5618 layers, or whether we should resume the target transparently. */
5619
5620 removed_any = 0;
5621
5760d0ab 5622 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5623 {
5624 if (!bs->stop)
5625 continue;
5626
f431efe5 5627 b = bs->breakpoint_at;
348d480f
PA
5628 b->ops->check_status (bs);
5629 if (bs->stop)
28010a5d 5630 {
348d480f 5631 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5632
429374b8
JK
5633 if (bs->stop)
5634 {
5635 ++(b->hit_count);
8d3788bd 5636 observer_notify_breakpoint_modified (b);
c906108c 5637
4a64f543 5638 /* We will stop here. */
429374b8
JK
5639 if (b->disposition == disp_disable)
5640 {
816338b5
SS
5641 --(b->enable_count);
5642 if (b->enable_count <= 0
5643 && b->enable_state != bp_permanent)
429374b8 5644 b->enable_state = bp_disabled;
f431efe5 5645 removed_any = 1;
429374b8
JK
5646 }
5647 if (b->silent)
5648 bs->print = 0;
5649 bs->commands = b->commands;
9add0f1b 5650 incref_counted_command_line (bs->commands);
abf85f46
JK
5651 if (command_line_is_silent (bs->commands
5652 ? bs->commands->commands : NULL))
5653 bs->print = 0;
9d6e6e84
HZ
5654
5655 b->ops->after_condition_true (bs);
429374b8
JK
5656 }
5657
348d480f 5658 }
a9b3a50f
PA
5659
5660 /* Print nothing for this entry if we don't stop or don't
5661 print. */
5662 if (!bs->stop || !bs->print)
5663 bs->print_it = print_it_noop;
429374b8 5664 }
876fa593 5665
d983da9c
DJ
5666 /* If we aren't stopping, the value of some hardware watchpoint may
5667 not have changed, but the intermediate memory locations we are
5668 watching may have. Don't bother if we're stopping; this will get
5669 done later. */
d832cb68 5670 need_remove_insert = 0;
5760d0ab
JK
5671 if (! bpstat_causes_stop (bs_head))
5672 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5673 if (!bs->stop
f431efe5
PA
5674 && bs->breakpoint_at
5675 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5676 {
3a5c3e22
PA
5677 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5678
5679 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5680 need_remove_insert = 1;
d983da9c
DJ
5681 }
5682
d832cb68 5683 if (need_remove_insert)
44702360 5684 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5685 else if (removed_any)
44702360 5686 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5687
5760d0ab 5688 return bs_head;
c906108c 5689}
628fe4e4
JK
5690
5691static void
5692handle_jit_event (void)
5693{
5694 struct frame_info *frame;
5695 struct gdbarch *gdbarch;
5696
5697 /* Switch terminal for any messages produced by
5698 breakpoint_re_set. */
5699 target_terminal_ours_for_output ();
5700
5701 frame = get_current_frame ();
5702 gdbarch = get_frame_arch (frame);
5703
5704 jit_event_handler (gdbarch);
5705
5706 target_terminal_inferior ();
5707}
5708
5709/* Prepare WHAT final decision for infrun. */
5710
5711/* Decide what infrun needs to do with this bpstat. */
5712
c906108c 5713struct bpstat_what
0e30163f 5714bpstat_what (bpstat bs_head)
c906108c 5715{
c906108c 5716 struct bpstat_what retval;
628fe4e4 5717 int jit_event = 0;
0e30163f 5718 bpstat bs;
c906108c 5719
628fe4e4 5720 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5721 retval.call_dummy = STOP_NONE;
186c406b 5722 retval.is_longjmp = 0;
628fe4e4 5723
0e30163f 5724 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5725 {
628fe4e4
JK
5726 /* Extract this BS's action. After processing each BS, we check
5727 if its action overrides all we've seem so far. */
5728 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5729 enum bptype bptype;
5730
c906108c 5731 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5732 {
5733 /* I suspect this can happen if it was a momentary
5734 breakpoint which has since been deleted. */
5735 bptype = bp_none;
5736 }
20874c92 5737 else
f431efe5 5738 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5739
5740 switch (bptype)
c906108c
SS
5741 {
5742 case bp_none:
628fe4e4 5743 break;
c906108c
SS
5744 case bp_breakpoint:
5745 case bp_hardware_breakpoint:
5746 case bp_until:
5747 case bp_finish:
a9b3a50f 5748 case bp_shlib_event:
c906108c
SS
5749 if (bs->stop)
5750 {
5751 if (bs->print)
628fe4e4 5752 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5753 else
628fe4e4 5754 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5755 }
5756 else
628fe4e4 5757 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5758 break;
5759 case bp_watchpoint:
5760 case bp_hardware_watchpoint:
5761 case bp_read_watchpoint:
5762 case bp_access_watchpoint:
5763 if (bs->stop)
5764 {
5765 if (bs->print)
628fe4e4 5766 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5767 else
628fe4e4 5768 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5769 }
5770 else
628fe4e4
JK
5771 {
5772 /* There was a watchpoint, but we're not stopping.
5773 This requires no further action. */
5774 }
c906108c
SS
5775 break;
5776 case bp_longjmp:
e2e4d78b 5777 case bp_longjmp_call_dummy:
186c406b 5778 case bp_exception:
628fe4e4 5779 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5780 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5781 break;
5782 case bp_longjmp_resume:
186c406b 5783 case bp_exception_resume:
628fe4e4 5784 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5785 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5786 break;
5787 case bp_step_resume:
5788 if (bs->stop)
628fe4e4
JK
5789 this_action = BPSTAT_WHAT_STEP_RESUME;
5790 else
c906108c 5791 {
628fe4e4
JK
5792 /* It is for the wrong frame. */
5793 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5794 }
c906108c 5795 break;
2c03e5be
PA
5796 case bp_hp_step_resume:
5797 if (bs->stop)
5798 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5799 else
5800 {
5801 /* It is for the wrong frame. */
5802 this_action = BPSTAT_WHAT_SINGLE;
5803 }
5804 break;
c906108c 5805 case bp_watchpoint_scope:
c4093a6a 5806 case bp_thread_event:
1900040c 5807 case bp_overlay_event:
0fd8e87f 5808 case bp_longjmp_master:
aa7d318d 5809 case bp_std_terminate_master:
186c406b 5810 case bp_exception_master:
628fe4e4 5811 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5812 break;
ce78b96d 5813 case bp_catchpoint:
c5aa993b
JM
5814 if (bs->stop)
5815 {
5816 if (bs->print)
628fe4e4 5817 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5818 else
628fe4e4 5819 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5820 }
5821 else
628fe4e4
JK
5822 {
5823 /* There was a catchpoint, but we're not stopping.
5824 This requires no further action. */
5825 }
5826 break;
628fe4e4
JK
5827 case bp_jit_event:
5828 jit_event = 1;
5829 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5830 break;
c906108c 5831 case bp_call_dummy:
53a5351d
JM
5832 /* Make sure the action is stop (silent or noisy),
5833 so infrun.c pops the dummy frame. */
aa7d318d 5834 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5835 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5836 break;
5837 case bp_std_terminate:
5838 /* Make sure the action is stop (silent or noisy),
5839 so infrun.c pops the dummy frame. */
aa7d318d 5840 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5841 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5842 break;
1042e4c0 5843 case bp_tracepoint:
7a697b8d 5844 case bp_fast_tracepoint:
0fb4aa4b 5845 case bp_static_tracepoint:
1042e4c0
SS
5846 /* Tracepoint hits should not be reported back to GDB, and
5847 if one got through somehow, it should have been filtered
5848 out already. */
5849 internal_error (__FILE__, __LINE__,
7a697b8d 5850 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5851 break;
5852 case bp_gnu_ifunc_resolver:
5853 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5854 this_action = BPSTAT_WHAT_SINGLE;
5855 break;
5856 case bp_gnu_ifunc_resolver_return:
5857 /* The breakpoint will be removed, execution will restart from the
5858 PC of the former breakpoint. */
5859 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5860 break;
e7e0cddf
SS
5861
5862 case bp_dprintf:
a11cfd87
HZ
5863 if (bs->stop)
5864 this_action = BPSTAT_WHAT_STOP_SILENT;
5865 else
5866 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5867 break;
5868
628fe4e4
JK
5869 default:
5870 internal_error (__FILE__, __LINE__,
5871 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5872 }
628fe4e4
JK
5873
5874 retval.main_action = max (retval.main_action, this_action);
c906108c 5875 }
628fe4e4 5876
0e30163f
JK
5877 /* These operations may affect the bs->breakpoint_at state so they are
5878 delayed after MAIN_ACTION is decided above. */
5879
628fe4e4
JK
5880 if (jit_event)
5881 {
5882 if (debug_infrun)
5883 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5884
5885 handle_jit_event ();
5886 }
5887
0e30163f
JK
5888 for (bs = bs_head; bs != NULL; bs = bs->next)
5889 {
5890 struct breakpoint *b = bs->breakpoint_at;
5891
5892 if (b == NULL)
5893 continue;
5894 switch (b->type)
5895 {
5896 case bp_gnu_ifunc_resolver:
5897 gnu_ifunc_resolver_stop (b);
5898 break;
5899 case bp_gnu_ifunc_resolver_return:
5900 gnu_ifunc_resolver_return_stop (b);
5901 break;
5902 }
5903 }
5904
c906108c
SS
5905 return retval;
5906}
5907
5908/* Nonzero if we should step constantly (e.g. watchpoints on machines
5909 without hardware support). This isn't related to a specific bpstat,
5910 just to things like whether watchpoints are set. */
5911
c5aa993b 5912int
fba45db2 5913bpstat_should_step (void)
c906108c
SS
5914{
5915 struct breakpoint *b;
cc59ec59 5916
c906108c 5917 ALL_BREAKPOINTS (b)
717a8278 5918 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5919 return 1;
c906108c
SS
5920 return 0;
5921}
5922
67822962
PA
5923int
5924bpstat_causes_stop (bpstat bs)
5925{
5926 for (; bs != NULL; bs = bs->next)
5927 if (bs->stop)
5928 return 1;
5929
5930 return 0;
5931}
5932
c906108c 5933\f
c5aa993b 5934
170b53b2
UW
5935/* Compute a string of spaces suitable to indent the next line
5936 so it starts at the position corresponding to the table column
5937 named COL_NAME in the currently active table of UIOUT. */
5938
5939static char *
5940wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5941{
5942 static char wrap_indent[80];
5943 int i, total_width, width, align;
5944 char *text;
5945
5946 total_width = 0;
5947 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5948 {
5949 if (strcmp (text, col_name) == 0)
5950 {
5951 gdb_assert (total_width < sizeof wrap_indent);
5952 memset (wrap_indent, ' ', total_width);
5953 wrap_indent[total_width] = 0;
5954
5955 return wrap_indent;
5956 }
5957
5958 total_width += width + 1;
5959 }
5960
5961 return NULL;
5962}
5963
b775012e
LM
5964/* Determine if the locations of this breakpoint will have their conditions
5965 evaluated by the target, host or a mix of both. Returns the following:
5966
5967 "host": Host evals condition.
5968 "host or target": Host or Target evals condition.
5969 "target": Target evals condition.
5970*/
5971
5972static const char *
5973bp_condition_evaluator (struct breakpoint *b)
5974{
5975 struct bp_location *bl;
5976 char host_evals = 0;
5977 char target_evals = 0;
5978
5979 if (!b)
5980 return NULL;
5981
5982 if (!is_breakpoint (b))
5983 return NULL;
5984
5985 if (gdb_evaluates_breakpoint_condition_p ()
5986 || !target_supports_evaluation_of_breakpoint_conditions ())
5987 return condition_evaluation_host;
5988
5989 for (bl = b->loc; bl; bl = bl->next)
5990 {
5991 if (bl->cond_bytecode)
5992 target_evals++;
5993 else
5994 host_evals++;
5995 }
5996
5997 if (host_evals && target_evals)
5998 return condition_evaluation_both;
5999 else if (target_evals)
6000 return condition_evaluation_target;
6001 else
6002 return condition_evaluation_host;
6003}
6004
6005/* Determine the breakpoint location's condition evaluator. This is
6006 similar to bp_condition_evaluator, but for locations. */
6007
6008static const char *
6009bp_location_condition_evaluator (struct bp_location *bl)
6010{
6011 if (bl && !is_breakpoint (bl->owner))
6012 return NULL;
6013
6014 if (gdb_evaluates_breakpoint_condition_p ()
6015 || !target_supports_evaluation_of_breakpoint_conditions ())
6016 return condition_evaluation_host;
6017
6018 if (bl && bl->cond_bytecode)
6019 return condition_evaluation_target;
6020 else
6021 return condition_evaluation_host;
6022}
6023
859825b8
JK
6024/* Print the LOC location out of the list of B->LOC locations. */
6025
170b53b2
UW
6026static void
6027print_breakpoint_location (struct breakpoint *b,
6028 struct bp_location *loc)
0d381245 6029{
79a45e25 6030 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6031 struct cleanup *old_chain = save_current_program_space ();
6032
859825b8
JK
6033 if (loc != NULL && loc->shlib_disabled)
6034 loc = NULL;
6035
6c95b8df
PA
6036 if (loc != NULL)
6037 set_current_program_space (loc->pspace);
6038
56435ebe
TT
6039 if (b->display_canonical)
6040 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 6041 else if (loc && loc->symtab)
0d381245
VP
6042 {
6043 struct symbol *sym
6044 = find_pc_sect_function (loc->address, loc->section);
6045 if (sym)
6046 {
6047 ui_out_text (uiout, "in ");
6048 ui_out_field_string (uiout, "func",
6049 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6050 ui_out_text (uiout, " ");
6051 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6052 ui_out_text (uiout, "at ");
0d381245 6053 }
05cba821
JK
6054 ui_out_field_string (uiout, "file",
6055 symtab_to_filename_for_display (loc->symtab));
0d381245 6056 ui_out_text (uiout, ":");
05cba821 6057
0d381245 6058 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6059 ui_out_field_string (uiout, "fullname",
6060 symtab_to_fullname (loc->symtab));
0d381245 6061
f8eba3c6 6062 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6063 }
859825b8 6064 else if (loc)
0d381245 6065 {
f99d8bf4
PA
6066 struct ui_file *stb = mem_fileopen ();
6067 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6068
f99d8bf4 6069 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6070 demangle, "");
0d381245 6071 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6072
6073 do_cleanups (stb_chain);
0d381245 6074 }
859825b8
JK
6075 else
6076 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 6077
b775012e
LM
6078 if (loc && is_breakpoint (b)
6079 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6080 && bp_condition_evaluator (b) == condition_evaluation_both)
6081 {
6082 ui_out_text (uiout, " (");
6083 ui_out_field_string (uiout, "evaluated-by",
6084 bp_location_condition_evaluator (loc));
6085 ui_out_text (uiout, ")");
6086 }
6087
6c95b8df 6088 do_cleanups (old_chain);
0d381245
VP
6089}
6090
269b11a2
PA
6091static const char *
6092bptype_string (enum bptype type)
c906108c 6093{
c4093a6a
JM
6094 struct ep_type_description
6095 {
6096 enum bptype type;
6097 char *description;
6098 };
6099 static struct ep_type_description bptypes[] =
c906108c 6100 {
c5aa993b
JM
6101 {bp_none, "?deleted?"},
6102 {bp_breakpoint, "breakpoint"},
c906108c 6103 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
6104 {bp_until, "until"},
6105 {bp_finish, "finish"},
6106 {bp_watchpoint, "watchpoint"},
c906108c 6107 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6108 {bp_read_watchpoint, "read watchpoint"},
6109 {bp_access_watchpoint, "acc watchpoint"},
6110 {bp_longjmp, "longjmp"},
6111 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6112 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6113 {bp_exception, "exception"},
6114 {bp_exception_resume, "exception resume"},
c5aa993b 6115 {bp_step_resume, "step resume"},
2c03e5be 6116 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6117 {bp_watchpoint_scope, "watchpoint scope"},
6118 {bp_call_dummy, "call dummy"},
aa7d318d 6119 {bp_std_terminate, "std::terminate"},
c5aa993b 6120 {bp_shlib_event, "shlib events"},
c4093a6a 6121 {bp_thread_event, "thread events"},
1900040c 6122 {bp_overlay_event, "overlay events"},
0fd8e87f 6123 {bp_longjmp_master, "longjmp master"},
aa7d318d 6124 {bp_std_terminate_master, "std::terminate master"},
186c406b 6125 {bp_exception_master, "exception master"},
ce78b96d 6126 {bp_catchpoint, "catchpoint"},
1042e4c0 6127 {bp_tracepoint, "tracepoint"},
7a697b8d 6128 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6129 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6130 {bp_dprintf, "dprintf"},
4efc6507 6131 {bp_jit_event, "jit events"},
0e30163f
JK
6132 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6133 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6134 };
269b11a2
PA
6135
6136 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6137 || ((int) type != bptypes[(int) type].type))
6138 internal_error (__FILE__, __LINE__,
6139 _("bptypes table does not describe type #%d."),
6140 (int) type);
6141
6142 return bptypes[(int) type].description;
6143}
6144
998580f1
MK
6145/* For MI, output a field named 'thread-groups' with a list as the value.
6146 For CLI, prefix the list with the string 'inf'. */
6147
6148static void
6149output_thread_groups (struct ui_out *uiout,
6150 const char *field_name,
6151 VEC(int) *inf_num,
6152 int mi_only)
6153{
752eb8b4 6154 struct cleanup *back_to;
998580f1
MK
6155 int is_mi = ui_out_is_mi_like_p (uiout);
6156 int inf;
6157 int i;
6158
6159 /* For backward compatibility, don't display inferiors in CLI unless
6160 there are several. Always display them for MI. */
6161 if (!is_mi && mi_only)
6162 return;
6163
752eb8b4
TT
6164 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6165
998580f1
MK
6166 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6167 {
6168 if (is_mi)
6169 {
6170 char mi_group[10];
6171
6172 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6173 ui_out_field_string (uiout, NULL, mi_group);
6174 }
6175 else
6176 {
6177 if (i == 0)
6178 ui_out_text (uiout, " inf ");
6179 else
6180 ui_out_text (uiout, ", ");
6181
6182 ui_out_text (uiout, plongest (inf));
6183 }
6184 }
6185
6186 do_cleanups (back_to);
6187}
6188
269b11a2
PA
6189/* Print B to gdb_stdout. */
6190
6191static void
6192print_one_breakpoint_location (struct breakpoint *b,
6193 struct bp_location *loc,
6194 int loc_number,
6195 struct bp_location **last_loc,
269b11a2
PA
6196 int allflag)
6197{
6198 struct command_line *l;
c2c6d25f 6199 static char bpenables[] = "nynny";
c906108c 6200
79a45e25 6201 struct ui_out *uiout = current_uiout;
0d381245
VP
6202 int header_of_multiple = 0;
6203 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6204 struct value_print_options opts;
6205
6206 get_user_print_options (&opts);
0d381245
VP
6207
6208 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6209 /* See comment in print_one_breakpoint concerning treatment of
6210 breakpoints with single disabled location. */
0d381245
VP
6211 if (loc == NULL
6212 && (b->loc != NULL
6213 && (b->loc->next != NULL || !b->loc->enabled)))
6214 header_of_multiple = 1;
6215 if (loc == NULL)
6216 loc = b->loc;
6217
c4093a6a
JM
6218 annotate_record ();
6219
6220 /* 1 */
6221 annotate_field (0);
0d381245
VP
6222 if (part_of_multiple)
6223 {
6224 char *formatted;
0c6773c1 6225 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6226 ui_out_field_string (uiout, "number", formatted);
6227 xfree (formatted);
6228 }
6229 else
6230 {
6231 ui_out_field_int (uiout, "number", b->number);
6232 }
c4093a6a
JM
6233
6234 /* 2 */
6235 annotate_field (1);
0d381245
VP
6236 if (part_of_multiple)
6237 ui_out_field_skip (uiout, "type");
269b11a2
PA
6238 else
6239 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6240
6241 /* 3 */
6242 annotate_field (2);
0d381245
VP
6243 if (part_of_multiple)
6244 ui_out_field_skip (uiout, "disp");
6245 else
2cec12e5 6246 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6247
c4093a6a
JM
6248
6249 /* 4 */
6250 annotate_field (3);
0d381245 6251 if (part_of_multiple)
54e52265 6252 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6253 else
4a64f543
MS
6254 ui_out_field_fmt (uiout, "enabled", "%c",
6255 bpenables[(int) b->enable_state]);
54e52265 6256 ui_out_spaces (uiout, 2);
0d381245 6257
c4093a6a
JM
6258
6259 /* 5 and 6 */
3086aeae 6260 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6261 {
4a64f543
MS
6262 /* Although the print_one can possibly print all locations,
6263 calling it here is not likely to get any nice result. So,
6264 make sure there's just one location. */
0d381245 6265 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6266 b->ops->print_one (b, last_loc);
0d381245 6267 }
3086aeae
DJ
6268 else
6269 switch (b->type)
6270 {
6271 case bp_none:
6272 internal_error (__FILE__, __LINE__,
e2e0b3e5 6273 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6274 break;
c906108c 6275
3086aeae
DJ
6276 case bp_watchpoint:
6277 case bp_hardware_watchpoint:
6278 case bp_read_watchpoint:
6279 case bp_access_watchpoint:
3a5c3e22
PA
6280 {
6281 struct watchpoint *w = (struct watchpoint *) b;
6282
6283 /* Field 4, the address, is omitted (which makes the columns
6284 not line up too nicely with the headers, but the effect
6285 is relatively readable). */
6286 if (opts.addressprint)
6287 ui_out_field_skip (uiout, "addr");
6288 annotate_field (5);
6289 ui_out_field_string (uiout, "what", w->exp_string);
6290 }
3086aeae
DJ
6291 break;
6292
3086aeae
DJ
6293 case bp_breakpoint:
6294 case bp_hardware_breakpoint:
6295 case bp_until:
6296 case bp_finish:
6297 case bp_longjmp:
6298 case bp_longjmp_resume:
e2e4d78b 6299 case bp_longjmp_call_dummy:
186c406b
TT
6300 case bp_exception:
6301 case bp_exception_resume:
3086aeae 6302 case bp_step_resume:
2c03e5be 6303 case bp_hp_step_resume:
3086aeae
DJ
6304 case bp_watchpoint_scope:
6305 case bp_call_dummy:
aa7d318d 6306 case bp_std_terminate:
3086aeae
DJ
6307 case bp_shlib_event:
6308 case bp_thread_event:
6309 case bp_overlay_event:
0fd8e87f 6310 case bp_longjmp_master:
aa7d318d 6311 case bp_std_terminate_master:
186c406b 6312 case bp_exception_master:
1042e4c0 6313 case bp_tracepoint:
7a697b8d 6314 case bp_fast_tracepoint:
0fb4aa4b 6315 case bp_static_tracepoint:
e7e0cddf 6316 case bp_dprintf:
4efc6507 6317 case bp_jit_event:
0e30163f
JK
6318 case bp_gnu_ifunc_resolver:
6319 case bp_gnu_ifunc_resolver_return:
79a45b7d 6320 if (opts.addressprint)
3086aeae
DJ
6321 {
6322 annotate_field (4);
54e52265 6323 if (header_of_multiple)
0d381245 6324 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6325 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6326 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6327 else
5af949e3
UW
6328 ui_out_field_core_addr (uiout, "addr",
6329 loc->gdbarch, loc->address);
3086aeae
DJ
6330 }
6331 annotate_field (5);
0d381245 6332 if (!header_of_multiple)
170b53b2 6333 print_breakpoint_location (b, loc);
0d381245 6334 if (b->loc)
a6d9a66e 6335 *last_loc = b->loc;
3086aeae
DJ
6336 break;
6337 }
c906108c 6338
6c95b8df 6339
998580f1 6340 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6341 {
6342 struct inferior *inf;
998580f1
MK
6343 VEC(int) *inf_num = NULL;
6344 int mi_only = 1;
6c95b8df 6345
998580f1 6346 ALL_INFERIORS (inf)
6c95b8df
PA
6347 {
6348 if (inf->pspace == loc->pspace)
998580f1 6349 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6350 }
998580f1
MK
6351
6352 /* For backward compatibility, don't display inferiors in CLI unless
6353 there are several. Always display for MI. */
6354 if (allflag
6355 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6356 && (number_of_program_spaces () > 1
6357 || number_of_inferiors () > 1)
6358 /* LOC is for existing B, it cannot be in
6359 moribund_locations and thus having NULL OWNER. */
6360 && loc->owner->type != bp_catchpoint))
6361 mi_only = 0;
6362 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6363 VEC_free (int, inf_num);
6c95b8df
PA
6364 }
6365
4a306c9a 6366 if (!part_of_multiple)
c4093a6a 6367 {
4a306c9a
JB
6368 if (b->thread != -1)
6369 {
6370 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6371 "stop only in" line a little further down. */
4a306c9a
JB
6372 ui_out_text (uiout, " thread ");
6373 ui_out_field_int (uiout, "thread", b->thread);
6374 }
6375 else if (b->task != 0)
6376 {
6377 ui_out_text (uiout, " task ");
6378 ui_out_field_int (uiout, "task", b->task);
6379 }
c4093a6a 6380 }
f1310107 6381
8b93c638 6382 ui_out_text (uiout, "\n");
f1310107 6383
348d480f 6384 if (!part_of_multiple)
f1310107
TJB
6385 b->ops->print_one_detail (b, uiout);
6386
0d381245 6387 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6388 {
6389 annotate_field (6);
8b93c638 6390 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6391 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6392 the frame ID. */
5af949e3
UW
6393 ui_out_field_core_addr (uiout, "frame",
6394 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6395 ui_out_text (uiout, "\n");
c4093a6a
JM
6396 }
6397
28010a5d 6398 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6399 {
6400 annotate_field (7);
d77f58be 6401 if (is_tracepoint (b))
1042e4c0
SS
6402 ui_out_text (uiout, "\ttrace only if ");
6403 else
6404 ui_out_text (uiout, "\tstop only if ");
0101ce28 6405 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6406
6407 /* Print whether the target is doing the breakpoint's condition
6408 evaluation. If GDB is doing the evaluation, don't print anything. */
6409 if (is_breakpoint (b)
6410 && breakpoint_condition_evaluation_mode ()
6411 == condition_evaluation_target)
6412 {
6413 ui_out_text (uiout, " (");
6414 ui_out_field_string (uiout, "evaluated-by",
6415 bp_condition_evaluator (b));
6416 ui_out_text (uiout, " evals)");
6417 }
0101ce28
JJ
6418 ui_out_text (uiout, "\n");
6419 }
6420
0d381245 6421 if (!part_of_multiple && b->thread != -1)
c4093a6a 6422 {
4a64f543 6423 /* FIXME should make an annotation for this. */
8b93c638
JM
6424 ui_out_text (uiout, "\tstop only in thread ");
6425 ui_out_field_int (uiout, "thread", b->thread);
6426 ui_out_text (uiout, "\n");
c4093a6a
JM
6427 }
6428
556ec64d
YQ
6429 if (!part_of_multiple)
6430 {
6431 if (b->hit_count)
31f56a27
YQ
6432 {
6433 /* FIXME should make an annotation for this. */
6434 if (is_catchpoint (b))
6435 ui_out_text (uiout, "\tcatchpoint");
6436 else if (is_tracepoint (b))
6437 ui_out_text (uiout, "\ttracepoint");
6438 else
6439 ui_out_text (uiout, "\tbreakpoint");
6440 ui_out_text (uiout, " already hit ");
6441 ui_out_field_int (uiout, "times", b->hit_count);
6442 if (b->hit_count == 1)
6443 ui_out_text (uiout, " time\n");
6444 else
6445 ui_out_text (uiout, " times\n");
6446 }
556ec64d
YQ
6447 else
6448 {
31f56a27
YQ
6449 /* Output the count also if it is zero, but only if this is mi. */
6450 if (ui_out_is_mi_like_p (uiout))
6451 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6452 }
6453 }
8b93c638 6454
0d381245 6455 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6456 {
6457 annotate_field (8);
8b93c638
JM
6458 ui_out_text (uiout, "\tignore next ");
6459 ui_out_field_int (uiout, "ignore", b->ignore_count);
6460 ui_out_text (uiout, " hits\n");
c4093a6a 6461 }
059fb39f 6462
816338b5
SS
6463 /* Note that an enable count of 1 corresponds to "enable once"
6464 behavior, which is reported by the combination of enablement and
6465 disposition, so we don't need to mention it here. */
6466 if (!part_of_multiple && b->enable_count > 1)
6467 {
6468 annotate_field (8);
6469 ui_out_text (uiout, "\tdisable after ");
6470 /* Tweak the wording to clarify that ignore and enable counts
6471 are distinct, and have additive effect. */
6472 if (b->ignore_count)
6473 ui_out_text (uiout, "additional ");
6474 else
6475 ui_out_text (uiout, "next ");
6476 ui_out_field_int (uiout, "enable", b->enable_count);
6477 ui_out_text (uiout, " hits\n");
6478 }
6479
f196051f
SS
6480 if (!part_of_multiple && is_tracepoint (b))
6481 {
6482 struct tracepoint *tp = (struct tracepoint *) b;
6483
6484 if (tp->traceframe_usage)
6485 {
6486 ui_out_text (uiout, "\ttrace buffer usage ");
6487 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6488 ui_out_text (uiout, " bytes\n");
6489 }
6490 }
d3ce09f5 6491
9add0f1b 6492 l = b->commands ? b->commands->commands : NULL;
059fb39f 6493 if (!part_of_multiple && l)
c4093a6a 6494 {
3b31d625
EZ
6495 struct cleanup *script_chain;
6496
c4093a6a 6497 annotate_field (9);
3b31d625 6498 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6499 print_command_lines (uiout, l, 4);
3b31d625 6500 do_cleanups (script_chain);
c4093a6a 6501 }
d24317b4 6502
d9b3f62e 6503 if (is_tracepoint (b))
1042e4c0 6504 {
d9b3f62e
PA
6505 struct tracepoint *t = (struct tracepoint *) b;
6506
6507 if (!part_of_multiple && t->pass_count)
6508 {
6509 annotate_field (10);
6510 ui_out_text (uiout, "\tpass count ");
6511 ui_out_field_int (uiout, "pass", t->pass_count);
6512 ui_out_text (uiout, " \n");
6513 }
f2a8bc8a
YQ
6514
6515 /* Don't display it when tracepoint or tracepoint location is
6516 pending. */
6517 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6518 {
6519 annotate_field (11);
6520
6521 if (ui_out_is_mi_like_p (uiout))
6522 ui_out_field_string (uiout, "installed",
6523 loc->inserted ? "y" : "n");
6524 else
6525 {
6526 if (loc->inserted)
6527 ui_out_text (uiout, "\t");
6528 else
6529 ui_out_text (uiout, "\tnot ");
6530 ui_out_text (uiout, "installed on target\n");
6531 }
6532 }
1042e4c0
SS
6533 }
6534
d24317b4
VP
6535 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6536 {
3a5c3e22
PA
6537 if (is_watchpoint (b))
6538 {
6539 struct watchpoint *w = (struct watchpoint *) b;
6540
6541 ui_out_field_string (uiout, "original-location", w->exp_string);
6542 }
6543 else if (b->addr_string)
d24317b4 6544 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6545 }
c4093a6a 6546}
c5aa993b 6547
0d381245
VP
6548static void
6549print_one_breakpoint (struct breakpoint *b,
4a64f543 6550 struct bp_location **last_loc,
6c95b8df 6551 int allflag)
0d381245 6552{
8d3788bd 6553 struct cleanup *bkpt_chain;
79a45e25 6554 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6555
6556 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6557
12c5a436 6558 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6559 do_cleanups (bkpt_chain);
0d381245
VP
6560
6561 /* If this breakpoint has custom print function,
6562 it's already printed. Otherwise, print individual
6563 locations, if any. */
6564 if (b->ops == NULL || b->ops->print_one == NULL)
6565 {
4a64f543
MS
6566 /* If breakpoint has a single location that is disabled, we
6567 print it as if it had several locations, since otherwise it's
6568 hard to represent "breakpoint enabled, location disabled"
6569 situation.
6570
6571 Note that while hardware watchpoints have several locations
a3be7890 6572 internally, that's not a property exposed to user. */
0d381245 6573 if (b->loc
a5606eee 6574 && !is_hardware_watchpoint (b)
8d3788bd 6575 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6576 {
6577 struct bp_location *loc;
6578 int n = 1;
8d3788bd 6579
0d381245 6580 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6581 {
6582 struct cleanup *inner2 =
6583 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6584 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6585 do_cleanups (inner2);
6586 }
0d381245
VP
6587 }
6588 }
6589}
6590
a6d9a66e
UW
6591static int
6592breakpoint_address_bits (struct breakpoint *b)
6593{
6594 int print_address_bits = 0;
6595 struct bp_location *loc;
6596
6597 for (loc = b->loc; loc; loc = loc->next)
6598 {
c7437ca6
PA
6599 int addr_bit;
6600
6601 /* Software watchpoints that aren't watching memory don't have
6602 an address to print. */
6603 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6604 continue;
6605
6606 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6607 if (addr_bit > print_address_bits)
6608 print_address_bits = addr_bit;
6609 }
6610
6611 return print_address_bits;
6612}
0d381245 6613
c4093a6a
JM
6614struct captured_breakpoint_query_args
6615 {
6616 int bnum;
6617 };
c5aa993b 6618
c4093a6a 6619static int
2b65245e 6620do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6621{
6622 struct captured_breakpoint_query_args *args = data;
52f0bd74 6623 struct breakpoint *b;
a6d9a66e 6624 struct bp_location *dummy_loc = NULL;
cc59ec59 6625
c4093a6a
JM
6626 ALL_BREAKPOINTS (b)
6627 {
6628 if (args->bnum == b->number)
c5aa993b 6629 {
12c5a436 6630 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6631 return GDB_RC_OK;
c5aa993b 6632 }
c4093a6a
JM
6633 }
6634 return GDB_RC_NONE;
6635}
c5aa993b 6636
c4093a6a 6637enum gdb_rc
4a64f543
MS
6638gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6639 char **error_message)
c4093a6a
JM
6640{
6641 struct captured_breakpoint_query_args args;
cc59ec59 6642
c4093a6a
JM
6643 args.bnum = bnum;
6644 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6645 an error. */
b0b13bb4
DJ
6646 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6647 error_message, RETURN_MASK_ALL) < 0)
6648 return GDB_RC_FAIL;
6649 else
6650 return GDB_RC_OK;
c4093a6a 6651}
c5aa993b 6652
09d682a4
TT
6653/* Return true if this breakpoint was set by the user, false if it is
6654 internal or momentary. */
6655
6656int
6657user_breakpoint_p (struct breakpoint *b)
6658{
46c6471b 6659 return b->number > 0;
09d682a4
TT
6660}
6661
7f3b0473 6662/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6663 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6664 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6665 FILTER is non-NULL, call it on each breakpoint and only include the
6666 ones for which it returns non-zero. Return the total number of
6667 breakpoints listed. */
c906108c 6668
d77f58be 6669static int
e5a67952 6670breakpoint_1 (char *args, int allflag,
4a64f543 6671 int (*filter) (const struct breakpoint *))
c4093a6a 6672{
52f0bd74 6673 struct breakpoint *b;
a6d9a66e 6674 struct bp_location *last_loc = NULL;
7f3b0473 6675 int nr_printable_breakpoints;
3b31d625 6676 struct cleanup *bkpttbl_chain;
79a45b7d 6677 struct value_print_options opts;
a6d9a66e 6678 int print_address_bits = 0;
269b11a2 6679 int print_type_col_width = 14;
79a45e25 6680 struct ui_out *uiout = current_uiout;
269b11a2 6681
79a45b7d
TT
6682 get_user_print_options (&opts);
6683
4a64f543
MS
6684 /* Compute the number of rows in the table, as well as the size
6685 required for address fields. */
7f3b0473
AC
6686 nr_printable_breakpoints = 0;
6687 ALL_BREAKPOINTS (b)
e5a67952
MS
6688 {
6689 /* If we have a filter, only list the breakpoints it accepts. */
6690 if (filter && !filter (b))
6691 continue;
6692
6693 /* If we have an "args" string, it is a list of breakpoints to
6694 accept. Skip the others. */
6695 if (args != NULL && *args != '\0')
6696 {
6697 if (allflag && parse_and_eval_long (args) != b->number)
6698 continue;
6699 if (!allflag && !number_is_in_list (args, b->number))
6700 continue;
6701 }
269b11a2 6702
e5a67952
MS
6703 if (allflag || user_breakpoint_p (b))
6704 {
6705 int addr_bit, type_len;
a6d9a66e 6706
e5a67952
MS
6707 addr_bit = breakpoint_address_bits (b);
6708 if (addr_bit > print_address_bits)
6709 print_address_bits = addr_bit;
269b11a2 6710
e5a67952
MS
6711 type_len = strlen (bptype_string (b->type));
6712 if (type_len > print_type_col_width)
6713 print_type_col_width = type_len;
6714
6715 nr_printable_breakpoints++;
6716 }
6717 }
7f3b0473 6718
79a45b7d 6719 if (opts.addressprint)
3b31d625 6720 bkpttbl_chain
3e43a32a
MS
6721 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6722 nr_printable_breakpoints,
3b31d625 6723 "BreakpointTable");
8b93c638 6724 else
3b31d625 6725 bkpttbl_chain
3e43a32a
MS
6726 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6727 nr_printable_breakpoints,
3b31d625 6728 "BreakpointTable");
8b93c638 6729
7f3b0473 6730 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6731 annotate_breakpoints_headers ();
6732 if (nr_printable_breakpoints > 0)
6733 annotate_field (0);
4a64f543 6734 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6735 if (nr_printable_breakpoints > 0)
6736 annotate_field (1);
269b11a2 6737 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6738 "type", "Type"); /* 2 */
d7faa9e7
AC
6739 if (nr_printable_breakpoints > 0)
6740 annotate_field (2);
4a64f543 6741 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6742 if (nr_printable_breakpoints > 0)
6743 annotate_field (3);
54e52265 6744 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6745 if (opts.addressprint)
e5a67952
MS
6746 {
6747 if (nr_printable_breakpoints > 0)
6748 annotate_field (4);
6749 if (print_address_bits <= 32)
6750 ui_out_table_header (uiout, 10, ui_left,
6751 "addr", "Address"); /* 5 */
6752 else
6753 ui_out_table_header (uiout, 18, ui_left,
6754 "addr", "Address"); /* 5 */
6755 }
d7faa9e7
AC
6756 if (nr_printable_breakpoints > 0)
6757 annotate_field (5);
6758 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6759 ui_out_table_body (uiout);
6760 if (nr_printable_breakpoints > 0)
6761 annotate_breakpoints_table ();
7f3b0473 6762
c4093a6a 6763 ALL_BREAKPOINTS (b)
e5a67952
MS
6764 {
6765 QUIT;
6766 /* If we have a filter, only list the breakpoints it accepts. */
6767 if (filter && !filter (b))
6768 continue;
6769
6770 /* If we have an "args" string, it is a list of breakpoints to
6771 accept. Skip the others. */
6772
6773 if (args != NULL && *args != '\0')
6774 {
6775 if (allflag) /* maintenance info breakpoint */
6776 {
6777 if (parse_and_eval_long (args) != b->number)
6778 continue;
6779 }
6780 else /* all others */
6781 {
6782 if (!number_is_in_list (args, b->number))
6783 continue;
6784 }
6785 }
6786 /* We only print out user settable breakpoints unless the
6787 allflag is set. */
6788 if (allflag || user_breakpoint_p (b))
12c5a436 6789 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6790 }
6791
3b31d625 6792 do_cleanups (bkpttbl_chain);
698384cd 6793
7f3b0473 6794 if (nr_printable_breakpoints == 0)
c906108c 6795 {
4a64f543
MS
6796 /* If there's a filter, let the caller decide how to report
6797 empty list. */
d77f58be
SS
6798 if (!filter)
6799 {
e5a67952 6800 if (args == NULL || *args == '\0')
d77f58be
SS
6801 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6802 else
4a64f543 6803 ui_out_message (uiout, 0,
e5a67952
MS
6804 "No breakpoint or watchpoint matching '%s'.\n",
6805 args);
d77f58be 6806 }
c906108c
SS
6807 }
6808 else
c4093a6a 6809 {
a6d9a66e
UW
6810 if (last_loc && !server_command)
6811 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6812 }
c906108c 6813
4a64f543 6814 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6815 there have been breakpoints? */
c906108c 6816 annotate_breakpoints_table_end ();
d77f58be
SS
6817
6818 return nr_printable_breakpoints;
c906108c
SS
6819}
6820
ad443146
SS
6821/* Display the value of default-collect in a way that is generally
6822 compatible with the breakpoint list. */
6823
6824static void
6825default_collect_info (void)
6826{
79a45e25
PA
6827 struct ui_out *uiout = current_uiout;
6828
ad443146
SS
6829 /* If it has no value (which is frequently the case), say nothing; a
6830 message like "No default-collect." gets in user's face when it's
6831 not wanted. */
6832 if (!*default_collect)
6833 return;
6834
6835 /* The following phrase lines up nicely with per-tracepoint collect
6836 actions. */
6837 ui_out_text (uiout, "default collect ");
6838 ui_out_field_string (uiout, "default-collect", default_collect);
6839 ui_out_text (uiout, " \n");
6840}
6841
c906108c 6842static void
e5a67952 6843breakpoints_info (char *args, int from_tty)
c906108c 6844{
e5a67952 6845 breakpoint_1 (args, 0, NULL);
ad443146
SS
6846
6847 default_collect_info ();
d77f58be
SS
6848}
6849
6850static void
e5a67952 6851watchpoints_info (char *args, int from_tty)
d77f58be 6852{
e5a67952 6853 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6854 struct ui_out *uiout = current_uiout;
d77f58be
SS
6855
6856 if (num_printed == 0)
6857 {
e5a67952 6858 if (args == NULL || *args == '\0')
d77f58be
SS
6859 ui_out_message (uiout, 0, "No watchpoints.\n");
6860 else
e5a67952 6861 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6862 }
c906108c
SS
6863}
6864
7a292a7a 6865static void
e5a67952 6866maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6867{
e5a67952 6868 breakpoint_1 (args, 1, NULL);
ad443146
SS
6869
6870 default_collect_info ();
c906108c
SS
6871}
6872
0d381245 6873static int
714835d5 6874breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6875 struct program_space *pspace,
714835d5 6876 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6877{
6878 struct bp_location *bl = b->loc;
cc59ec59 6879
0d381245
VP
6880 for (; bl; bl = bl->next)
6881 {
6c95b8df
PA
6882 if (bl->pspace == pspace
6883 && bl->address == pc
0d381245
VP
6884 && (!overlay_debugging || bl->section == section))
6885 return 1;
6886 }
6887 return 0;
6888}
6889
672f9b60 6890/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6891 concerns with logical breakpoints, so we match program spaces, not
6892 address spaces. */
c906108c
SS
6893
6894static void
6c95b8df
PA
6895describe_other_breakpoints (struct gdbarch *gdbarch,
6896 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6897 struct obj_section *section, int thread)
c906108c 6898{
52f0bd74
AC
6899 int others = 0;
6900 struct breakpoint *b;
c906108c
SS
6901
6902 ALL_BREAKPOINTS (b)
672f9b60
KP
6903 others += (user_breakpoint_p (b)
6904 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6905 if (others > 0)
6906 {
a3f17187
AC
6907 if (others == 1)
6908 printf_filtered (_("Note: breakpoint "));
6909 else /* if (others == ???) */
6910 printf_filtered (_("Note: breakpoints "));
c906108c 6911 ALL_BREAKPOINTS (b)
672f9b60 6912 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6913 {
6914 others--;
6915 printf_filtered ("%d", b->number);
6916 if (b->thread == -1 && thread != -1)
6917 printf_filtered (" (all threads)");
6918 else if (b->thread != -1)
6919 printf_filtered (" (thread %d)", b->thread);
6920 printf_filtered ("%s%s ",
059fb39f 6921 ((b->enable_state == bp_disabled
f8eba3c6 6922 || b->enable_state == bp_call_disabled)
0d381245
VP
6923 ? " (disabled)"
6924 : b->enable_state == bp_permanent
6925 ? " (permanent)"
6926 : ""),
6927 (others > 1) ? ","
6928 : ((others == 1) ? " and" : ""));
6929 }
a3f17187 6930 printf_filtered (_("also set at pc "));
5af949e3 6931 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6932 printf_filtered (".\n");
6933 }
6934}
6935\f
c906108c 6936
e4f237da
KB
6937/* Return true iff it is meaningful to use the address member of
6938 BPT. For some breakpoint types, the address member is irrelevant
6939 and it makes no sense to attempt to compare it to other addresses
6940 (or use it for any other purpose either).
6941
4a64f543
MS
6942 More specifically, each of the following breakpoint types will
6943 always have a zero valued address and we don't want to mark
6944 breakpoints of any of these types to be a duplicate of an actual
6945 breakpoint at address zero:
e4f237da
KB
6946
6947 bp_watchpoint
2d134ed3
PA
6948 bp_catchpoint
6949
6950*/
e4f237da
KB
6951
6952static int
6953breakpoint_address_is_meaningful (struct breakpoint *bpt)
6954{
6955 enum bptype type = bpt->type;
6956
2d134ed3
PA
6957 return (type != bp_watchpoint && type != bp_catchpoint);
6958}
6959
6960/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6961 true if LOC1 and LOC2 represent the same watchpoint location. */
6962
6963static int
4a64f543
MS
6964watchpoint_locations_match (struct bp_location *loc1,
6965 struct bp_location *loc2)
2d134ed3 6966{
3a5c3e22
PA
6967 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6968 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6969
6970 /* Both of them must exist. */
6971 gdb_assert (w1 != NULL);
6972 gdb_assert (w2 != NULL);
2bdf28a0 6973
4a64f543
MS
6974 /* If the target can evaluate the condition expression in hardware,
6975 then we we need to insert both watchpoints even if they are at
6976 the same place. Otherwise the watchpoint will only trigger when
6977 the condition of whichever watchpoint was inserted evaluates to
6978 true, not giving a chance for GDB to check the condition of the
6979 other watchpoint. */
3a5c3e22 6980 if ((w1->cond_exp
4a64f543
MS
6981 && target_can_accel_watchpoint_condition (loc1->address,
6982 loc1->length,
0cf6dd15 6983 loc1->watchpoint_type,
3a5c3e22
PA
6984 w1->cond_exp))
6985 || (w2->cond_exp
4a64f543
MS
6986 && target_can_accel_watchpoint_condition (loc2->address,
6987 loc2->length,
0cf6dd15 6988 loc2->watchpoint_type,
3a5c3e22 6989 w2->cond_exp)))
0cf6dd15
TJB
6990 return 0;
6991
85d721b8
PA
6992 /* Note that this checks the owner's type, not the location's. In
6993 case the target does not support read watchpoints, but does
6994 support access watchpoints, we'll have bp_read_watchpoint
6995 watchpoints with hw_access locations. Those should be considered
6996 duplicates of hw_read locations. The hw_read locations will
6997 become hw_access locations later. */
2d134ed3
PA
6998 return (loc1->owner->type == loc2->owner->type
6999 && loc1->pspace->aspace == loc2->pspace->aspace
7000 && loc1->address == loc2->address
7001 && loc1->length == loc2->length);
e4f237da
KB
7002}
7003
31e77af2 7004/* See breakpoint.h. */
6c95b8df 7005
31e77af2 7006int
6c95b8df
PA
7007breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7008 struct address_space *aspace2, CORE_ADDR addr2)
7009{
f5656ead 7010 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7011 || aspace1 == aspace2)
7012 && addr1 == addr2);
7013}
7014
f1310107
TJB
7015/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7016 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7017 matches ASPACE2. On targets that have global breakpoints, the address
7018 space doesn't really matter. */
7019
7020static int
7021breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7022 int len1, struct address_space *aspace2,
7023 CORE_ADDR addr2)
7024{
f5656ead 7025 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7026 || aspace1 == aspace2)
7027 && addr2 >= addr1 && addr2 < addr1 + len1);
7028}
7029
7030/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7031 a ranged breakpoint. In most targets, a match happens only if ASPACE
7032 matches the breakpoint's address space. On targets that have global
7033 breakpoints, the address space doesn't really matter. */
7034
7035static int
7036breakpoint_location_address_match (struct bp_location *bl,
7037 struct address_space *aspace,
7038 CORE_ADDR addr)
7039{
7040 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7041 aspace, addr)
7042 || (bl->length
7043 && breakpoint_address_match_range (bl->pspace->aspace,
7044 bl->address, bl->length,
7045 aspace, addr)));
7046}
7047
1e4d1764
YQ
7048/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7049 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7050 true, otherwise returns false. */
7051
7052static int
7053tracepoint_locations_match (struct bp_location *loc1,
7054 struct bp_location *loc2)
7055{
7056 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7057 /* Since tracepoint locations are never duplicated with others', tracepoint
7058 locations at the same address of different tracepoints are regarded as
7059 different locations. */
7060 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7061 else
7062 return 0;
7063}
7064
2d134ed3
PA
7065/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7066 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7067 represent the same location. */
7068
7069static int
4a64f543
MS
7070breakpoint_locations_match (struct bp_location *loc1,
7071 struct bp_location *loc2)
2d134ed3 7072{
2bdf28a0
JK
7073 int hw_point1, hw_point2;
7074
7075 /* Both of them must not be in moribund_locations. */
7076 gdb_assert (loc1->owner != NULL);
7077 gdb_assert (loc2->owner != NULL);
7078
7079 hw_point1 = is_hardware_watchpoint (loc1->owner);
7080 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7081
7082 if (hw_point1 != hw_point2)
7083 return 0;
7084 else if (hw_point1)
7085 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7086 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7087 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7088 else
f1310107
TJB
7089 /* We compare bp_location.length in order to cover ranged breakpoints. */
7090 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7091 loc2->pspace->aspace, loc2->address)
7092 && loc1->length == loc2->length);
2d134ed3
PA
7093}
7094
76897487
KB
7095static void
7096breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7097 int bnum, int have_bnum)
7098{
f63fbe86
MS
7099 /* The longest string possibly returned by hex_string_custom
7100 is 50 chars. These must be at least that big for safety. */
7101 char astr1[64];
7102 char astr2[64];
76897487 7103
bb599908
PH
7104 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7105 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7106 if (have_bnum)
8a3fe4f8 7107 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7108 bnum, astr1, astr2);
7109 else
8a3fe4f8 7110 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7111}
7112
4a64f543
MS
7113/* Adjust a breakpoint's address to account for architectural
7114 constraints on breakpoint placement. Return the adjusted address.
7115 Note: Very few targets require this kind of adjustment. For most
7116 targets, this function is simply the identity function. */
76897487
KB
7117
7118static CORE_ADDR
a6d9a66e
UW
7119adjust_breakpoint_address (struct gdbarch *gdbarch,
7120 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7121{
a6d9a66e 7122 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7123 {
7124 /* Very few targets need any kind of breakpoint adjustment. */
7125 return bpaddr;
7126 }
88f7da05
KB
7127 else if (bptype == bp_watchpoint
7128 || bptype == bp_hardware_watchpoint
7129 || bptype == bp_read_watchpoint
7130 || bptype == bp_access_watchpoint
fe798b75 7131 || bptype == bp_catchpoint)
88f7da05
KB
7132 {
7133 /* Watchpoints and the various bp_catch_* eventpoints should not
7134 have their addresses modified. */
7135 return bpaddr;
7136 }
76897487
KB
7137 else
7138 {
7139 CORE_ADDR adjusted_bpaddr;
7140
7141 /* Some targets have architectural constraints on the placement
7142 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7143 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7144
7145 /* An adjusted breakpoint address can significantly alter
7146 a user's expectations. Print a warning if an adjustment
7147 is required. */
7148 if (adjusted_bpaddr != bpaddr)
7149 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7150
7151 return adjusted_bpaddr;
7152 }
7153}
7154
28010a5d
PA
7155void
7156init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7157 struct breakpoint *owner)
7cc221ef 7158{
7cc221ef
DJ
7159 memset (loc, 0, sizeof (*loc));
7160
348d480f
PA
7161 gdb_assert (ops != NULL);
7162
28010a5d
PA
7163 loc->ops = ops;
7164 loc->owner = owner;
511a6cd4 7165 loc->cond = NULL;
b775012e 7166 loc->cond_bytecode = NULL;
0d381245
VP
7167 loc->shlib_disabled = 0;
7168 loc->enabled = 1;
e049a4b5 7169
28010a5d 7170 switch (owner->type)
e049a4b5
DJ
7171 {
7172 case bp_breakpoint:
7173 case bp_until:
7174 case bp_finish:
7175 case bp_longjmp:
7176 case bp_longjmp_resume:
e2e4d78b 7177 case bp_longjmp_call_dummy:
186c406b
TT
7178 case bp_exception:
7179 case bp_exception_resume:
e049a4b5 7180 case bp_step_resume:
2c03e5be 7181 case bp_hp_step_resume:
e049a4b5
DJ
7182 case bp_watchpoint_scope:
7183 case bp_call_dummy:
aa7d318d 7184 case bp_std_terminate:
e049a4b5
DJ
7185 case bp_shlib_event:
7186 case bp_thread_event:
7187 case bp_overlay_event:
4efc6507 7188 case bp_jit_event:
0fd8e87f 7189 case bp_longjmp_master:
aa7d318d 7190 case bp_std_terminate_master:
186c406b 7191 case bp_exception_master:
0e30163f
JK
7192 case bp_gnu_ifunc_resolver:
7193 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7194 case bp_dprintf:
e049a4b5 7195 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7196 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7197 break;
7198 case bp_hardware_breakpoint:
7199 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7200 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7201 break;
7202 case bp_hardware_watchpoint:
7203 case bp_read_watchpoint:
7204 case bp_access_watchpoint:
7205 loc->loc_type = bp_loc_hardware_watchpoint;
7206 break;
7207 case bp_watchpoint:
ce78b96d 7208 case bp_catchpoint:
15c3d785
PA
7209 case bp_tracepoint:
7210 case bp_fast_tracepoint:
0fb4aa4b 7211 case bp_static_tracepoint:
e049a4b5
DJ
7212 loc->loc_type = bp_loc_other;
7213 break;
7214 default:
e2e0b3e5 7215 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7216 }
7217
f431efe5 7218 loc->refc = 1;
28010a5d
PA
7219}
7220
7221/* Allocate a struct bp_location. */
7222
7223static struct bp_location *
7224allocate_bp_location (struct breakpoint *bpt)
7225{
348d480f
PA
7226 return bpt->ops->allocate_location (bpt);
7227}
7cc221ef 7228
f431efe5
PA
7229static void
7230free_bp_location (struct bp_location *loc)
fe3f5fa8 7231{
348d480f 7232 loc->ops->dtor (loc);
fe3f5fa8
VP
7233 xfree (loc);
7234}
7235
f431efe5
PA
7236/* Increment reference count. */
7237
7238static void
7239incref_bp_location (struct bp_location *bl)
7240{
7241 ++bl->refc;
7242}
7243
7244/* Decrement reference count. If the reference count reaches 0,
7245 destroy the bp_location. Sets *BLP to NULL. */
7246
7247static void
7248decref_bp_location (struct bp_location **blp)
7249{
0807b50c
PA
7250 gdb_assert ((*blp)->refc > 0);
7251
f431efe5
PA
7252 if (--(*blp)->refc == 0)
7253 free_bp_location (*blp);
7254 *blp = NULL;
7255}
7256
346774a9 7257/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7258
346774a9
PA
7259static void
7260add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7261{
346774a9 7262 struct breakpoint *b1;
c906108c 7263
346774a9
PA
7264 /* Add this breakpoint to the end of the chain so that a list of
7265 breakpoints will come out in order of increasing numbers. */
7266
7267 b1 = breakpoint_chain;
7268 if (b1 == 0)
7269 breakpoint_chain = b;
7270 else
7271 {
7272 while (b1->next)
7273 b1 = b1->next;
7274 b1->next = b;
7275 }
7276}
7277
7278/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7279
7280static void
7281init_raw_breakpoint_without_location (struct breakpoint *b,
7282 struct gdbarch *gdbarch,
28010a5d 7283 enum bptype bptype,
c0a91b2b 7284 const struct breakpoint_ops *ops)
346774a9 7285{
c906108c 7286 memset (b, 0, sizeof (*b));
2219d63c 7287
348d480f
PA
7288 gdb_assert (ops != NULL);
7289
28010a5d 7290 b->ops = ops;
4d28f7a8 7291 b->type = bptype;
a6d9a66e 7292 b->gdbarch = gdbarch;
c906108c
SS
7293 b->language = current_language->la_language;
7294 b->input_radix = input_radix;
7295 b->thread = -1;
b5de0fa7 7296 b->enable_state = bp_enabled;
c906108c
SS
7297 b->next = 0;
7298 b->silent = 0;
7299 b->ignore_count = 0;
7300 b->commands = NULL;
818dd999 7301 b->frame_id = null_frame_id;
0d381245 7302 b->condition_not_parsed = 0;
84f4c1fe 7303 b->py_bp_object = NULL;
d0fb5eae 7304 b->related_breakpoint = b;
346774a9
PA
7305}
7306
7307/* Helper to set_raw_breakpoint below. Creates a breakpoint
7308 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7309
7310static struct breakpoint *
7311set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7312 enum bptype bptype,
c0a91b2b 7313 const struct breakpoint_ops *ops)
346774a9
PA
7314{
7315 struct breakpoint *b = XNEW (struct breakpoint);
7316
348d480f 7317 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7318 add_to_breakpoint_chain (b);
0d381245
VP
7319 return b;
7320}
7321
0e30163f
JK
7322/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7323 resolutions should be made as the user specified the location explicitly
7324 enough. */
7325
0d381245 7326static void
0e30163f 7327set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7328{
2bdf28a0
JK
7329 gdb_assert (loc->owner != NULL);
7330
0d381245 7331 if (loc->owner->type == bp_breakpoint
1042e4c0 7332 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7333 || is_tracepoint (loc->owner))
0d381245 7334 {
0e30163f 7335 int is_gnu_ifunc;
2c02bd72 7336 const char *function_name;
6a3a010b 7337 CORE_ADDR func_addr;
0e30163f 7338
2c02bd72 7339 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7340 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7341
7342 if (is_gnu_ifunc && !explicit_loc)
7343 {
7344 struct breakpoint *b = loc->owner;
7345
7346 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7347 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7348 &loc->requested_address))
7349 {
7350 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7351 loc->address = adjust_breakpoint_address (loc->gdbarch,
7352 loc->requested_address,
7353 b->type);
7354 }
7355 else if (b->type == bp_breakpoint && b->loc == loc
7356 && loc->next == NULL && b->related_breakpoint == b)
7357 {
7358 /* Create only the whole new breakpoint of this type but do not
7359 mess more complicated breakpoints with multiple locations. */
7360 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7361 /* Remember the resolver's address for use by the return
7362 breakpoint. */
7363 loc->related_address = func_addr;
0e30163f
JK
7364 }
7365 }
7366
2c02bd72
DE
7367 if (function_name)
7368 loc->function_name = xstrdup (function_name);
0d381245
VP
7369 }
7370}
7371
a6d9a66e 7372/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7373struct gdbarch *
a6d9a66e
UW
7374get_sal_arch (struct symtab_and_line sal)
7375{
7376 if (sal.section)
7377 return get_objfile_arch (sal.section->objfile);
7378 if (sal.symtab)
7379 return get_objfile_arch (sal.symtab->objfile);
7380
7381 return NULL;
7382}
7383
346774a9
PA
7384/* Low level routine for partially initializing a breakpoint of type
7385 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7386 file name, and line number are provided by SAL.
0d381245
VP
7387
7388 It is expected that the caller will complete the initialization of
7389 the newly created breakpoint struct as well as output any status
c56053d2 7390 information regarding the creation of a new breakpoint. */
0d381245 7391
346774a9
PA
7392static void
7393init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7394 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7395 const struct breakpoint_ops *ops)
0d381245 7396{
28010a5d 7397 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7398
3742cc8b 7399 add_location_to_breakpoint (b, &sal);
0d381245 7400
6c95b8df
PA
7401 if (bptype != bp_catchpoint)
7402 gdb_assert (sal.pspace != NULL);
7403
f8eba3c6
TT
7404 /* Store the program space that was used to set the breakpoint,
7405 except for ordinary breakpoints, which are independent of the
7406 program space. */
7407 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7408 b->pspace = sal.pspace;
346774a9 7409}
c906108c 7410
346774a9
PA
7411/* set_raw_breakpoint is a low level routine for allocating and
7412 partially initializing a breakpoint of type BPTYPE. The newly
7413 created breakpoint's address, section, source file name, and line
7414 number are provided by SAL. The newly created and partially
7415 initialized breakpoint is added to the breakpoint chain and
7416 is also returned as the value of this function.
7417
7418 It is expected that the caller will complete the initialization of
7419 the newly created breakpoint struct as well as output any status
7420 information regarding the creation of a new breakpoint. In
7421 particular, set_raw_breakpoint does NOT set the breakpoint
7422 number! Care should be taken to not allow an error to occur
7423 prior to completing the initialization of the breakpoint. If this
7424 should happen, a bogus breakpoint will be left on the chain. */
7425
7426struct breakpoint *
7427set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7428 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7429 const struct breakpoint_ops *ops)
346774a9
PA
7430{
7431 struct breakpoint *b = XNEW (struct breakpoint);
7432
348d480f 7433 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7434 add_to_breakpoint_chain (b);
c906108c
SS
7435 return b;
7436}
7437
c2c6d25f
JM
7438
7439/* Note that the breakpoint object B describes a permanent breakpoint
7440 instruction, hard-wired into the inferior's code. */
7441void
7442make_breakpoint_permanent (struct breakpoint *b)
7443{
0d381245 7444 struct bp_location *bl;
cc59ec59 7445
b5de0fa7 7446 b->enable_state = bp_permanent;
c2c6d25f 7447
4a64f543
MS
7448 /* By definition, permanent breakpoints are already present in the
7449 code. Mark all locations as inserted. For now,
7450 make_breakpoint_permanent is called in just one place, so it's
7451 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7452 multiple locations or not, but it's easy to implement. */
0d381245
VP
7453 for (bl = b->loc; bl; bl = bl->next)
7454 bl->inserted = 1;
c2c6d25f
JM
7455}
7456
53a5351d 7457/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7458 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7459 initiated the operation. */
c906108c
SS
7460
7461void
186c406b 7462set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7463{
35df4500 7464 struct breakpoint *b, *b_tmp;
186c406b 7465 int thread = tp->num;
0fd8e87f
UW
7466
7467 /* To avoid having to rescan all objfile symbols at every step,
7468 we maintain a list of continually-inserted but always disabled
7469 longjmp "master" breakpoints. Here, we simply create momentary
7470 clones of those and enable them for the requested thread. */
35df4500 7471 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7472 if (b->pspace == current_program_space
186c406b
TT
7473 && (b->type == bp_longjmp_master
7474 || b->type == bp_exception_master))
0fd8e87f 7475 {
06edf0c0
PA
7476 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7477 struct breakpoint *clone;
cc59ec59 7478
e2e4d78b
JK
7479 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7480 after their removal. */
06edf0c0 7481 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7482 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7483 clone->thread = thread;
7484 }
186c406b
TT
7485
7486 tp->initiating_frame = frame;
c906108c
SS
7487}
7488
611c83ae 7489/* Delete all longjmp breakpoints from THREAD. */
c906108c 7490void
611c83ae 7491delete_longjmp_breakpoint (int thread)
c906108c 7492{
35df4500 7493 struct breakpoint *b, *b_tmp;
c906108c 7494
35df4500 7495 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7496 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7497 {
7498 if (b->thread == thread)
7499 delete_breakpoint (b);
7500 }
c906108c
SS
7501}
7502
f59f708a
PA
7503void
7504delete_longjmp_breakpoint_at_next_stop (int thread)
7505{
7506 struct breakpoint *b, *b_tmp;
7507
7508 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7509 if (b->type == bp_longjmp || b->type == bp_exception)
7510 {
7511 if (b->thread == thread)
7512 b->disposition = disp_del_at_next_stop;
7513 }
7514}
7515
e2e4d78b
JK
7516/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7517 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7518 pointer to any of them. Return NULL if this system cannot place longjmp
7519 breakpoints. */
7520
7521struct breakpoint *
7522set_longjmp_breakpoint_for_call_dummy (void)
7523{
7524 struct breakpoint *b, *retval = NULL;
7525
7526 ALL_BREAKPOINTS (b)
7527 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7528 {
7529 struct breakpoint *new_b;
7530
7531 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7532 &momentary_breakpoint_ops,
7533 1);
e2e4d78b
JK
7534 new_b->thread = pid_to_thread_id (inferior_ptid);
7535
7536 /* Link NEW_B into the chain of RETVAL breakpoints. */
7537
7538 gdb_assert (new_b->related_breakpoint == new_b);
7539 if (retval == NULL)
7540 retval = new_b;
7541 new_b->related_breakpoint = retval;
7542 while (retval->related_breakpoint != new_b->related_breakpoint)
7543 retval = retval->related_breakpoint;
7544 retval->related_breakpoint = new_b;
7545 }
7546
7547 return retval;
7548}
7549
7550/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7551 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7552 stack.
7553
7554 You should call this function only at places where it is safe to currently
7555 unwind the whole stack. Failed stack unwind would discard live dummy
7556 frames. */
7557
7558void
b67a2c6f 7559check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7560{
7561 struct breakpoint *b, *b_tmp;
7562
7563 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7564 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7565 {
7566 struct breakpoint *dummy_b = b->related_breakpoint;
7567
7568 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7569 dummy_b = dummy_b->related_breakpoint;
7570 if (dummy_b->type != bp_call_dummy
7571 || frame_find_by_id (dummy_b->frame_id) != NULL)
7572 continue;
7573
b67a2c6f 7574 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7575
7576 while (b->related_breakpoint != b)
7577 {
7578 if (b_tmp == b->related_breakpoint)
7579 b_tmp = b->related_breakpoint->next;
7580 delete_breakpoint (b->related_breakpoint);
7581 }
7582 delete_breakpoint (b);
7583 }
7584}
7585
1900040c
MS
7586void
7587enable_overlay_breakpoints (void)
7588{
52f0bd74 7589 struct breakpoint *b;
1900040c
MS
7590
7591 ALL_BREAKPOINTS (b)
7592 if (b->type == bp_overlay_event)
7593 {
7594 b->enable_state = bp_enabled;
44702360 7595 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7596 overlay_events_enabled = 1;
1900040c
MS
7597 }
7598}
7599
7600void
7601disable_overlay_breakpoints (void)
7602{
52f0bd74 7603 struct breakpoint *b;
1900040c
MS
7604
7605 ALL_BREAKPOINTS (b)
7606 if (b->type == bp_overlay_event)
7607 {
7608 b->enable_state = bp_disabled;
44702360 7609 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7610 overlay_events_enabled = 0;
1900040c
MS
7611 }
7612}
7613
aa7d318d
TT
7614/* Set an active std::terminate breakpoint for each std::terminate
7615 master breakpoint. */
7616void
7617set_std_terminate_breakpoint (void)
7618{
35df4500 7619 struct breakpoint *b, *b_tmp;
aa7d318d 7620
35df4500 7621 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7622 if (b->pspace == current_program_space
7623 && b->type == bp_std_terminate_master)
7624 {
06edf0c0 7625 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7626 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7627 }
7628}
7629
7630/* Delete all the std::terminate breakpoints. */
7631void
7632delete_std_terminate_breakpoint (void)
7633{
35df4500 7634 struct breakpoint *b, *b_tmp;
aa7d318d 7635
35df4500 7636 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7637 if (b->type == bp_std_terminate)
7638 delete_breakpoint (b);
7639}
7640
c4093a6a 7641struct breakpoint *
a6d9a66e 7642create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7643{
7644 struct breakpoint *b;
c4093a6a 7645
06edf0c0
PA
7646 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7647 &internal_breakpoint_ops);
7648
b5de0fa7 7649 b->enable_state = bp_enabled;
c4093a6a 7650 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7651 b->addr_string
7652 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7653
44702360 7654 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7655
c4093a6a
JM
7656 return b;
7657}
7658
7659void
7660remove_thread_event_breakpoints (void)
7661{
35df4500 7662 struct breakpoint *b, *b_tmp;
c4093a6a 7663
35df4500 7664 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7665 if (b->type == bp_thread_event
7666 && b->loc->pspace == current_program_space)
c4093a6a
JM
7667 delete_breakpoint (b);
7668}
7669
0101ce28
JJ
7670struct lang_and_radix
7671 {
7672 enum language lang;
7673 int radix;
7674 };
7675
4efc6507
DE
7676/* Create a breakpoint for JIT code registration and unregistration. */
7677
7678struct breakpoint *
7679create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7680{
7681 struct breakpoint *b;
7682
06edf0c0
PA
7683 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7684 &internal_breakpoint_ops);
44702360 7685 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7686 return b;
7687}
0101ce28 7688
03673fc7
PP
7689/* Remove JIT code registration and unregistration breakpoint(s). */
7690
7691void
7692remove_jit_event_breakpoints (void)
7693{
7694 struct breakpoint *b, *b_tmp;
7695
7696 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7697 if (b->type == bp_jit_event
7698 && b->loc->pspace == current_program_space)
7699 delete_breakpoint (b);
7700}
7701
cae688ec
JJ
7702void
7703remove_solib_event_breakpoints (void)
7704{
35df4500 7705 struct breakpoint *b, *b_tmp;
cae688ec 7706
35df4500 7707 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7708 if (b->type == bp_shlib_event
7709 && b->loc->pspace == current_program_space)
cae688ec
JJ
7710 delete_breakpoint (b);
7711}
7712
f37f681c
PA
7713/* See breakpoint.h. */
7714
7715void
7716remove_solib_event_breakpoints_at_next_stop (void)
7717{
7718 struct breakpoint *b, *b_tmp;
7719
7720 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7721 if (b->type == bp_shlib_event
7722 && b->loc->pspace == current_program_space)
7723 b->disposition = disp_del_at_next_stop;
7724}
7725
04086b45
PA
7726/* Helper for create_solib_event_breakpoint /
7727 create_and_insert_solib_event_breakpoint. Allows specifying which
7728 INSERT_MODE to pass through to update_global_location_list. */
7729
7730static struct breakpoint *
7731create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7732 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7733{
7734 struct breakpoint *b;
7735
06edf0c0
PA
7736 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7737 &internal_breakpoint_ops);
04086b45 7738 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7739 return b;
7740}
7741
04086b45
PA
7742struct breakpoint *
7743create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7744{
7745 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7746}
7747
f37f681c
PA
7748/* See breakpoint.h. */
7749
7750struct breakpoint *
7751create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7752{
7753 struct breakpoint *b;
7754
04086b45
PA
7755 /* Explicitly tell update_global_location_list to insert
7756 locations. */
7757 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7758 if (!b->loc->inserted)
7759 {
7760 delete_breakpoint (b);
7761 return NULL;
7762 }
7763 return b;
7764}
7765
cae688ec
JJ
7766/* Disable any breakpoints that are on code in shared libraries. Only
7767 apply to enabled breakpoints, disabled ones can just stay disabled. */
7768
7769void
cb851954 7770disable_breakpoints_in_shlibs (void)
cae688ec 7771{
876fa593 7772 struct bp_location *loc, **locp_tmp;
cae688ec 7773
876fa593 7774 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7775 {
2bdf28a0 7776 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7777 struct breakpoint *b = loc->owner;
2bdf28a0 7778
4a64f543
MS
7779 /* We apply the check to all breakpoints, including disabled for
7780 those with loc->duplicate set. This is so that when breakpoint
7781 becomes enabled, or the duplicate is removed, gdb will try to
7782 insert all breakpoints. If we don't set shlib_disabled here,
7783 we'll try to insert those breakpoints and fail. */
1042e4c0 7784 if (((b->type == bp_breakpoint)
508ccb1f 7785 || (b->type == bp_jit_event)
1042e4c0 7786 || (b->type == bp_hardware_breakpoint)
d77f58be 7787 || (is_tracepoint (b)))
6c95b8df 7788 && loc->pspace == current_program_space
0d381245 7789 && !loc->shlib_disabled
6c95b8df 7790 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7791 )
0d381245
VP
7792 {
7793 loc->shlib_disabled = 1;
7794 }
cae688ec
JJ
7795 }
7796}
7797
63644780
NB
7798/* Disable any breakpoints and tracepoints that are in SOLIB upon
7799 notification of unloaded_shlib. Only apply to enabled breakpoints,
7800 disabled ones can just stay disabled. */
84acb35a 7801
75149521 7802static void
84acb35a
JJ
7803disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7804{
876fa593 7805 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7806 int disabled_shlib_breaks = 0;
7807
c86cf029
VP
7808 /* SunOS a.out shared libraries are always mapped, so do not
7809 disable breakpoints; they will only be reported as unloaded
7810 through clear_solib when GDB discards its shared library
7811 list. See clear_solib for more information. */
7812 if (exec_bfd != NULL
7813 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7814 return;
7815
876fa593 7816 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7817 {
2bdf28a0 7818 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7819 struct breakpoint *b = loc->owner;
cc59ec59 7820
1e4d1764 7821 if (solib->pspace == loc->pspace
e2dd7057 7822 && !loc->shlib_disabled
1e4d1764
YQ
7823 && (((b->type == bp_breakpoint
7824 || b->type == bp_jit_event
7825 || b->type == bp_hardware_breakpoint)
7826 && (loc->loc_type == bp_loc_hardware_breakpoint
7827 || loc->loc_type == bp_loc_software_breakpoint))
7828 || is_tracepoint (b))
e2dd7057 7829 && solib_contains_address_p (solib, loc->address))
84acb35a 7830 {
e2dd7057
PP
7831 loc->shlib_disabled = 1;
7832 /* At this point, we cannot rely on remove_breakpoint
7833 succeeding so we must mark the breakpoint as not inserted
7834 to prevent future errors occurring in remove_breakpoints. */
7835 loc->inserted = 0;
8d3788bd
VP
7836
7837 /* This may cause duplicate notifications for the same breakpoint. */
7838 observer_notify_breakpoint_modified (b);
7839
e2dd7057
PP
7840 if (!disabled_shlib_breaks)
7841 {
7842 target_terminal_ours_for_output ();
3e43a32a
MS
7843 warning (_("Temporarily disabling breakpoints "
7844 "for unloaded shared library \"%s\""),
e2dd7057 7845 solib->so_name);
84acb35a 7846 }
e2dd7057 7847 disabled_shlib_breaks = 1;
84acb35a
JJ
7848 }
7849 }
84acb35a
JJ
7850}
7851
63644780
NB
7852/* Disable any breakpoints and tracepoints in OBJFILE upon
7853 notification of free_objfile. Only apply to enabled breakpoints,
7854 disabled ones can just stay disabled. */
7855
7856static void
7857disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7858{
7859 struct breakpoint *b;
7860
7861 if (objfile == NULL)
7862 return;
7863
d03de421
PA
7864 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7865 managed by the user with add-symbol-file/remove-symbol-file.
7866 Similarly to how breakpoints in shared libraries are handled in
7867 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7868 shlib_disabled so they end up uninserted on the next global
7869 location list update. Shared libraries not loaded by the user
7870 aren't handled here -- they're already handled in
7871 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7872 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7873 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7874 main objfile). */
7875 if ((objfile->flags & OBJF_SHARED) == 0
7876 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7877 return;
7878
7879 ALL_BREAKPOINTS (b)
7880 {
7881 struct bp_location *loc;
7882 int bp_modified = 0;
7883
7884 if (!is_breakpoint (b) && !is_tracepoint (b))
7885 continue;
7886
7887 for (loc = b->loc; loc != NULL; loc = loc->next)
7888 {
7889 CORE_ADDR loc_addr = loc->address;
7890
7891 if (loc->loc_type != bp_loc_hardware_breakpoint
7892 && loc->loc_type != bp_loc_software_breakpoint)
7893 continue;
7894
7895 if (loc->shlib_disabled != 0)
7896 continue;
7897
7898 if (objfile->pspace != loc->pspace)
7899 continue;
7900
7901 if (loc->loc_type != bp_loc_hardware_breakpoint
7902 && loc->loc_type != bp_loc_software_breakpoint)
7903 continue;
7904
7905 if (is_addr_in_objfile (loc_addr, objfile))
7906 {
7907 loc->shlib_disabled = 1;
08351840
PA
7908 /* At this point, we don't know whether the object was
7909 unmapped from the inferior or not, so leave the
7910 inserted flag alone. We'll handle failure to
7911 uninsert quietly, in case the object was indeed
7912 unmapped. */
63644780
NB
7913
7914 mark_breakpoint_location_modified (loc);
7915
7916 bp_modified = 1;
7917 }
7918 }
7919
7920 if (bp_modified)
7921 observer_notify_breakpoint_modified (b);
7922 }
7923}
7924
ce78b96d
JB
7925/* FORK & VFORK catchpoints. */
7926
e29a4733
PA
7927/* An instance of this type is used to represent a fork or vfork
7928 catchpoint. It includes a "struct breakpoint" as a kind of base
7929 class; users downcast to "struct breakpoint *" when needed. A
7930 breakpoint is really of this type iff its ops pointer points to
7931 CATCH_FORK_BREAKPOINT_OPS. */
7932
7933struct fork_catchpoint
7934{
7935 /* The base class. */
7936 struct breakpoint base;
7937
7938 /* Process id of a child process whose forking triggered this
7939 catchpoint. This field is only valid immediately after this
7940 catchpoint has triggered. */
7941 ptid_t forked_inferior_pid;
7942};
7943
4a64f543
MS
7944/* Implement the "insert" breakpoint_ops method for fork
7945 catchpoints. */
ce78b96d 7946
77b06cd7
TJB
7947static int
7948insert_catch_fork (struct bp_location *bl)
ce78b96d 7949{
dfd4cc63 7950 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7951}
7952
4a64f543
MS
7953/* Implement the "remove" breakpoint_ops method for fork
7954 catchpoints. */
ce78b96d
JB
7955
7956static int
77b06cd7 7957remove_catch_fork (struct bp_location *bl)
ce78b96d 7958{
dfd4cc63 7959 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7960}
7961
7962/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7963 catchpoints. */
7964
7965static int
f1310107 7966breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7967 struct address_space *aspace, CORE_ADDR bp_addr,
7968 const struct target_waitstatus *ws)
ce78b96d 7969{
e29a4733
PA
7970 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7971
f90263c1
TT
7972 if (ws->kind != TARGET_WAITKIND_FORKED)
7973 return 0;
7974
7975 c->forked_inferior_pid = ws->value.related_pid;
7976 return 1;
ce78b96d
JB
7977}
7978
4a64f543
MS
7979/* Implement the "print_it" breakpoint_ops method for fork
7980 catchpoints. */
ce78b96d
JB
7981
7982static enum print_stop_action
348d480f 7983print_it_catch_fork (bpstat bs)
ce78b96d 7984{
36dfb11c 7985 struct ui_out *uiout = current_uiout;
348d480f
PA
7986 struct breakpoint *b = bs->breakpoint_at;
7987 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7988
ce78b96d 7989 annotate_catchpoint (b->number);
36dfb11c
TT
7990 if (b->disposition == disp_del)
7991 ui_out_text (uiout, "\nTemporary catchpoint ");
7992 else
7993 ui_out_text (uiout, "\nCatchpoint ");
7994 if (ui_out_is_mi_like_p (uiout))
7995 {
7996 ui_out_field_string (uiout, "reason",
7997 async_reason_lookup (EXEC_ASYNC_FORK));
7998 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7999 }
8000 ui_out_field_int (uiout, "bkptno", b->number);
8001 ui_out_text (uiout, " (forked process ");
8002 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8003 ui_out_text (uiout, "), ");
ce78b96d
JB
8004 return PRINT_SRC_AND_LOC;
8005}
8006
4a64f543
MS
8007/* Implement the "print_one" breakpoint_ops method for fork
8008 catchpoints. */
ce78b96d
JB
8009
8010static void
a6d9a66e 8011print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8012{
e29a4733 8013 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8014 struct value_print_options opts;
79a45e25 8015 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8016
8017 get_user_print_options (&opts);
8018
4a64f543
MS
8019 /* Field 4, the address, is omitted (which makes the columns not
8020 line up too nicely with the headers, but the effect is relatively
8021 readable). */
79a45b7d 8022 if (opts.addressprint)
ce78b96d
JB
8023 ui_out_field_skip (uiout, "addr");
8024 annotate_field (5);
8025 ui_out_text (uiout, "fork");
e29a4733 8026 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8027 {
8028 ui_out_text (uiout, ", process ");
8029 ui_out_field_int (uiout, "what",
e29a4733 8030 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8031 ui_out_spaces (uiout, 1);
8032 }
8ac3646f
TT
8033
8034 if (ui_out_is_mi_like_p (uiout))
8035 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8036}
8037
8038/* Implement the "print_mention" breakpoint_ops method for fork
8039 catchpoints. */
8040
8041static void
8042print_mention_catch_fork (struct breakpoint *b)
8043{
8044 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8045}
8046
6149aea9
PA
8047/* Implement the "print_recreate" breakpoint_ops method for fork
8048 catchpoints. */
8049
8050static void
8051print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8052{
8053 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8054 print_recreate_thread (b, fp);
6149aea9
PA
8055}
8056
ce78b96d
JB
8057/* The breakpoint_ops structure to be used in fork catchpoints. */
8058
2060206e 8059static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8060
4a64f543
MS
8061/* Implement the "insert" breakpoint_ops method for vfork
8062 catchpoints. */
ce78b96d 8063
77b06cd7
TJB
8064static int
8065insert_catch_vfork (struct bp_location *bl)
ce78b96d 8066{
dfd4cc63 8067 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8068}
8069
4a64f543
MS
8070/* Implement the "remove" breakpoint_ops method for vfork
8071 catchpoints. */
ce78b96d
JB
8072
8073static int
77b06cd7 8074remove_catch_vfork (struct bp_location *bl)
ce78b96d 8075{
dfd4cc63 8076 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8077}
8078
8079/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8080 catchpoints. */
8081
8082static int
f1310107 8083breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8084 struct address_space *aspace, CORE_ADDR bp_addr,
8085 const struct target_waitstatus *ws)
ce78b96d 8086{
e29a4733
PA
8087 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8088
f90263c1
TT
8089 if (ws->kind != TARGET_WAITKIND_VFORKED)
8090 return 0;
8091
8092 c->forked_inferior_pid = ws->value.related_pid;
8093 return 1;
ce78b96d
JB
8094}
8095
4a64f543
MS
8096/* Implement the "print_it" breakpoint_ops method for vfork
8097 catchpoints. */
ce78b96d
JB
8098
8099static enum print_stop_action
348d480f 8100print_it_catch_vfork (bpstat bs)
ce78b96d 8101{
36dfb11c 8102 struct ui_out *uiout = current_uiout;
348d480f 8103 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8104 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8105
ce78b96d 8106 annotate_catchpoint (b->number);
36dfb11c
TT
8107 if (b->disposition == disp_del)
8108 ui_out_text (uiout, "\nTemporary catchpoint ");
8109 else
8110 ui_out_text (uiout, "\nCatchpoint ");
8111 if (ui_out_is_mi_like_p (uiout))
8112 {
8113 ui_out_field_string (uiout, "reason",
8114 async_reason_lookup (EXEC_ASYNC_VFORK));
8115 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8116 }
8117 ui_out_field_int (uiout, "bkptno", b->number);
8118 ui_out_text (uiout, " (vforked process ");
8119 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8120 ui_out_text (uiout, "), ");
ce78b96d
JB
8121 return PRINT_SRC_AND_LOC;
8122}
8123
4a64f543
MS
8124/* Implement the "print_one" breakpoint_ops method for vfork
8125 catchpoints. */
ce78b96d
JB
8126
8127static void
a6d9a66e 8128print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8129{
e29a4733 8130 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8131 struct value_print_options opts;
79a45e25 8132 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8133
8134 get_user_print_options (&opts);
4a64f543
MS
8135 /* Field 4, the address, is omitted (which makes the columns not
8136 line up too nicely with the headers, but the effect is relatively
8137 readable). */
79a45b7d 8138 if (opts.addressprint)
ce78b96d
JB
8139 ui_out_field_skip (uiout, "addr");
8140 annotate_field (5);
8141 ui_out_text (uiout, "vfork");
e29a4733 8142 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8143 {
8144 ui_out_text (uiout, ", process ");
8145 ui_out_field_int (uiout, "what",
e29a4733 8146 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8147 ui_out_spaces (uiout, 1);
8148 }
8ac3646f
TT
8149
8150 if (ui_out_is_mi_like_p (uiout))
8151 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8152}
8153
8154/* Implement the "print_mention" breakpoint_ops method for vfork
8155 catchpoints. */
8156
8157static void
8158print_mention_catch_vfork (struct breakpoint *b)
8159{
8160 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8161}
8162
6149aea9
PA
8163/* Implement the "print_recreate" breakpoint_ops method for vfork
8164 catchpoints. */
8165
8166static void
8167print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8168{
8169 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8170 print_recreate_thread (b, fp);
6149aea9
PA
8171}
8172
ce78b96d
JB
8173/* The breakpoint_ops structure to be used in vfork catchpoints. */
8174
2060206e 8175static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8176
edcc5120
TT
8177/* An instance of this type is used to represent an solib catchpoint.
8178 It includes a "struct breakpoint" as a kind of base class; users
8179 downcast to "struct breakpoint *" when needed. A breakpoint is
8180 really of this type iff its ops pointer points to
8181 CATCH_SOLIB_BREAKPOINT_OPS. */
8182
8183struct solib_catchpoint
8184{
8185 /* The base class. */
8186 struct breakpoint base;
8187
8188 /* True for "catch load", false for "catch unload". */
8189 unsigned char is_load;
8190
8191 /* Regular expression to match, if any. COMPILED is only valid when
8192 REGEX is non-NULL. */
8193 char *regex;
8194 regex_t compiled;
8195};
8196
8197static void
8198dtor_catch_solib (struct breakpoint *b)
8199{
8200 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8201
8202 if (self->regex)
8203 regfree (&self->compiled);
8204 xfree (self->regex);
8205
8206 base_breakpoint_ops.dtor (b);
8207}
8208
8209static int
8210insert_catch_solib (struct bp_location *ignore)
8211{
8212 return 0;
8213}
8214
8215static int
8216remove_catch_solib (struct bp_location *ignore)
8217{
8218 return 0;
8219}
8220
8221static int
8222breakpoint_hit_catch_solib (const struct bp_location *bl,
8223 struct address_space *aspace,
8224 CORE_ADDR bp_addr,
8225 const struct target_waitstatus *ws)
8226{
8227 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8228 struct breakpoint *other;
8229
8230 if (ws->kind == TARGET_WAITKIND_LOADED)
8231 return 1;
8232
8233 ALL_BREAKPOINTS (other)
8234 {
8235 struct bp_location *other_bl;
8236
8237 if (other == bl->owner)
8238 continue;
8239
8240 if (other->type != bp_shlib_event)
8241 continue;
8242
8243 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8244 continue;
8245
8246 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8247 {
8248 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8249 return 1;
8250 }
8251 }
8252
8253 return 0;
8254}
8255
8256static void
8257check_status_catch_solib (struct bpstats *bs)
8258{
8259 struct solib_catchpoint *self
8260 = (struct solib_catchpoint *) bs->breakpoint_at;
8261 int ix;
8262
8263 if (self->is_load)
8264 {
8265 struct so_list *iter;
8266
8267 for (ix = 0;
8268 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8269 ix, iter);
8270 ++ix)
8271 {
8272 if (!self->regex
8273 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8274 return;
8275 }
8276 }
8277 else
8278 {
8279 char *iter;
8280
8281 for (ix = 0;
8282 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8283 ix, iter);
8284 ++ix)
8285 {
8286 if (!self->regex
8287 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8288 return;
8289 }
8290 }
8291
8292 bs->stop = 0;
8293 bs->print_it = print_it_noop;
8294}
8295
8296static enum print_stop_action
8297print_it_catch_solib (bpstat bs)
8298{
8299 struct breakpoint *b = bs->breakpoint_at;
8300 struct ui_out *uiout = current_uiout;
8301
8302 annotate_catchpoint (b->number);
8303 if (b->disposition == disp_del)
8304 ui_out_text (uiout, "\nTemporary catchpoint ");
8305 else
8306 ui_out_text (uiout, "\nCatchpoint ");
8307 ui_out_field_int (uiout, "bkptno", b->number);
8308 ui_out_text (uiout, "\n");
8309 if (ui_out_is_mi_like_p (uiout))
8310 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8311 print_solib_event (1);
8312 return PRINT_SRC_AND_LOC;
8313}
8314
8315static void
8316print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8317{
8318 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8319 struct value_print_options opts;
8320 struct ui_out *uiout = current_uiout;
8321 char *msg;
8322
8323 get_user_print_options (&opts);
8324 /* Field 4, the address, is omitted (which makes the columns not
8325 line up too nicely with the headers, but the effect is relatively
8326 readable). */
8327 if (opts.addressprint)
8328 {
8329 annotate_field (4);
8330 ui_out_field_skip (uiout, "addr");
8331 }
8332
8333 annotate_field (5);
8334 if (self->is_load)
8335 {
8336 if (self->regex)
8337 msg = xstrprintf (_("load of library matching %s"), self->regex);
8338 else
8339 msg = xstrdup (_("load of library"));
8340 }
8341 else
8342 {
8343 if (self->regex)
8344 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8345 else
8346 msg = xstrdup (_("unload of library"));
8347 }
8348 ui_out_field_string (uiout, "what", msg);
8349 xfree (msg);
8ac3646f
TT
8350
8351 if (ui_out_is_mi_like_p (uiout))
8352 ui_out_field_string (uiout, "catch-type",
8353 self->is_load ? "load" : "unload");
edcc5120
TT
8354}
8355
8356static void
8357print_mention_catch_solib (struct breakpoint *b)
8358{
8359 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8360
8361 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8362 self->is_load ? "load" : "unload");
8363}
8364
8365static void
8366print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8367{
8368 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8369
8370 fprintf_unfiltered (fp, "%s %s",
8371 b->disposition == disp_del ? "tcatch" : "catch",
8372 self->is_load ? "load" : "unload");
8373 if (self->regex)
8374 fprintf_unfiltered (fp, " %s", self->regex);
8375 fprintf_unfiltered (fp, "\n");
8376}
8377
8378static struct breakpoint_ops catch_solib_breakpoint_ops;
8379
91985142
MG
8380/* Shared helper function (MI and CLI) for creating and installing
8381 a shared object event catchpoint. If IS_LOAD is non-zero then
8382 the events to be caught are load events, otherwise they are
8383 unload events. If IS_TEMP is non-zero the catchpoint is a
8384 temporary one. If ENABLED is non-zero the catchpoint is
8385 created in an enabled state. */
edcc5120 8386
91985142
MG
8387void
8388add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8389{
8390 struct solib_catchpoint *c;
8391 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8392 struct cleanup *cleanup;
8393
edcc5120
TT
8394 if (!arg)
8395 arg = "";
8396 arg = skip_spaces (arg);
8397
8398 c = XCNEW (struct solib_catchpoint);
8399 cleanup = make_cleanup (xfree, c);
8400
8401 if (*arg != '\0')
8402 {
8403 int errcode;
8404
8405 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8406 if (errcode != 0)
8407 {
8408 char *err = get_regcomp_error (errcode, &c->compiled);
8409
8410 make_cleanup (xfree, err);
8411 error (_("Invalid regexp (%s): %s"), err, arg);
8412 }
8413 c->regex = xstrdup (arg);
8414 }
8415
8416 c->is_load = is_load;
91985142 8417 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8418 &catch_solib_breakpoint_ops);
8419
91985142
MG
8420 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8421
edcc5120
TT
8422 discard_cleanups (cleanup);
8423 install_breakpoint (0, &c->base, 1);
8424}
8425
91985142
MG
8426/* A helper function that does all the work for "catch load" and
8427 "catch unload". */
8428
8429static void
8430catch_load_or_unload (char *arg, int from_tty, int is_load,
8431 struct cmd_list_element *command)
8432{
8433 int tempflag;
8434 const int enabled = 1;
8435
8436 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8437
8438 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8439}
8440
edcc5120
TT
8441static void
8442catch_load_command_1 (char *arg, int from_tty,
8443 struct cmd_list_element *command)
8444{
8445 catch_load_or_unload (arg, from_tty, 1, command);
8446}
8447
8448static void
8449catch_unload_command_1 (char *arg, int from_tty,
8450 struct cmd_list_element *command)
8451{
8452 catch_load_or_unload (arg, from_tty, 0, command);
8453}
8454
be5c67c1
PA
8455/* An instance of this type is used to represent a syscall catchpoint.
8456 It includes a "struct breakpoint" as a kind of base class; users
8457 downcast to "struct breakpoint *" when needed. A breakpoint is
8458 really of this type iff its ops pointer points to
8459 CATCH_SYSCALL_BREAKPOINT_OPS. */
8460
8461struct syscall_catchpoint
8462{
8463 /* The base class. */
8464 struct breakpoint base;
8465
8466 /* Syscall numbers used for the 'catch syscall' feature. If no
8467 syscall has been specified for filtering, its value is NULL.
8468 Otherwise, it holds a list of all syscalls to be caught. The
8469 list elements are allocated with xmalloc. */
8470 VEC(int) *syscalls_to_be_caught;
8471};
8472
8473/* Implement the "dtor" breakpoint_ops method for syscall
8474 catchpoints. */
8475
8476static void
8477dtor_catch_syscall (struct breakpoint *b)
8478{
8479 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8480
8481 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8482
2060206e 8483 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8484}
8485
fa3064dd
YQ
8486static const struct inferior_data *catch_syscall_inferior_data = NULL;
8487
8488struct catch_syscall_inferior_data
8489{
8490 /* We keep a count of the number of times the user has requested a
8491 particular syscall to be tracked, and pass this information to the
8492 target. This lets capable targets implement filtering directly. */
8493
8494 /* Number of times that "any" syscall is requested. */
8495 int any_syscall_count;
8496
8497 /* Count of each system call. */
8498 VEC(int) *syscalls_counts;
8499
8500 /* This counts all syscall catch requests, so we can readily determine
8501 if any catching is necessary. */
8502 int total_syscalls_count;
8503};
8504
8505static struct catch_syscall_inferior_data*
8506get_catch_syscall_inferior_data (struct inferior *inf)
8507{
8508 struct catch_syscall_inferior_data *inf_data;
8509
8510 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8511 if (inf_data == NULL)
8512 {
41bf6aca 8513 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8514 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8515 }
8516
8517 return inf_data;
8518}
8519
8520static void
8521catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8522{
8523 xfree (arg);
8524}
8525
8526
a96d9b2e
SDJ
8527/* Implement the "insert" breakpoint_ops method for syscall
8528 catchpoints. */
8529
77b06cd7
TJB
8530static int
8531insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8532{
be5c67c1 8533 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8534 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8535 struct catch_syscall_inferior_data *inf_data
8536 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8537
fa3064dd 8538 ++inf_data->total_syscalls_count;
be5c67c1 8539 if (!c->syscalls_to_be_caught)
fa3064dd 8540 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8541 else
8542 {
8543 int i, iter;
cc59ec59 8544
a96d9b2e 8545 for (i = 0;
be5c67c1 8546 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8547 i++)
8548 {
8549 int elem;
cc59ec59 8550
fa3064dd 8551 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8552 {
fa3064dd 8553 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8554 uintptr_t vec_addr_offset
8555 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8556 uintptr_t vec_addr;
fa3064dd
YQ
8557 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8558 vec_addr = ((uintptr_t) VEC_address (int,
8559 inf_data->syscalls_counts)
8560 + vec_addr_offset);
a96d9b2e
SDJ
8561 memset ((void *) vec_addr, 0,
8562 (iter + 1 - old_size) * sizeof (int));
8563 }
fa3064dd
YQ
8564 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8565 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8566 }
8567 }
8568
dfd4cc63 8569 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8570 inf_data->total_syscalls_count != 0,
8571 inf_data->any_syscall_count,
8572 VEC_length (int,
8573 inf_data->syscalls_counts),
8574 VEC_address (int,
8575 inf_data->syscalls_counts));
a96d9b2e
SDJ
8576}
8577
8578/* Implement the "remove" breakpoint_ops method for syscall
8579 catchpoints. */
8580
8581static int
77b06cd7 8582remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8583{
be5c67c1 8584 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8585 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8586 struct catch_syscall_inferior_data *inf_data
8587 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8588
fa3064dd 8589 --inf_data->total_syscalls_count;
be5c67c1 8590 if (!c->syscalls_to_be_caught)
fa3064dd 8591 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8592 else
8593 {
8594 int i, iter;
cc59ec59 8595
a96d9b2e 8596 for (i = 0;
be5c67c1 8597 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8598 i++)
8599 {
8600 int elem;
fa3064dd 8601 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8602 /* Shouldn't happen. */
8603 continue;
fa3064dd
YQ
8604 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8605 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8606 }
8607 }
8608
dfd4cc63 8609 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8610 inf_data->total_syscalls_count != 0,
8611 inf_data->any_syscall_count,
8612 VEC_length (int,
8613 inf_data->syscalls_counts),
3e43a32a 8614 VEC_address (int,
fa3064dd 8615 inf_data->syscalls_counts));
a96d9b2e
SDJ
8616}
8617
8618/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8619 catchpoints. */
8620
8621static int
f1310107 8622breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8623 struct address_space *aspace, CORE_ADDR bp_addr,
8624 const struct target_waitstatus *ws)
a96d9b2e 8625{
4a64f543
MS
8626 /* We must check if we are catching specific syscalls in this
8627 breakpoint. If we are, then we must guarantee that the called
8628 syscall is the same syscall we are catching. */
a96d9b2e 8629 int syscall_number = 0;
be5c67c1
PA
8630 const struct syscall_catchpoint *c
8631 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8632
f90263c1
TT
8633 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8634 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8635 return 0;
8636
f90263c1
TT
8637 syscall_number = ws->value.syscall_number;
8638
a96d9b2e 8639 /* Now, checking if the syscall is the same. */
be5c67c1 8640 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8641 {
8642 int i, iter;
cc59ec59 8643
a96d9b2e 8644 for (i = 0;
be5c67c1 8645 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8646 i++)
8647 if (syscall_number == iter)
4924df79
GKB
8648 return 1;
8649
8650 return 0;
a96d9b2e
SDJ
8651 }
8652
8653 return 1;
8654}
8655
8656/* Implement the "print_it" breakpoint_ops method for syscall
8657 catchpoints. */
8658
8659static enum print_stop_action
348d480f 8660print_it_catch_syscall (bpstat bs)
a96d9b2e 8661{
36dfb11c 8662 struct ui_out *uiout = current_uiout;
348d480f 8663 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8664 /* These are needed because we want to know in which state a
8665 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8666 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8667 must print "called syscall" or "returned from syscall". */
8668 ptid_t ptid;
8669 struct target_waitstatus last;
8670 struct syscall s;
a96d9b2e
SDJ
8671
8672 get_last_target_status (&ptid, &last);
8673
8674 get_syscall_by_number (last.value.syscall_number, &s);
8675
8676 annotate_catchpoint (b->number);
8677
36dfb11c
TT
8678 if (b->disposition == disp_del)
8679 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8680 else
36dfb11c
TT
8681 ui_out_text (uiout, "\nCatchpoint ");
8682 if (ui_out_is_mi_like_p (uiout))
8683 {
8684 ui_out_field_string (uiout, "reason",
8685 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8686 ? EXEC_ASYNC_SYSCALL_ENTRY
8687 : EXEC_ASYNC_SYSCALL_RETURN));
8688 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8689 }
8690 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8691
8692 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8693 ui_out_text (uiout, " (call to syscall ");
8694 else
8695 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8696
36dfb11c
TT
8697 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8698 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8699 if (s.name != NULL)
8700 ui_out_field_string (uiout, "syscall-name", s.name);
8701
8702 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8703
8704 return PRINT_SRC_AND_LOC;
8705}
8706
8707/* Implement the "print_one" breakpoint_ops method for syscall
8708 catchpoints. */
8709
8710static void
8711print_one_catch_syscall (struct breakpoint *b,
f1310107 8712 struct bp_location **last_loc)
a96d9b2e 8713{
be5c67c1 8714 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8715 struct value_print_options opts;
79a45e25 8716 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8717
8718 get_user_print_options (&opts);
4a64f543
MS
8719 /* Field 4, the address, is omitted (which makes the columns not
8720 line up too nicely with the headers, but the effect is relatively
8721 readable). */
a96d9b2e
SDJ
8722 if (opts.addressprint)
8723 ui_out_field_skip (uiout, "addr");
8724 annotate_field (5);
8725
be5c67c1
PA
8726 if (c->syscalls_to_be_caught
8727 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8728 ui_out_text (uiout, "syscalls \"");
8729 else
8730 ui_out_text (uiout, "syscall \"");
8731
be5c67c1 8732 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8733 {
8734 int i, iter;
8735 char *text = xstrprintf ("%s", "");
cc59ec59 8736
a96d9b2e 8737 for (i = 0;
be5c67c1 8738 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8739 i++)
8740 {
8741 char *x = text;
8742 struct syscall s;
8743 get_syscall_by_number (iter, &s);
8744
8745 if (s.name != NULL)
8746 text = xstrprintf ("%s%s, ", text, s.name);
8747 else
8748 text = xstrprintf ("%s%d, ", text, iter);
8749
8750 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8751 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8752 on every call. */
8753 xfree (x);
8754 }
8755 /* Remove the last comma. */
8756 text[strlen (text) - 2] = '\0';
8757 ui_out_field_string (uiout, "what", text);
8758 }
8759 else
8760 ui_out_field_string (uiout, "what", "<any syscall>");
8761 ui_out_text (uiout, "\" ");
8ac3646f
TT
8762
8763 if (ui_out_is_mi_like_p (uiout))
8764 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8765}
8766
8767/* Implement the "print_mention" breakpoint_ops method for syscall
8768 catchpoints. */
8769
8770static void
8771print_mention_catch_syscall (struct breakpoint *b)
8772{
be5c67c1
PA
8773 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8774
8775 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8776 {
8777 int i, iter;
8778
be5c67c1 8779 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8780 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8781 else
8782 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8783
8784 for (i = 0;
be5c67c1 8785 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8786 i++)
8787 {
8788 struct syscall s;
8789 get_syscall_by_number (iter, &s);
8790
8791 if (s.name)
8792 printf_filtered (" '%s' [%d]", s.name, s.number);
8793 else
8794 printf_filtered (" %d", s.number);
8795 }
8796 printf_filtered (")");
8797 }
8798 else
8799 printf_filtered (_("Catchpoint %d (any syscall)"),
8800 b->number);
8801}
8802
6149aea9
PA
8803/* Implement the "print_recreate" breakpoint_ops method for syscall
8804 catchpoints. */
8805
8806static void
8807print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8808{
be5c67c1
PA
8809 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8810
6149aea9
PA
8811 fprintf_unfiltered (fp, "catch syscall");
8812
be5c67c1 8813 if (c->syscalls_to_be_caught)
6149aea9
PA
8814 {
8815 int i, iter;
8816
8817 for (i = 0;
be5c67c1 8818 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8819 i++)
8820 {
8821 struct syscall s;
8822
8823 get_syscall_by_number (iter, &s);
8824 if (s.name)
8825 fprintf_unfiltered (fp, " %s", s.name);
8826 else
8827 fprintf_unfiltered (fp, " %d", s.number);
8828 }
8829 }
d9b3f62e 8830 print_recreate_thread (b, fp);
6149aea9
PA
8831}
8832
a96d9b2e
SDJ
8833/* The breakpoint_ops structure to be used in syscall catchpoints. */
8834
2060206e 8835static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8836
8837/* Returns non-zero if 'b' is a syscall catchpoint. */
8838
8839static int
8840syscall_catchpoint_p (struct breakpoint *b)
8841{
8842 return (b->ops == &catch_syscall_breakpoint_ops);
8843}
8844
346774a9
PA
8845/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8846 is non-zero, then make the breakpoint temporary. If COND_STRING is
8847 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8848 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8849
ab04a2af 8850void
346774a9
PA
8851init_catchpoint (struct breakpoint *b,
8852 struct gdbarch *gdbarch, int tempflag,
8853 char *cond_string,
c0a91b2b 8854 const struct breakpoint_ops *ops)
c906108c 8855{
c5aa993b 8856 struct symtab_and_line sal;
346774a9 8857
fe39c653 8858 init_sal (&sal);
6c95b8df 8859 sal.pspace = current_program_space;
c5aa993b 8860
28010a5d 8861 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8862
1b36a34b 8863 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8864 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8865}
8866
28010a5d 8867void
3ea46bff 8868install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8869{
8870 add_to_breakpoint_chain (b);
3a5c3e22 8871 set_breakpoint_number (internal, b);
558a9d82
YQ
8872 if (is_tracepoint (b))
8873 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8874 if (!internal)
8875 mention (b);
c56053d2 8876 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8877
8878 if (update_gll)
44702360 8879 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8880}
8881
9b70b993 8882static void
a6d9a66e
UW
8883create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8884 int tempflag, char *cond_string,
c0a91b2b 8885 const struct breakpoint_ops *ops)
c906108c 8886{
e29a4733 8887 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8888
e29a4733
PA
8889 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8890
8891 c->forked_inferior_pid = null_ptid;
8892
3ea46bff 8893 install_breakpoint (0, &c->base, 1);
c906108c
SS
8894}
8895
fe798b75
JB
8896/* Exec catchpoints. */
8897
b4d90040
PA
8898/* An instance of this type is used to represent an exec catchpoint.
8899 It includes a "struct breakpoint" as a kind of base class; users
8900 downcast to "struct breakpoint *" when needed. A breakpoint is
8901 really of this type iff its ops pointer points to
8902 CATCH_EXEC_BREAKPOINT_OPS. */
8903
8904struct exec_catchpoint
8905{
8906 /* The base class. */
8907 struct breakpoint base;
8908
8909 /* Filename of a program whose exec triggered this catchpoint.
8910 This field is only valid immediately after this catchpoint has
8911 triggered. */
8912 char *exec_pathname;
8913};
8914
8915/* Implement the "dtor" breakpoint_ops method for exec
8916 catchpoints. */
8917
8918static void
8919dtor_catch_exec (struct breakpoint *b)
8920{
8921 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8922
8923 xfree (c->exec_pathname);
348d480f 8924
2060206e 8925 base_breakpoint_ops.dtor (b);
b4d90040
PA
8926}
8927
77b06cd7
TJB
8928static int
8929insert_catch_exec (struct bp_location *bl)
c906108c 8930{
dfd4cc63 8931 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8932}
c906108c 8933
fe798b75 8934static int
77b06cd7 8935remove_catch_exec (struct bp_location *bl)
fe798b75 8936{
dfd4cc63 8937 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8938}
c906108c 8939
fe798b75 8940static int
f1310107 8941breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8942 struct address_space *aspace, CORE_ADDR bp_addr,
8943 const struct target_waitstatus *ws)
fe798b75 8944{
b4d90040
PA
8945 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8946
f90263c1
TT
8947 if (ws->kind != TARGET_WAITKIND_EXECD)
8948 return 0;
8949
8950 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8951 return 1;
fe798b75 8952}
c906108c 8953
fe798b75 8954static enum print_stop_action
348d480f 8955print_it_catch_exec (bpstat bs)
fe798b75 8956{
36dfb11c 8957 struct ui_out *uiout = current_uiout;
348d480f 8958 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8959 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8960
fe798b75 8961 annotate_catchpoint (b->number);
36dfb11c
TT
8962 if (b->disposition == disp_del)
8963 ui_out_text (uiout, "\nTemporary catchpoint ");
8964 else
8965 ui_out_text (uiout, "\nCatchpoint ");
8966 if (ui_out_is_mi_like_p (uiout))
8967 {
8968 ui_out_field_string (uiout, "reason",
8969 async_reason_lookup (EXEC_ASYNC_EXEC));
8970 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8971 }
8972 ui_out_field_int (uiout, "bkptno", b->number);
8973 ui_out_text (uiout, " (exec'd ");
8974 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8975 ui_out_text (uiout, "), ");
8976
fe798b75 8977 return PRINT_SRC_AND_LOC;
c906108c
SS
8978}
8979
fe798b75 8980static void
a6d9a66e 8981print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8982{
b4d90040 8983 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8984 struct value_print_options opts;
79a45e25 8985 struct ui_out *uiout = current_uiout;
fe798b75
JB
8986
8987 get_user_print_options (&opts);
8988
8989 /* Field 4, the address, is omitted (which makes the columns
8990 not line up too nicely with the headers, but the effect
8991 is relatively readable). */
8992 if (opts.addressprint)
8993 ui_out_field_skip (uiout, "addr");
8994 annotate_field (5);
8995 ui_out_text (uiout, "exec");
b4d90040 8996 if (c->exec_pathname != NULL)
fe798b75
JB
8997 {
8998 ui_out_text (uiout, ", program \"");
b4d90040 8999 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
9000 ui_out_text (uiout, "\" ");
9001 }
8ac3646f
TT
9002
9003 if (ui_out_is_mi_like_p (uiout))
9004 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
9005}
9006
9007static void
9008print_mention_catch_exec (struct breakpoint *b)
9009{
9010 printf_filtered (_("Catchpoint %d (exec)"), b->number);
9011}
9012
6149aea9
PA
9013/* Implement the "print_recreate" breakpoint_ops method for exec
9014 catchpoints. */
9015
9016static void
9017print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
9018{
9019 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 9020 print_recreate_thread (b, fp);
6149aea9
PA
9021}
9022
2060206e 9023static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 9024
a96d9b2e
SDJ
9025static void
9026create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 9027 const struct breakpoint_ops *ops)
a96d9b2e 9028{
be5c67c1 9029 struct syscall_catchpoint *c;
a96d9b2e 9030 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 9031
be5c67c1
PA
9032 c = XNEW (struct syscall_catchpoint);
9033 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
9034 c->syscalls_to_be_caught = filter;
a96d9b2e 9035
3ea46bff 9036 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
9037}
9038
c906108c 9039static int
fba45db2 9040hw_breakpoint_used_count (void)
c906108c 9041{
c906108c 9042 int i = 0;
f1310107
TJB
9043 struct breakpoint *b;
9044 struct bp_location *bl;
c906108c
SS
9045
9046 ALL_BREAKPOINTS (b)
c5aa993b 9047 {
d6b74ac4 9048 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
9049 for (bl = b->loc; bl; bl = bl->next)
9050 {
9051 /* Special types of hardware breakpoints may use more than
9052 one register. */
348d480f 9053 i += b->ops->resources_needed (bl);
f1310107 9054 }
c5aa993b 9055 }
c906108c
SS
9056
9057 return i;
9058}
9059
a1398e0c
PA
9060/* Returns the resources B would use if it were a hardware
9061 watchpoint. */
9062
c906108c 9063static int
a1398e0c 9064hw_watchpoint_use_count (struct breakpoint *b)
c906108c 9065{
c906108c 9066 int i = 0;
e09342b5 9067 struct bp_location *bl;
c906108c 9068
a1398e0c
PA
9069 if (!breakpoint_enabled (b))
9070 return 0;
9071
9072 for (bl = b->loc; bl; bl = bl->next)
9073 {
9074 /* Special types of hardware watchpoints may use more than
9075 one register. */
9076 i += b->ops->resources_needed (bl);
9077 }
9078
9079 return i;
9080}
9081
9082/* Returns the sum the used resources of all hardware watchpoints of
9083 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9084 the sum of the used resources of all hardware watchpoints of other
9085 types _not_ TYPE. */
9086
9087static int
9088hw_watchpoint_used_count_others (struct breakpoint *except,
9089 enum bptype type, int *other_type_used)
9090{
9091 int i = 0;
9092 struct breakpoint *b;
9093
c906108c
SS
9094 *other_type_used = 0;
9095 ALL_BREAKPOINTS (b)
e09342b5 9096 {
a1398e0c
PA
9097 if (b == except)
9098 continue;
e09342b5
TJB
9099 if (!breakpoint_enabled (b))
9100 continue;
9101
a1398e0c
PA
9102 if (b->type == type)
9103 i += hw_watchpoint_use_count (b);
9104 else if (is_hardware_watchpoint (b))
9105 *other_type_used = 1;
e09342b5
TJB
9106 }
9107
c906108c
SS
9108 return i;
9109}
9110
c906108c 9111void
fba45db2 9112disable_watchpoints_before_interactive_call_start (void)
c906108c 9113{
c5aa993b 9114 struct breakpoint *b;
c906108c
SS
9115
9116 ALL_BREAKPOINTS (b)
c5aa993b 9117 {
cc60f2e3 9118 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 9119 {
b5de0fa7 9120 b->enable_state = bp_call_disabled;
44702360 9121 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
9122 }
9123 }
c906108c
SS
9124}
9125
9126void
fba45db2 9127enable_watchpoints_after_interactive_call_stop (void)
c906108c 9128{
c5aa993b 9129 struct breakpoint *b;
c906108c
SS
9130
9131 ALL_BREAKPOINTS (b)
c5aa993b 9132 {
cc60f2e3 9133 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 9134 {
b5de0fa7 9135 b->enable_state = bp_enabled;
44702360 9136 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
9137 }
9138 }
c906108c
SS
9139}
9140
8bea4e01
UW
9141void
9142disable_breakpoints_before_startup (void)
9143{
6c95b8df 9144 current_program_space->executing_startup = 1;
44702360 9145 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
9146}
9147
9148void
9149enable_breakpoints_after_startup (void)
9150{
6c95b8df 9151 current_program_space->executing_startup = 0;
f8eba3c6 9152 breakpoint_re_set ();
8bea4e01
UW
9153}
9154
c906108c
SS
9155
9156/* Set a breakpoint that will evaporate an end of command
9157 at address specified by SAL.
9158 Restrict it to frame FRAME if FRAME is nonzero. */
9159
9160struct breakpoint *
a6d9a66e
UW
9161set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9162 struct frame_id frame_id, enum bptype type)
c906108c 9163{
52f0bd74 9164 struct breakpoint *b;
edb3359d 9165
193facb3
JK
9166 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9167 tail-called one. */
9168 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9169
06edf0c0 9170 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9171 b->enable_state = bp_enabled;
9172 b->disposition = disp_donttouch;
818dd999 9173 b->frame_id = frame_id;
c906108c 9174
4a64f543
MS
9175 /* If we're debugging a multi-threaded program, then we want
9176 momentary breakpoints to be active in only a single thread of
9177 control. */
39f77062
KB
9178 if (in_thread_list (inferior_ptid))
9179 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9180
44702360 9181 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 9182
c906108c
SS
9183 return b;
9184}
611c83ae 9185
06edf0c0 9186/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
9187 The new breakpoint will have type TYPE, use OPS as its
9188 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 9189
06edf0c0
PA
9190static struct breakpoint *
9191momentary_breakpoint_from_master (struct breakpoint *orig,
9192 enum bptype type,
a1aa2221
LM
9193 const struct breakpoint_ops *ops,
9194 int loc_enabled)
e58b0e63
PA
9195{
9196 struct breakpoint *copy;
9197
06edf0c0 9198 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9199 copy->loc = allocate_bp_location (copy);
0e30163f 9200 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9201
a6d9a66e 9202 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9203 copy->loc->requested_address = orig->loc->requested_address;
9204 copy->loc->address = orig->loc->address;
9205 copy->loc->section = orig->loc->section;
6c95b8df 9206 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9207 copy->loc->probe = orig->loc->probe;
f8eba3c6 9208 copy->loc->line_number = orig->loc->line_number;
2f202fde 9209 copy->loc->symtab = orig->loc->symtab;
a1aa2221 9210 copy->loc->enabled = loc_enabled;
e58b0e63
PA
9211 copy->frame_id = orig->frame_id;
9212 copy->thread = orig->thread;
6c95b8df 9213 copy->pspace = orig->pspace;
e58b0e63
PA
9214
9215 copy->enable_state = bp_enabled;
9216 copy->disposition = disp_donttouch;
9217 copy->number = internal_breakpoint_number--;
9218
44702360 9219 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
9220 return copy;
9221}
9222
06edf0c0
PA
9223/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9224 ORIG is NULL. */
9225
9226struct breakpoint *
9227clone_momentary_breakpoint (struct breakpoint *orig)
9228{
9229 /* If there's nothing to clone, then return nothing. */
9230 if (orig == NULL)
9231 return NULL;
9232
a1aa2221 9233 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9234}
9235
611c83ae 9236struct breakpoint *
a6d9a66e
UW
9237set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9238 enum bptype type)
611c83ae
PA
9239{
9240 struct symtab_and_line sal;
9241
9242 sal = find_pc_line (pc, 0);
9243 sal.pc = pc;
9244 sal.section = find_pc_overlay (pc);
9245 sal.explicit_pc = 1;
9246
a6d9a66e 9247 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9248}
c906108c 9249\f
c5aa993b 9250
c906108c
SS
9251/* Tell the user we have just set a breakpoint B. */
9252
9253static void
fba45db2 9254mention (struct breakpoint *b)
c906108c 9255{
348d480f 9256 b->ops->print_mention (b);
79a45e25 9257 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9258 return;
c906108c
SS
9259 printf_filtered ("\n");
9260}
c906108c 9261\f
c5aa993b 9262
0d381245 9263static struct bp_location *
39d61571 9264add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9265 const struct symtab_and_line *sal)
9266{
9267 struct bp_location *loc, **tmp;
3742cc8b
YQ
9268 CORE_ADDR adjusted_address;
9269 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9270
9271 if (loc_gdbarch == NULL)
9272 loc_gdbarch = b->gdbarch;
9273
9274 /* Adjust the breakpoint's address prior to allocating a location.
9275 Once we call allocate_bp_location(), that mostly uninitialized
9276 location will be placed on the location chain. Adjustment of the
9277 breakpoint may cause target_read_memory() to be called and we do
9278 not want its scan of the location chain to find a breakpoint and
9279 location that's only been partially initialized. */
9280 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9281 sal->pc, b->type);
0d381245 9282
d30113d4 9283 /* Sort the locations by their ADDRESS. */
39d61571 9284 loc = allocate_bp_location (b);
d30113d4
JK
9285 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9286 tmp = &((*tmp)->next))
0d381245 9287 ;
d30113d4 9288 loc->next = *tmp;
0d381245 9289 *tmp = loc;
3742cc8b 9290
0d381245 9291 loc->requested_address = sal->pc;
3742cc8b 9292 loc->address = adjusted_address;
6c95b8df 9293 loc->pspace = sal->pspace;
729662a5
TT
9294 loc->probe.probe = sal->probe;
9295 loc->probe.objfile = sal->objfile;
6c95b8df 9296 gdb_assert (loc->pspace != NULL);
0d381245 9297 loc->section = sal->section;
3742cc8b 9298 loc->gdbarch = loc_gdbarch;
f8eba3c6 9299 loc->line_number = sal->line;
2f202fde 9300 loc->symtab = sal->symtab;
f8eba3c6 9301
0e30163f
JK
9302 set_breakpoint_location_function (loc,
9303 sal->explicit_pc || sal->explicit_line);
0d381245
VP
9304 return loc;
9305}
514f746b
AR
9306\f
9307
9308/* Return 1 if LOC is pointing to a permanent breakpoint,
9309 return 0 otherwise. */
9310
9311static int
9312bp_loc_is_permanent (struct bp_location *loc)
9313{
9314 int len;
9315 CORE_ADDR addr;
1afeeb75 9316 const gdb_byte *bpoint;
514f746b 9317 gdb_byte *target_mem;
939c61fa
JK
9318 struct cleanup *cleanup;
9319 int retval = 0;
514f746b
AR
9320
9321 gdb_assert (loc != NULL);
9322
9323 addr = loc->address;
1afeeb75 9324 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9325
939c61fa 9326 /* Software breakpoints unsupported? */
1afeeb75 9327 if (bpoint == NULL)
939c61fa
JK
9328 return 0;
9329
514f746b
AR
9330 target_mem = alloca (len);
9331
939c61fa
JK
9332 /* Enable the automatic memory restoration from breakpoints while
9333 we read the memory. Otherwise we could say about our temporary
9334 breakpoints they are permanent. */
6c95b8df
PA
9335 cleanup = save_current_space_and_thread ();
9336
9337 switch_to_program_space_and_thread (loc->pspace);
9338 make_show_memory_breakpoints_cleanup (0);
939c61fa 9339
514f746b 9340 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9341 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9342 retval = 1;
514f746b 9343
939c61fa
JK
9344 do_cleanups (cleanup);
9345
9346 return retval;
514f746b
AR
9347}
9348
e7e0cddf
SS
9349/* Build a command list for the dprintf corresponding to the current
9350 settings of the dprintf style options. */
9351
9352static void
9353update_dprintf_command_list (struct breakpoint *b)
9354{
9355 char *dprintf_args = b->extra_string;
9356 char *printf_line = NULL;
9357
9358 if (!dprintf_args)
9359 return;
9360
9361 dprintf_args = skip_spaces (dprintf_args);
9362
9363 /* Allow a comma, as it may have terminated a location, but don't
9364 insist on it. */
9365 if (*dprintf_args == ',')
9366 ++dprintf_args;
9367 dprintf_args = skip_spaces (dprintf_args);
9368
9369 if (*dprintf_args != '"')
9370 error (_("Bad format string, missing '\"'."));
9371
d3ce09f5 9372 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9373 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9374 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9375 {
9376 if (!dprintf_function)
9377 error (_("No function supplied for dprintf call"));
9378
9379 if (dprintf_channel && strlen (dprintf_channel) > 0)
9380 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9381 dprintf_function,
9382 dprintf_channel,
9383 dprintf_args);
9384 else
9385 printf_line = xstrprintf ("call (void) %s (%s)",
9386 dprintf_function,
9387 dprintf_args);
9388 }
d3ce09f5
SS
9389 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9390 {
9391 if (target_can_run_breakpoint_commands ())
9392 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9393 else
9394 {
9395 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9396 printf_line = xstrprintf ("printf %s", dprintf_args);
9397 }
9398 }
e7e0cddf
SS
9399 else
9400 internal_error (__FILE__, __LINE__,
9401 _("Invalid dprintf style."));
9402
f28045c2 9403 gdb_assert (printf_line != NULL);
9d6e6e84 9404 /* Manufacture a printf sequence. */
f28045c2 9405 {
9d6e6e84
HZ
9406 struct command_line *printf_cmd_line
9407 = xmalloc (sizeof (struct command_line));
e7e0cddf 9408
f28045c2
YQ
9409 printf_cmd_line = xmalloc (sizeof (struct command_line));
9410 printf_cmd_line->control_type = simple_control;
9411 printf_cmd_line->body_count = 0;
9412 printf_cmd_line->body_list = NULL;
9d6e6e84 9413 printf_cmd_line->next = NULL;
f28045c2 9414 printf_cmd_line->line = printf_line;
e7e0cddf 9415
f28045c2
YQ
9416 breakpoint_set_commands (b, printf_cmd_line);
9417 }
e7e0cddf
SS
9418}
9419
9420/* Update all dprintf commands, making their command lists reflect
9421 current style settings. */
9422
9423static void
9424update_dprintf_commands (char *args, int from_tty,
9425 struct cmd_list_element *c)
9426{
9427 struct breakpoint *b;
9428
9429 ALL_BREAKPOINTS (b)
9430 {
9431 if (b->type == bp_dprintf)
9432 update_dprintf_command_list (b);
9433 }
9434}
c3f6f71d 9435
018d34a4
VP
9436/* Create a breakpoint with SAL as location. Use ADDR_STRING
9437 as textual description of the location, and COND_STRING
db107f19 9438 as condition expression. */
018d34a4
VP
9439
9440static void
d9b3f62e
PA
9441init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9442 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9443 char *filter, char *cond_string,
e7e0cddf 9444 char *extra_string,
d9b3f62e
PA
9445 enum bptype type, enum bpdisp disposition,
9446 int thread, int task, int ignore_count,
c0a91b2b 9447 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9448 int enabled, int internal, unsigned flags,
9449 int display_canonical)
018d34a4 9450{
0d381245 9451 int i;
018d34a4
VP
9452
9453 if (type == bp_hardware_breakpoint)
9454 {
fbbd034e
AS
9455 int target_resources_ok;
9456
9457 i = hw_breakpoint_used_count ();
9458 target_resources_ok =
9459 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9460 i + 1, 0);
9461 if (target_resources_ok == 0)
9462 error (_("No hardware breakpoint support in the target."));
9463 else if (target_resources_ok < 0)
9464 error (_("Hardware breakpoints used exceeds limit."));
9465 }
9466
6c95b8df
PA
9467 gdb_assert (sals.nelts > 0);
9468
0d381245
VP
9469 for (i = 0; i < sals.nelts; ++i)
9470 {
9471 struct symtab_and_line sal = sals.sals[i];
9472 struct bp_location *loc;
9473
9474 if (from_tty)
5af949e3
UW
9475 {
9476 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9477 if (!loc_gdbarch)
9478 loc_gdbarch = gdbarch;
9479
9480 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9481 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9482 }
0d381245
VP
9483
9484 if (i == 0)
9485 {
d9b3f62e 9486 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9487 b->thread = thread;
4a306c9a 9488 b->task = task;
855a6e68 9489
0d381245 9490 b->cond_string = cond_string;
e7e0cddf 9491 b->extra_string = extra_string;
0d381245 9492 b->ignore_count = ignore_count;
41447f92 9493 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9494 b->disposition = disposition;
6c95b8df 9495
44f238bb
PA
9496 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9497 b->loc->inserted = 1;
9498
0fb4aa4b
PA
9499 if (type == bp_static_tracepoint)
9500 {
d9b3f62e 9501 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9502 struct static_tracepoint_marker marker;
9503
983af33b 9504 if (strace_marker_p (b))
0fb4aa4b
PA
9505 {
9506 /* We already know the marker exists, otherwise, we
9507 wouldn't see a sal for it. */
9508 char *p = &addr_string[3];
9509 char *endp;
9510 char *marker_str;
0fb4aa4b 9511
e9cafbcc 9512 p = skip_spaces (p);
0fb4aa4b 9513
e9cafbcc 9514 endp = skip_to_space (p);
0fb4aa4b
PA
9515
9516 marker_str = savestring (p, endp - p);
d9b3f62e 9517 t->static_trace_marker_id = marker_str;
0fb4aa4b 9518
3e43a32a
MS
9519 printf_filtered (_("Probed static tracepoint "
9520 "marker \"%s\"\n"),
d9b3f62e 9521 t->static_trace_marker_id);
0fb4aa4b
PA
9522 }
9523 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9524 {
d9b3f62e 9525 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9526 release_static_tracepoint_marker (&marker);
9527
3e43a32a
MS
9528 printf_filtered (_("Probed static tracepoint "
9529 "marker \"%s\"\n"),
d9b3f62e 9530 t->static_trace_marker_id);
0fb4aa4b
PA
9531 }
9532 else
3e43a32a
MS
9533 warning (_("Couldn't determine the static "
9534 "tracepoint marker to probe"));
0fb4aa4b
PA
9535 }
9536
0d381245
VP
9537 loc = b->loc;
9538 }
9539 else
018d34a4 9540 {
39d61571 9541 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9542 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9543 loc->inserted = 1;
0d381245
VP
9544 }
9545
514f746b
AR
9546 if (bp_loc_is_permanent (loc))
9547 make_breakpoint_permanent (b);
9548
0d381245
VP
9549 if (b->cond_string)
9550 {
bbc13ae3
KS
9551 const char *arg = b->cond_string;
9552
1bb9788d
TT
9553 loc->cond = parse_exp_1 (&arg, loc->address,
9554 block_for_pc (loc->address), 0);
0d381245 9555 if (*arg)
588ae58c 9556 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9557 }
e7e0cddf
SS
9558
9559 /* Dynamic printf requires and uses additional arguments on the
9560 command line, otherwise it's an error. */
9561 if (type == bp_dprintf)
9562 {
9563 if (b->extra_string)
9564 update_dprintf_command_list (b);
9565 else
9566 error (_("Format string required"));
9567 }
9568 else if (b->extra_string)
588ae58c 9569 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9570 }
018d34a4 9571
56435ebe 9572 b->display_canonical = display_canonical;
018d34a4
VP
9573 if (addr_string)
9574 b->addr_string = addr_string;
9575 else
9576 /* addr_string has to be used or breakpoint_re_set will delete
9577 me. */
5af949e3
UW
9578 b->addr_string
9579 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9580 b->filter = filter;
d9b3f62e 9581}
018d34a4 9582
d9b3f62e
PA
9583static void
9584create_breakpoint_sal (struct gdbarch *gdbarch,
9585 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9586 char *filter, char *cond_string,
e7e0cddf 9587 char *extra_string,
d9b3f62e
PA
9588 enum bptype type, enum bpdisp disposition,
9589 int thread, int task, int ignore_count,
c0a91b2b 9590 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9591 int enabled, int internal, unsigned flags,
9592 int display_canonical)
d9b3f62e
PA
9593{
9594 struct breakpoint *b;
9595 struct cleanup *old_chain;
9596
9597 if (is_tracepoint_type (type))
9598 {
9599 struct tracepoint *t;
9600
9601 t = XCNEW (struct tracepoint);
9602 b = &t->base;
9603 }
9604 else
9605 b = XNEW (struct breakpoint);
9606
9607 old_chain = make_cleanup (xfree, b);
9608
9609 init_breakpoint_sal (b, gdbarch,
9610 sals, addr_string,
e7e0cddf 9611 filter, cond_string, extra_string,
d9b3f62e
PA
9612 type, disposition,
9613 thread, task, ignore_count,
9614 ops, from_tty,
44f238bb
PA
9615 enabled, internal, flags,
9616 display_canonical);
d9b3f62e
PA
9617 discard_cleanups (old_chain);
9618
3ea46bff 9619 install_breakpoint (internal, b, 0);
018d34a4
VP
9620}
9621
9622/* Add SALS.nelts breakpoints to the breakpoint table. For each
9623 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9624 value. COND_STRING, if not NULL, specified the condition to be
9625 used for all breakpoints. Essentially the only case where
9626 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9627 function. In that case, it's still not possible to specify
9628 separate conditions for different overloaded functions, so
9629 we take just a single condition string.
9630
c3f6f71d 9631 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9632 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9633 array contents). If the function fails (error() is called), the
9634 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9635 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9636
9637static void
8cdf0e15 9638create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9639 struct linespec_result *canonical,
e7e0cddf 9640 char *cond_string, char *extra_string,
8cdf0e15
VP
9641 enum bptype type, enum bpdisp disposition,
9642 int thread, int task, int ignore_count,
c0a91b2b 9643 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9644 int enabled, int internal, unsigned flags)
c906108c 9645{
018d34a4 9646 int i;
f8eba3c6 9647 struct linespec_sals *lsal;
cc59ec59 9648
f8eba3c6
TT
9649 if (canonical->pre_expanded)
9650 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9651
9652 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9653 {
f8eba3c6
TT
9654 /* Note that 'addr_string' can be NULL in the case of a plain
9655 'break', without arguments. */
9656 char *addr_string = (canonical->addr_string
9657 ? xstrdup (canonical->addr_string)
9658 : NULL);
9659 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9660 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9661
f8eba3c6
TT
9662 make_cleanup (xfree, filter_string);
9663 create_breakpoint_sal (gdbarch, lsal->sals,
9664 addr_string,
9665 filter_string,
e7e0cddf
SS
9666 cond_string, extra_string,
9667 type, disposition,
84f4c1fe 9668 thread, task, ignore_count, ops,
44f238bb 9669 from_tty, enabled, internal, flags,
56435ebe 9670 canonical->special_display);
f8eba3c6 9671 discard_cleanups (inner);
c3f6f71d 9672 }
c3f6f71d 9673}
c906108c 9674
9998af43 9675/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9676 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9677 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9678 address strings. ADDRESS points to the end of the SAL.
9679
9680 The array and the line spec strings are allocated on the heap, it is
9681 the caller's responsibility to free them. */
c906108c 9682
b9362cc7 9683static void
c3f6f71d 9684parse_breakpoint_sals (char **address,
58438ac1 9685 struct linespec_result *canonical)
c3f6f71d 9686{
c3f6f71d 9687 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9688 breakpoint. */
c3f6f71d
JM
9689 if ((*address) == NULL
9690 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9691 {
1bfeeb0f
JL
9692 /* The last displayed codepoint, if it's valid, is our default breakpoint
9693 address. */
9694 if (last_displayed_sal_is_valid ())
c906108c 9695 {
f8eba3c6 9696 struct linespec_sals lsal;
c3f6f71d 9697 struct symtab_and_line sal;
1c8cdcb1 9698 CORE_ADDR pc;
cc59ec59 9699
4a64f543 9700 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9701 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9702 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9703
9704 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9705 corresponding to the last call to print_frame_info.
9706 Be sure to reinitialize LINE with NOTCURRENT == 0
9707 as the breakpoint line number is inappropriate otherwise.
9708 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9709 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9710 pc = sal.pc;
9711 sal = find_pc_line (pc, 0);
00903456 9712
4a64f543 9713 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9714 where PC is the last displayed codepoint's address. So
9715 make sure to set sal.explicit_pc to prevent GDB from
9716 trying to expand the list of sals to include all other
9717 instances with the same symtab and line. */
1c8cdcb1 9718 sal.pc = pc;
00903456
JK
9719 sal.explicit_pc = 1;
9720
f8eba3c6
TT
9721 lsal.sals.sals[0] = sal;
9722 lsal.sals.nelts = 1;
9723 lsal.canonical = NULL;
9724
9725 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9726 }
9727 else
8a3fe4f8 9728 error (_("No default breakpoint address now."));
c906108c
SS
9729 }
9730 else
9731 {
cc80f267
JK
9732 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9733
c906108c 9734 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9735 current_source_symtab (which is decode_line_1's default).
9736 This should produce the results we want almost all of the
cc80f267
JK
9737 time while leaving default_breakpoint_* alone.
9738
9739 ObjC: However, don't match an Objective-C method name which
9740 may have a '+' or '-' succeeded by a '['. */
9741 if (last_displayed_sal_is_valid ()
9742 && (!cursal.symtab
9743 || ((strchr ("+-", (*address)[0]) != NULL)
9744 && ((*address)[1] != '['))))
f8eba3c6
TT
9745 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9746 get_last_displayed_symtab (),
9747 get_last_displayed_line (),
9748 canonical, NULL, NULL);
c906108c 9749 else
f8eba3c6 9750 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9751 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9752 }
c3f6f71d 9753}
c906108c 9754
c906108c 9755
c3f6f71d 9756/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9757 inserted as a breakpoint. If it can't throw an error. */
c906108c 9758
b9362cc7 9759static void
23e7acfb 9760breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9761{
9762 int i;
cc59ec59 9763
c3f6f71d 9764 for (i = 0; i < sals->nelts; i++)
ee53e872 9765 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9766}
9767
7a697b8d
SS
9768/* Fast tracepoints may have restrictions on valid locations. For
9769 instance, a fast tracepoint using a jump instead of a trap will
9770 likely have to overwrite more bytes than a trap would, and so can
9771 only be placed where the instruction is longer than the jump, or a
9772 multi-instruction sequence does not have a jump into the middle of
9773 it, etc. */
9774
9775static void
9776check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9777 struct symtabs_and_lines *sals)
9778{
9779 int i, rslt;
9780 struct symtab_and_line *sal;
9781 char *msg;
9782 struct cleanup *old_chain;
9783
9784 for (i = 0; i < sals->nelts; i++)
9785 {
f8eba3c6
TT
9786 struct gdbarch *sarch;
9787
7a697b8d
SS
9788 sal = &sals->sals[i];
9789
f8eba3c6
TT
9790 sarch = get_sal_arch (*sal);
9791 /* We fall back to GDBARCH if there is no architecture
9792 associated with SAL. */
9793 if (sarch == NULL)
9794 sarch = gdbarch;
9795 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9796 NULL, &msg);
9797 old_chain = make_cleanup (xfree, msg);
9798
9799 if (!rslt)
9800 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9801 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9802
9803 do_cleanups (old_chain);
9804 }
9805}
9806
af4908ba
KS
9807/* Issue an invalid thread ID error. */
9808
9809static void ATTRIBUTE_NORETURN
9810invalid_thread_id_error (int id)
9811{
9812 error (_("Unknown thread %d."), id);
9813}
9814
018d34a4
VP
9815/* Given TOK, a string specification of condition and thread, as
9816 accepted by the 'break' command, extract the condition
9817 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9818 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9819 If no condition is found, *COND_STRING is set to NULL.
9820 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9821
9822static void
bbc13ae3 9823find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9824 char **cond_string, int *thread, int *task,
9825 char **rest)
018d34a4
VP
9826{
9827 *cond_string = NULL;
9828 *thread = -1;
ed1d1739
KS
9829 *task = 0;
9830 *rest = NULL;
9831
018d34a4
VP
9832 while (tok && *tok)
9833 {
bbc13ae3 9834 const char *end_tok;
018d34a4 9835 int toklen;
bbc13ae3
KS
9836 const char *cond_start = NULL;
9837 const char *cond_end = NULL;
cc59ec59 9838
bbc13ae3 9839 tok = skip_spaces_const (tok);
e7e0cddf
SS
9840
9841 if ((*tok == '"' || *tok == ',') && rest)
9842 {
9843 *rest = savestring (tok, strlen (tok));
9844 return;
9845 }
9846
bbc13ae3 9847 end_tok = skip_to_space_const (tok);
d634f2de 9848
018d34a4 9849 toklen = end_tok - tok;
d634f2de 9850
018d34a4
VP
9851 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9852 {
f7545552
TT
9853 struct expression *expr;
9854
018d34a4 9855 tok = cond_start = end_tok + 1;
1bb9788d 9856 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9857 xfree (expr);
018d34a4 9858 cond_end = tok;
d634f2de 9859 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9860 }
9861 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9862 {
9863 char *tmptok;
d634f2de 9864
018d34a4 9865 tok = end_tok + 1;
bbc13ae3 9866 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9867 if (tok == tmptok)
9868 error (_("Junk after thread keyword."));
9869 if (!valid_thread_id (*thread))
af4908ba 9870 invalid_thread_id_error (*thread);
bbc13ae3 9871 tok = tmptok;
018d34a4 9872 }
4a306c9a
JB
9873 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9874 {
9875 char *tmptok;
9876
9877 tok = end_tok + 1;
bbc13ae3 9878 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9879 if (tok == tmptok)
9880 error (_("Junk after task keyword."));
9881 if (!valid_task_id (*task))
b6199126 9882 error (_("Unknown task %d."), *task);
bbc13ae3 9883 tok = tmptok;
4a306c9a 9884 }
e7e0cddf
SS
9885 else if (rest)
9886 {
9887 *rest = savestring (tok, strlen (tok));
ccab2054 9888 return;
e7e0cddf 9889 }
018d34a4
VP
9890 else
9891 error (_("Junk at end of arguments."));
9892 }
9893}
9894
0fb4aa4b
PA
9895/* Decode a static tracepoint marker spec. */
9896
9897static struct symtabs_and_lines
9898decode_static_tracepoint_spec (char **arg_p)
9899{
9900 VEC(static_tracepoint_marker_p) *markers = NULL;
9901 struct symtabs_and_lines sals;
0fb4aa4b
PA
9902 struct cleanup *old_chain;
9903 char *p = &(*arg_p)[3];
9904 char *endp;
9905 char *marker_str;
9906 int i;
9907
e9cafbcc 9908 p = skip_spaces (p);
0fb4aa4b 9909
e9cafbcc 9910 endp = skip_to_space (p);
0fb4aa4b
PA
9911
9912 marker_str = savestring (p, endp - p);
9913 old_chain = make_cleanup (xfree, marker_str);
9914
9915 markers = target_static_tracepoint_markers_by_strid (marker_str);
9916 if (VEC_empty(static_tracepoint_marker_p, markers))
9917 error (_("No known static tracepoint marker named %s"), marker_str);
9918
9919 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9920 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9921
9922 for (i = 0; i < sals.nelts; i++)
9923 {
9924 struct static_tracepoint_marker *marker;
9925
9926 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9927
9928 init_sal (&sals.sals[i]);
9929
9930 sals.sals[i] = find_pc_line (marker->address, 0);
9931 sals.sals[i].pc = marker->address;
9932
9933 release_static_tracepoint_marker (marker);
9934 }
9935
9936 do_cleanups (old_chain);
9937
9938 *arg_p = endp;
9939 return sals;
9940}
9941
fd9b8c24
PA
9942/* Set a breakpoint. This function is shared between CLI and MI
9943 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9944 modes of operations, selected by the PARSE_ARG parameter. If
9945 non-zero, the function will parse ARG, extracting location,
9946 condition, thread and extra string. Otherwise, ARG is just the
9947 breakpoint's location, with condition, thread, and extra string
9948 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9949 If INTERNAL is non-zero, the breakpoint number will be allocated
9950 from the internal breakpoint count. Returns true if any breakpoint
9951 was created; false otherwise. */
0101ce28 9952
8cdf0e15
VP
9953int
9954create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9955 char *arg, char *cond_string,
9956 int thread, char *extra_string,
f6de8ec2 9957 int parse_arg,
0fb4aa4b 9958 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9959 int ignore_count,
9960 enum auto_boolean pending_break_support,
c0a91b2b 9961 const struct breakpoint_ops *ops,
44f238bb
PA
9962 int from_tty, int enabled, int internal,
9963 unsigned flags)
c3f6f71d 9964{
b78a6381 9965 volatile struct gdb_exception e;
f8eba3c6 9966 char *copy_arg = NULL;
c3f6f71d 9967 char *addr_start = arg;
7efd8fc2 9968 struct linespec_result canonical;
c3f6f71d 9969 struct cleanup *old_chain;
80c99de1 9970 struct cleanup *bkpt_chain = NULL;
0101ce28 9971 int pending = 0;
4a306c9a 9972 int task = 0;
86b17b60 9973 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9974
348d480f
PA
9975 gdb_assert (ops != NULL);
9976
7efd8fc2 9977 init_linespec_result (&canonical);
c3f6f71d 9978
b78a6381
TT
9979 TRY_CATCH (e, RETURN_MASK_ALL)
9980 {
983af33b
SDJ
9981 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9982 addr_start, &copy_arg);
b78a6381 9983 }
0101ce28
JJ
9984
9985 /* If caller is interested in rc value from parse, set value. */
05ff989b 9986 switch (e.reason)
0101ce28 9987 {
983af33b
SDJ
9988 case GDB_NO_ERROR:
9989 if (VEC_empty (linespec_sals, canonical.sals))
9990 return 0;
9991 break;
05ff989b
AC
9992 case RETURN_ERROR:
9993 switch (e.error)
0101ce28 9994 {
05ff989b 9995 case NOT_FOUND_ERROR:
0101ce28 9996
05ff989b
AC
9997 /* If pending breakpoint support is turned off, throw
9998 error. */
fa8d40ab
JJ
9999
10000 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
10001 throw_exception (e);
10002
10003 exception_print (gdb_stderr, e);
fa8d40ab 10004
05ff989b
AC
10005 /* If pending breakpoint support is auto query and the user
10006 selects no, then simply return the error code. */
059fb39f 10007 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
10008 && !nquery (_("Make %s pending on future shared library load? "),
10009 bptype_string (type_wanted)))
fd9b8c24 10010 return 0;
fa8d40ab 10011
05ff989b
AC
10012 /* At this point, either the user was queried about setting
10013 a pending breakpoint and selected yes, or pending
10014 breakpoint behavior is on and thus a pending breakpoint
10015 is defaulted on behalf of the user. */
f8eba3c6
TT
10016 {
10017 struct linespec_sals lsal;
10018
10019 copy_arg = xstrdup (addr_start);
10020 lsal.canonical = xstrdup (copy_arg);
10021 lsal.sals.nelts = 1;
10022 lsal.sals.sals = XNEW (struct symtab_and_line);
10023 init_sal (&lsal.sals.sals[0]);
10024 pending = 1;
10025 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
10026 }
05ff989b
AC
10027 break;
10028 default:
98deb0da 10029 throw_exception (e);
0101ce28 10030 }
2abae994 10031 break;
05ff989b 10032 default:
983af33b 10033 throw_exception (e);
0101ce28 10034 }
c3f6f71d 10035
4a64f543 10036 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 10037 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 10038
c3f6f71d
JM
10039 /* ----------------------------- SNIP -----------------------------
10040 Anything added to the cleanup chain beyond this point is assumed
10041 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
10042 then the memory is not reclaimed. */
10043 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 10044
c3f6f71d
JM
10045 /* Resolve all line numbers to PC's and verify that the addresses
10046 are ok for the target. */
0101ce28 10047 if (!pending)
f8eba3c6
TT
10048 {
10049 int ix;
10050 struct linespec_sals *iter;
10051
10052 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10053 breakpoint_sals_to_pc (&iter->sals);
10054 }
c3f6f71d 10055
7a697b8d 10056 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 10057 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
10058 {
10059 int ix;
10060 struct linespec_sals *iter;
10061
10062 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10063 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10064 }
7a697b8d 10065
c3f6f71d
JM
10066 /* Verify that condition can be parsed, before setting any
10067 breakpoints. Allocate a separate condition expression for each
4a64f543 10068 breakpoint. */
0101ce28 10069 if (!pending)
c3f6f71d 10070 {
f6de8ec2 10071 if (parse_arg)
72b2ff0e 10072 {
0878d0fa 10073 char *rest;
52d361e1
YQ
10074 struct linespec_sals *lsal;
10075
10076 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10077
0878d0fa
YQ
10078 /* Here we only parse 'arg' to separate condition
10079 from thread number, so parsing in context of first
10080 sal is OK. When setting the breakpoint we'll
10081 re-parse it in context of each sal. */
10082
10083 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10084 &thread, &task, &rest);
10085 if (cond_string)
10086 make_cleanup (xfree, cond_string);
10087 if (rest)
10088 make_cleanup (xfree, rest);
10089 if (rest)
10090 extra_string = rest;
72b2ff0e 10091 }
2f069f6f 10092 else
72b2ff0e 10093 {
0878d0fa
YQ
10094 if (*arg != '\0')
10095 error (_("Garbage '%s' at end of location"), arg);
10096
10097 /* Create a private copy of condition string. */
10098 if (cond_string)
10099 {
10100 cond_string = xstrdup (cond_string);
10101 make_cleanup (xfree, cond_string);
10102 }
10103 /* Create a private copy of any extra string. */
10104 if (extra_string)
10105 {
10106 extra_string = xstrdup (extra_string);
10107 make_cleanup (xfree, extra_string);
10108 }
72b2ff0e 10109 }
0fb4aa4b 10110
52d361e1 10111 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 10112 cond_string, extra_string, type_wanted,
d9b3f62e
PA
10113 tempflag ? disp_del : disp_donttouch,
10114 thread, task, ignore_count, ops,
44f238bb 10115 from_tty, enabled, internal, flags);
c906108c 10116 }
0101ce28
JJ
10117 else
10118 {
0101ce28
JJ
10119 struct breakpoint *b;
10120
0101ce28
JJ
10121 make_cleanup (xfree, copy_arg);
10122
bfccc43c
YQ
10123 if (is_tracepoint_type (type_wanted))
10124 {
10125 struct tracepoint *t;
10126
10127 t = XCNEW (struct tracepoint);
10128 b = &t->base;
10129 }
10130 else
10131 b = XNEW (struct breakpoint);
10132
10133 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10134
f8eba3c6 10135 b->addr_string = copy_arg;
f6de8ec2 10136 if (parse_arg)
e12c7713
MK
10137 b->cond_string = NULL;
10138 else
10139 {
10140 /* Create a private copy of condition string. */
10141 if (cond_string)
10142 {
10143 cond_string = xstrdup (cond_string);
10144 make_cleanup (xfree, cond_string);
10145 }
10146 b->cond_string = cond_string;
10147 }
e7e0cddf 10148 b->extra_string = NULL;
0101ce28 10149 b->ignore_count = ignore_count;
0101ce28 10150 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 10151 b->condition_not_parsed = 1;
41447f92 10152 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
10153 if ((type_wanted != bp_breakpoint
10154 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 10155 b->pspace = current_program_space;
8bea4e01 10156
bfccc43c 10157 install_breakpoint (internal, b, 0);
0101ce28
JJ
10158 }
10159
f8eba3c6 10160 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 10161 {
3e43a32a
MS
10162 warning (_("Multiple breakpoints were set.\nUse the "
10163 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10164 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10165 }
10166
80c99de1
PA
10167 /* That's it. Discard the cleanups for data inserted into the
10168 breakpoint. */
10169 discard_cleanups (bkpt_chain);
10170 /* But cleanup everything else. */
c3f6f71d 10171 do_cleanups (old_chain);
217dc9e2 10172
80c99de1 10173 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 10174 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
10175
10176 return 1;
c3f6f71d 10177}
c906108c 10178
348d480f 10179/* Set a breakpoint.
72b2ff0e
VP
10180 ARG is a string describing breakpoint address,
10181 condition, and thread.
10182 FLAG specifies if a breakpoint is hardware on,
10183 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10184 and BP_TEMPFLAG. */
348d480f 10185
98deb0da 10186static void
72b2ff0e 10187break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10188{
72b2ff0e 10189 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10190 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10191 ? bp_hardware_breakpoint
10192 : bp_breakpoint);
55aa24fb
SDJ
10193 struct breakpoint_ops *ops;
10194 const char *arg_cp = arg;
10195
10196 /* Matching breakpoints on probes. */
10197 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10198 ops = &bkpt_probe_breakpoint_ops;
10199 else
10200 ops = &bkpt_breakpoint_ops;
c3f6f71d 10201
8cdf0e15
VP
10202 create_breakpoint (get_current_arch (),
10203 arg,
e7e0cddf 10204 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10205 tempflag, type_wanted,
8cdf0e15
VP
10206 0 /* Ignore count */,
10207 pending_break_support,
55aa24fb 10208 ops,
8cdf0e15 10209 from_tty,
84f4c1fe 10210 1 /* enabled */,
44f238bb
PA
10211 0 /* internal */,
10212 0);
c906108c
SS
10213}
10214
c906108c
SS
10215/* Helper function for break_command_1 and disassemble_command. */
10216
10217void
fba45db2 10218resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10219{
10220 CORE_ADDR pc;
10221
10222 if (sal->pc == 0 && sal->symtab != NULL)
10223 {
10224 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10225 error (_("No line %d in file \"%s\"."),
05cba821 10226 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10227 sal->pc = pc;
6a048695 10228
4a64f543
MS
10229 /* If this SAL corresponds to a breakpoint inserted using a line
10230 number, then skip the function prologue if necessary. */
6a048695 10231 if (sal->explicit_line)
059acae7 10232 skip_prologue_sal (sal);
c906108c
SS
10233 }
10234
10235 if (sal->section == 0 && sal->symtab != NULL)
10236 {
346d1dfe 10237 const struct blockvector *bv;
3977b71f 10238 const struct block *b;
c5aa993b 10239 struct symbol *sym;
c906108c 10240
801e3a5b 10241 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
10242 if (bv != NULL)
10243 {
7f0df278 10244 sym = block_linkage_function (b);
c906108c
SS
10245 if (sym != NULL)
10246 {
10247 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 10248 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
10249 }
10250 else
10251 {
4a64f543
MS
10252 /* It really is worthwhile to have the section, so we'll
10253 just have to look harder. This case can be executed
10254 if we have line numbers but no functions (as can
10255 happen in assembly source). */
c906108c 10256
7cbd4a93 10257 struct bound_minimal_symbol msym;
6c95b8df
PA
10258 struct cleanup *old_chain = save_current_space_and_thread ();
10259
10260 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10261
10262 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10263 if (msym.minsym)
efd66ac6 10264 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10265
10266 do_cleanups (old_chain);
c906108c
SS
10267 }
10268 }
10269 }
10270}
10271
10272void
fba45db2 10273break_command (char *arg, int from_tty)
c906108c 10274{
db107f19 10275 break_command_1 (arg, 0, from_tty);
c906108c
SS
10276}
10277
c906108c 10278void
fba45db2 10279tbreak_command (char *arg, int from_tty)
c906108c 10280{
db107f19 10281 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10282}
10283
c906108c 10284static void
fba45db2 10285hbreak_command (char *arg, int from_tty)
c906108c 10286{
db107f19 10287 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10288}
10289
10290static void
fba45db2 10291thbreak_command (char *arg, int from_tty)
c906108c 10292{
db107f19 10293 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10294}
10295
10296static void
fba45db2 10297stop_command (char *arg, int from_tty)
c906108c 10298{
a3f17187 10299 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10300Usage: stop in <function | address>\n\
a3f17187 10301 stop at <line>\n"));
c906108c
SS
10302}
10303
10304static void
fba45db2 10305stopin_command (char *arg, int from_tty)
c906108c
SS
10306{
10307 int badInput = 0;
10308
c5aa993b 10309 if (arg == (char *) NULL)
c906108c
SS
10310 badInput = 1;
10311 else if (*arg != '*')
10312 {
10313 char *argptr = arg;
10314 int hasColon = 0;
10315
4a64f543 10316 /* Look for a ':'. If this is a line number specification, then
53a5351d 10317 say it is bad, otherwise, it should be an address or
4a64f543 10318 function/method name. */
c906108c 10319 while (*argptr && !hasColon)
c5aa993b
JM
10320 {
10321 hasColon = (*argptr == ':');
10322 argptr++;
10323 }
c906108c
SS
10324
10325 if (hasColon)
c5aa993b 10326 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10327 else
c5aa993b 10328 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10329 }
10330
10331 if (badInput)
a3f17187 10332 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10333 else
db107f19 10334 break_command_1 (arg, 0, from_tty);
c906108c
SS
10335}
10336
10337static void
fba45db2 10338stopat_command (char *arg, int from_tty)
c906108c
SS
10339{
10340 int badInput = 0;
10341
c5aa993b 10342 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10343 badInput = 1;
10344 else
10345 {
10346 char *argptr = arg;
10347 int hasColon = 0;
10348
4a64f543
MS
10349 /* Look for a ':'. If there is a '::' then get out, otherwise
10350 it is probably a line number. */
c906108c 10351 while (*argptr && !hasColon)
c5aa993b
JM
10352 {
10353 hasColon = (*argptr == ':');
10354 argptr++;
10355 }
c906108c
SS
10356
10357 if (hasColon)
c5aa993b 10358 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10359 else
c5aa993b 10360 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10361 }
10362
10363 if (badInput)
a3f17187 10364 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10365 else
db107f19 10366 break_command_1 (arg, 0, from_tty);
c906108c
SS
10367}
10368
e7e0cddf
SS
10369/* The dynamic printf command is mostly like a regular breakpoint, but
10370 with a prewired command list consisting of a single output command,
10371 built from extra arguments supplied on the dprintf command
10372 line. */
10373
da821c7b 10374static void
e7e0cddf
SS
10375dprintf_command (char *arg, int from_tty)
10376{
10377 create_breakpoint (get_current_arch (),
10378 arg,
10379 NULL, 0, NULL, 1 /* parse arg */,
10380 0, bp_dprintf,
10381 0 /* Ignore count */,
10382 pending_break_support,
10383 &dprintf_breakpoint_ops,
10384 from_tty,
10385 1 /* enabled */,
10386 0 /* internal */,
10387 0);
10388}
10389
d3ce09f5
SS
10390static void
10391agent_printf_command (char *arg, int from_tty)
10392{
10393 error (_("May only run agent-printf on the target"));
10394}
10395
f1310107
TJB
10396/* Implement the "breakpoint_hit" breakpoint_ops method for
10397 ranged breakpoints. */
10398
10399static int
10400breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10401 struct address_space *aspace,
09ac7c10
TT
10402 CORE_ADDR bp_addr,
10403 const struct target_waitstatus *ws)
f1310107 10404{
09ac7c10 10405 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10406 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10407 return 0;
10408
f1310107
TJB
10409 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10410 bl->length, aspace, bp_addr);
10411}
10412
10413/* Implement the "resources_needed" breakpoint_ops method for
10414 ranged breakpoints. */
10415
10416static int
10417resources_needed_ranged_breakpoint (const struct bp_location *bl)
10418{
10419 return target_ranged_break_num_registers ();
10420}
10421
10422/* Implement the "print_it" breakpoint_ops method for
10423 ranged breakpoints. */
10424
10425static enum print_stop_action
348d480f 10426print_it_ranged_breakpoint (bpstat bs)
f1310107 10427{
348d480f 10428 struct breakpoint *b = bs->breakpoint_at;
f1310107 10429 struct bp_location *bl = b->loc;
79a45e25 10430 struct ui_out *uiout = current_uiout;
f1310107
TJB
10431
10432 gdb_assert (b->type == bp_hardware_breakpoint);
10433
10434 /* Ranged breakpoints have only one location. */
10435 gdb_assert (bl && bl->next == NULL);
10436
10437 annotate_breakpoint (b->number);
10438 if (b->disposition == disp_del)
10439 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10440 else
10441 ui_out_text (uiout, "\nRanged breakpoint ");
10442 if (ui_out_is_mi_like_p (uiout))
10443 {
10444 ui_out_field_string (uiout, "reason",
10445 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10446 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10447 }
10448 ui_out_field_int (uiout, "bkptno", b->number);
10449 ui_out_text (uiout, ", ");
10450
10451 return PRINT_SRC_AND_LOC;
10452}
10453
10454/* Implement the "print_one" breakpoint_ops method for
10455 ranged breakpoints. */
10456
10457static void
10458print_one_ranged_breakpoint (struct breakpoint *b,
10459 struct bp_location **last_loc)
10460{
10461 struct bp_location *bl = b->loc;
10462 struct value_print_options opts;
79a45e25 10463 struct ui_out *uiout = current_uiout;
f1310107
TJB
10464
10465 /* Ranged breakpoints have only one location. */
10466 gdb_assert (bl && bl->next == NULL);
10467
10468 get_user_print_options (&opts);
10469
10470 if (opts.addressprint)
10471 /* We don't print the address range here, it will be printed later
10472 by print_one_detail_ranged_breakpoint. */
10473 ui_out_field_skip (uiout, "addr");
10474 annotate_field (5);
10475 print_breakpoint_location (b, bl);
10476 *last_loc = bl;
10477}
10478
10479/* Implement the "print_one_detail" breakpoint_ops method for
10480 ranged breakpoints. */
10481
10482static void
10483print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10484 struct ui_out *uiout)
10485{
10486 CORE_ADDR address_start, address_end;
10487 struct bp_location *bl = b->loc;
f99d8bf4
PA
10488 struct ui_file *stb = mem_fileopen ();
10489 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10490
10491 gdb_assert (bl);
10492
10493 address_start = bl->address;
10494 address_end = address_start + bl->length - 1;
10495
10496 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10497 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10498 print_core_address (bl->gdbarch, address_start),
10499 print_core_address (bl->gdbarch, address_end));
10500 ui_out_field_stream (uiout, "addr", stb);
10501 ui_out_text (uiout, "\n");
10502
10503 do_cleanups (cleanup);
10504}
10505
10506/* Implement the "print_mention" breakpoint_ops method for
10507 ranged breakpoints. */
10508
10509static void
10510print_mention_ranged_breakpoint (struct breakpoint *b)
10511{
10512 struct bp_location *bl = b->loc;
79a45e25 10513 struct ui_out *uiout = current_uiout;
f1310107
TJB
10514
10515 gdb_assert (bl);
10516 gdb_assert (b->type == bp_hardware_breakpoint);
10517
10518 if (ui_out_is_mi_like_p (uiout))
10519 return;
10520
10521 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10522 b->number, paddress (bl->gdbarch, bl->address),
10523 paddress (bl->gdbarch, bl->address + bl->length - 1));
10524}
10525
10526/* Implement the "print_recreate" breakpoint_ops method for
10527 ranged breakpoints. */
10528
10529static void
10530print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10531{
10532 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10533 b->addr_string_range_end);
d9b3f62e 10534 print_recreate_thread (b, fp);
f1310107
TJB
10535}
10536
10537/* The breakpoint_ops structure to be used in ranged breakpoints. */
10538
2060206e 10539static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10540
10541/* Find the address where the end of the breakpoint range should be
10542 placed, given the SAL of the end of the range. This is so that if
10543 the user provides a line number, the end of the range is set to the
10544 last instruction of the given line. */
10545
10546static CORE_ADDR
10547find_breakpoint_range_end (struct symtab_and_line sal)
10548{
10549 CORE_ADDR end;
10550
10551 /* If the user provided a PC value, use it. Otherwise,
10552 find the address of the end of the given location. */
10553 if (sal.explicit_pc)
10554 end = sal.pc;
10555 else
10556 {
10557 int ret;
10558 CORE_ADDR start;
10559
10560 ret = find_line_pc_range (sal, &start, &end);
10561 if (!ret)
10562 error (_("Could not find location of the end of the range."));
10563
10564 /* find_line_pc_range returns the start of the next line. */
10565 end--;
10566 }
10567
10568 return end;
10569}
10570
10571/* Implement the "break-range" CLI command. */
10572
10573static void
10574break_range_command (char *arg, int from_tty)
10575{
10576 char *arg_start, *addr_string_start, *addr_string_end;
10577 struct linespec_result canonical_start, canonical_end;
10578 int bp_count, can_use_bp, length;
10579 CORE_ADDR end;
10580 struct breakpoint *b;
10581 struct symtab_and_line sal_start, sal_end;
f1310107 10582 struct cleanup *cleanup_bkpt;
f8eba3c6 10583 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10584
10585 /* We don't support software ranged breakpoints. */
10586 if (target_ranged_break_num_registers () < 0)
10587 error (_("This target does not support hardware ranged breakpoints."));
10588
10589 bp_count = hw_breakpoint_used_count ();
10590 bp_count += target_ranged_break_num_registers ();
10591 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10592 bp_count, 0);
10593 if (can_use_bp < 0)
10594 error (_("Hardware breakpoints used exceeds limit."));
10595
f8eba3c6 10596 arg = skip_spaces (arg);
f1310107
TJB
10597 if (arg == NULL || arg[0] == '\0')
10598 error(_("No address range specified."));
10599
f1310107
TJB
10600 init_linespec_result (&canonical_start);
10601
f8eba3c6
TT
10602 arg_start = arg;
10603 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10604
f8eba3c6 10605 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10606
10607 if (arg[0] != ',')
10608 error (_("Too few arguments."));
f8eba3c6 10609 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10610 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10611
10612 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10613
10614 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10615 || lsal_start->sals.nelts != 1)
f1310107
TJB
10616 error (_("Cannot create a ranged breakpoint with multiple locations."));
10617
f8eba3c6
TT
10618 sal_start = lsal_start->sals.sals[0];
10619 addr_string_start = savestring (arg_start, arg - arg_start);
10620 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10621
10622 arg++; /* Skip the comma. */
f8eba3c6 10623 arg = skip_spaces (arg);
f1310107
TJB
10624
10625 /* Parse the end location. */
10626
f1310107
TJB
10627 init_linespec_result (&canonical_end);
10628 arg_start = arg;
10629
f8eba3c6 10630 /* We call decode_line_full directly here instead of using
f1310107
TJB
10631 parse_breakpoint_sals because we need to specify the start location's
10632 symtab and line as the default symtab and line for the end of the
10633 range. This makes it possible to have ranges like "foo.c:27, +14",
10634 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10635 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10636 sal_start.symtab, sal_start.line,
10637 &canonical_end, NULL, NULL);
10638
10639 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10640
f8eba3c6 10641 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10642 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10643
10644 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10645 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10646 || lsal_end->sals.nelts != 1)
f1310107
TJB
10647 error (_("Cannot create a ranged breakpoint with multiple locations."));
10648
f8eba3c6
TT
10649 sal_end = lsal_end->sals.sals[0];
10650 addr_string_end = savestring (arg_start, arg - arg_start);
10651 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10652
10653 end = find_breakpoint_range_end (sal_end);
10654 if (sal_start.pc > end)
177b42fe 10655 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10656
10657 length = end - sal_start.pc + 1;
10658 if (length < 0)
10659 /* Length overflowed. */
10660 error (_("Address range too large."));
10661 else if (length == 1)
10662 {
10663 /* This range is simple enough to be handled by
10664 the `hbreak' command. */
10665 hbreak_command (addr_string_start, 1);
10666
10667 do_cleanups (cleanup_bkpt);
10668
10669 return;
10670 }
10671
10672 /* Now set up the breakpoint. */
10673 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10674 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10675 set_breakpoint_count (breakpoint_count + 1);
10676 b->number = breakpoint_count;
10677 b->disposition = disp_donttouch;
f8eba3c6
TT
10678 b->addr_string = xstrdup (addr_string_start);
10679 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10680 b->loc->length = length;
10681
f8eba3c6 10682 do_cleanups (cleanup_bkpt);
f1310107
TJB
10683
10684 mention (b);
8d3788bd 10685 observer_notify_breakpoint_created (b);
44702360 10686 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10687}
10688
4a64f543
MS
10689/* Return non-zero if EXP is verified as constant. Returned zero
10690 means EXP is variable. Also the constant detection may fail for
10691 some constant expressions and in such case still falsely return
10692 zero. */
2e6e3d9c 10693
65d79d4b
SDJ
10694static int
10695watchpoint_exp_is_const (const struct expression *exp)
10696{
10697 int i = exp->nelts;
10698
10699 while (i > 0)
10700 {
10701 int oplenp, argsp;
10702
10703 /* We are only interested in the descriptor of each element. */
10704 operator_length (exp, i, &oplenp, &argsp);
10705 i -= oplenp;
10706
10707 switch (exp->elts[i].opcode)
10708 {
10709 case BINOP_ADD:
10710 case BINOP_SUB:
10711 case BINOP_MUL:
10712 case BINOP_DIV:
10713 case BINOP_REM:
10714 case BINOP_MOD:
10715 case BINOP_LSH:
10716 case BINOP_RSH:
10717 case BINOP_LOGICAL_AND:
10718 case BINOP_LOGICAL_OR:
10719 case BINOP_BITWISE_AND:
10720 case BINOP_BITWISE_IOR:
10721 case BINOP_BITWISE_XOR:
10722 case BINOP_EQUAL:
10723 case BINOP_NOTEQUAL:
10724 case BINOP_LESS:
10725 case BINOP_GTR:
10726 case BINOP_LEQ:
10727 case BINOP_GEQ:
10728 case BINOP_REPEAT:
10729 case BINOP_COMMA:
10730 case BINOP_EXP:
10731 case BINOP_MIN:
10732 case BINOP_MAX:
10733 case BINOP_INTDIV:
10734 case BINOP_CONCAT:
65d79d4b
SDJ
10735 case TERNOP_COND:
10736 case TERNOP_SLICE:
65d79d4b
SDJ
10737
10738 case OP_LONG:
10739 case OP_DOUBLE:
10740 case OP_DECFLOAT:
10741 case OP_LAST:
10742 case OP_COMPLEX:
10743 case OP_STRING:
65d79d4b
SDJ
10744 case OP_ARRAY:
10745 case OP_TYPE:
608b4967
TT
10746 case OP_TYPEOF:
10747 case OP_DECLTYPE:
6e72ca20 10748 case OP_TYPEID:
65d79d4b
SDJ
10749 case OP_NAME:
10750 case OP_OBJC_NSSTRING:
10751
10752 case UNOP_NEG:
10753 case UNOP_LOGICAL_NOT:
10754 case UNOP_COMPLEMENT:
10755 case UNOP_ADDR:
10756 case UNOP_HIGH:
aeaa2474 10757 case UNOP_CAST:
9eaf6705
TT
10758
10759 case UNOP_CAST_TYPE:
10760 case UNOP_REINTERPRET_CAST:
10761 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10762 /* Unary, binary and ternary operators: We have to check
10763 their operands. If they are constant, then so is the
10764 result of that operation. For instance, if A and B are
10765 determined to be constants, then so is "A + B".
10766
10767 UNOP_IND is one exception to the rule above, because the
10768 value of *ADDR is not necessarily a constant, even when
10769 ADDR is. */
65d79d4b
SDJ
10770 break;
10771
10772 case OP_VAR_VALUE:
10773 /* Check whether the associated symbol is a constant.
4a64f543 10774
65d79d4b 10775 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10776 possible that a buggy compiler could mark a variable as
10777 constant even when it is not, and TYPE_CONST would return
10778 true in this case, while SYMBOL_CLASS wouldn't.
10779
10780 We also have to check for function symbols because they
10781 are always constant. */
65d79d4b
SDJ
10782 {
10783 struct symbol *s = exp->elts[i + 2].symbol;
10784
10785 if (SYMBOL_CLASS (s) != LOC_BLOCK
10786 && SYMBOL_CLASS (s) != LOC_CONST
10787 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10788 return 0;
10789 break;
10790 }
10791
10792 /* The default action is to return 0 because we are using
10793 the optimistic approach here: If we don't know something,
10794 then it is not a constant. */
10795 default:
10796 return 0;
10797 }
10798 }
10799
10800 return 1;
10801}
10802
3a5c3e22
PA
10803/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10804
10805static void
10806dtor_watchpoint (struct breakpoint *self)
10807{
10808 struct watchpoint *w = (struct watchpoint *) self;
10809
10810 xfree (w->cond_exp);
10811 xfree (w->exp);
10812 xfree (w->exp_string);
10813 xfree (w->exp_string_reparse);
10814 value_free (w->val);
10815
10816 base_breakpoint_ops.dtor (self);
10817}
10818
348d480f
PA
10819/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10820
10821static void
10822re_set_watchpoint (struct breakpoint *b)
10823{
3a5c3e22
PA
10824 struct watchpoint *w = (struct watchpoint *) b;
10825
348d480f
PA
10826 /* Watchpoint can be either on expression using entirely global
10827 variables, or it can be on local variables.
10828
10829 Watchpoints of the first kind are never auto-deleted, and even
10830 persist across program restarts. Since they can use variables
10831 from shared libraries, we need to reparse expression as libraries
10832 are loaded and unloaded.
10833
10834 Watchpoints on local variables can also change meaning as result
10835 of solib event. For example, if a watchpoint uses both a local
10836 and a global variables in expression, it's a local watchpoint,
10837 but unloading of a shared library will make the expression
10838 invalid. This is not a very common use case, but we still
10839 re-evaluate expression, to avoid surprises to the user.
10840
10841 Note that for local watchpoints, we re-evaluate it only if
10842 watchpoints frame id is still valid. If it's not, it means the
10843 watchpoint is out of scope and will be deleted soon. In fact,
10844 I'm not sure we'll ever be called in this case.
10845
10846 If a local watchpoint's frame id is still valid, then
3a5c3e22 10847 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10848
3a5c3e22
PA
10849 Don't do anything about disabled watchpoints, since they will be
10850 reevaluated again when enabled. */
10851 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10852}
10853
77b06cd7
TJB
10854/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10855
10856static int
10857insert_watchpoint (struct bp_location *bl)
10858{
3a5c3e22
PA
10859 struct watchpoint *w = (struct watchpoint *) bl->owner;
10860 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10861
10862 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10863 w->cond_exp);
77b06cd7
TJB
10864}
10865
10866/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10867
10868static int
10869remove_watchpoint (struct bp_location *bl)
10870{
3a5c3e22
PA
10871 struct watchpoint *w = (struct watchpoint *) bl->owner;
10872 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10873
10874 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10875 w->cond_exp);
e09342b5
TJB
10876}
10877
e09342b5 10878static int
348d480f 10879breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10880 struct address_space *aspace, CORE_ADDR bp_addr,
10881 const struct target_waitstatus *ws)
e09342b5 10882{
348d480f 10883 struct breakpoint *b = bl->owner;
3a5c3e22 10884 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10885
348d480f
PA
10886 /* Continuable hardware watchpoints are treated as non-existent if the
10887 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10888 some data address). Otherwise gdb won't stop on a break instruction
10889 in the code (not from a breakpoint) when a hardware watchpoint has
10890 been defined. Also skip watchpoints which we know did not trigger
10891 (did not match the data address). */
10892 if (is_hardware_watchpoint (b)
3a5c3e22 10893 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10894 return 0;
9c06b0b4 10895
348d480f 10896 return 1;
9c06b0b4
TJB
10897}
10898
348d480f
PA
10899static void
10900check_status_watchpoint (bpstat bs)
9c06b0b4 10901{
348d480f 10902 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10903
348d480f 10904 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10905}
10906
10907/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10908 hardware watchpoints. */
9c06b0b4
TJB
10909
10910static int
348d480f 10911resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10912{
3a5c3e22
PA
10913 struct watchpoint *w = (struct watchpoint *) bl->owner;
10914 int length = w->exact? 1 : bl->length;
348d480f
PA
10915
10916 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10917}
10918
10919/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10920 hardware watchpoints. */
9c06b0b4
TJB
10921
10922static int
348d480f 10923works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10924{
efa80663
PA
10925 /* Read and access watchpoints only work with hardware support. */
10926 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10927}
10928
9c06b0b4 10929static enum print_stop_action
348d480f 10930print_it_watchpoint (bpstat bs)
9c06b0b4 10931{
348d480f
PA
10932 struct cleanup *old_chain;
10933 struct breakpoint *b;
f99d8bf4 10934 struct ui_file *stb;
348d480f 10935 enum print_stop_action result;
3a5c3e22 10936 struct watchpoint *w;
79a45e25 10937 struct ui_out *uiout = current_uiout;
348d480f
PA
10938
10939 gdb_assert (bs->bp_location_at != NULL);
10940
348d480f 10941 b = bs->breakpoint_at;
3a5c3e22 10942 w = (struct watchpoint *) b;
348d480f 10943
f99d8bf4
PA
10944 stb = mem_fileopen ();
10945 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10946
10947 switch (b->type)
10948 {
348d480f 10949 case bp_watchpoint:
9c06b0b4
TJB
10950 case bp_hardware_watchpoint:
10951 annotate_watchpoint (b->number);
10952 if (ui_out_is_mi_like_p (uiout))
10953 ui_out_field_string
10954 (uiout, "reason",
10955 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10956 mention (b);
10957 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10958 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10959 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10960 ui_out_field_stream (uiout, "old", stb);
10961 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10962 watchpoint_value_print (w->val, stb);
348d480f
PA
10963 ui_out_field_stream (uiout, "new", stb);
10964 ui_out_text (uiout, "\n");
10965 /* More than one watchpoint may have been triggered. */
10966 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10967 break;
10968
10969 case bp_read_watchpoint:
10970 if (ui_out_is_mi_like_p (uiout))
10971 ui_out_field_string
10972 (uiout, "reason",
10973 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10974 mention (b);
10975 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10976 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10977 watchpoint_value_print (w->val, stb);
348d480f
PA
10978 ui_out_field_stream (uiout, "value", stb);
10979 ui_out_text (uiout, "\n");
10980 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10981 break;
10982
10983 case bp_access_watchpoint:
348d480f
PA
10984 if (bs->old_val != NULL)
10985 {
10986 annotate_watchpoint (b->number);
10987 if (ui_out_is_mi_like_p (uiout))
10988 ui_out_field_string
10989 (uiout, "reason",
10990 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10991 mention (b);
10992 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10993 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10994 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10995 ui_out_field_stream (uiout, "old", stb);
10996 ui_out_text (uiout, "\nNew value = ");
10997 }
10998 else
10999 {
11000 mention (b);
11001 if (ui_out_is_mi_like_p (uiout))
11002 ui_out_field_string
11003 (uiout, "reason",
11004 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11005 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11006 ui_out_text (uiout, "\nValue = ");
11007 }
f99d8bf4 11008 watchpoint_value_print (w->val, stb);
348d480f
PA
11009 ui_out_field_stream (uiout, "new", stb);
11010 ui_out_text (uiout, "\n");
11011 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11012 break;
11013 default:
348d480f 11014 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11015 }
11016
348d480f
PA
11017 do_cleanups (old_chain);
11018 return result;
11019}
11020
11021/* Implement the "print_mention" breakpoint_ops method for hardware
11022 watchpoints. */
11023
11024static void
11025print_mention_watchpoint (struct breakpoint *b)
11026{
11027 struct cleanup *ui_out_chain;
3a5c3e22 11028 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11029 struct ui_out *uiout = current_uiout;
348d480f
PA
11030
11031 switch (b->type)
11032 {
11033 case bp_watchpoint:
11034 ui_out_text (uiout, "Watchpoint ");
11035 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11036 break;
11037 case bp_hardware_watchpoint:
11038 ui_out_text (uiout, "Hardware watchpoint ");
11039 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11040 break;
11041 case bp_read_watchpoint:
11042 ui_out_text (uiout, "Hardware read watchpoint ");
11043 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11044 break;
11045 case bp_access_watchpoint:
11046 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11047 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11048 break;
11049 default:
11050 internal_error (__FILE__, __LINE__,
11051 _("Invalid hardware watchpoint type."));
11052 }
11053
11054 ui_out_field_int (uiout, "number", b->number);
11055 ui_out_text (uiout, ": ");
3a5c3e22 11056 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
11057 do_cleanups (ui_out_chain);
11058}
11059
11060/* Implement the "print_recreate" breakpoint_ops method for
11061 watchpoints. */
11062
11063static void
11064print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11065{
3a5c3e22
PA
11066 struct watchpoint *w = (struct watchpoint *) b;
11067
348d480f
PA
11068 switch (b->type)
11069 {
11070 case bp_watchpoint:
11071 case bp_hardware_watchpoint:
11072 fprintf_unfiltered (fp, "watch");
11073 break;
11074 case bp_read_watchpoint:
11075 fprintf_unfiltered (fp, "rwatch");
11076 break;
11077 case bp_access_watchpoint:
11078 fprintf_unfiltered (fp, "awatch");
11079 break;
11080 default:
11081 internal_error (__FILE__, __LINE__,
11082 _("Invalid watchpoint type."));
11083 }
11084
3a5c3e22 11085 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 11086 print_recreate_thread (b, fp);
348d480f
PA
11087}
11088
427cd150
TT
11089/* Implement the "explains_signal" breakpoint_ops method for
11090 watchpoints. */
11091
47591c29 11092static int
427cd150
TT
11093explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11094{
11095 /* A software watchpoint cannot cause a signal other than
11096 GDB_SIGNAL_TRAP. */
11097 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 11098 return 0;
427cd150 11099
47591c29 11100 return 1;
427cd150
TT
11101}
11102
348d480f
PA
11103/* The breakpoint_ops structure to be used in hardware watchpoints. */
11104
2060206e 11105static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
11106
11107/* Implement the "insert" breakpoint_ops method for
11108 masked hardware watchpoints. */
11109
11110static int
11111insert_masked_watchpoint (struct bp_location *bl)
11112{
3a5c3e22
PA
11113 struct watchpoint *w = (struct watchpoint *) bl->owner;
11114
11115 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11116 bl->watchpoint_type);
11117}
11118
11119/* Implement the "remove" breakpoint_ops method for
11120 masked hardware watchpoints. */
11121
11122static int
11123remove_masked_watchpoint (struct bp_location *bl)
11124{
3a5c3e22
PA
11125 struct watchpoint *w = (struct watchpoint *) bl->owner;
11126
11127 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11128 bl->watchpoint_type);
11129}
11130
11131/* Implement the "resources_needed" breakpoint_ops method for
11132 masked hardware watchpoints. */
11133
11134static int
11135resources_needed_masked_watchpoint (const struct bp_location *bl)
11136{
3a5c3e22
PA
11137 struct watchpoint *w = (struct watchpoint *) bl->owner;
11138
11139 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
11140}
11141
11142/* Implement the "works_in_software_mode" breakpoint_ops method for
11143 masked hardware watchpoints. */
11144
11145static int
11146works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11147{
11148 return 0;
11149}
11150
11151/* Implement the "print_it" breakpoint_ops method for
11152 masked hardware watchpoints. */
11153
11154static enum print_stop_action
11155print_it_masked_watchpoint (bpstat bs)
11156{
11157 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11158 struct ui_out *uiout = current_uiout;
348d480f
PA
11159
11160 /* Masked watchpoints have only one location. */
11161 gdb_assert (b->loc && b->loc->next == NULL);
11162
11163 switch (b->type)
11164 {
11165 case bp_hardware_watchpoint:
11166 annotate_watchpoint (b->number);
11167 if (ui_out_is_mi_like_p (uiout))
11168 ui_out_field_string
11169 (uiout, "reason",
11170 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11171 break;
11172
11173 case bp_read_watchpoint:
11174 if (ui_out_is_mi_like_p (uiout))
11175 ui_out_field_string
11176 (uiout, "reason",
11177 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11178 break;
11179
11180 case bp_access_watchpoint:
11181 if (ui_out_is_mi_like_p (uiout))
11182 ui_out_field_string
11183 (uiout, "reason",
11184 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11185 break;
11186 default:
11187 internal_error (__FILE__, __LINE__,
11188 _("Invalid hardware watchpoint type."));
11189 }
11190
11191 mention (b);
9c06b0b4
TJB
11192 ui_out_text (uiout, _("\n\
11193Check the underlying instruction at PC for the memory\n\
11194address and value which triggered this watchpoint.\n"));
11195 ui_out_text (uiout, "\n");
11196
11197 /* More than one watchpoint may have been triggered. */
11198 return PRINT_UNKNOWN;
11199}
11200
11201/* Implement the "print_one_detail" breakpoint_ops method for
11202 masked hardware watchpoints. */
11203
11204static void
11205print_one_detail_masked_watchpoint (const struct breakpoint *b,
11206 struct ui_out *uiout)
11207{
3a5c3e22
PA
11208 struct watchpoint *w = (struct watchpoint *) b;
11209
9c06b0b4
TJB
11210 /* Masked watchpoints have only one location. */
11211 gdb_assert (b->loc && b->loc->next == NULL);
11212
11213 ui_out_text (uiout, "\tmask ");
3a5c3e22 11214 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11215 ui_out_text (uiout, "\n");
11216}
11217
11218/* Implement the "print_mention" breakpoint_ops method for
11219 masked hardware watchpoints. */
11220
11221static void
11222print_mention_masked_watchpoint (struct breakpoint *b)
11223{
3a5c3e22 11224 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11225 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11226 struct cleanup *ui_out_chain;
11227
11228 switch (b->type)
11229 {
11230 case bp_hardware_watchpoint:
11231 ui_out_text (uiout, "Masked hardware watchpoint ");
11232 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11233 break;
11234 case bp_read_watchpoint:
11235 ui_out_text (uiout, "Masked hardware read watchpoint ");
11236 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11237 break;
11238 case bp_access_watchpoint:
11239 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11240 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11241 break;
11242 default:
11243 internal_error (__FILE__, __LINE__,
11244 _("Invalid hardware watchpoint type."));
11245 }
11246
11247 ui_out_field_int (uiout, "number", b->number);
11248 ui_out_text (uiout, ": ");
3a5c3e22 11249 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11250 do_cleanups (ui_out_chain);
11251}
11252
11253/* Implement the "print_recreate" breakpoint_ops method for
11254 masked hardware watchpoints. */
11255
11256static void
11257print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11258{
3a5c3e22 11259 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11260 char tmp[40];
11261
11262 switch (b->type)
11263 {
11264 case bp_hardware_watchpoint:
11265 fprintf_unfiltered (fp, "watch");
11266 break;
11267 case bp_read_watchpoint:
11268 fprintf_unfiltered (fp, "rwatch");
11269 break;
11270 case bp_access_watchpoint:
11271 fprintf_unfiltered (fp, "awatch");
11272 break;
11273 default:
11274 internal_error (__FILE__, __LINE__,
11275 _("Invalid hardware watchpoint type."));
11276 }
11277
3a5c3e22
PA
11278 sprintf_vma (tmp, w->hw_wp_mask);
11279 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11280 print_recreate_thread (b, fp);
9c06b0b4
TJB
11281}
11282
11283/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11284
2060206e 11285static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11286
11287/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11288
11289static int
11290is_masked_watchpoint (const struct breakpoint *b)
11291{
11292 return b->ops == &masked_watchpoint_breakpoint_ops;
11293}
11294
53a5351d
JM
11295/* accessflag: hw_write: watch write,
11296 hw_read: watch read,
11297 hw_access: watch access (read or write) */
c906108c 11298static void
bbc13ae3 11299watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11300 int just_location, int internal)
c906108c 11301{
a9634178 11302 volatile struct gdb_exception e;
d983da9c 11303 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11304 struct expression *exp;
270140bd 11305 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11306 struct value *val, *mark, *result;
bb9d5f81 11307 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11308 struct frame_info *frame;
bbc13ae3
KS
11309 const char *exp_start = NULL;
11310 const char *exp_end = NULL;
11311 const char *tok, *end_tok;
9c06b0b4 11312 int toklen = -1;
bbc13ae3
KS
11313 const char *cond_start = NULL;
11314 const char *cond_end = NULL;
c906108c 11315 enum bptype bp_type;
37e4754d 11316 int thread = -1;
0cf6dd15 11317 int pc = 0;
9c06b0b4
TJB
11318 /* Flag to indicate whether we are going to use masks for
11319 the hardware watchpoint. */
11320 int use_mask = 0;
11321 CORE_ADDR mask = 0;
3a5c3e22 11322 struct watchpoint *w;
bbc13ae3
KS
11323 char *expression;
11324 struct cleanup *back_to;
c906108c 11325
37e4754d
LM
11326 /* Make sure that we actually have parameters to parse. */
11327 if (arg != NULL && arg[0] != '\0')
11328 {
bbc13ae3
KS
11329 const char *value_start;
11330
11331 exp_end = arg + strlen (arg);
37e4754d 11332
9c06b0b4
TJB
11333 /* Look for "parameter value" pairs at the end
11334 of the arguments string. */
bbc13ae3 11335 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11336 {
11337 /* Skip whitespace at the end of the argument list. */
11338 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11339 tok--;
11340
11341 /* Find the beginning of the last token.
11342 This is the value of the parameter. */
11343 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11344 tok--;
11345 value_start = tok + 1;
11346
11347 /* Skip whitespace. */
11348 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11349 tok--;
11350
11351 end_tok = tok;
11352
11353 /* Find the beginning of the second to last token.
11354 This is the parameter itself. */
11355 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11356 tok--;
11357 tok++;
11358 toklen = end_tok - tok + 1;
11359
11360 if (toklen == 6 && !strncmp (tok, "thread", 6))
11361 {
11362 /* At this point we've found a "thread" token, which means
11363 the user is trying to set a watchpoint that triggers
11364 only in a specific thread. */
11365 char *endp;
37e4754d 11366
9c06b0b4
TJB
11367 if (thread != -1)
11368 error(_("You can specify only one thread."));
37e4754d 11369
9c06b0b4
TJB
11370 /* Extract the thread ID from the next token. */
11371 thread = strtol (value_start, &endp, 0);
37e4754d 11372
9c06b0b4
TJB
11373 /* Check if the user provided a valid numeric value for the
11374 thread ID. */
11375 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11376 error (_("Invalid thread ID specification %s."), value_start);
11377
11378 /* Check if the thread actually exists. */
11379 if (!valid_thread_id (thread))
af4908ba 11380 invalid_thread_id_error (thread);
9c06b0b4
TJB
11381 }
11382 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11383 {
11384 /* We've found a "mask" token, which means the user wants to
11385 create a hardware watchpoint that is going to have the mask
11386 facility. */
11387 struct value *mask_value, *mark;
37e4754d 11388
9c06b0b4
TJB
11389 if (use_mask)
11390 error(_("You can specify only one mask."));
37e4754d 11391
9c06b0b4 11392 use_mask = just_location = 1;
37e4754d 11393
9c06b0b4
TJB
11394 mark = value_mark ();
11395 mask_value = parse_to_comma_and_eval (&value_start);
11396 mask = value_as_address (mask_value);
11397 value_free_to_mark (mark);
11398 }
11399 else
11400 /* We didn't recognize what we found. We should stop here. */
11401 break;
37e4754d 11402
9c06b0b4
TJB
11403 /* Truncate the string and get rid of the "parameter value" pair before
11404 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11405 exp_end = tok;
9c06b0b4 11406 }
37e4754d 11407 }
bbc13ae3
KS
11408 else
11409 exp_end = arg;
37e4754d 11410
bbc13ae3
KS
11411 /* Parse the rest of the arguments. From here on out, everything
11412 is in terms of a newly allocated string instead of the original
11413 ARG. */
c906108c 11414 innermost_block = NULL;
bbc13ae3
KS
11415 expression = savestring (arg, exp_end - arg);
11416 back_to = make_cleanup (xfree, expression);
11417 exp_start = arg = expression;
1bb9788d 11418 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11419 exp_end = arg;
fa8a61dc
TT
11420 /* Remove trailing whitespace from the expression before saving it.
11421 This makes the eventual display of the expression string a bit
11422 prettier. */
11423 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11424 --exp_end;
11425
65d79d4b
SDJ
11426 /* Checking if the expression is not constant. */
11427 if (watchpoint_exp_is_const (exp))
11428 {
11429 int len;
11430
11431 len = exp_end - exp_start;
11432 while (len > 0 && isspace (exp_start[len - 1]))
11433 len--;
11434 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11435 }
11436
c906108c
SS
11437 exp_valid_block = innermost_block;
11438 mark = value_mark ();
3a1115a0 11439 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11440
bb9d5f81
PP
11441 if (val != NULL && just_location)
11442 {
11443 saved_bitpos = value_bitpos (val);
11444 saved_bitsize = value_bitsize (val);
11445 }
11446
06a64a0b
TT
11447 if (just_location)
11448 {
9c06b0b4
TJB
11449 int ret;
11450
06a64a0b 11451 exp_valid_block = NULL;
a1442452 11452 val = value_addr (result);
06a64a0b
TT
11453 release_value (val);
11454 value_free_to_mark (mark);
9c06b0b4
TJB
11455
11456 if (use_mask)
11457 {
11458 ret = target_masked_watch_num_registers (value_as_address (val),
11459 mask);
11460 if (ret == -1)
11461 error (_("This target does not support masked watchpoints."));
11462 else if (ret == -2)
11463 error (_("Invalid mask or memory region."));
11464 }
06a64a0b
TT
11465 }
11466 else if (val != NULL)
fa4727a6 11467 release_value (val);
c906108c 11468
bbc13ae3
KS
11469 tok = skip_spaces_const (arg);
11470 end_tok = skip_to_space_const (tok);
c906108c
SS
11471
11472 toklen = end_tok - tok;
11473 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11474 {
2d134ed3
PA
11475 struct expression *cond;
11476
60e1c644 11477 innermost_block = NULL;
c906108c 11478 tok = cond_start = end_tok + 1;
1bb9788d 11479 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11480
11481 /* The watchpoint expression may not be local, but the condition
11482 may still be. E.g.: `watch global if local > 0'. */
11483 cond_exp_valid_block = innermost_block;
11484
2d134ed3 11485 xfree (cond);
c906108c
SS
11486 cond_end = tok;
11487 }
11488 if (*tok)
8a3fe4f8 11489 error (_("Junk at end of command."));
c906108c 11490
d983da9c 11491 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11492
11493 /* If the expression is "local", then set up a "watchpoint scope"
11494 breakpoint at the point where we've left the scope of the watchpoint
11495 expression. Create the scope breakpoint before the watchpoint, so
11496 that we will encounter it first in bpstat_stop_status. */
60e1c644 11497 if (exp_valid_block && frame)
d983da9c 11498 {
edb3359d
DJ
11499 if (frame_id_p (frame_unwind_caller_id (frame)))
11500 {
11501 scope_breakpoint
a6d9a66e
UW
11502 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11503 frame_unwind_caller_pc (frame),
06edf0c0
PA
11504 bp_watchpoint_scope,
11505 &momentary_breakpoint_ops);
d983da9c 11506
edb3359d 11507 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11508
edb3359d
DJ
11509 /* Automatically delete the breakpoint when it hits. */
11510 scope_breakpoint->disposition = disp_del;
d983da9c 11511
edb3359d
DJ
11512 /* Only break in the proper frame (help with recursion). */
11513 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11514
edb3359d 11515 /* Set the address at which we will stop. */
a6d9a66e
UW
11516 scope_breakpoint->loc->gdbarch
11517 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11518 scope_breakpoint->loc->requested_address
11519 = frame_unwind_caller_pc (frame);
11520 scope_breakpoint->loc->address
a6d9a66e
UW
11521 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11522 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11523 scope_breakpoint->type);
11524 }
d983da9c
DJ
11525 }
11526
e8369a73
AB
11527 /* Now set up the breakpoint. We create all watchpoints as hardware
11528 watchpoints here even if hardware watchpoints are turned off, a call
11529 to update_watchpoint later in this function will cause the type to
11530 drop back to bp_watchpoint (software watchpoint) if required. */
11531
11532 if (accessflag == hw_read)
11533 bp_type = bp_read_watchpoint;
11534 else if (accessflag == hw_access)
11535 bp_type = bp_access_watchpoint;
11536 else
11537 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11538
11539 w = XCNEW (struct watchpoint);
11540 b = &w->base;
348d480f 11541 if (use_mask)
3a5c3e22
PA
11542 init_raw_breakpoint_without_location (b, NULL, bp_type,
11543 &masked_watchpoint_breakpoint_ops);
348d480f 11544 else
3a5c3e22
PA
11545 init_raw_breakpoint_without_location (b, NULL, bp_type,
11546 &watchpoint_breakpoint_ops);
37e4754d 11547 b->thread = thread;
b5de0fa7 11548 b->disposition = disp_donttouch;
348d480f 11549 b->pspace = current_program_space;
3a5c3e22
PA
11550 w->exp = exp;
11551 w->exp_valid_block = exp_valid_block;
11552 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11553 if (just_location)
11554 {
11555 struct type *t = value_type (val);
11556 CORE_ADDR addr = value_as_address (val);
11557 char *name;
11558
11559 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11560 name = type_to_string (t);
11561
3a5c3e22 11562 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11563 core_addr_to_string (addr));
06a64a0b
TT
11564 xfree (name);
11565
3a5c3e22 11566 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11567 (int) (exp_end - exp_start), exp_start);
11568
06a64a0b
TT
11569 /* The above expression is in C. */
11570 b->language = language_c;
11571 }
11572 else
3a5c3e22 11573 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11574
11575 if (use_mask)
11576 {
3a5c3e22 11577 w->hw_wp_mask = mask;
9c06b0b4
TJB
11578 }
11579 else
11580 {
3a5c3e22 11581 w->val = val;
bb9d5f81
PP
11582 w->val_bitpos = saved_bitpos;
11583 w->val_bitsize = saved_bitsize;
3a5c3e22 11584 w->val_valid = 1;
9c06b0b4 11585 }
77b06cd7 11586
c906108c
SS
11587 if (cond_start)
11588 b->cond_string = savestring (cond_start, cond_end - cond_start);
11589 else
11590 b->cond_string = 0;
c5aa993b 11591
c906108c 11592 if (frame)
f6bc2008 11593 {
3a5c3e22
PA
11594 w->watchpoint_frame = get_frame_id (frame);
11595 w->watchpoint_thread = inferior_ptid;
f6bc2008 11596 }
c906108c 11597 else
f6bc2008 11598 {
3a5c3e22
PA
11599 w->watchpoint_frame = null_frame_id;
11600 w->watchpoint_thread = null_ptid;
f6bc2008 11601 }
c906108c 11602
d983da9c 11603 if (scope_breakpoint != NULL)
c906108c 11604 {
d983da9c
DJ
11605 /* The scope breakpoint is related to the watchpoint. We will
11606 need to act on them together. */
11607 b->related_breakpoint = scope_breakpoint;
11608 scope_breakpoint->related_breakpoint = b;
c906108c 11609 }
d983da9c 11610
06a64a0b
TT
11611 if (!just_location)
11612 value_free_to_mark (mark);
2d134ed3 11613
a9634178
TJB
11614 TRY_CATCH (e, RETURN_MASK_ALL)
11615 {
11616 /* Finally update the new watchpoint. This creates the locations
11617 that should be inserted. */
3a5c3e22 11618 update_watchpoint (w, 1);
a9634178
TJB
11619 }
11620 if (e.reason < 0)
11621 {
11622 delete_breakpoint (b);
11623 throw_exception (e);
11624 }
11625
3ea46bff 11626 install_breakpoint (internal, b, 1);
bbc13ae3 11627 do_cleanups (back_to);
c906108c
SS
11628}
11629
e09342b5 11630/* Return count of debug registers needed to watch the given expression.
e09342b5 11631 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11632
c906108c 11633static int
a9634178 11634can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11635{
11636 int found_memory_cnt = 0;
2e70b7b9 11637 struct value *head = v;
c906108c
SS
11638
11639 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11640 if (!can_use_hw_watchpoints)
c906108c 11641 return 0;
c5aa993b 11642
5c44784c
JM
11643 /* Make sure that the value of the expression depends only upon
11644 memory contents, and values computed from them within GDB. If we
11645 find any register references or function calls, we can't use a
11646 hardware watchpoint.
11647
11648 The idea here is that evaluating an expression generates a series
11649 of values, one holding the value of every subexpression. (The
11650 expression a*b+c has five subexpressions: a, b, a*b, c, and
11651 a*b+c.) GDB's values hold almost enough information to establish
11652 the criteria given above --- they identify memory lvalues,
11653 register lvalues, computed values, etcetera. So we can evaluate
11654 the expression, and then scan the chain of values that leaves
11655 behind to decide whether we can detect any possible change to the
11656 expression's final value using only hardware watchpoints.
11657
11658 However, I don't think that the values returned by inferior
11659 function calls are special in any way. So this function may not
11660 notice that an expression involving an inferior function call
11661 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11662 for (; v; v = value_next (v))
c906108c 11663 {
5c44784c 11664 if (VALUE_LVAL (v) == lval_memory)
c906108c 11665 {
8464be76
DJ
11666 if (v != head && value_lazy (v))
11667 /* A lazy memory lvalue in the chain is one that GDB never
11668 needed to fetch; we either just used its address (e.g.,
11669 `a' in `a.b') or we never needed it at all (e.g., `a'
11670 in `a,b'). This doesn't apply to HEAD; if that is
11671 lazy then it was not readable, but watch it anyway. */
5c44784c 11672 ;
53a5351d 11673 else
5c44784c
JM
11674 {
11675 /* Ahh, memory we actually used! Check if we can cover
11676 it with hardware watchpoints. */
df407dfe 11677 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11678
11679 /* We only watch structs and arrays if user asked for it
11680 explicitly, never if they just happen to appear in a
11681 middle of some value chain. */
11682 if (v == head
11683 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11684 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11685 {
42ae5230 11686 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11687 int len;
11688 int num_regs;
11689
a9634178 11690 len = (target_exact_watchpoints
e09342b5
TJB
11691 && is_scalar_type_recursive (vtype))?
11692 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11693
e09342b5
TJB
11694 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11695 if (!num_regs)
2e70b7b9
MS
11696 return 0;
11697 else
e09342b5 11698 found_memory_cnt += num_regs;
2e70b7b9 11699 }
5c44784c 11700 }
c5aa993b 11701 }
5086187c
AC
11702 else if (VALUE_LVAL (v) != not_lval
11703 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11704 return 0; /* These are values from the history (e.g., $1). */
5086187c 11705 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11706 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11707 }
11708
11709 /* The expression itself looks suitable for using a hardware
11710 watchpoint, but give the target machine a chance to reject it. */
11711 return found_memory_cnt;
11712}
11713
8b93c638 11714void
84f4c1fe 11715watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11716{
84f4c1fe 11717 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11718}
11719
06a64a0b
TT
11720/* A helper function that looks for the "-location" argument and then
11721 calls watch_command_1. */
11722
11723static void
11724watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11725{
11726 int just_location = 0;
11727
11728 if (arg
11729 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11730 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11731 {
e9cafbcc 11732 arg = skip_spaces (arg);
06a64a0b
TT
11733 just_location = 1;
11734 }
11735
84f4c1fe 11736 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11737}
8926118c 11738
c5aa993b 11739static void
fba45db2 11740watch_command (char *arg, int from_tty)
c906108c 11741{
06a64a0b 11742 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11743}
11744
8b93c638 11745void
84f4c1fe 11746rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11747{
84f4c1fe 11748 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11749}
8926118c 11750
c5aa993b 11751static void
fba45db2 11752rwatch_command (char *arg, int from_tty)
c906108c 11753{
06a64a0b 11754 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11755}
11756
8b93c638 11757void
84f4c1fe 11758awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11759{
84f4c1fe 11760 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11761}
8926118c 11762
c5aa993b 11763static void
fba45db2 11764awatch_command (char *arg, int from_tty)
c906108c 11765{
06a64a0b 11766 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11767}
c906108c 11768\f
c5aa993b 11769
43ff13b4 11770/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11771 because it uses the mechanisms of breakpoints. */
11772
bfec99b2
PA
11773struct until_break_command_continuation_args
11774{
11775 struct breakpoint *breakpoint;
11776 struct breakpoint *breakpoint2;
186c406b 11777 int thread_num;
bfec99b2
PA
11778};
11779
43ff13b4 11780/* This function is called by fetch_inferior_event via the
4a64f543 11781 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11782 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11783 command. */
c2c6d25f 11784static void
fa4cd53f 11785until_break_command_continuation (void *arg, int err)
43ff13b4 11786{
bfec99b2
PA
11787 struct until_break_command_continuation_args *a = arg;
11788
11789 delete_breakpoint (a->breakpoint);
11790 if (a->breakpoint2)
11791 delete_breakpoint (a->breakpoint2);
186c406b 11792 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11793}
11794
c906108c 11795void
ae66c1fc 11796until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11797{
11798 struct symtabs_and_lines sals;
11799 struct symtab_and_line sal;
8556afb4
PA
11800 struct frame_info *frame;
11801 struct gdbarch *frame_gdbarch;
11802 struct frame_id stack_frame_id;
11803 struct frame_id caller_frame_id;
c906108c 11804 struct breakpoint *breakpoint;
f107f563 11805 struct breakpoint *breakpoint2 = NULL;
c906108c 11806 struct cleanup *old_chain;
186c406b
TT
11807 int thread;
11808 struct thread_info *tp;
c906108c 11809
70509625 11810 clear_proceed_status (0);
c906108c
SS
11811
11812 /* Set a breakpoint where the user wants it and at return from
4a64f543 11813 this function. */
c5aa993b 11814
1bfeeb0f 11815 if (last_displayed_sal_is_valid ())
f8eba3c6 11816 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11817 get_last_displayed_symtab (),
f8eba3c6 11818 get_last_displayed_line ());
c906108c 11819 else
f8eba3c6
TT
11820 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11821 (struct symtab *) NULL, 0);
c5aa993b 11822
c906108c 11823 if (sals.nelts != 1)
8a3fe4f8 11824 error (_("Couldn't get information on specified line."));
c5aa993b 11825
c906108c 11826 sal = sals.sals[0];
4a64f543 11827 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11828
c906108c 11829 if (*arg)
8a3fe4f8 11830 error (_("Junk at end of arguments."));
c5aa993b 11831
c906108c 11832 resolve_sal_pc (&sal);
c5aa993b 11833
186c406b
TT
11834 tp = inferior_thread ();
11835 thread = tp->num;
11836
883bc8d1
PA
11837 old_chain = make_cleanup (null_cleanup, NULL);
11838
8556afb4
PA
11839 /* Note linespec handling above invalidates the frame chain.
11840 Installing a breakpoint also invalidates the frame chain (as it
11841 may need to switch threads), so do any frame handling before
11842 that. */
11843
11844 frame = get_selected_frame (NULL);
11845 frame_gdbarch = get_frame_arch (frame);
11846 stack_frame_id = get_stack_frame_id (frame);
11847 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11848
ae66c1fc
EZ
11849 /* Keep within the current frame, or in frames called by the current
11850 one. */
edb3359d 11851
883bc8d1 11852 if (frame_id_p (caller_frame_id))
c906108c 11853 {
883bc8d1
PA
11854 struct symtab_and_line sal2;
11855
11856 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11857 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11858 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11859 sal2,
11860 caller_frame_id,
f107f563
VP
11861 bp_until);
11862 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11863
883bc8d1 11864 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11865 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11866 }
c5aa993b 11867
c70a6932
JK
11868 /* set_momentary_breakpoint could invalidate FRAME. */
11869 frame = NULL;
11870
883bc8d1
PA
11871 if (anywhere)
11872 /* If the user told us to continue until a specified location,
11873 we don't specify a frame at which we need to stop. */
11874 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11875 null_frame_id, bp_until);
11876 else
11877 /* Otherwise, specify the selected frame, because we want to stop
11878 only at the very same frame. */
11879 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11880 stack_frame_id, bp_until);
11881 make_cleanup_delete_breakpoint (breakpoint);
11882
a493e3e2 11883 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11884
4a64f543
MS
11885 /* If we are running asynchronously, and proceed call above has
11886 actually managed to start the target, arrange for breakpoints to
11887 be deleted when the target stops. Otherwise, we're already
11888 stopped and delete breakpoints via cleanup chain. */
f107f563 11889
8ea051c5 11890 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11891 {
bfec99b2
PA
11892 struct until_break_command_continuation_args *args;
11893 args = xmalloc (sizeof (*args));
f107f563 11894
bfec99b2
PA
11895 args->breakpoint = breakpoint;
11896 args->breakpoint2 = breakpoint2;
186c406b 11897 args->thread_num = thread;
f107f563
VP
11898
11899 discard_cleanups (old_chain);
95e54da7
PA
11900 add_continuation (inferior_thread (),
11901 until_break_command_continuation, args,
604ead4a 11902 xfree);
f107f563
VP
11903 }
11904 else
c5aa993b 11905 do_cleanups (old_chain);
c906108c 11906}
ae66c1fc 11907
c906108c
SS
11908/* This function attempts to parse an optional "if <cond>" clause
11909 from the arg string. If one is not found, it returns NULL.
c5aa993b 11910
c906108c
SS
11911 Else, it returns a pointer to the condition string. (It does not
11912 attempt to evaluate the string against a particular block.) And,
11913 it updates arg to point to the first character following the parsed
4a64f543 11914 if clause in the arg string. */
53a5351d 11915
916703c0 11916char *
fba45db2 11917ep_parse_optional_if_clause (char **arg)
c906108c 11918{
c5aa993b
JM
11919 char *cond_string;
11920
11921 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11922 return NULL;
c5aa993b 11923
4a64f543 11924 /* Skip the "if" keyword. */
c906108c 11925 (*arg) += 2;
c5aa993b 11926
c906108c 11927 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11928 condition string. */
e9cafbcc 11929 *arg = skip_spaces (*arg);
c906108c 11930 cond_string = *arg;
c5aa993b 11931
4a64f543
MS
11932 /* Assume that the condition occupies the remainder of the arg
11933 string. */
c906108c 11934 (*arg) += strlen (cond_string);
c5aa993b 11935
c906108c
SS
11936 return cond_string;
11937}
c5aa993b 11938
c906108c
SS
11939/* Commands to deal with catching events, such as signals, exceptions,
11940 process start/exit, etc. */
c5aa993b
JM
11941
11942typedef enum
11943{
44feb3ce
TT
11944 catch_fork_temporary, catch_vfork_temporary,
11945 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11946}
11947catch_fork_kind;
11948
c906108c 11949static void
cc59ec59
MS
11950catch_fork_command_1 (char *arg, int from_tty,
11951 struct cmd_list_element *command)
c906108c 11952{
a6d9a66e 11953 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11954 char *cond_string = NULL;
44feb3ce
TT
11955 catch_fork_kind fork_kind;
11956 int tempflag;
11957
11958 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11959 tempflag = (fork_kind == catch_fork_temporary
11960 || fork_kind == catch_vfork_temporary);
c5aa993b 11961
44feb3ce
TT
11962 if (!arg)
11963 arg = "";
e9cafbcc 11964 arg = skip_spaces (arg);
c5aa993b 11965
c906108c 11966 /* The allowed syntax is:
c5aa993b
JM
11967 catch [v]fork
11968 catch [v]fork if <cond>
11969
4a64f543 11970 First, check if there's an if clause. */
c906108c 11971 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11972
c906108c 11973 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11974 error (_("Junk at end of arguments."));
c5aa993b 11975
c906108c 11976 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11977 and enable reporting of such events. */
c5aa993b
JM
11978 switch (fork_kind)
11979 {
44feb3ce
TT
11980 case catch_fork_temporary:
11981 case catch_fork_permanent:
a6d9a66e 11982 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11983 &catch_fork_breakpoint_ops);
c906108c 11984 break;
44feb3ce
TT
11985 case catch_vfork_temporary:
11986 case catch_vfork_permanent:
a6d9a66e 11987 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11988 &catch_vfork_breakpoint_ops);
c906108c 11989 break;
c5aa993b 11990 default:
8a3fe4f8 11991 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11992 break;
c5aa993b 11993 }
c906108c
SS
11994}
11995
11996static void
cc59ec59
MS
11997catch_exec_command_1 (char *arg, int from_tty,
11998 struct cmd_list_element *command)
c906108c 11999{
b4d90040 12000 struct exec_catchpoint *c;
a6d9a66e 12001 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 12002 int tempflag;
c5aa993b 12003 char *cond_string = NULL;
c906108c 12004
44feb3ce
TT
12005 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12006
12007 if (!arg)
12008 arg = "";
e9cafbcc 12009 arg = skip_spaces (arg);
c906108c
SS
12010
12011 /* The allowed syntax is:
c5aa993b
JM
12012 catch exec
12013 catch exec if <cond>
c906108c 12014
4a64f543 12015 First, check if there's an if clause. */
c906108c
SS
12016 cond_string = ep_parse_optional_if_clause (&arg);
12017
12018 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12019 error (_("Junk at end of arguments."));
c906108c 12020
b4d90040
PA
12021 c = XNEW (struct exec_catchpoint);
12022 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
12023 &catch_exec_breakpoint_ops);
12024 c->exec_pathname = NULL;
12025
3ea46bff 12026 install_breakpoint (0, &c->base, 1);
c906108c 12027}
c5aa993b 12028
9ac4176b 12029void
28010a5d
PA
12030init_ada_exception_breakpoint (struct breakpoint *b,
12031 struct gdbarch *gdbarch,
12032 struct symtab_and_line sal,
12033 char *addr_string,
c0a91b2b 12034 const struct breakpoint_ops *ops,
28010a5d 12035 int tempflag,
349774ef 12036 int enabled,
28010a5d 12037 int from_tty)
f7f9143b 12038{
f7f9143b
JB
12039 if (from_tty)
12040 {
5af949e3
UW
12041 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12042 if (!loc_gdbarch)
12043 loc_gdbarch = gdbarch;
12044
6c95b8df
PA
12045 describe_other_breakpoints (loc_gdbarch,
12046 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
12047 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12048 version for exception catchpoints, because two catchpoints
12049 used for different exception names will use the same address.
12050 In this case, a "breakpoint ... also set at..." warning is
4a64f543 12051 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 12052 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
12053 the user what type of catchpoint it is. The above is good
12054 enough for now, though. */
12055 }
12056
28010a5d 12057 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 12058
349774ef 12059 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 12060 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
12061 b->addr_string = addr_string;
12062 b->language = language_ada;
f7f9143b
JB
12063}
12064
a96d9b2e
SDJ
12065/* Splits the argument using space as delimiter. Returns an xmalloc'd
12066 filter list, or NULL if no filtering is required. */
12067static VEC(int) *
12068catch_syscall_split_args (char *arg)
12069{
12070 VEC(int) *result = NULL;
29d0bb3d 12071 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
12072
12073 while (*arg != '\0')
12074 {
12075 int i, syscall_number;
12076 char *endptr;
12077 char cur_name[128];
12078 struct syscall s;
12079
12080 /* Skip whitespace. */
529480d0 12081 arg = skip_spaces (arg);
a96d9b2e
SDJ
12082
12083 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12084 cur_name[i] = arg[i];
12085 cur_name[i] = '\0';
12086 arg += i;
12087
12088 /* Check if the user provided a syscall name or a number. */
12089 syscall_number = (int) strtol (cur_name, &endptr, 0);
12090 if (*endptr == '\0')
bccd0dd2 12091 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
12092 else
12093 {
12094 /* We have a name. Let's check if it's valid and convert it
12095 to a number. */
12096 get_syscall_by_name (cur_name, &s);
12097
12098 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
12099 /* Here we have to issue an error instead of a warning,
12100 because GDB cannot do anything useful if there's no
12101 syscall number to be caught. */
a96d9b2e
SDJ
12102 error (_("Unknown syscall name '%s'."), cur_name);
12103 }
12104
12105 /* Ok, it's valid. */
12106 VEC_safe_push (int, result, s.number);
12107 }
12108
12109 discard_cleanups (cleanup);
12110 return result;
12111}
12112
12113/* Implement the "catch syscall" command. */
12114
12115static void
cc59ec59
MS
12116catch_syscall_command_1 (char *arg, int from_tty,
12117 struct cmd_list_element *command)
a96d9b2e
SDJ
12118{
12119 int tempflag;
12120 VEC(int) *filter;
12121 struct syscall s;
12122 struct gdbarch *gdbarch = get_current_arch ();
12123
12124 /* Checking if the feature if supported. */
12125 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12126 error (_("The feature 'catch syscall' is not supported on \
ea666128 12127this architecture yet."));
a96d9b2e
SDJ
12128
12129 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12130
e9cafbcc 12131 arg = skip_spaces (arg);
a96d9b2e
SDJ
12132
12133 /* We need to do this first "dummy" translation in order
12134 to get the syscall XML file loaded or, most important,
12135 to display a warning to the user if there's no XML file
12136 for his/her architecture. */
12137 get_syscall_by_number (0, &s);
12138
12139 /* The allowed syntax is:
12140 catch syscall
12141 catch syscall <name | number> [<name | number> ... <name | number>]
12142
12143 Let's check if there's a syscall name. */
12144
12145 if (arg != NULL)
12146 filter = catch_syscall_split_args (arg);
12147 else
12148 filter = NULL;
12149
12150 create_syscall_event_catchpoint (tempflag, filter,
12151 &catch_syscall_breakpoint_ops);
12152}
12153
c906108c 12154static void
fba45db2 12155catch_command (char *arg, int from_tty)
c906108c 12156{
44feb3ce 12157 error (_("Catch requires an event name."));
c906108c
SS
12158}
12159\f
12160
12161static void
fba45db2 12162tcatch_command (char *arg, int from_tty)
c906108c 12163{
44feb3ce 12164 error (_("Catch requires an event name."));
c906108c
SS
12165}
12166
8a2c437b
TT
12167/* A qsort comparison function that sorts breakpoints in order. */
12168
12169static int
12170compare_breakpoints (const void *a, const void *b)
12171{
12172 const breakpoint_p *ba = a;
12173 uintptr_t ua = (uintptr_t) *ba;
12174 const breakpoint_p *bb = b;
12175 uintptr_t ub = (uintptr_t) *bb;
12176
12177 if ((*ba)->number < (*bb)->number)
12178 return -1;
12179 else if ((*ba)->number > (*bb)->number)
12180 return 1;
12181
12182 /* Now sort by address, in case we see, e..g, two breakpoints with
12183 the number 0. */
12184 if (ua < ub)
12185 return -1;
94b0e70d 12186 return ua > ub ? 1 : 0;
8a2c437b
TT
12187}
12188
80f8a6eb 12189/* Delete breakpoints by address or line. */
c906108c
SS
12190
12191static void
fba45db2 12192clear_command (char *arg, int from_tty)
c906108c 12193{
8a2c437b 12194 struct breakpoint *b, *prev;
d6e956e5
VP
12195 VEC(breakpoint_p) *found = 0;
12196 int ix;
c906108c
SS
12197 int default_match;
12198 struct symtabs_and_lines sals;
12199 struct symtab_and_line sal;
c906108c 12200 int i;
8a2c437b 12201 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12202
12203 if (arg)
12204 {
39cf75f7
DE
12205 sals = decode_line_with_current_source (arg,
12206 (DECODE_LINE_FUNFIRSTLINE
12207 | DECODE_LINE_LIST_MODE));
cf4ded82 12208 make_cleanup (xfree, sals.sals);
c906108c
SS
12209 default_match = 0;
12210 }
12211 else
12212 {
c5aa993b 12213 sals.sals = (struct symtab_and_line *)
c906108c 12214 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12215 make_cleanup (xfree, sals.sals);
4a64f543 12216 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12217
12218 /* Set sal's line, symtab, pc, and pspace to the values
12219 corresponding to the last call to print_frame_info. If the
12220 codepoint is not valid, this will set all the fields to 0. */
12221 get_last_displayed_sal (&sal);
c906108c 12222 if (sal.symtab == 0)
8a3fe4f8 12223 error (_("No source file specified."));
c906108c
SS
12224
12225 sals.sals[0] = sal;
12226 sals.nelts = 1;
12227
12228 default_match = 1;
12229 }
12230
4a64f543
MS
12231 /* We don't call resolve_sal_pc here. That's not as bad as it
12232 seems, because all existing breakpoints typically have both
12233 file/line and pc set. So, if clear is given file/line, we can
12234 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12235
12236 We only support clearing given the address explicitly
12237 present in breakpoint table. Say, we've set breakpoint
4a64f543 12238 at file:line. There were several PC values for that file:line,
ed0616c6 12239 due to optimization, all in one block.
4a64f543
MS
12240
12241 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12242 PC corresponding to the same file:line, the breakpoint won't
12243 be cleared. We probably can still clear the breakpoint, but
12244 since the other PC value is never presented to user, user
12245 can only find it by guessing, and it does not seem important
12246 to support that. */
12247
4a64f543
MS
12248 /* For each line spec given, delete bps which correspond to it. Do
12249 it in two passes, solely to preserve the current behavior that
12250 from_tty is forced true if we delete more than one
12251 breakpoint. */
c906108c 12252
80f8a6eb 12253 found = NULL;
8a2c437b 12254 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12255 for (i = 0; i < sals.nelts; i++)
12256 {
05cba821
JK
12257 const char *sal_fullname;
12258
c906108c 12259 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12260 If line given (pc == 0), clear all bpts on specified line.
12261 If defaulting, clear all bpts on default line
c906108c 12262 or at default pc.
c5aa993b
JM
12263
12264 defaulting sal.pc != 0 tests to do
12265
12266 0 1 pc
12267 1 1 pc _and_ line
12268 0 0 line
12269 1 0 <can't happen> */
c906108c
SS
12270
12271 sal = sals.sals[i];
05cba821
JK
12272 sal_fullname = (sal.symtab == NULL
12273 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12274
4a64f543 12275 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12276 ALL_BREAKPOINTS (b)
c5aa993b 12277 {
0d381245 12278 int match = 0;
4a64f543 12279 /* Are we going to delete b? */
cc60f2e3 12280 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12281 {
12282 struct bp_location *loc = b->loc;
12283 for (; loc; loc = loc->next)
12284 {
f8eba3c6
TT
12285 /* If the user specified file:line, don't allow a PC
12286 match. This matches historical gdb behavior. */
12287 int pc_match = (!sal.explicit_line
12288 && sal.pc
12289 && (loc->pspace == sal.pspace)
12290 && (loc->address == sal.pc)
12291 && (!section_is_overlay (loc->section)
12292 || loc->section == sal.section));
4aac40c8
TT
12293 int line_match = 0;
12294
12295 if ((default_match || sal.explicit_line)
2f202fde 12296 && loc->symtab != NULL
05cba821 12297 && sal_fullname != NULL
4aac40c8 12298 && sal.pspace == loc->pspace
05cba821
JK
12299 && loc->line_number == sal.line
12300 && filename_cmp (symtab_to_fullname (loc->symtab),
12301 sal_fullname) == 0)
12302 line_match = 1;
4aac40c8 12303
0d381245
VP
12304 if (pc_match || line_match)
12305 {
12306 match = 1;
12307 break;
12308 }
12309 }
12310 }
12311
12312 if (match)
d6e956e5 12313 VEC_safe_push(breakpoint_p, found, b);
c906108c 12314 }
80f8a6eb 12315 }
8a2c437b 12316
80f8a6eb 12317 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12318 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12319 {
12320 if (arg)
8a3fe4f8 12321 error (_("No breakpoint at %s."), arg);
80f8a6eb 12322 else
8a3fe4f8 12323 error (_("No breakpoint at this line."));
80f8a6eb 12324 }
c906108c 12325
8a2c437b
TT
12326 /* Remove duplicates from the vec. */
12327 qsort (VEC_address (breakpoint_p, found),
12328 VEC_length (breakpoint_p, found),
12329 sizeof (breakpoint_p),
12330 compare_breakpoints);
12331 prev = VEC_index (breakpoint_p, found, 0);
12332 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12333 {
12334 if (b == prev)
12335 {
12336 VEC_ordered_remove (breakpoint_p, found, ix);
12337 --ix;
12338 }
12339 }
12340
d6e956e5 12341 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12342 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12343 if (from_tty)
a3f17187 12344 {
d6e956e5 12345 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12346 printf_unfiltered (_("Deleted breakpoint "));
12347 else
12348 printf_unfiltered (_("Deleted breakpoints "));
12349 }
d6e956e5
VP
12350
12351 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12352 {
c5aa993b 12353 if (from_tty)
d6e956e5
VP
12354 printf_unfiltered ("%d ", b->number);
12355 delete_breakpoint (b);
c906108c 12356 }
80f8a6eb
MS
12357 if (from_tty)
12358 putchar_unfiltered ('\n');
8a2c437b
TT
12359
12360 do_cleanups (cleanups);
c906108c
SS
12361}
12362\f
12363/* Delete breakpoint in BS if they are `delete' breakpoints and
12364 all breakpoints that are marked for deletion, whether hit or not.
12365 This is called after any breakpoint is hit, or after errors. */
12366
12367void
fba45db2 12368breakpoint_auto_delete (bpstat bs)
c906108c 12369{
35df4500 12370 struct breakpoint *b, *b_tmp;
c906108c
SS
12371
12372 for (; bs; bs = bs->next)
f431efe5
PA
12373 if (bs->breakpoint_at
12374 && bs->breakpoint_at->disposition == disp_del
c906108c 12375 && bs->stop)
f431efe5 12376 delete_breakpoint (bs->breakpoint_at);
c906108c 12377
35df4500 12378 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12379 {
b5de0fa7 12380 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12381 delete_breakpoint (b);
12382 }
c906108c
SS
12383}
12384
4a64f543
MS
12385/* A comparison function for bp_location AP and BP being interfaced to
12386 qsort. Sort elements primarily by their ADDRESS (no matter what
12387 does breakpoint_address_is_meaningful say for its OWNER),
12388 secondarily by ordering first bp_permanent OWNERed elements and
12389 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12390 qsort being an unstable algorithm. */
876fa593
JK
12391
12392static int
494cfb0f 12393bp_location_compare (const void *ap, const void *bp)
876fa593 12394{
494cfb0f
JK
12395 struct bp_location *a = *(void **) ap;
12396 struct bp_location *b = *(void **) bp;
2bdf28a0 12397 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
12398 int a_perm = a->owner->enable_state == bp_permanent;
12399 int b_perm = b->owner->enable_state == bp_permanent;
12400
12401 if (a->address != b->address)
12402 return (a->address > b->address) - (a->address < b->address);
12403
dea2aa5f
LM
12404 /* Sort locations at the same address by their pspace number, keeping
12405 locations of the same inferior (in a multi-inferior environment)
12406 grouped. */
12407
12408 if (a->pspace->num != b->pspace->num)
12409 return ((a->pspace->num > b->pspace->num)
12410 - (a->pspace->num < b->pspace->num));
12411
876fa593
JK
12412 /* Sort permanent breakpoints first. */
12413 if (a_perm != b_perm)
12414 return (a_perm < b_perm) - (a_perm > b_perm);
12415
c56a97f9
JK
12416 /* Make the internal GDB representation stable across GDB runs
12417 where A and B memory inside GDB can differ. Breakpoint locations of
12418 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12419
12420 if (a->owner->number != b->owner->number)
c56a97f9
JK
12421 return ((a->owner->number > b->owner->number)
12422 - (a->owner->number < b->owner->number));
876fa593
JK
12423
12424 return (a > b) - (a < b);
12425}
12426
876fa593 12427/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12428 bp_location_shadow_len_after_address_max according to the current
12429 content of the bp_location array. */
f7545552
TT
12430
12431static void
876fa593 12432bp_location_target_extensions_update (void)
f7545552 12433{
876fa593
JK
12434 struct bp_location *bl, **blp_tmp;
12435
12436 bp_location_placed_address_before_address_max = 0;
12437 bp_location_shadow_len_after_address_max = 0;
12438
12439 ALL_BP_LOCATIONS (bl, blp_tmp)
12440 {
12441 CORE_ADDR start, end, addr;
12442
12443 if (!bp_location_has_shadow (bl))
12444 continue;
12445
12446 start = bl->target_info.placed_address;
12447 end = start + bl->target_info.shadow_len;
12448
12449 gdb_assert (bl->address >= start);
12450 addr = bl->address - start;
12451 if (addr > bp_location_placed_address_before_address_max)
12452 bp_location_placed_address_before_address_max = addr;
12453
12454 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12455
12456 gdb_assert (bl->address < end);
12457 addr = end - bl->address;
12458 if (addr > bp_location_shadow_len_after_address_max)
12459 bp_location_shadow_len_after_address_max = addr;
12460 }
f7545552
TT
12461}
12462
1e4d1764
YQ
12463/* Download tracepoint locations if they haven't been. */
12464
12465static void
12466download_tracepoint_locations (void)
12467{
7ed2c994 12468 struct breakpoint *b;
1e4d1764
YQ
12469 struct cleanup *old_chain;
12470
12471 if (!target_can_download_tracepoint ())
12472 return;
12473
12474 old_chain = save_current_space_and_thread ();
12475
7ed2c994 12476 ALL_TRACEPOINTS (b)
1e4d1764 12477 {
7ed2c994 12478 struct bp_location *bl;
1e4d1764 12479 struct tracepoint *t;
f2a8bc8a 12480 int bp_location_downloaded = 0;
1e4d1764 12481
7ed2c994 12482 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12483 ? !may_insert_fast_tracepoints
12484 : !may_insert_tracepoints))
12485 continue;
12486
7ed2c994
YQ
12487 for (bl = b->loc; bl; bl = bl->next)
12488 {
12489 /* In tracepoint, locations are _never_ duplicated, so
12490 should_be_inserted is equivalent to
12491 unduplicated_should_be_inserted. */
12492 if (!should_be_inserted (bl) || bl->inserted)
12493 continue;
1e4d1764 12494
7ed2c994 12495 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12496
7ed2c994 12497 target_download_tracepoint (bl);
1e4d1764 12498
7ed2c994 12499 bl->inserted = 1;
f2a8bc8a 12500 bp_location_downloaded = 1;
7ed2c994
YQ
12501 }
12502 t = (struct tracepoint *) b;
12503 t->number_on_target = b->number;
f2a8bc8a
YQ
12504 if (bp_location_downloaded)
12505 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12506 }
12507
12508 do_cleanups (old_chain);
12509}
12510
934709f0
PW
12511/* Swap the insertion/duplication state between two locations. */
12512
12513static void
12514swap_insertion (struct bp_location *left, struct bp_location *right)
12515{
12516 const int left_inserted = left->inserted;
12517 const int left_duplicate = left->duplicate;
b775012e 12518 const int left_needs_update = left->needs_update;
934709f0
PW
12519 const struct bp_target_info left_target_info = left->target_info;
12520
1e4d1764
YQ
12521 /* Locations of tracepoints can never be duplicated. */
12522 if (is_tracepoint (left->owner))
12523 gdb_assert (!left->duplicate);
12524 if (is_tracepoint (right->owner))
12525 gdb_assert (!right->duplicate);
12526
934709f0
PW
12527 left->inserted = right->inserted;
12528 left->duplicate = right->duplicate;
b775012e 12529 left->needs_update = right->needs_update;
934709f0
PW
12530 left->target_info = right->target_info;
12531 right->inserted = left_inserted;
12532 right->duplicate = left_duplicate;
b775012e 12533 right->needs_update = left_needs_update;
934709f0
PW
12534 right->target_info = left_target_info;
12535}
12536
b775012e
LM
12537/* Force the re-insertion of the locations at ADDRESS. This is called
12538 once a new/deleted/modified duplicate location is found and we are evaluating
12539 conditions on the target's side. Such conditions need to be updated on
12540 the target. */
12541
12542static void
12543force_breakpoint_reinsertion (struct bp_location *bl)
12544{
12545 struct bp_location **locp = NULL, **loc2p;
12546 struct bp_location *loc;
12547 CORE_ADDR address = 0;
12548 int pspace_num;
12549
12550 address = bl->address;
12551 pspace_num = bl->pspace->num;
12552
12553 /* This is only meaningful if the target is
12554 evaluating conditions and if the user has
12555 opted for condition evaluation on the target's
12556 side. */
12557 if (gdb_evaluates_breakpoint_condition_p ()
12558 || !target_supports_evaluation_of_breakpoint_conditions ())
12559 return;
12560
12561 /* Flag all breakpoint locations with this address and
12562 the same program space as the location
12563 as "its condition has changed". We need to
12564 update the conditions on the target's side. */
12565 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12566 {
12567 loc = *loc2p;
12568
12569 if (!is_breakpoint (loc->owner)
12570 || pspace_num != loc->pspace->num)
12571 continue;
12572
12573 /* Flag the location appropriately. We use a different state to
12574 let everyone know that we already updated the set of locations
12575 with addr bl->address and program space bl->pspace. This is so
12576 we don't have to keep calling these functions just to mark locations
12577 that have already been marked. */
12578 loc->condition_changed = condition_updated;
12579
12580 /* Free the agent expression bytecode as well. We will compute
12581 it later on. */
12582 if (loc->cond_bytecode)
12583 {
12584 free_agent_expr (loc->cond_bytecode);
12585 loc->cond_bytecode = NULL;
12586 }
12587 }
12588}
44702360
PA
12589/* Called whether new breakpoints are created, or existing breakpoints
12590 deleted, to update the global location list and recompute which
12591 locations are duplicate of which.
b775012e 12592
04086b45
PA
12593 The INSERT_MODE flag determines whether locations may not, may, or
12594 shall be inserted now. See 'enum ugll_insert_mode' for more
12595 info. */
b60e7edf 12596
0d381245 12597static void
44702360 12598update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12599{
74960c60 12600 struct breakpoint *b;
876fa593 12601 struct bp_location **locp, *loc;
f7545552 12602 struct cleanup *cleanups;
b775012e
LM
12603 /* Last breakpoint location address that was marked for update. */
12604 CORE_ADDR last_addr = 0;
12605 /* Last breakpoint location program space that was marked for update. */
12606 int last_pspace_num = -1;
f7545552 12607
2d134ed3
PA
12608 /* Used in the duplicates detection below. When iterating over all
12609 bp_locations, points to the first bp_location of a given address.
12610 Breakpoints and watchpoints of different types are never
12611 duplicates of each other. Keep one pointer for each type of
12612 breakpoint/watchpoint, so we only need to loop over all locations
12613 once. */
12614 struct bp_location *bp_loc_first; /* breakpoint */
12615 struct bp_location *wp_loc_first; /* hardware watchpoint */
12616 struct bp_location *awp_loc_first; /* access watchpoint */
12617 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12618
4a64f543
MS
12619 /* Saved former bp_location array which we compare against the newly
12620 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12621 struct bp_location **old_location, **old_locp;
12622 unsigned old_location_count;
12623
12624 old_location = bp_location;
12625 old_location_count = bp_location_count;
12626 bp_location = NULL;
12627 bp_location_count = 0;
12628 cleanups = make_cleanup (xfree, old_location);
0d381245 12629
74960c60 12630 ALL_BREAKPOINTS (b)
876fa593
JK
12631 for (loc = b->loc; loc; loc = loc->next)
12632 bp_location_count++;
12633
12634 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12635 locp = bp_location;
12636 ALL_BREAKPOINTS (b)
12637 for (loc = b->loc; loc; loc = loc->next)
12638 *locp++ = loc;
12639 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12640 bp_location_compare);
876fa593
JK
12641
12642 bp_location_target_extensions_update ();
74960c60 12643
4a64f543
MS
12644 /* Identify bp_location instances that are no longer present in the
12645 new list, and therefore should be freed. Note that it's not
12646 necessary that those locations should be removed from inferior --
12647 if there's another location at the same address (previously
12648 marked as duplicate), we don't need to remove/insert the
12649 location.
876fa593 12650
4a64f543
MS
12651 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12652 and former bp_location array state respectively. */
876fa593
JK
12653
12654 locp = bp_location;
12655 for (old_locp = old_location; old_locp < old_location + old_location_count;
12656 old_locp++)
74960c60 12657 {
876fa593 12658 struct bp_location *old_loc = *old_locp;
c7d46a38 12659 struct bp_location **loc2p;
876fa593 12660
e5dd4106 12661 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12662 not, we have to free it. */
c7d46a38 12663 int found_object = 0;
20874c92
VP
12664 /* Tells if the location should remain inserted in the target. */
12665 int keep_in_target = 0;
12666 int removed = 0;
876fa593 12667
4a64f543
MS
12668 /* Skip LOCP entries which will definitely never be needed.
12669 Stop either at or being the one matching OLD_LOC. */
876fa593 12670 while (locp < bp_location + bp_location_count
c7d46a38 12671 && (*locp)->address < old_loc->address)
876fa593 12672 locp++;
c7d46a38
PA
12673
12674 for (loc2p = locp;
12675 (loc2p < bp_location + bp_location_count
12676 && (*loc2p)->address == old_loc->address);
12677 loc2p++)
12678 {
b775012e
LM
12679 /* Check if this is a new/duplicated location or a duplicated
12680 location that had its condition modified. If so, we want to send
12681 its condition to the target if evaluation of conditions is taking
12682 place there. */
12683 if ((*loc2p)->condition_changed == condition_modified
12684 && (last_addr != old_loc->address
12685 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12686 {
b775012e
LM
12687 force_breakpoint_reinsertion (*loc2p);
12688 last_pspace_num = old_loc->pspace->num;
c7d46a38 12689 }
b775012e
LM
12690
12691 if (*loc2p == old_loc)
12692 found_object = 1;
c7d46a38 12693 }
74960c60 12694
b775012e
LM
12695 /* We have already handled this address, update it so that we don't
12696 have to go through updates again. */
12697 last_addr = old_loc->address;
12698
12699 /* Target-side condition evaluation: Handle deleted locations. */
12700 if (!found_object)
12701 force_breakpoint_reinsertion (old_loc);
12702
4a64f543
MS
12703 /* If this location is no longer present, and inserted, look if
12704 there's maybe a new location at the same address. If so,
12705 mark that one inserted, and don't remove this one. This is
12706 needed so that we don't have a time window where a breakpoint
12707 at certain location is not inserted. */
74960c60 12708
876fa593 12709 if (old_loc->inserted)
0d381245 12710 {
4a64f543
MS
12711 /* If the location is inserted now, we might have to remove
12712 it. */
74960c60 12713
876fa593 12714 if (found_object && should_be_inserted (old_loc))
74960c60 12715 {
4a64f543
MS
12716 /* The location is still present in the location list,
12717 and still should be inserted. Don't do anything. */
20874c92 12718 keep_in_target = 1;
74960c60
VP
12719 }
12720 else
12721 {
b775012e
LM
12722 /* This location still exists, but it won't be kept in the
12723 target since it may have been disabled. We proceed to
12724 remove its target-side condition. */
12725
4a64f543
MS
12726 /* The location is either no longer present, or got
12727 disabled. See if there's another location at the
12728 same address, in which case we don't need to remove
12729 this one from the target. */
876fa593 12730
2bdf28a0 12731 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12732 if (breakpoint_address_is_meaningful (old_loc->owner))
12733 {
876fa593 12734 for (loc2p = locp;
c7d46a38
PA
12735 (loc2p < bp_location + bp_location_count
12736 && (*loc2p)->address == old_loc->address);
876fa593
JK
12737 loc2p++)
12738 {
12739 struct bp_location *loc2 = *loc2p;
12740
2d134ed3 12741 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12742 {
85d721b8
PA
12743 /* Read watchpoint locations are switched to
12744 access watchpoints, if the former are not
12745 supported, but the latter are. */
12746 if (is_hardware_watchpoint (old_loc->owner))
12747 {
12748 gdb_assert (is_hardware_watchpoint (loc2->owner));
12749 loc2->watchpoint_type = old_loc->watchpoint_type;
12750 }
12751
934709f0
PW
12752 /* loc2 is a duplicated location. We need to check
12753 if it should be inserted in case it will be
12754 unduplicated. */
12755 if (loc2 != old_loc
12756 && unduplicated_should_be_inserted (loc2))
c7d46a38 12757 {
934709f0 12758 swap_insertion (old_loc, loc2);
c7d46a38
PA
12759 keep_in_target = 1;
12760 break;
12761 }
876fa593
JK
12762 }
12763 }
12764 }
74960c60
VP
12765 }
12766
20874c92
VP
12767 if (!keep_in_target)
12768 {
876fa593 12769 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12770 {
4a64f543
MS
12771 /* This is just about all we can do. We could keep
12772 this location on the global list, and try to
12773 remove it next time, but there's no particular
12774 reason why we will succeed next time.
20874c92 12775
4a64f543
MS
12776 Note that at this point, old_loc->owner is still
12777 valid, as delete_breakpoint frees the breakpoint
12778 only after calling us. */
3e43a32a
MS
12779 printf_filtered (_("warning: Error removing "
12780 "breakpoint %d\n"),
876fa593 12781 old_loc->owner->number);
20874c92
VP
12782 }
12783 removed = 1;
12784 }
0d381245 12785 }
74960c60
VP
12786
12787 if (!found_object)
1c5cfe86 12788 {
db82e815
PA
12789 if (removed && non_stop
12790 && breakpoint_address_is_meaningful (old_loc->owner)
12791 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12792 {
db82e815
PA
12793 /* This location was removed from the target. In
12794 non-stop mode, a race condition is possible where
12795 we've removed a breakpoint, but stop events for that
12796 breakpoint are already queued and will arrive later.
12797 We apply an heuristic to be able to distinguish such
12798 SIGTRAPs from other random SIGTRAPs: we keep this
12799 breakpoint location for a bit, and will retire it
12800 after we see some number of events. The theory here
12801 is that reporting of events should, "on the average",
12802 be fair, so after a while we'll see events from all
12803 threads that have anything of interest, and no longer
12804 need to keep this breakpoint location around. We
12805 don't hold locations forever so to reduce chances of
12806 mistaking a non-breakpoint SIGTRAP for a breakpoint
12807 SIGTRAP.
12808
12809 The heuristic failing can be disastrous on
12810 decr_pc_after_break targets.
12811
12812 On decr_pc_after_break targets, like e.g., x86-linux,
12813 if we fail to recognize a late breakpoint SIGTRAP,
12814 because events_till_retirement has reached 0 too
12815 soon, we'll fail to do the PC adjustment, and report
12816 a random SIGTRAP to the user. When the user resumes
12817 the inferior, it will most likely immediately crash
2dec564e 12818 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12819 corrupted, because of being resumed e.g., in the
12820 middle of a multi-byte instruction, or skipped a
12821 one-byte instruction. This was actually seen happen
12822 on native x86-linux, and should be less rare on
12823 targets that do not support new thread events, like
12824 remote, due to the heuristic depending on
12825 thread_count.
12826
12827 Mistaking a random SIGTRAP for a breakpoint trap
12828 causes similar symptoms (PC adjustment applied when
12829 it shouldn't), but then again, playing with SIGTRAPs
12830 behind the debugger's back is asking for trouble.
12831
12832 Since hardware watchpoint traps are always
12833 distinguishable from other traps, so we don't need to
12834 apply keep hardware watchpoint moribund locations
12835 around. We simply always ignore hardware watchpoint
12836 traps we can no longer explain. */
12837
876fa593
JK
12838 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12839 old_loc->owner = NULL;
20874c92 12840
876fa593 12841 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12842 }
12843 else
f431efe5
PA
12844 {
12845 old_loc->owner = NULL;
12846 decref_bp_location (&old_loc);
12847 }
20874c92 12848 }
74960c60 12849 }
1c5cfe86 12850
348d480f
PA
12851 /* Rescan breakpoints at the same address and section, marking the
12852 first one as "first" and any others as "duplicates". This is so
12853 that the bpt instruction is only inserted once. If we have a
12854 permanent breakpoint at the same place as BPT, make that one the
12855 official one, and the rest as duplicates. Permanent breakpoints
12856 are sorted first for the same address.
12857
12858 Do the same for hardware watchpoints, but also considering the
12859 watchpoint's type (regular/access/read) and length. */
12860
12861 bp_loc_first = NULL;
12862 wp_loc_first = NULL;
12863 awp_loc_first = NULL;
12864 rwp_loc_first = NULL;
12865 ALL_BP_LOCATIONS (loc, locp)
12866 {
12867 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12868 non-NULL. */
348d480f 12869 struct bp_location **loc_first_p;
d3fbdd86 12870 b = loc->owner;
348d480f 12871
6f380991 12872 if (!unduplicated_should_be_inserted (loc)
348d480f 12873 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12874 /* Don't detect duplicate for tracepoint locations because they are
12875 never duplicated. See the comments in field `duplicate' of
12876 `struct bp_location'. */
348d480f 12877 || is_tracepoint (b))
b775012e
LM
12878 {
12879 /* Clear the condition modification flag. */
12880 loc->condition_changed = condition_unchanged;
12881 continue;
12882 }
348d480f
PA
12883
12884 /* Permanent breakpoint should always be inserted. */
12885 if (b->enable_state == bp_permanent && ! loc->inserted)
12886 internal_error (__FILE__, __LINE__,
12887 _("allegedly permanent breakpoint is not "
12888 "actually inserted"));
12889
12890 if (b->type == bp_hardware_watchpoint)
12891 loc_first_p = &wp_loc_first;
12892 else if (b->type == bp_read_watchpoint)
12893 loc_first_p = &rwp_loc_first;
12894 else if (b->type == bp_access_watchpoint)
12895 loc_first_p = &awp_loc_first;
12896 else
12897 loc_first_p = &bp_loc_first;
12898
12899 if (*loc_first_p == NULL
12900 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12901 || !breakpoint_locations_match (loc, *loc_first_p))
12902 {
12903 *loc_first_p = loc;
12904 loc->duplicate = 0;
b775012e
LM
12905
12906 if (is_breakpoint (loc->owner) && loc->condition_changed)
12907 {
12908 loc->needs_update = 1;
12909 /* Clear the condition modification flag. */
12910 loc->condition_changed = condition_unchanged;
12911 }
348d480f
PA
12912 continue;
12913 }
12914
934709f0
PW
12915
12916 /* This and the above ensure the invariant that the first location
12917 is not duplicated, and is the inserted one.
12918 All following are marked as duplicated, and are not inserted. */
12919 if (loc->inserted)
12920 swap_insertion (loc, *loc_first_p);
348d480f
PA
12921 loc->duplicate = 1;
12922
b775012e
LM
12923 /* Clear the condition modification flag. */
12924 loc->condition_changed = condition_unchanged;
12925
348d480f
PA
12926 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12927 && b->enable_state != bp_permanent)
12928 internal_error (__FILE__, __LINE__,
12929 _("another breakpoint was inserted on top of "
12930 "a permanent breakpoint"));
12931 }
12932
04086b45
PA
12933 if (insert_mode == UGLL_INSERT
12934 || (breakpoints_always_inserted_mode ()
12935 && (have_live_inferiors ()
12936 || (gdbarch_has_global_breakpoints (target_gdbarch ())))))
b775012e 12937 {
04086b45 12938 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12939 insert_breakpoint_locations ();
12940 else
12941 {
44702360
PA
12942 /* Even though the caller told us to not insert new
12943 locations, we may still need to update conditions on the
12944 target's side of breakpoints that were already inserted
12945 if the target is evaluating breakpoint conditions. We
b775012e
LM
12946 only update conditions for locations that are marked
12947 "needs_update". */
12948 update_inserted_breakpoint_locations ();
12949 }
12950 }
348d480f 12951
04086b45 12952 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12953 download_tracepoint_locations ();
12954
348d480f
PA
12955 do_cleanups (cleanups);
12956}
12957
12958void
12959breakpoint_retire_moribund (void)
12960{
12961 struct bp_location *loc;
12962 int ix;
12963
12964 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12965 if (--(loc->events_till_retirement) == 0)
12966 {
12967 decref_bp_location (&loc);
12968 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12969 --ix;
12970 }
12971}
12972
12973static void
44702360 12974update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12975{
bfd189b1 12976 volatile struct gdb_exception e;
348d480f
PA
12977
12978 TRY_CATCH (e, RETURN_MASK_ERROR)
44702360 12979 update_global_location_list (insert_mode);
348d480f
PA
12980}
12981
12982/* Clear BKP from a BPS. */
12983
12984static void
12985bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12986{
12987 bpstat bs;
12988
12989 for (bs = bps; bs; bs = bs->next)
12990 if (bs->breakpoint_at == bpt)
12991 {
12992 bs->breakpoint_at = NULL;
12993 bs->old_val = NULL;
12994 /* bs->commands will be freed later. */
12995 }
12996}
12997
12998/* Callback for iterate_over_threads. */
12999static int
13000bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
13001{
13002 struct breakpoint *bpt = data;
13003
13004 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
13005 return 0;
13006}
13007
13008/* Helper for breakpoint and tracepoint breakpoint_ops->mention
13009 callbacks. */
13010
13011static void
13012say_where (struct breakpoint *b)
13013{
13014 struct value_print_options opts;
13015
13016 get_user_print_options (&opts);
13017
13018 /* i18n: cagney/2005-02-11: Below needs to be merged into a
13019 single string. */
13020 if (b->loc == NULL)
13021 {
13022 printf_filtered (_(" (%s) pending."), b->addr_string);
13023 }
13024 else
13025 {
2f202fde 13026 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
13027 {
13028 printf_filtered (" at ");
13029 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
13030 gdb_stdout);
13031 }
2f202fde 13032 if (b->loc->symtab != NULL)
f8eba3c6
TT
13033 {
13034 /* If there is a single location, we can print the location
13035 more nicely. */
13036 if (b->loc->next == NULL)
13037 printf_filtered (": file %s, line %d.",
05cba821
JK
13038 symtab_to_filename_for_display (b->loc->symtab),
13039 b->loc->line_number);
f8eba3c6
TT
13040 else
13041 /* This is not ideal, but each location may have a
13042 different file name, and this at least reflects the
13043 real situation somewhat. */
13044 printf_filtered (": %s.", b->addr_string);
13045 }
348d480f
PA
13046
13047 if (b->loc->next)
13048 {
13049 struct bp_location *loc = b->loc;
13050 int n = 0;
13051 for (; loc; loc = loc->next)
13052 ++n;
13053 printf_filtered (" (%d locations)", n);
13054 }
13055 }
13056}
13057
348d480f
PA
13058/* Default bp_location_ops methods. */
13059
13060static void
13061bp_location_dtor (struct bp_location *self)
13062{
13063 xfree (self->cond);
b775012e
LM
13064 if (self->cond_bytecode)
13065 free_agent_expr (self->cond_bytecode);
348d480f 13066 xfree (self->function_name);
8b4f3082
PA
13067
13068 VEC_free (agent_expr_p, self->target_info.conditions);
13069 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
13070}
13071
13072static const struct bp_location_ops bp_location_ops =
13073{
13074 bp_location_dtor
13075};
13076
2060206e
PA
13077/* Default breakpoint_ops methods all breakpoint_ops ultimately
13078 inherit from. */
348d480f 13079
2060206e
PA
13080static void
13081base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
13082{
13083 decref_counted_command_line (&self->commands);
13084 xfree (self->cond_string);
fb81d016 13085 xfree (self->extra_string);
348d480f 13086 xfree (self->addr_string);
f8eba3c6 13087 xfree (self->filter);
348d480f 13088 xfree (self->addr_string_range_end);
348d480f
PA
13089}
13090
2060206e
PA
13091static struct bp_location *
13092base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
13093{
13094 struct bp_location *loc;
13095
13096 loc = XNEW (struct bp_location);
13097 init_bp_location (loc, &bp_location_ops, self);
13098 return loc;
13099}
13100
2060206e
PA
13101static void
13102base_breakpoint_re_set (struct breakpoint *b)
13103{
13104 /* Nothing to re-set. */
13105}
13106
13107#define internal_error_pure_virtual_called() \
13108 gdb_assert_not_reached ("pure virtual function called")
13109
13110static int
13111base_breakpoint_insert_location (struct bp_location *bl)
13112{
13113 internal_error_pure_virtual_called ();
13114}
13115
13116static int
13117base_breakpoint_remove_location (struct bp_location *bl)
13118{
13119 internal_error_pure_virtual_called ();
13120}
13121
13122static int
13123base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13124 struct address_space *aspace,
09ac7c10
TT
13125 CORE_ADDR bp_addr,
13126 const struct target_waitstatus *ws)
2060206e
PA
13127{
13128 internal_error_pure_virtual_called ();
13129}
13130
13131static void
13132base_breakpoint_check_status (bpstat bs)
13133{
13134 /* Always stop. */
13135}
13136
13137/* A "works_in_software_mode" breakpoint_ops method that just internal
13138 errors. */
13139
13140static int
13141base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13142{
13143 internal_error_pure_virtual_called ();
13144}
13145
13146/* A "resources_needed" breakpoint_ops method that just internal
13147 errors. */
13148
13149static int
13150base_breakpoint_resources_needed (const struct bp_location *bl)
13151{
13152 internal_error_pure_virtual_called ();
13153}
13154
13155static enum print_stop_action
13156base_breakpoint_print_it (bpstat bs)
13157{
13158 internal_error_pure_virtual_called ();
13159}
13160
13161static void
13162base_breakpoint_print_one_detail (const struct breakpoint *self,
13163 struct ui_out *uiout)
13164{
13165 /* nothing */
13166}
13167
13168static void
13169base_breakpoint_print_mention (struct breakpoint *b)
13170{
13171 internal_error_pure_virtual_called ();
13172}
13173
13174static void
13175base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13176{
13177 internal_error_pure_virtual_called ();
13178}
13179
983af33b
SDJ
13180static void
13181base_breakpoint_create_sals_from_address (char **arg,
13182 struct linespec_result *canonical,
13183 enum bptype type_wanted,
13184 char *addr_start,
13185 char **copy_arg)
13186{
13187 internal_error_pure_virtual_called ();
13188}
13189
13190static void
13191base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13192 struct linespec_result *c,
983af33b 13193 char *cond_string,
e7e0cddf 13194 char *extra_string,
983af33b
SDJ
13195 enum bptype type_wanted,
13196 enum bpdisp disposition,
13197 int thread,
13198 int task, int ignore_count,
13199 const struct breakpoint_ops *o,
13200 int from_tty, int enabled,
44f238bb 13201 int internal, unsigned flags)
983af33b
SDJ
13202{
13203 internal_error_pure_virtual_called ();
13204}
13205
13206static void
13207base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13208 struct symtabs_and_lines *sals)
13209{
13210 internal_error_pure_virtual_called ();
13211}
13212
ab04a2af
TT
13213/* The default 'explains_signal' method. */
13214
47591c29 13215static int
427cd150 13216base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13217{
47591c29 13218 return 1;
ab04a2af
TT
13219}
13220
9d6e6e84
HZ
13221/* The default "after_condition_true" method. */
13222
13223static void
13224base_breakpoint_after_condition_true (struct bpstats *bs)
13225{
13226 /* Nothing to do. */
13227}
13228
ab04a2af 13229struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13230{
13231 base_breakpoint_dtor,
13232 base_breakpoint_allocate_location,
13233 base_breakpoint_re_set,
13234 base_breakpoint_insert_location,
13235 base_breakpoint_remove_location,
13236 base_breakpoint_breakpoint_hit,
13237 base_breakpoint_check_status,
13238 base_breakpoint_resources_needed,
13239 base_breakpoint_works_in_software_mode,
13240 base_breakpoint_print_it,
13241 NULL,
13242 base_breakpoint_print_one_detail,
13243 base_breakpoint_print_mention,
983af33b
SDJ
13244 base_breakpoint_print_recreate,
13245 base_breakpoint_create_sals_from_address,
13246 base_breakpoint_create_breakpoints_sal,
13247 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13248 base_breakpoint_explains_signal,
13249 base_breakpoint_after_condition_true,
2060206e
PA
13250};
13251
13252/* Default breakpoint_ops methods. */
13253
13254static void
348d480f
PA
13255bkpt_re_set (struct breakpoint *b)
13256{
06edf0c0
PA
13257 /* FIXME: is this still reachable? */
13258 if (b->addr_string == NULL)
13259 {
13260 /* Anything without a string can't be re-set. */
348d480f 13261 delete_breakpoint (b);
06edf0c0 13262 return;
348d480f 13263 }
06edf0c0
PA
13264
13265 breakpoint_re_set_default (b);
348d480f
PA
13266}
13267
ef370185
JB
13268/* Copy SRC's shadow buffer and whatever else we'd set if we actually
13269 inserted DEST, so we can remove it later, in case SRC is removed
13270 first. */
13271
13272static void
13273bp_target_info_copy_insertion_state (struct bp_target_info *dest,
13274 const struct bp_target_info *src)
13275{
13276 dest->shadow_len = src->shadow_len;
13277 memcpy (dest->shadow_contents, src->shadow_contents, src->shadow_len);
13278 dest->placed_size = src->placed_size;
13279}
13280
2060206e 13281static int
348d480f
PA
13282bkpt_insert_location (struct bp_location *bl)
13283{
13284 if (bl->loc_type == bp_loc_hardware_breakpoint)
13285 return target_insert_hw_breakpoint (bl->gdbarch,
13286 &bl->target_info);
13287 else
ef370185
JB
13288 {
13289 struct bp_target_info *bp_tgt = &bl->target_info;
13290 int ret;
13291 int sss_slot;
13292
13293 /* There is no need to insert a breakpoint if an unconditional
13294 raw/sss breakpoint is already inserted at that location. */
13295 sss_slot = find_single_step_breakpoint (bp_tgt->placed_address_space,
13296 bp_tgt->placed_address);
13297 if (sss_slot >= 0)
13298 {
13299 struct bp_target_info *sss_bp_tgt = single_step_breakpoints[sss_slot];
13300
13301 bp_target_info_copy_insertion_state (bp_tgt, sss_bp_tgt);
13302 return 0;
13303 }
13304
13305 return target_insert_breakpoint (bl->gdbarch, bp_tgt);
13306 }
348d480f
PA
13307}
13308
2060206e 13309static int
348d480f
PA
13310bkpt_remove_location (struct bp_location *bl)
13311{
13312 if (bl->loc_type == bp_loc_hardware_breakpoint)
13313 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13314 else
ef370185
JB
13315 {
13316 struct bp_target_info *bp_tgt = &bl->target_info;
13317 struct address_space *aspace = bp_tgt->placed_address_space;
13318 CORE_ADDR address = bp_tgt->placed_address;
13319
13320 /* Only remove the breakpoint if there is no raw/sss breakpoint
13321 still inserted at this location. Otherwise, we would be
13322 effectively disabling the raw/sss breakpoint. */
13323 if (single_step_breakpoint_inserted_here_p (aspace, address))
13324 return 0;
13325
13326 return target_remove_breakpoint (bl->gdbarch, bp_tgt);
13327 }
348d480f
PA
13328}
13329
2060206e 13330static int
348d480f 13331bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13332 struct address_space *aspace, CORE_ADDR bp_addr,
13333 const struct target_waitstatus *ws)
348d480f 13334{
09ac7c10 13335 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13336 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13337 return 0;
13338
348d480f
PA
13339 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13340 aspace, bp_addr))
13341 return 0;
13342
13343 if (overlay_debugging /* unmapped overlay section */
13344 && section_is_overlay (bl->section)
13345 && !section_is_mapped (bl->section))
13346 return 0;
13347
13348 return 1;
13349}
13350
cd1608cc
PA
13351static int
13352dprintf_breakpoint_hit (const struct bp_location *bl,
13353 struct address_space *aspace, CORE_ADDR bp_addr,
13354 const struct target_waitstatus *ws)
13355{
13356 if (dprintf_style == dprintf_style_agent
13357 && target_can_run_breakpoint_commands ())
13358 {
13359 /* An agent-style dprintf never causes a stop. If we see a trap
13360 for this address it must be for a breakpoint that happens to
13361 be set at the same address. */
13362 return 0;
13363 }
13364
13365 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13366}
13367
2060206e 13368static int
348d480f
PA
13369bkpt_resources_needed (const struct bp_location *bl)
13370{
13371 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13372
13373 return 1;
13374}
13375
2060206e 13376static enum print_stop_action
348d480f
PA
13377bkpt_print_it (bpstat bs)
13378{
348d480f
PA
13379 struct breakpoint *b;
13380 const struct bp_location *bl;
001c8c33 13381 int bp_temp;
79a45e25 13382 struct ui_out *uiout = current_uiout;
348d480f
PA
13383
13384 gdb_assert (bs->bp_location_at != NULL);
13385
13386 bl = bs->bp_location_at;
13387 b = bs->breakpoint_at;
13388
001c8c33
PA
13389 bp_temp = b->disposition == disp_del;
13390 if (bl->address != bl->requested_address)
13391 breakpoint_adjustment_warning (bl->requested_address,
13392 bl->address,
13393 b->number, 1);
13394 annotate_breakpoint (b->number);
13395 if (bp_temp)
13396 ui_out_text (uiout, "\nTemporary breakpoint ");
13397 else
13398 ui_out_text (uiout, "\nBreakpoint ");
13399 if (ui_out_is_mi_like_p (uiout))
348d480f 13400 {
001c8c33
PA
13401 ui_out_field_string (uiout, "reason",
13402 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13403 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13404 }
001c8c33
PA
13405 ui_out_field_int (uiout, "bkptno", b->number);
13406 ui_out_text (uiout, ", ");
06edf0c0 13407
001c8c33 13408 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13409}
13410
2060206e 13411static void
06edf0c0
PA
13412bkpt_print_mention (struct breakpoint *b)
13413{
79a45e25 13414 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13415 return;
13416
13417 switch (b->type)
13418 {
13419 case bp_breakpoint:
13420 case bp_gnu_ifunc_resolver:
13421 if (b->disposition == disp_del)
13422 printf_filtered (_("Temporary breakpoint"));
13423 else
13424 printf_filtered (_("Breakpoint"));
13425 printf_filtered (_(" %d"), b->number);
13426 if (b->type == bp_gnu_ifunc_resolver)
13427 printf_filtered (_(" at gnu-indirect-function resolver"));
13428 break;
13429 case bp_hardware_breakpoint:
13430 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13431 break;
e7e0cddf
SS
13432 case bp_dprintf:
13433 printf_filtered (_("Dprintf %d"), b->number);
13434 break;
06edf0c0
PA
13435 }
13436
13437 say_where (b);
13438}
13439
2060206e 13440static void
06edf0c0
PA
13441bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13442{
13443 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13444 fprintf_unfiltered (fp, "tbreak");
13445 else if (tp->type == bp_breakpoint)
13446 fprintf_unfiltered (fp, "break");
13447 else if (tp->type == bp_hardware_breakpoint
13448 && tp->disposition == disp_del)
13449 fprintf_unfiltered (fp, "thbreak");
13450 else if (tp->type == bp_hardware_breakpoint)
13451 fprintf_unfiltered (fp, "hbreak");
13452 else
13453 internal_error (__FILE__, __LINE__,
13454 _("unhandled breakpoint type %d"), (int) tp->type);
13455
2060206e 13456 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13457 print_recreate_thread (tp, fp);
06edf0c0
PA
13458}
13459
983af33b
SDJ
13460static void
13461bkpt_create_sals_from_address (char **arg,
13462 struct linespec_result *canonical,
13463 enum bptype type_wanted,
13464 char *addr_start, char **copy_arg)
13465{
13466 create_sals_from_address_default (arg, canonical, type_wanted,
13467 addr_start, copy_arg);
13468}
13469
13470static void
13471bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13472 struct linespec_result *canonical,
983af33b 13473 char *cond_string,
e7e0cddf 13474 char *extra_string,
983af33b
SDJ
13475 enum bptype type_wanted,
13476 enum bpdisp disposition,
13477 int thread,
13478 int task, int ignore_count,
13479 const struct breakpoint_ops *ops,
13480 int from_tty, int enabled,
44f238bb 13481 int internal, unsigned flags)
983af33b 13482{
023fa29b 13483 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13484 cond_string, extra_string,
13485 type_wanted,
983af33b
SDJ
13486 disposition, thread, task,
13487 ignore_count, ops, from_tty,
44f238bb 13488 enabled, internal, flags);
983af33b
SDJ
13489}
13490
13491static void
13492bkpt_decode_linespec (struct breakpoint *b, char **s,
13493 struct symtabs_and_lines *sals)
13494{
13495 decode_linespec_default (b, s, sals);
13496}
13497
06edf0c0
PA
13498/* Virtual table for internal breakpoints. */
13499
13500static void
13501internal_bkpt_re_set (struct breakpoint *b)
13502{
13503 switch (b->type)
13504 {
13505 /* Delete overlay event and longjmp master breakpoints; they
13506 will be reset later by breakpoint_re_set. */
13507 case bp_overlay_event:
13508 case bp_longjmp_master:
13509 case bp_std_terminate_master:
13510 case bp_exception_master:
13511 delete_breakpoint (b);
13512 break;
13513
13514 /* This breakpoint is special, it's set up when the inferior
13515 starts and we really don't want to touch it. */
13516 case bp_shlib_event:
13517
13518 /* Like bp_shlib_event, this breakpoint type is special. Once
13519 it is set up, we do not want to touch it. */
13520 case bp_thread_event:
13521 break;
13522 }
13523}
13524
13525static void
13526internal_bkpt_check_status (bpstat bs)
13527{
a9b3a50f
PA
13528 if (bs->breakpoint_at->type == bp_shlib_event)
13529 {
13530 /* If requested, stop when the dynamic linker notifies GDB of
13531 events. This allows the user to get control and place
13532 breakpoints in initializer routines for dynamically loaded
13533 objects (among other things). */
13534 bs->stop = stop_on_solib_events;
13535 bs->print = stop_on_solib_events;
13536 }
13537 else
13538 bs->stop = 0;
06edf0c0
PA
13539}
13540
13541static enum print_stop_action
13542internal_bkpt_print_it (bpstat bs)
13543{
06edf0c0 13544 struct breakpoint *b;
06edf0c0 13545
06edf0c0
PA
13546 b = bs->breakpoint_at;
13547
06edf0c0
PA
13548 switch (b->type)
13549 {
348d480f
PA
13550 case bp_shlib_event:
13551 /* Did we stop because the user set the stop_on_solib_events
13552 variable? (If so, we report this as a generic, "Stopped due
13553 to shlib event" message.) */
edcc5120 13554 print_solib_event (0);
348d480f
PA
13555 break;
13556
13557 case bp_thread_event:
13558 /* Not sure how we will get here.
13559 GDB should not stop for these breakpoints. */
13560 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13561 break;
13562
13563 case bp_overlay_event:
13564 /* By analogy with the thread event, GDB should not stop for these. */
13565 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13566 break;
13567
13568 case bp_longjmp_master:
13569 /* These should never be enabled. */
13570 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13571 break;
13572
13573 case bp_std_terminate_master:
13574 /* These should never be enabled. */
13575 printf_filtered (_("std::terminate Master Breakpoint: "
13576 "gdb should not stop!\n"));
348d480f
PA
13577 break;
13578
13579 case bp_exception_master:
13580 /* These should never be enabled. */
13581 printf_filtered (_("Exception Master Breakpoint: "
13582 "gdb should not stop!\n"));
06edf0c0
PA
13583 break;
13584 }
13585
001c8c33 13586 return PRINT_NOTHING;
06edf0c0
PA
13587}
13588
13589static void
13590internal_bkpt_print_mention (struct breakpoint *b)
13591{
13592 /* Nothing to mention. These breakpoints are internal. */
13593}
13594
06edf0c0
PA
13595/* Virtual table for momentary breakpoints */
13596
13597static void
13598momentary_bkpt_re_set (struct breakpoint *b)
13599{
13600 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13601 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13602 Otherwise these should have been blown away via the cleanup chain
13603 or by breakpoint_init_inferior when we rerun the executable. */
13604}
13605
13606static void
13607momentary_bkpt_check_status (bpstat bs)
13608{
13609 /* Nothing. The point of these breakpoints is causing a stop. */
13610}
13611
13612static enum print_stop_action
13613momentary_bkpt_print_it (bpstat bs)
13614{
79a45e25
PA
13615 struct ui_out *uiout = current_uiout;
13616
001c8c33 13617 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13618 {
001c8c33 13619 struct breakpoint *b = bs->breakpoint_at;
348d480f 13620
001c8c33
PA
13621 switch (b->type)
13622 {
13623 case bp_finish:
13624 ui_out_field_string
13625 (uiout, "reason",
13626 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13627 break;
348d480f 13628
001c8c33
PA
13629 case bp_until:
13630 ui_out_field_string
13631 (uiout, "reason",
13632 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13633 break;
13634 }
348d480f
PA
13635 }
13636
001c8c33 13637 return PRINT_UNKNOWN;
348d480f
PA
13638}
13639
06edf0c0
PA
13640static void
13641momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13642{
06edf0c0 13643 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13644}
13645
e2e4d78b
JK
13646/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13647
13648 It gets cleared already on the removal of the first one of such placed
13649 breakpoints. This is OK as they get all removed altogether. */
13650
13651static void
13652longjmp_bkpt_dtor (struct breakpoint *self)
13653{
13654 struct thread_info *tp = find_thread_id (self->thread);
13655
13656 if (tp)
13657 tp->initiating_frame = null_frame_id;
13658
13659 momentary_breakpoint_ops.dtor (self);
13660}
13661
55aa24fb
SDJ
13662/* Specific methods for probe breakpoints. */
13663
13664static int
13665bkpt_probe_insert_location (struct bp_location *bl)
13666{
13667 int v = bkpt_insert_location (bl);
13668
13669 if (v == 0)
13670 {
13671 /* The insertion was successful, now let's set the probe's semaphore
13672 if needed. */
729662a5
TT
13673 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13674 bl->probe.objfile,
13675 bl->gdbarch);
55aa24fb
SDJ
13676 }
13677
13678 return v;
13679}
13680
13681static int
13682bkpt_probe_remove_location (struct bp_location *bl)
13683{
13684 /* Let's clear the semaphore before removing the location. */
729662a5
TT
13685 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13686 bl->probe.objfile,
13687 bl->gdbarch);
55aa24fb
SDJ
13688
13689 return bkpt_remove_location (bl);
13690}
13691
13692static void
13693bkpt_probe_create_sals_from_address (char **arg,
13694 struct linespec_result *canonical,
13695 enum bptype type_wanted,
13696 char *addr_start, char **copy_arg)
13697{
13698 struct linespec_sals lsal;
13699
13700 lsal.sals = parse_probes (arg, canonical);
13701
13702 *copy_arg = xstrdup (canonical->addr_string);
13703 lsal.canonical = xstrdup (*copy_arg);
13704
13705 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13706}
13707
13708static void
13709bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13710 struct symtabs_and_lines *sals)
13711{
13712 *sals = parse_probes (s, NULL);
13713 if (!sals->sals)
13714 error (_("probe not found"));
13715}
13716
348d480f 13717/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13718
348d480f
PA
13719static void
13720tracepoint_re_set (struct breakpoint *b)
13721{
13722 breakpoint_re_set_default (b);
13723}
876fa593 13724
348d480f
PA
13725static int
13726tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13727 struct address_space *aspace, CORE_ADDR bp_addr,
13728 const struct target_waitstatus *ws)
348d480f
PA
13729{
13730 /* By definition, the inferior does not report stops at
13731 tracepoints. */
13732 return 0;
74960c60
VP
13733}
13734
13735static void
348d480f
PA
13736tracepoint_print_one_detail (const struct breakpoint *self,
13737 struct ui_out *uiout)
74960c60 13738{
d9b3f62e
PA
13739 struct tracepoint *tp = (struct tracepoint *) self;
13740 if (tp->static_trace_marker_id)
348d480f
PA
13741 {
13742 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13743
348d480f
PA
13744 ui_out_text (uiout, "\tmarker id is ");
13745 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13746 tp->static_trace_marker_id);
348d480f
PA
13747 ui_out_text (uiout, "\n");
13748 }
0d381245
VP
13749}
13750
a474d7c2 13751static void
348d480f 13752tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13753{
79a45e25 13754 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13755 return;
cc59ec59 13756
348d480f
PA
13757 switch (b->type)
13758 {
13759 case bp_tracepoint:
13760 printf_filtered (_("Tracepoint"));
13761 printf_filtered (_(" %d"), b->number);
13762 break;
13763 case bp_fast_tracepoint:
13764 printf_filtered (_("Fast tracepoint"));
13765 printf_filtered (_(" %d"), b->number);
13766 break;
13767 case bp_static_tracepoint:
13768 printf_filtered (_("Static tracepoint"));
13769 printf_filtered (_(" %d"), b->number);
13770 break;
13771 default:
13772 internal_error (__FILE__, __LINE__,
13773 _("unhandled tracepoint type %d"), (int) b->type);
13774 }
13775
13776 say_where (b);
a474d7c2
PA
13777}
13778
348d480f 13779static void
d9b3f62e 13780tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13781{
d9b3f62e
PA
13782 struct tracepoint *tp = (struct tracepoint *) self;
13783
13784 if (self->type == bp_fast_tracepoint)
348d480f 13785 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13786 if (self->type == bp_static_tracepoint)
348d480f 13787 fprintf_unfiltered (fp, "strace");
d9b3f62e 13788 else if (self->type == bp_tracepoint)
348d480f
PA
13789 fprintf_unfiltered (fp, "trace");
13790 else
13791 internal_error (__FILE__, __LINE__,
d9b3f62e 13792 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13793
d9b3f62e
PA
13794 fprintf_unfiltered (fp, " %s", self->addr_string);
13795 print_recreate_thread (self, fp);
13796
13797 if (tp->pass_count)
13798 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13799}
13800
983af33b
SDJ
13801static void
13802tracepoint_create_sals_from_address (char **arg,
13803 struct linespec_result *canonical,
13804 enum bptype type_wanted,
13805 char *addr_start, char **copy_arg)
13806{
13807 create_sals_from_address_default (arg, canonical, type_wanted,
13808 addr_start, copy_arg);
13809}
13810
13811static void
13812tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13813 struct linespec_result *canonical,
983af33b 13814 char *cond_string,
e7e0cddf 13815 char *extra_string,
983af33b
SDJ
13816 enum bptype type_wanted,
13817 enum bpdisp disposition,
13818 int thread,
13819 int task, int ignore_count,
13820 const struct breakpoint_ops *ops,
13821 int from_tty, int enabled,
44f238bb 13822 int internal, unsigned flags)
983af33b 13823{
023fa29b 13824 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13825 cond_string, extra_string,
13826 type_wanted,
983af33b
SDJ
13827 disposition, thread, task,
13828 ignore_count, ops, from_tty,
44f238bb 13829 enabled, internal, flags);
983af33b
SDJ
13830}
13831
13832static void
13833tracepoint_decode_linespec (struct breakpoint *b, char **s,
13834 struct symtabs_and_lines *sals)
13835{
13836 decode_linespec_default (b, s, sals);
13837}
13838
2060206e 13839struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13840
55aa24fb
SDJ
13841/* The breakpoint_ops structure to be use on tracepoints placed in a
13842 static probe. */
13843
13844static void
13845tracepoint_probe_create_sals_from_address (char **arg,
13846 struct linespec_result *canonical,
13847 enum bptype type_wanted,
13848 char *addr_start, char **copy_arg)
13849{
13850 /* We use the same method for breakpoint on probes. */
13851 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13852 addr_start, copy_arg);
13853}
13854
13855static void
13856tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13857 struct symtabs_and_lines *sals)
13858{
13859 /* We use the same method for breakpoint on probes. */
13860 bkpt_probe_decode_linespec (b, s, sals);
13861}
13862
13863static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13864
5c2b4418
HZ
13865/* Dprintf breakpoint_ops methods. */
13866
13867static void
13868dprintf_re_set (struct breakpoint *b)
13869{
13870 breakpoint_re_set_default (b);
13871
13872 /* This breakpoint could have been pending, and be resolved now, and
13873 if so, we should now have the extra string. If we don't, the
13874 dprintf was malformed when created, but we couldn't tell because
13875 we can't extract the extra string until the location is
13876 resolved. */
13877 if (b->loc != NULL && b->extra_string == NULL)
13878 error (_("Format string required"));
13879
13880 /* 1 - connect to target 1, that can run breakpoint commands.
13881 2 - create a dprintf, which resolves fine.
13882 3 - disconnect from target 1
13883 4 - connect to target 2, that can NOT run breakpoint commands.
13884
13885 After steps #3/#4, you'll want the dprintf command list to
13886 be updated, because target 1 and 2 may well return different
13887 answers for target_can_run_breakpoint_commands().
13888 Given absence of finer grained resetting, we get to do
13889 it all the time. */
13890 if (b->extra_string != NULL)
13891 update_dprintf_command_list (b);
13892}
13893
2d9442cc
HZ
13894/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13895
13896static void
13897dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13898{
13899 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13900 tp->extra_string);
13901 print_recreate_thread (tp, fp);
13902}
13903
9d6e6e84
HZ
13904/* Implement the "after_condition_true" breakpoint_ops method for
13905 dprintf.
13906
13907 dprintf's are implemented with regular commands in their command
13908 list, but we run the commands here instead of before presenting the
13909 stop to the user, as dprintf's don't actually cause a stop. This
13910 also makes it so that the commands of multiple dprintfs at the same
13911 address are all handled. */
13912
13913static void
13914dprintf_after_condition_true (struct bpstats *bs)
13915{
13916 struct cleanup *old_chain;
13917 struct bpstats tmp_bs = { NULL };
13918 struct bpstats *tmp_bs_p = &tmp_bs;
13919
13920 /* dprintf's never cause a stop. This wasn't set in the
13921 check_status hook instead because that would make the dprintf's
13922 condition not be evaluated. */
13923 bs->stop = 0;
13924
13925 /* Run the command list here. Take ownership of it instead of
13926 copying. We never want these commands to run later in
13927 bpstat_do_actions, if a breakpoint that causes a stop happens to
13928 be set at same address as this dprintf, or even if running the
13929 commands here throws. */
13930 tmp_bs.commands = bs->commands;
13931 bs->commands = NULL;
13932 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13933
13934 bpstat_do_actions_1 (&tmp_bs_p);
13935
13936 /* 'tmp_bs.commands' will usually be NULL by now, but
13937 bpstat_do_actions_1 may return early without processing the whole
13938 list. */
13939 do_cleanups (old_chain);
13940}
13941
983af33b
SDJ
13942/* The breakpoint_ops structure to be used on static tracepoints with
13943 markers (`-m'). */
13944
13945static void
13946strace_marker_create_sals_from_address (char **arg,
13947 struct linespec_result *canonical,
13948 enum bptype type_wanted,
13949 char *addr_start, char **copy_arg)
13950{
13951 struct linespec_sals lsal;
13952
13953 lsal.sals = decode_static_tracepoint_spec (arg);
13954
13955 *copy_arg = savestring (addr_start, *arg - addr_start);
13956
13957 canonical->addr_string = xstrdup (*copy_arg);
13958 lsal.canonical = xstrdup (*copy_arg);
13959 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13960}
13961
13962static void
13963strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13964 struct linespec_result *canonical,
983af33b 13965 char *cond_string,
e7e0cddf 13966 char *extra_string,
983af33b
SDJ
13967 enum bptype type_wanted,
13968 enum bpdisp disposition,
13969 int thread,
13970 int task, int ignore_count,
13971 const struct breakpoint_ops *ops,
13972 int from_tty, int enabled,
44f238bb 13973 int internal, unsigned flags)
983af33b
SDJ
13974{
13975 int i;
52d361e1
YQ
13976 struct linespec_sals *lsal = VEC_index (linespec_sals,
13977 canonical->sals, 0);
983af33b
SDJ
13978
13979 /* If the user is creating a static tracepoint by marker id
13980 (strace -m MARKER_ID), then store the sals index, so that
13981 breakpoint_re_set can try to match up which of the newly
13982 found markers corresponds to this one, and, don't try to
13983 expand multiple locations for each sal, given than SALS
13984 already should contain all sals for MARKER_ID. */
13985
13986 for (i = 0; i < lsal->sals.nelts; ++i)
13987 {
13988 struct symtabs_and_lines expanded;
13989 struct tracepoint *tp;
13990 struct cleanup *old_chain;
13991 char *addr_string;
13992
13993 expanded.nelts = 1;
13994 expanded.sals = &lsal->sals.sals[i];
13995
13996 addr_string = xstrdup (canonical->addr_string);
13997 old_chain = make_cleanup (xfree, addr_string);
13998
13999 tp = XCNEW (struct tracepoint);
14000 init_breakpoint_sal (&tp->base, gdbarch, expanded,
14001 addr_string, NULL,
e7e0cddf
SS
14002 cond_string, extra_string,
14003 type_wanted, disposition,
983af33b 14004 thread, task, ignore_count, ops,
44f238bb 14005 from_tty, enabled, internal, flags,
983af33b
SDJ
14006 canonical->special_display);
14007 /* Given that its possible to have multiple markers with
14008 the same string id, if the user is creating a static
14009 tracepoint by marker id ("strace -m MARKER_ID"), then
14010 store the sals index, so that breakpoint_re_set can
14011 try to match up which of the newly found markers
14012 corresponds to this one */
14013 tp->static_trace_marker_id_idx = i;
14014
14015 install_breakpoint (internal, &tp->base, 0);
14016
14017 discard_cleanups (old_chain);
14018 }
14019}
14020
14021static void
14022strace_marker_decode_linespec (struct breakpoint *b, char **s,
14023 struct symtabs_and_lines *sals)
14024{
14025 struct tracepoint *tp = (struct tracepoint *) b;
14026
14027 *sals = decode_static_tracepoint_spec (s);
14028 if (sals->nelts > tp->static_trace_marker_id_idx)
14029 {
14030 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
14031 sals->nelts = 1;
14032 }
14033 else
14034 error (_("marker %s not found"), tp->static_trace_marker_id);
14035}
14036
14037static struct breakpoint_ops strace_marker_breakpoint_ops;
14038
14039static int
14040strace_marker_p (struct breakpoint *b)
14041{
14042 return b->ops == &strace_marker_breakpoint_ops;
14043}
14044
53a5351d 14045/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 14046 structures. */
c906108c
SS
14047
14048void
fba45db2 14049delete_breakpoint (struct breakpoint *bpt)
c906108c 14050{
52f0bd74 14051 struct breakpoint *b;
c906108c 14052
8a3fe4f8 14053 gdb_assert (bpt != NULL);
c906108c 14054
4a64f543
MS
14055 /* Has this bp already been deleted? This can happen because
14056 multiple lists can hold pointers to bp's. bpstat lists are
14057 especial culprits.
14058
14059 One example of this happening is a watchpoint's scope bp. When
14060 the scope bp triggers, we notice that the watchpoint is out of
14061 scope, and delete it. We also delete its scope bp. But the
14062 scope bp is marked "auto-deleting", and is already on a bpstat.
14063 That bpstat is then checked for auto-deleting bp's, which are
14064 deleted.
14065
14066 A real solution to this problem might involve reference counts in
14067 bp's, and/or giving them pointers back to their referencing
14068 bpstat's, and teaching delete_breakpoint to only free a bp's
14069 storage when no more references were extent. A cheaper bandaid
14070 was chosen. */
c906108c
SS
14071 if (bpt->type == bp_none)
14072 return;
14073
4a64f543
MS
14074 /* At least avoid this stale reference until the reference counting
14075 of breakpoints gets resolved. */
d0fb5eae 14076 if (bpt->related_breakpoint != bpt)
e5a0a904 14077 {
d0fb5eae 14078 struct breakpoint *related;
3a5c3e22 14079 struct watchpoint *w;
d0fb5eae
JK
14080
14081 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 14082 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 14083 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
14084 w = (struct watchpoint *) bpt;
14085 else
14086 w = NULL;
14087 if (w != NULL)
14088 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
14089
14090 /* Unlink bpt from the bpt->related_breakpoint ring. */
14091 for (related = bpt; related->related_breakpoint != bpt;
14092 related = related->related_breakpoint);
14093 related->related_breakpoint = bpt->related_breakpoint;
14094 bpt->related_breakpoint = bpt;
e5a0a904
JK
14095 }
14096
a9634178
TJB
14097 /* watch_command_1 creates a watchpoint but only sets its number if
14098 update_watchpoint succeeds in creating its bp_locations. If there's
14099 a problem in that process, we'll be asked to delete the half-created
14100 watchpoint. In that case, don't announce the deletion. */
14101 if (bpt->number)
14102 observer_notify_breakpoint_deleted (bpt);
c906108c 14103
c906108c
SS
14104 if (breakpoint_chain == bpt)
14105 breakpoint_chain = bpt->next;
14106
c906108c
SS
14107 ALL_BREAKPOINTS (b)
14108 if (b->next == bpt)
c5aa993b
JM
14109 {
14110 b->next = bpt->next;
14111 break;
14112 }
c906108c 14113
f431efe5
PA
14114 /* Be sure no bpstat's are pointing at the breakpoint after it's
14115 been freed. */
14116 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 14117 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
14118 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14119 commands are associated with the bpstat; if we remove it here,
14120 then the later call to bpstat_do_actions (&stop_bpstat); in
14121 event-top.c won't do anything, and temporary breakpoints with
14122 commands won't work. */
14123
14124 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14125
4a64f543
MS
14126 /* Now that breakpoint is removed from breakpoint list, update the
14127 global location list. This will remove locations that used to
14128 belong to this breakpoint. Do this before freeing the breakpoint
14129 itself, since remove_breakpoint looks at location's owner. It
14130 might be better design to have location completely
14131 self-contained, but it's not the case now. */
44702360 14132 update_global_location_list (UGLL_DONT_INSERT);
74960c60 14133
348d480f 14134 bpt->ops->dtor (bpt);
4a64f543
MS
14135 /* On the chance that someone will soon try again to delete this
14136 same bp, we mark it as deleted before freeing its storage. */
c906108c 14137 bpt->type = bp_none;
b8c9b27d 14138 xfree (bpt);
c906108c
SS
14139}
14140
4d6140d9
AC
14141static void
14142do_delete_breakpoint_cleanup (void *b)
14143{
14144 delete_breakpoint (b);
14145}
14146
14147struct cleanup *
14148make_cleanup_delete_breakpoint (struct breakpoint *b)
14149{
14150 return make_cleanup (do_delete_breakpoint_cleanup, b);
14151}
14152
51be5b68
PA
14153/* Iterator function to call a user-provided callback function once
14154 for each of B and its related breakpoints. */
14155
14156static void
14157iterate_over_related_breakpoints (struct breakpoint *b,
14158 void (*function) (struct breakpoint *,
14159 void *),
14160 void *data)
14161{
14162 struct breakpoint *related;
14163
14164 related = b;
14165 do
14166 {
14167 struct breakpoint *next;
14168
14169 /* FUNCTION may delete RELATED. */
14170 next = related->related_breakpoint;
14171
14172 if (next == related)
14173 {
14174 /* RELATED is the last ring entry. */
14175 function (related, data);
14176
14177 /* FUNCTION may have deleted it, so we'd never reach back to
14178 B. There's nothing left to do anyway, so just break
14179 out. */
14180 break;
14181 }
14182 else
14183 function (related, data);
14184
14185 related = next;
14186 }
14187 while (related != b);
14188}
95a42b64
TT
14189
14190static void
14191do_delete_breakpoint (struct breakpoint *b, void *ignore)
14192{
14193 delete_breakpoint (b);
14194}
14195
51be5b68
PA
14196/* A callback for map_breakpoint_numbers that calls
14197 delete_breakpoint. */
14198
14199static void
14200do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14201{
14202 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14203}
14204
c906108c 14205void
fba45db2 14206delete_command (char *arg, int from_tty)
c906108c 14207{
35df4500 14208 struct breakpoint *b, *b_tmp;
c906108c 14209
ea9365bb
TT
14210 dont_repeat ();
14211
c906108c
SS
14212 if (arg == 0)
14213 {
14214 int breaks_to_delete = 0;
14215
46c6471b
PA
14216 /* Delete all breakpoints if no argument. Do not delete
14217 internal breakpoints, these have to be deleted with an
14218 explicit breakpoint number argument. */
c5aa993b 14219 ALL_BREAKPOINTS (b)
46c6471b 14220 if (user_breakpoint_p (b))
973d738b
DJ
14221 {
14222 breaks_to_delete = 1;
14223 break;
14224 }
c906108c
SS
14225
14226 /* Ask user only if there are some breakpoints to delete. */
14227 if (!from_tty
e2e0b3e5 14228 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14229 {
35df4500 14230 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14231 if (user_breakpoint_p (b))
c5aa993b 14232 delete_breakpoint (b);
c906108c
SS
14233 }
14234 }
14235 else
51be5b68 14236 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14237}
14238
0d381245
VP
14239static int
14240all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14241{
0d381245 14242 for (; loc; loc = loc->next)
8645ff69
UW
14243 if (!loc->shlib_disabled
14244 && !loc->pspace->executing_startup)
0d381245
VP
14245 return 0;
14246 return 1;
fe3f5fa8
VP
14247}
14248
776592bf
DE
14249/* Subroutine of update_breakpoint_locations to simplify it.
14250 Return non-zero if multiple fns in list LOC have the same name.
14251 Null names are ignored. */
14252
14253static int
14254ambiguous_names_p (struct bp_location *loc)
14255{
14256 struct bp_location *l;
14257 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14258 (int (*) (const void *,
14259 const void *)) streq,
776592bf
DE
14260 NULL, xcalloc, xfree);
14261
14262 for (l = loc; l != NULL; l = l->next)
14263 {
14264 const char **slot;
14265 const char *name = l->function_name;
14266
14267 /* Allow for some names to be NULL, ignore them. */
14268 if (name == NULL)
14269 continue;
14270
14271 slot = (const char **) htab_find_slot (htab, (const void *) name,
14272 INSERT);
4a64f543
MS
14273 /* NOTE: We can assume slot != NULL here because xcalloc never
14274 returns NULL. */
776592bf
DE
14275 if (*slot != NULL)
14276 {
14277 htab_delete (htab);
14278 return 1;
14279 }
14280 *slot = name;
14281 }
14282
14283 htab_delete (htab);
14284 return 0;
14285}
14286
0fb4aa4b
PA
14287/* When symbols change, it probably means the sources changed as well,
14288 and it might mean the static tracepoint markers are no longer at
14289 the same address or line numbers they used to be at last we
14290 checked. Losing your static tracepoints whenever you rebuild is
14291 undesirable. This function tries to resync/rematch gdb static
14292 tracepoints with the markers on the target, for static tracepoints
14293 that have not been set by marker id. Static tracepoint that have
14294 been set by marker id are reset by marker id in breakpoint_re_set.
14295 The heuristic is:
14296
14297 1) For a tracepoint set at a specific address, look for a marker at
14298 the old PC. If one is found there, assume to be the same marker.
14299 If the name / string id of the marker found is different from the
14300 previous known name, assume that means the user renamed the marker
14301 in the sources, and output a warning.
14302
14303 2) For a tracepoint set at a given line number, look for a marker
14304 at the new address of the old line number. If one is found there,
14305 assume to be the same marker. If the name / string id of the
14306 marker found is different from the previous known name, assume that
14307 means the user renamed the marker in the sources, and output a
14308 warning.
14309
14310 3) If a marker is no longer found at the same address or line, it
14311 may mean the marker no longer exists. But it may also just mean
14312 the code changed a bit. Maybe the user added a few lines of code
14313 that made the marker move up or down (in line number terms). Ask
14314 the target for info about the marker with the string id as we knew
14315 it. If found, update line number and address in the matching
14316 static tracepoint. This will get confused if there's more than one
14317 marker with the same ID (possible in UST, although unadvised
14318 precisely because it confuses tools). */
14319
14320static struct symtab_and_line
14321update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14322{
d9b3f62e 14323 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14324 struct static_tracepoint_marker marker;
14325 CORE_ADDR pc;
0fb4aa4b
PA
14326
14327 pc = sal.pc;
14328 if (sal.line)
14329 find_line_pc (sal.symtab, sal.line, &pc);
14330
14331 if (target_static_tracepoint_marker_at (pc, &marker))
14332 {
d9b3f62e 14333 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14334 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14335 b->number,
d9b3f62e 14336 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14337
d9b3f62e
PA
14338 xfree (tp->static_trace_marker_id);
14339 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14340 release_static_tracepoint_marker (&marker);
14341
14342 return sal;
14343 }
14344
14345 /* Old marker wasn't found on target at lineno. Try looking it up
14346 by string ID. */
14347 if (!sal.explicit_pc
14348 && sal.line != 0
14349 && sal.symtab != NULL
d9b3f62e 14350 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14351 {
14352 VEC(static_tracepoint_marker_p) *markers;
14353
14354 markers
d9b3f62e 14355 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14356
14357 if (!VEC_empty(static_tracepoint_marker_p, markers))
14358 {
80e1d417 14359 struct symtab_and_line sal2;
0fb4aa4b 14360 struct symbol *sym;
80e1d417 14361 struct static_tracepoint_marker *tpmarker;
79a45e25 14362 struct ui_out *uiout = current_uiout;
0fb4aa4b 14363
80e1d417 14364 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14365
d9b3f62e 14366 xfree (tp->static_trace_marker_id);
80e1d417 14367 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14368
14369 warning (_("marker for static tracepoint %d (%s) not "
14370 "found at previous line number"),
d9b3f62e 14371 b->number, tp->static_trace_marker_id);
0fb4aa4b 14372
80e1d417 14373 init_sal (&sal2);
0fb4aa4b 14374
80e1d417 14375 sal2.pc = tpmarker->address;
0fb4aa4b 14376
80e1d417
AS
14377 sal2 = find_pc_line (tpmarker->address, 0);
14378 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14379 ui_out_text (uiout, "Now in ");
14380 if (sym)
14381 {
14382 ui_out_field_string (uiout, "func",
14383 SYMBOL_PRINT_NAME (sym));
14384 ui_out_text (uiout, " at ");
14385 }
05cba821
JK
14386 ui_out_field_string (uiout, "file",
14387 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14388 ui_out_text (uiout, ":");
14389
14390 if (ui_out_is_mi_like_p (uiout))
14391 {
0b0865da 14392 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14393
f35a17b5 14394 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14395 }
14396
80e1d417 14397 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14398 ui_out_text (uiout, "\n");
14399
80e1d417 14400 b->loc->line_number = sal2.line;
2f202fde 14401 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14402
14403 xfree (b->addr_string);
14404 b->addr_string = xstrprintf ("%s:%d",
05cba821 14405 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14406 b->loc->line_number);
0fb4aa4b
PA
14407
14408 /* Might be nice to check if function changed, and warn if
14409 so. */
14410
80e1d417 14411 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14412 }
14413 }
14414 return sal;
14415}
14416
8d3788bd
VP
14417/* Returns 1 iff locations A and B are sufficiently same that
14418 we don't need to report breakpoint as changed. */
14419
14420static int
14421locations_are_equal (struct bp_location *a, struct bp_location *b)
14422{
14423 while (a && b)
14424 {
14425 if (a->address != b->address)
14426 return 0;
14427
14428 if (a->shlib_disabled != b->shlib_disabled)
14429 return 0;
14430
14431 if (a->enabled != b->enabled)
14432 return 0;
14433
14434 a = a->next;
14435 b = b->next;
14436 }
14437
14438 if ((a == NULL) != (b == NULL))
14439 return 0;
14440
14441 return 1;
14442}
14443
f1310107
TJB
14444/* Create new breakpoint locations for B (a hardware or software breakpoint)
14445 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14446 a ranged breakpoint. */
14447
0e30163f 14448void
0d381245 14449update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14450 struct symtabs_and_lines sals,
14451 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14452{
14453 int i;
0d381245
VP
14454 struct bp_location *existing_locations = b->loc;
14455
f8eba3c6
TT
14456 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14457 {
14458 /* Ranged breakpoints have only one start location and one end
14459 location. */
14460 b->enable_state = bp_disabled;
44702360 14461 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14462 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14463 "multiple locations found\n"),
14464 b->number);
14465 return;
14466 }
f1310107 14467
4a64f543
MS
14468 /* If there's no new locations, and all existing locations are
14469 pending, don't do anything. This optimizes the common case where
14470 all locations are in the same shared library, that was unloaded.
14471 We'd like to retain the location, so that when the library is
14472 loaded again, we don't loose the enabled/disabled status of the
14473 individual locations. */
0d381245 14474 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14475 return;
14476
fe3f5fa8
VP
14477 b->loc = NULL;
14478
0d381245 14479 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14480 {
f8eba3c6
TT
14481 struct bp_location *new_loc;
14482
14483 switch_to_program_space_and_thread (sals.sals[i].pspace);
14484
14485 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14486
0d381245
VP
14487 /* Reparse conditions, they might contain references to the
14488 old symtab. */
14489 if (b->cond_string != NULL)
14490 {
bbc13ae3 14491 const char *s;
bfd189b1 14492 volatile struct gdb_exception e;
fe3f5fa8 14493
0d381245
VP
14494 s = b->cond_string;
14495 TRY_CATCH (e, RETURN_MASK_ERROR)
14496 {
1bb9788d
TT
14497 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14498 block_for_pc (sals.sals[i].pc),
0d381245
VP
14499 0);
14500 }
14501 if (e.reason < 0)
14502 {
3e43a32a
MS
14503 warning (_("failed to reevaluate condition "
14504 "for breakpoint %d: %s"),
0d381245
VP
14505 b->number, e.message);
14506 new_loc->enabled = 0;
14507 }
14508 }
fe3f5fa8 14509
f1310107
TJB
14510 if (sals_end.nelts)
14511 {
14512 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14513
14514 new_loc->length = end - sals.sals[0].pc + 1;
14515 }
0d381245 14516 }
fe3f5fa8 14517
514f746b
AR
14518 /* Update locations of permanent breakpoints. */
14519 if (b->enable_state == bp_permanent)
14520 make_breakpoint_permanent (b);
14521
4a64f543
MS
14522 /* If possible, carry over 'disable' status from existing
14523 breakpoints. */
0d381245
VP
14524 {
14525 struct bp_location *e = existing_locations;
776592bf
DE
14526 /* If there are multiple breakpoints with the same function name,
14527 e.g. for inline functions, comparing function names won't work.
14528 Instead compare pc addresses; this is just a heuristic as things
14529 may have moved, but in practice it gives the correct answer
14530 often enough until a better solution is found. */
14531 int have_ambiguous_names = ambiguous_names_p (b->loc);
14532
0d381245
VP
14533 for (; e; e = e->next)
14534 {
14535 if (!e->enabled && e->function_name)
14536 {
14537 struct bp_location *l = b->loc;
776592bf
DE
14538 if (have_ambiguous_names)
14539 {
14540 for (; l; l = l->next)
f1310107 14541 if (breakpoint_locations_match (e, l))
776592bf
DE
14542 {
14543 l->enabled = 0;
14544 break;
14545 }
14546 }
14547 else
14548 {
14549 for (; l; l = l->next)
14550 if (l->function_name
14551 && strcmp (e->function_name, l->function_name) == 0)
14552 {
14553 l->enabled = 0;
14554 break;
14555 }
14556 }
0d381245
VP
14557 }
14558 }
14559 }
fe3f5fa8 14560
8d3788bd
VP
14561 if (!locations_are_equal (existing_locations, b->loc))
14562 observer_notify_breakpoint_modified (b);
14563
44702360 14564 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14565}
14566
ef23e705
TJB
14567/* Find the SaL locations corresponding to the given ADDR_STRING.
14568 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14569
14570static struct symtabs_and_lines
14571addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14572{
14573 char *s;
02d20e4a 14574 struct symtabs_and_lines sals = {0};
f8eba3c6 14575 volatile struct gdb_exception e;
ef23e705 14576
983af33b 14577 gdb_assert (b->ops != NULL);
ef23e705 14578 s = addr_string;
ef23e705
TJB
14579
14580 TRY_CATCH (e, RETURN_MASK_ERROR)
14581 {
983af33b 14582 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14583 }
14584 if (e.reason < 0)
14585 {
14586 int not_found_and_ok = 0;
14587 /* For pending breakpoints, it's expected that parsing will
14588 fail until the right shared library is loaded. User has
14589 already told to create pending breakpoints and don't need
14590 extra messages. If breakpoint is in bp_shlib_disabled
14591 state, then user already saw the message about that
14592 breakpoint being disabled, and don't want to see more
14593 errors. */
58438ac1 14594 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14595 && (b->condition_not_parsed
14596 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14597 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14598 || b->enable_state == bp_disabled))
14599 not_found_and_ok = 1;
14600
14601 if (!not_found_and_ok)
14602 {
14603 /* We surely don't want to warn about the same breakpoint
14604 10 times. One solution, implemented here, is disable
14605 the breakpoint on error. Another solution would be to
14606 have separate 'warning emitted' flag. Since this
14607 happens only when a binary has changed, I don't know
14608 which approach is better. */
14609 b->enable_state = bp_disabled;
14610 throw_exception (e);
14611 }
14612 }
14613
58438ac1 14614 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14615 {
f8eba3c6 14616 int i;
ef23e705 14617
f8eba3c6
TT
14618 for (i = 0; i < sals.nelts; ++i)
14619 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14620 if (b->condition_not_parsed && s && s[0])
14621 {
ed1d1739
KS
14622 char *cond_string, *extra_string;
14623 int thread, task;
ef23e705
TJB
14624
14625 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14626 &cond_string, &thread, &task,
14627 &extra_string);
ef23e705
TJB
14628 if (cond_string)
14629 b->cond_string = cond_string;
14630 b->thread = thread;
14631 b->task = task;
e7e0cddf
SS
14632 if (extra_string)
14633 b->extra_string = extra_string;
ef23e705
TJB
14634 b->condition_not_parsed = 0;
14635 }
14636
983af33b 14637 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14638 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14639
58438ac1
TT
14640 *found = 1;
14641 }
14642 else
14643 *found = 0;
ef23e705
TJB
14644
14645 return sals;
14646}
14647
348d480f
PA
14648/* The default re_set method, for typical hardware or software
14649 breakpoints. Reevaluate the breakpoint and recreate its
14650 locations. */
14651
14652static void
28010a5d 14653breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14654{
14655 int found;
f1310107 14656 struct symtabs_and_lines sals, sals_end;
ef23e705 14657 struct symtabs_and_lines expanded = {0};
f1310107 14658 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14659
14660 sals = addr_string_to_sals (b, b->addr_string, &found);
14661 if (found)
14662 {
14663 make_cleanup (xfree, sals.sals);
f8eba3c6 14664 expanded = sals;
ef23e705
TJB
14665 }
14666
f1310107
TJB
14667 if (b->addr_string_range_end)
14668 {
14669 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14670 if (found)
14671 {
14672 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14673 expanded_end = sals_end;
f1310107
TJB
14674 }
14675 }
14676
14677 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14678}
14679
983af33b
SDJ
14680/* Default method for creating SALs from an address string. It basically
14681 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14682
14683static void
14684create_sals_from_address_default (char **arg,
14685 struct linespec_result *canonical,
14686 enum bptype type_wanted,
14687 char *addr_start, char **copy_arg)
14688{
14689 parse_breakpoint_sals (arg, canonical);
14690}
14691
14692/* Call create_breakpoints_sal for the given arguments. This is the default
14693 function for the `create_breakpoints_sal' method of
14694 breakpoint_ops. */
14695
14696static void
14697create_breakpoints_sal_default (struct gdbarch *gdbarch,
14698 struct linespec_result *canonical,
983af33b 14699 char *cond_string,
e7e0cddf 14700 char *extra_string,
983af33b
SDJ
14701 enum bptype type_wanted,
14702 enum bpdisp disposition,
14703 int thread,
14704 int task, int ignore_count,
14705 const struct breakpoint_ops *ops,
14706 int from_tty, int enabled,
44f238bb 14707 int internal, unsigned flags)
983af33b
SDJ
14708{
14709 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14710 extra_string,
983af33b
SDJ
14711 type_wanted, disposition,
14712 thread, task, ignore_count, ops, from_tty,
44f238bb 14713 enabled, internal, flags);
983af33b
SDJ
14714}
14715
14716/* Decode the line represented by S by calling decode_line_full. This is the
14717 default function for the `decode_linespec' method of breakpoint_ops. */
14718
14719static void
14720decode_linespec_default (struct breakpoint *b, char **s,
14721 struct symtabs_and_lines *sals)
14722{
14723 struct linespec_result canonical;
14724
14725 init_linespec_result (&canonical);
14726 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14727 (struct symtab *) NULL, 0,
14728 &canonical, multiple_symbols_all,
14729 b->filter);
14730
14731 /* We should get 0 or 1 resulting SALs. */
14732 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14733
14734 if (VEC_length (linespec_sals, canonical.sals) > 0)
14735 {
14736 struct linespec_sals *lsal;
14737
14738 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14739 *sals = lsal->sals;
14740 /* Arrange it so the destructor does not free the
14741 contents. */
14742 lsal->sals.sals = NULL;
14743 }
14744
14745 destroy_linespec_result (&canonical);
14746}
14747
28010a5d
PA
14748/* Prepare the global context for a re-set of breakpoint B. */
14749
14750static struct cleanup *
14751prepare_re_set_context (struct breakpoint *b)
14752{
14753 struct cleanup *cleanups;
14754
14755 input_radix = b->input_radix;
14756 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14757 if (b->pspace != NULL)
14758 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14759 set_language (b->language);
14760
14761 return cleanups;
ef23e705
TJB
14762}
14763
c906108c
SS
14764/* Reset a breakpoint given it's struct breakpoint * BINT.
14765 The value we return ends up being the return value from catch_errors.
14766 Unused in this case. */
14767
14768static int
4efb68b1 14769breakpoint_re_set_one (void *bint)
c906108c 14770{
4a64f543 14771 /* Get past catch_errs. */
53a5351d 14772 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14773 struct cleanup *cleanups;
c906108c 14774
348d480f
PA
14775 cleanups = prepare_re_set_context (b);
14776 b->ops->re_set (b);
14777 do_cleanups (cleanups);
c906108c
SS
14778 return 0;
14779}
14780
69de3c6a 14781/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14782void
69de3c6a 14783breakpoint_re_set (void)
c906108c 14784{
35df4500 14785 struct breakpoint *b, *b_tmp;
c906108c
SS
14786 enum language save_language;
14787 int save_input_radix;
6c95b8df 14788 struct cleanup *old_chain;
c5aa993b 14789
c906108c
SS
14790 save_language = current_language->la_language;
14791 save_input_radix = input_radix;
6c95b8df
PA
14792 old_chain = save_current_program_space ();
14793
35df4500 14794 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14795 {
4a64f543 14796 /* Format possible error msg. */
fe3f5fa8 14797 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14798 b->number);
14799 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14800 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14801 do_cleanups (cleanups);
c5aa993b 14802 }
c906108c
SS
14803 set_language (save_language);
14804 input_radix = save_input_radix;
e62c965a 14805
0756c555 14806 jit_breakpoint_re_set ();
4efc6507 14807
6c95b8df
PA
14808 do_cleanups (old_chain);
14809
af02033e
PP
14810 create_overlay_event_breakpoint ();
14811 create_longjmp_master_breakpoint ();
14812 create_std_terminate_master_breakpoint ();
186c406b 14813 create_exception_master_breakpoint ();
c906108c
SS
14814}
14815\f
c906108c
SS
14816/* Reset the thread number of this breakpoint:
14817
14818 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14819 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14820void
fba45db2 14821breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14822{
14823 if (b->thread != -1)
14824 {
39f77062
KB
14825 if (in_thread_list (inferior_ptid))
14826 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14827
14828 /* We're being called after following a fork. The new fork is
14829 selected as current, and unless this was a vfork will have a
14830 different program space from the original thread. Reset that
14831 as well. */
14832 b->loc->pspace = current_program_space;
c906108c
SS
14833 }
14834}
14835
03ac34d5
MS
14836/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14837 If from_tty is nonzero, it prints a message to that effect,
14838 which ends with a period (no newline). */
14839
c906108c 14840void
fba45db2 14841set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14842{
52f0bd74 14843 struct breakpoint *b;
c906108c
SS
14844
14845 if (count < 0)
14846 count = 0;
14847
14848 ALL_BREAKPOINTS (b)
14849 if (b->number == bptnum)
c5aa993b 14850 {
d77f58be
SS
14851 if (is_tracepoint (b))
14852 {
14853 if (from_tty && count != 0)
14854 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14855 bptnum);
14856 return;
14857 }
14858
c5aa993b 14859 b->ignore_count = count;
221ea385
KS
14860 if (from_tty)
14861 {
14862 if (count == 0)
3e43a32a
MS
14863 printf_filtered (_("Will stop next time "
14864 "breakpoint %d is reached."),
221ea385
KS
14865 bptnum);
14866 else if (count == 1)
a3f17187 14867 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14868 bptnum);
14869 else
3e43a32a
MS
14870 printf_filtered (_("Will ignore next %d "
14871 "crossings of breakpoint %d."),
221ea385
KS
14872 count, bptnum);
14873 }
8d3788bd 14874 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14875 return;
14876 }
c906108c 14877
8a3fe4f8 14878 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14879}
14880
c906108c
SS
14881/* Command to set ignore-count of breakpoint N to COUNT. */
14882
14883static void
fba45db2 14884ignore_command (char *args, int from_tty)
c906108c
SS
14885{
14886 char *p = args;
52f0bd74 14887 int num;
c906108c
SS
14888
14889 if (p == 0)
e2e0b3e5 14890 error_no_arg (_("a breakpoint number"));
c5aa993b 14891
c906108c 14892 num = get_number (&p);
5c44784c 14893 if (num == 0)
8a3fe4f8 14894 error (_("bad breakpoint number: '%s'"), args);
c906108c 14895 if (*p == 0)
8a3fe4f8 14896 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14897
14898 set_ignore_count (num,
14899 longest_to_int (value_as_long (parse_and_eval (p))),
14900 from_tty);
221ea385
KS
14901 if (from_tty)
14902 printf_filtered ("\n");
c906108c
SS
14903}
14904\f
14905/* Call FUNCTION on each of the breakpoints
14906 whose numbers are given in ARGS. */
14907
14908static void
95a42b64
TT
14909map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14910 void *),
14911 void *data)
c906108c 14912{
52f0bd74
AC
14913 int num;
14914 struct breakpoint *b, *tmp;
11cf8741 14915 int match;
197f0a60 14916 struct get_number_or_range_state state;
c906108c 14917
197f0a60 14918 if (args == 0)
e2e0b3e5 14919 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14920
197f0a60
TT
14921 init_number_or_range (&state, args);
14922
14923 while (!state.finished)
c906108c 14924 {
e799154c 14925 const char *p = state.string;
197f0a60 14926
11cf8741 14927 match = 0;
c5aa993b 14928
197f0a60 14929 num = get_number_or_range (&state);
5c44784c 14930 if (num == 0)
c5aa993b 14931 {
8a3fe4f8 14932 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14933 }
14934 else
14935 {
14936 ALL_BREAKPOINTS_SAFE (b, tmp)
14937 if (b->number == num)
14938 {
11cf8741 14939 match = 1;
cdac0397 14940 function (b, data);
11cf8741 14941 break;
5c44784c 14942 }
11cf8741 14943 if (match == 0)
a3f17187 14944 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14945 }
c906108c
SS
14946 }
14947}
14948
0d381245
VP
14949static struct bp_location *
14950find_location_by_number (char *number)
14951{
14952 char *dot = strchr (number, '.');
14953 char *p1;
14954 int bp_num;
14955 int loc_num;
14956 struct breakpoint *b;
14957 struct bp_location *loc;
14958
14959 *dot = '\0';
14960
14961 p1 = number;
197f0a60 14962 bp_num = get_number (&p1);
0d381245
VP
14963 if (bp_num == 0)
14964 error (_("Bad breakpoint number '%s'"), number);
14965
14966 ALL_BREAKPOINTS (b)
14967 if (b->number == bp_num)
14968 {
14969 break;
14970 }
14971
14972 if (!b || b->number != bp_num)
14973 error (_("Bad breakpoint number '%s'"), number);
14974
14975 p1 = dot+1;
197f0a60 14976 loc_num = get_number (&p1);
0d381245
VP
14977 if (loc_num == 0)
14978 error (_("Bad breakpoint location number '%s'"), number);
14979
14980 --loc_num;
14981 loc = b->loc;
14982 for (;loc_num && loc; --loc_num, loc = loc->next)
14983 ;
14984 if (!loc)
14985 error (_("Bad breakpoint location number '%s'"), dot+1);
14986
14987 return loc;
14988}
14989
14990
1900040c
MS
14991/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14992 If from_tty is nonzero, it prints a message to that effect,
14993 which ends with a period (no newline). */
14994
c906108c 14995void
fba45db2 14996disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14997{
14998 /* Never disable a watchpoint scope breakpoint; we want to
14999 hit them when we leave scope so we can delete both the
15000 watchpoint and its scope breakpoint at that time. */
15001 if (bpt->type == bp_watchpoint_scope)
15002 return;
15003
c2c6d25f 15004 /* You can't disable permanent breakpoints. */
b5de0fa7 15005 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
15006 return;
15007
b5de0fa7 15008 bpt->enable_state = bp_disabled;
c906108c 15009
b775012e
LM
15010 /* Mark breakpoint locations modified. */
15011 mark_breakpoint_modified (bpt);
15012
d248b706
KY
15013 if (target_supports_enable_disable_tracepoint ()
15014 && current_trace_status ()->running && is_tracepoint (bpt))
15015 {
15016 struct bp_location *location;
15017
15018 for (location = bpt->loc; location; location = location->next)
15019 target_disable_tracepoint (location);
15020 }
15021
44702360 15022 update_global_location_list (UGLL_DONT_INSERT);
c906108c 15023
8d3788bd 15024 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15025}
15026
51be5b68
PA
15027/* A callback for iterate_over_related_breakpoints. */
15028
15029static void
15030do_disable_breakpoint (struct breakpoint *b, void *ignore)
15031{
15032 disable_breakpoint (b);
15033}
15034
95a42b64
TT
15035/* A callback for map_breakpoint_numbers that calls
15036 disable_breakpoint. */
15037
15038static void
15039do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
15040{
51be5b68 15041 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
15042}
15043
c906108c 15044static void
fba45db2 15045disable_command (char *args, int from_tty)
c906108c 15046{
c906108c 15047 if (args == 0)
46c6471b
PA
15048 {
15049 struct breakpoint *bpt;
15050
15051 ALL_BREAKPOINTS (bpt)
15052 if (user_breakpoint_p (bpt))
15053 disable_breakpoint (bpt);
15054 }
9eaabc75 15055 else
0d381245 15056 {
9eaabc75
MW
15057 char *num = extract_arg (&args);
15058
15059 while (num)
d248b706 15060 {
9eaabc75 15061 if (strchr (num, '.'))
b775012e 15062 {
9eaabc75
MW
15063 struct bp_location *loc = find_location_by_number (num);
15064
15065 if (loc)
15066 {
15067 if (loc->enabled)
15068 {
15069 loc->enabled = 0;
15070 mark_breakpoint_location_modified (loc);
15071 }
15072 if (target_supports_enable_disable_tracepoint ()
15073 && current_trace_status ()->running && loc->owner
15074 && is_tracepoint (loc->owner))
15075 target_disable_tracepoint (loc);
15076 }
44702360 15077 update_global_location_list (UGLL_DONT_INSERT);
b775012e 15078 }
9eaabc75
MW
15079 else
15080 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15081 num = extract_arg (&args);
d248b706 15082 }
0d381245 15083 }
c906108c
SS
15084}
15085
15086static void
816338b5
SS
15087enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15088 int count)
c906108c 15089{
afe38095 15090 int target_resources_ok;
c906108c
SS
15091
15092 if (bpt->type == bp_hardware_breakpoint)
15093 {
15094 int i;
c5aa993b 15095 i = hw_breakpoint_used_count ();
53a5351d 15096 target_resources_ok =
d92524f1 15097 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 15098 i + 1, 0);
c906108c 15099 if (target_resources_ok == 0)
8a3fe4f8 15100 error (_("No hardware breakpoint support in the target."));
c906108c 15101 else if (target_resources_ok < 0)
8a3fe4f8 15102 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
15103 }
15104
cc60f2e3 15105 if (is_watchpoint (bpt))
c906108c 15106 {
d07205c2
JK
15107 /* Initialize it just to avoid a GCC false warning. */
15108 enum enable_state orig_enable_state = 0;
bfd189b1 15109 volatile struct gdb_exception e;
dde02812
ES
15110
15111 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 15112 {
3a5c3e22
PA
15113 struct watchpoint *w = (struct watchpoint *) bpt;
15114
1e718ff1
TJB
15115 orig_enable_state = bpt->enable_state;
15116 bpt->enable_state = bp_enabled;
3a5c3e22 15117 update_watchpoint (w, 1 /* reparse */);
c906108c 15118 }
dde02812 15119 if (e.reason < 0)
c5aa993b 15120 {
1e718ff1 15121 bpt->enable_state = orig_enable_state;
dde02812
ES
15122 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15123 bpt->number);
15124 return;
c5aa993b 15125 }
c906108c 15126 }
0101ce28 15127
b4c291bb
KH
15128 if (bpt->enable_state != bp_permanent)
15129 bpt->enable_state = bp_enabled;
d248b706 15130
b775012e
LM
15131 bpt->enable_state = bp_enabled;
15132
15133 /* Mark breakpoint locations modified. */
15134 mark_breakpoint_modified (bpt);
15135
d248b706
KY
15136 if (target_supports_enable_disable_tracepoint ()
15137 && current_trace_status ()->running && is_tracepoint (bpt))
15138 {
15139 struct bp_location *location;
15140
15141 for (location = bpt->loc; location; location = location->next)
15142 target_enable_tracepoint (location);
15143 }
15144
b4c291bb 15145 bpt->disposition = disposition;
816338b5 15146 bpt->enable_count = count;
44702360 15147 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 15148
8d3788bd 15149 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15150}
15151
fe3f5fa8 15152
c906108c 15153void
fba45db2 15154enable_breakpoint (struct breakpoint *bpt)
c906108c 15155{
816338b5 15156 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15157}
15158
15159static void
15160do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15161{
15162 enable_breakpoint (bpt);
c906108c
SS
15163}
15164
95a42b64
TT
15165/* A callback for map_breakpoint_numbers that calls
15166 enable_breakpoint. */
15167
15168static void
15169do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15170{
51be5b68 15171 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15172}
15173
c906108c
SS
15174/* The enable command enables the specified breakpoints (or all defined
15175 breakpoints) so they once again become (or continue to be) effective
1272ad14 15176 in stopping the inferior. */
c906108c 15177
c906108c 15178static void
fba45db2 15179enable_command (char *args, int from_tty)
c906108c 15180{
c906108c 15181 if (args == 0)
46c6471b
PA
15182 {
15183 struct breakpoint *bpt;
15184
15185 ALL_BREAKPOINTS (bpt)
15186 if (user_breakpoint_p (bpt))
15187 enable_breakpoint (bpt);
15188 }
9eaabc75 15189 else
0d381245 15190 {
9eaabc75
MW
15191 char *num = extract_arg (&args);
15192
15193 while (num)
d248b706 15194 {
9eaabc75 15195 if (strchr (num, '.'))
b775012e 15196 {
9eaabc75
MW
15197 struct bp_location *loc = find_location_by_number (num);
15198
15199 if (loc)
15200 {
15201 if (!loc->enabled)
15202 {
15203 loc->enabled = 1;
15204 mark_breakpoint_location_modified (loc);
15205 }
15206 if (target_supports_enable_disable_tracepoint ()
15207 && current_trace_status ()->running && loc->owner
15208 && is_tracepoint (loc->owner))
15209 target_enable_tracepoint (loc);
15210 }
44702360 15211 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15212 }
9eaabc75
MW
15213 else
15214 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15215 num = extract_arg (&args);
d248b706 15216 }
0d381245 15217 }
c906108c
SS
15218}
15219
816338b5
SS
15220/* This struct packages up disposition data for application to multiple
15221 breakpoints. */
15222
15223struct disp_data
15224{
15225 enum bpdisp disp;
15226 int count;
15227};
15228
c906108c 15229static void
51be5b68
PA
15230do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15231{
816338b5 15232 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15233
816338b5 15234 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15235}
15236
15237static void
15238do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15239{
816338b5 15240 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15241
15242 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15243}
15244
c906108c 15245static void
fba45db2 15246enable_once_command (char *args, int from_tty)
c906108c 15247{
51be5b68 15248 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15249}
15250
816338b5
SS
15251static void
15252do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15253{
15254 struct disp_data disp = { disp_disable, *(int *) countptr };
15255
15256 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15257}
15258
15259static void
15260enable_count_command (char *args, int from_tty)
15261{
15262 int count = get_number (&args);
15263
15264 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15265}
15266
c906108c 15267static void
51be5b68 15268do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15269{
816338b5 15270 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15271
15272 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15273}
15274
c906108c 15275static void
fba45db2 15276enable_delete_command (char *args, int from_tty)
c906108c 15277{
51be5b68 15278 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15279}
15280\f
fa8d40ab
JJ
15281static void
15282set_breakpoint_cmd (char *args, int from_tty)
15283{
15284}
15285
15286static void
15287show_breakpoint_cmd (char *args, int from_tty)
15288{
15289}
15290
1f3b5d1b
PP
15291/* Invalidate last known value of any hardware watchpoint if
15292 the memory which that value represents has been written to by
15293 GDB itself. */
15294
15295static void
8de0566d
YQ
15296invalidate_bp_value_on_memory_change (struct inferior *inferior,
15297 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15298 const bfd_byte *data)
15299{
15300 struct breakpoint *bp;
15301
15302 ALL_BREAKPOINTS (bp)
15303 if (bp->enable_state == bp_enabled
3a5c3e22 15304 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15305 {
3a5c3e22 15306 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15307
3a5c3e22
PA
15308 if (wp->val_valid && wp->val)
15309 {
15310 struct bp_location *loc;
15311
15312 for (loc = bp->loc; loc != NULL; loc = loc->next)
15313 if (loc->loc_type == bp_loc_hardware_watchpoint
15314 && loc->address + loc->length > addr
15315 && addr + len > loc->address)
15316 {
15317 value_free (wp->val);
15318 wp->val = NULL;
15319 wp->val_valid = 0;
15320 }
15321 }
1f3b5d1b
PP
15322 }
15323}
15324
8181d85f
DJ
15325/* Create and insert a raw software breakpoint at PC. Return an
15326 identifier, which should be used to remove the breakpoint later.
15327 In general, places which call this should be using something on the
15328 breakpoint chain instead; this function should be eliminated
15329 someday. */
15330
15331void *
6c95b8df
PA
15332deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
15333 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
15334{
15335 struct bp_target_info *bp_tgt;
ef370185 15336 struct bp_location *bl;
8181d85f 15337
41bf6aca 15338 bp_tgt = XCNEW (struct bp_target_info);
8181d85f 15339
6c95b8df 15340 bp_tgt->placed_address_space = aspace;
8181d85f 15341 bp_tgt->placed_address = pc;
6c95b8df 15342
ef370185
JB
15343 /* If an unconditional non-raw breakpoint is already inserted at
15344 that location, there's no need to insert another. However, with
15345 target-side evaluation of breakpoint conditions, if the
15346 breakpoint that is currently inserted on the target is
15347 conditional, we need to make it unconditional. Note that a
15348 breakpoint with target-side commands is not reported even if
15349 unconditional, so we need to remove the commands from the target
15350 as well. */
15351 bl = find_non_raw_software_breakpoint_inserted_here (aspace, pc);
15352 if (bl != NULL
15353 && VEC_empty (agent_expr_p, bl->target_info.conditions)
15354 && VEC_empty (agent_expr_p, bl->target_info.tcommands))
15355 {
15356 bp_target_info_copy_insertion_state (bp_tgt, &bl->target_info);
15357 return bp_tgt;
15358 }
15359
a6d9a66e 15360 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
15361 {
15362 /* Could not insert the breakpoint. */
15363 xfree (bp_tgt);
15364 return NULL;
15365 }
15366
15367 return bp_tgt;
15368}
15369
4a64f543
MS
15370/* Remove a breakpoint BP inserted by
15371 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
15372
15373int
a6d9a66e 15374deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
15375{
15376 struct bp_target_info *bp_tgt = bp;
ef370185
JB
15377 struct address_space *aspace = bp_tgt->placed_address_space;
15378 CORE_ADDR address = bp_tgt->placed_address;
15379 struct bp_location *bl;
8181d85f
DJ
15380 int ret;
15381
ef370185
JB
15382 bl = find_non_raw_software_breakpoint_inserted_here (aspace, address);
15383
15384 /* Only remove the raw breakpoint if there are no other non-raw
15385 breakpoints still inserted at this location. Otherwise, we would
15386 be effectively disabling those breakpoints. */
15387 if (bl == NULL)
15388 ret = target_remove_breakpoint (gdbarch, bp_tgt);
15389 else if (!VEC_empty (agent_expr_p, bl->target_info.conditions)
15390 || !VEC_empty (agent_expr_p, bl->target_info.tcommands))
15391 {
15392 /* The target is evaluating conditions, and when we inserted the
15393 software single-step breakpoint, we had made the breakpoint
15394 unconditional and command-less on the target side. Reinsert
15395 to restore the conditions/commands. */
15396 ret = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
15397 }
15398 else
15399 ret = 0;
15400
8181d85f
DJ
15401 xfree (bp_tgt);
15402
15403 return ret;
15404}
15405
8181d85f
DJ
15406/* Create and insert a breakpoint for software single step. */
15407
15408void
6c95b8df 15409insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15410 struct address_space *aspace,
15411 CORE_ADDR next_pc)
8181d85f
DJ
15412{
15413 void **bpt_p;
15414
15415 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
15416 {
15417 bpt_p = &single_step_breakpoints[0];
15418 single_step_gdbarch[0] = gdbarch;
15419 }
8181d85f
DJ
15420 else
15421 {
15422 gdb_assert (single_step_breakpoints[1] == NULL);
15423 bpt_p = &single_step_breakpoints[1];
a6d9a66e 15424 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
15425 }
15426
4a64f543
MS
15427 /* NOTE drow/2006-04-11: A future improvement to this function would
15428 be to only create the breakpoints once, and actually put them on
15429 the breakpoint chain. That would let us use set_raw_breakpoint.
15430 We could adjust the addresses each time they were needed. Doing
15431 this requires corresponding changes elsewhere where single step
15432 breakpoints are handled, however. So, for now, we use this. */
8181d85f 15433
6c95b8df 15434 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 15435 if (*bpt_p == NULL)
5af949e3
UW
15436 error (_("Could not insert single-step breakpoint at %s"),
15437 paddress (gdbarch, next_pc));
8181d85f
DJ
15438}
15439
f02253f1
HZ
15440/* Check if the breakpoints used for software single stepping
15441 were inserted or not. */
15442
15443int
15444single_step_breakpoints_inserted (void)
15445{
15446 return (single_step_breakpoints[0] != NULL
15447 || single_step_breakpoints[1] != NULL);
15448}
15449
8181d85f
DJ
15450/* Remove and delete any breakpoints used for software single step. */
15451
15452void
15453remove_single_step_breakpoints (void)
15454{
15455 gdb_assert (single_step_breakpoints[0] != NULL);
15456
15457 /* See insert_single_step_breakpoint for more about this deprecated
15458 call. */
a6d9a66e
UW
15459 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15460 single_step_breakpoints[0]);
15461 single_step_gdbarch[0] = NULL;
8181d85f
DJ
15462 single_step_breakpoints[0] = NULL;
15463
15464 if (single_step_breakpoints[1] != NULL)
15465 {
a6d9a66e
UW
15466 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15467 single_step_breakpoints[1]);
15468 single_step_gdbarch[1] = NULL;
8181d85f
DJ
15469 single_step_breakpoints[1] = NULL;
15470 }
15471}
15472
d03285ec
UW
15473/* Delete software single step breakpoints without removing them from
15474 the inferior. This is intended to be used if the inferior's address
15475 space where they were inserted is already gone, e.g. after exit or
15476 exec. */
15477
15478void
15479cancel_single_step_breakpoints (void)
15480{
15481 int i;
15482
15483 for (i = 0; i < 2; i++)
15484 if (single_step_breakpoints[i])
15485 {
15486 xfree (single_step_breakpoints[i]);
15487 single_step_breakpoints[i] = NULL;
15488 single_step_gdbarch[i] = NULL;
15489 }
15490}
15491
15492/* Detach software single-step breakpoints from INFERIOR_PTID without
15493 removing them. */
15494
15495static void
15496detach_single_step_breakpoints (void)
15497{
15498 int i;
15499
15500 for (i = 0; i < 2; i++)
15501 if (single_step_breakpoints[i])
15502 target_remove_breakpoint (single_step_gdbarch[i],
15503 single_step_breakpoints[i]);
15504}
15505
ef370185
JB
15506/* Find the software single-step breakpoint that inserted at PC.
15507 Returns its slot if found, and -1 if not found. */
1aafd4da 15508
ef370185
JB
15509static int
15510find_single_step_breakpoint (struct address_space *aspace,
15511 CORE_ADDR pc)
1aafd4da
UW
15512{
15513 int i;
15514
15515 for (i = 0; i < 2; i++)
15516 {
15517 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
15518 if (bp_tgt
15519 && breakpoint_address_match (bp_tgt->placed_address_space,
15520 bp_tgt->placed_address,
15521 aspace, pc))
ef370185 15522 return i;
1aafd4da
UW
15523 }
15524
ef370185
JB
15525 return -1;
15526}
15527
15528/* Check whether a software single-step breakpoint is inserted at
15529 PC. */
15530
15531int
15532single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15533 CORE_ADDR pc)
15534{
15535 return find_single_step_breakpoint (aspace, pc) >= 0;
1aafd4da
UW
15536}
15537
a96d9b2e
SDJ
15538/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15539 non-zero otherwise. */
15540static int
15541is_syscall_catchpoint_enabled (struct breakpoint *bp)
15542{
15543 if (syscall_catchpoint_p (bp)
15544 && bp->enable_state != bp_disabled
15545 && bp->enable_state != bp_call_disabled)
15546 return 1;
15547 else
15548 return 0;
15549}
15550
15551int
15552catch_syscall_enabled (void)
15553{
fa3064dd
YQ
15554 struct catch_syscall_inferior_data *inf_data
15555 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15556
fa3064dd 15557 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15558}
15559
15560int
15561catching_syscall_number (int syscall_number)
15562{
15563 struct breakpoint *bp;
15564
15565 ALL_BREAKPOINTS (bp)
15566 if (is_syscall_catchpoint_enabled (bp))
15567 {
be5c67c1
PA
15568 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15569
15570 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15571 {
15572 int i, iter;
15573 for (i = 0;
be5c67c1 15574 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15575 i++)
15576 if (syscall_number == iter)
15577 return 1;
15578 }
15579 else
15580 return 1;
15581 }
15582
15583 return 0;
15584}
15585
15586/* Complete syscall names. Used by "catch syscall". */
49c4e619 15587static VEC (char_ptr) *
a96d9b2e 15588catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15589 const char *text, const char *word)
a96d9b2e
SDJ
15590{
15591 const char **list = get_syscall_names ();
49c4e619 15592 VEC (char_ptr) *retlist
b45627a0 15593 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15594
c38eea1a
MS
15595 xfree (list);
15596 return retlist;
a96d9b2e
SDJ
15597}
15598
1042e4c0
SS
15599/* Tracepoint-specific operations. */
15600
15601/* Set tracepoint count to NUM. */
15602static void
15603set_tracepoint_count (int num)
15604{
15605 tracepoint_count = num;
4fa62494 15606 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15607}
15608
70221824 15609static void
1042e4c0
SS
15610trace_command (char *arg, int from_tty)
15611{
55aa24fb
SDJ
15612 struct breakpoint_ops *ops;
15613 const char *arg_cp = arg;
15614
15615 if (arg && probe_linespec_to_ops (&arg_cp))
15616 ops = &tracepoint_probe_breakpoint_ops;
15617 else
15618 ops = &tracepoint_breakpoint_ops;
15619
558a9d82
YQ
15620 create_breakpoint (get_current_arch (),
15621 arg,
15622 NULL, 0, NULL, 1 /* parse arg */,
15623 0 /* tempflag */,
15624 bp_tracepoint /* type_wanted */,
15625 0 /* Ignore count */,
15626 pending_break_support,
15627 ops,
15628 from_tty,
15629 1 /* enabled */,
15630 0 /* internal */, 0);
1042e4c0
SS
15631}
15632
70221824 15633static void
7a697b8d
SS
15634ftrace_command (char *arg, int from_tty)
15635{
558a9d82
YQ
15636 create_breakpoint (get_current_arch (),
15637 arg,
15638 NULL, 0, NULL, 1 /* parse arg */,
15639 0 /* tempflag */,
15640 bp_fast_tracepoint /* type_wanted */,
15641 0 /* Ignore count */,
15642 pending_break_support,
15643 &tracepoint_breakpoint_ops,
15644 from_tty,
15645 1 /* enabled */,
15646 0 /* internal */, 0);
0fb4aa4b
PA
15647}
15648
15649/* strace command implementation. Creates a static tracepoint. */
15650
70221824 15651static void
0fb4aa4b
PA
15652strace_command (char *arg, int from_tty)
15653{
983af33b
SDJ
15654 struct breakpoint_ops *ops;
15655
15656 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15657 or with a normal static tracepoint. */
15658 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15659 ops = &strace_marker_breakpoint_ops;
15660 else
15661 ops = &tracepoint_breakpoint_ops;
15662
558a9d82
YQ
15663 create_breakpoint (get_current_arch (),
15664 arg,
15665 NULL, 0, NULL, 1 /* parse arg */,
15666 0 /* tempflag */,
15667 bp_static_tracepoint /* type_wanted */,
15668 0 /* Ignore count */,
15669 pending_break_support,
15670 ops,
15671 from_tty,
15672 1 /* enabled */,
15673 0 /* internal */, 0);
7a697b8d
SS
15674}
15675
409873ef
SS
15676/* Set up a fake reader function that gets command lines from a linked
15677 list that was acquired during tracepoint uploading. */
15678
15679static struct uploaded_tp *this_utp;
3149d8c1 15680static int next_cmd;
409873ef
SS
15681
15682static char *
15683read_uploaded_action (void)
15684{
15685 char *rslt;
15686
3149d8c1 15687 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15688
3149d8c1 15689 next_cmd++;
409873ef
SS
15690
15691 return rslt;
15692}
15693
00bf0b85
SS
15694/* Given information about a tracepoint as recorded on a target (which
15695 can be either a live system or a trace file), attempt to create an
15696 equivalent GDB tracepoint. This is not a reliable process, since
15697 the target does not necessarily have all the information used when
15698 the tracepoint was originally defined. */
15699
d9b3f62e 15700struct tracepoint *
00bf0b85 15701create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15702{
409873ef 15703 char *addr_str, small_buf[100];
d9b3f62e 15704 struct tracepoint *tp;
fd9b8c24 15705
409873ef
SS
15706 if (utp->at_string)
15707 addr_str = utp->at_string;
15708 else
15709 {
15710 /* In the absence of a source location, fall back to raw
15711 address. Since there is no way to confirm that the address
15712 means the same thing as when the trace was started, warn the
15713 user. */
3e43a32a
MS
15714 warning (_("Uploaded tracepoint %d has no "
15715 "source location, using raw address"),
409873ef 15716 utp->number);
8c042590 15717 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15718 addr_str = small_buf;
15719 }
15720
15721 /* There's not much we can do with a sequence of bytecodes. */
15722 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15723 warning (_("Uploaded tracepoint %d condition "
15724 "has no source form, ignoring it"),
409873ef 15725 utp->number);
d5551862 15726
8cdf0e15 15727 if (!create_breakpoint (get_current_arch (),
409873ef 15728 addr_str,
e7e0cddf
SS
15729 utp->cond_string, -1, NULL,
15730 0 /* parse cond/thread */,
8cdf0e15 15731 0 /* tempflag */,
0fb4aa4b 15732 utp->type /* type_wanted */,
8cdf0e15
VP
15733 0 /* Ignore count */,
15734 pending_break_support,
348d480f 15735 &tracepoint_breakpoint_ops,
8cdf0e15 15736 0 /* from_tty */,
84f4c1fe 15737 utp->enabled /* enabled */,
44f238bb
PA
15738 0 /* internal */,
15739 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15740 return NULL;
15741
409873ef 15742 /* Get the tracepoint we just created. */
fd9b8c24
PA
15743 tp = get_tracepoint (tracepoint_count);
15744 gdb_assert (tp != NULL);
d5551862 15745
00bf0b85
SS
15746 if (utp->pass > 0)
15747 {
8c042590
PM
15748 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15749 tp->base.number);
00bf0b85 15750
409873ef 15751 trace_pass_command (small_buf, 0);
00bf0b85
SS
15752 }
15753
409873ef
SS
15754 /* If we have uploaded versions of the original commands, set up a
15755 special-purpose "reader" function and call the usual command line
15756 reader, then pass the result to the breakpoint command-setting
15757 function. */
3149d8c1 15758 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15759 {
409873ef 15760 struct command_line *cmd_list;
00bf0b85 15761
409873ef 15762 this_utp = utp;
3149d8c1 15763 next_cmd = 0;
d5551862 15764
409873ef
SS
15765 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15766
d9b3f62e 15767 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15768 }
3149d8c1
SS
15769 else if (!VEC_empty (char_ptr, utp->actions)
15770 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15771 warning (_("Uploaded tracepoint %d actions "
15772 "have no source form, ignoring them"),
409873ef 15773 utp->number);
00bf0b85 15774
f196051f
SS
15775 /* Copy any status information that might be available. */
15776 tp->base.hit_count = utp->hit_count;
15777 tp->traceframe_usage = utp->traceframe_usage;
15778
00bf0b85 15779 return tp;
d9b3f62e 15780}
00bf0b85 15781
1042e4c0
SS
15782/* Print information on tracepoint number TPNUM_EXP, or all if
15783 omitted. */
15784
15785static void
e5a67952 15786tracepoints_info (char *args, int from_tty)
1042e4c0 15787{
79a45e25 15788 struct ui_out *uiout = current_uiout;
e5a67952 15789 int num_printed;
1042e4c0 15790
e5a67952 15791 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15792
15793 if (num_printed == 0)
1042e4c0 15794 {
e5a67952 15795 if (args == NULL || *args == '\0')
d77f58be
SS
15796 ui_out_message (uiout, 0, "No tracepoints.\n");
15797 else
e5a67952 15798 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15799 }
ad443146
SS
15800
15801 default_collect_info ();
1042e4c0
SS
15802}
15803
4a64f543 15804/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15805 Not supported by all targets. */
15806static void
15807enable_trace_command (char *args, int from_tty)
15808{
15809 enable_command (args, from_tty);
15810}
15811
4a64f543 15812/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15813 Not supported by all targets. */
15814static void
15815disable_trace_command (char *args, int from_tty)
15816{
15817 disable_command (args, from_tty);
15818}
15819
4a64f543 15820/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15821static void
15822delete_trace_command (char *arg, int from_tty)
15823{
35df4500 15824 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15825
15826 dont_repeat ();
15827
15828 if (arg == 0)
15829 {
15830 int breaks_to_delete = 0;
15831
15832 /* Delete all breakpoints if no argument.
15833 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15834 have to be deleted with an explicit breakpoint number
15835 argument. */
1042e4c0 15836 ALL_TRACEPOINTS (b)
46c6471b 15837 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15838 {
15839 breaks_to_delete = 1;
15840 break;
15841 }
1042e4c0
SS
15842
15843 /* Ask user only if there are some breakpoints to delete. */
15844 if (!from_tty
15845 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15846 {
35df4500 15847 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15848 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15849 delete_breakpoint (b);
1042e4c0
SS
15850 }
15851 }
15852 else
51be5b68 15853 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15854}
15855
197f0a60
TT
15856/* Helper function for trace_pass_command. */
15857
15858static void
d9b3f62e 15859trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15860{
d9b3f62e 15861 tp->pass_count = count;
6f6484cd 15862 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15863 if (from_tty)
15864 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15865 tp->base.number, count);
197f0a60
TT
15866}
15867
1042e4c0
SS
15868/* Set passcount for tracepoint.
15869
15870 First command argument is passcount, second is tracepoint number.
15871 If tracepoint number omitted, apply to most recently defined.
15872 Also accepts special argument "all". */
15873
15874static void
15875trace_pass_command (char *args, int from_tty)
15876{
d9b3f62e 15877 struct tracepoint *t1;
1042e4c0 15878 unsigned int count;
1042e4c0
SS
15879
15880 if (args == 0 || *args == 0)
3e43a32a
MS
15881 error (_("passcount command requires an "
15882 "argument (count + optional TP num)"));
1042e4c0 15883
4a64f543 15884 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15885
529480d0 15886 args = skip_spaces (args);
1042e4c0
SS
15887 if (*args && strncasecmp (args, "all", 3) == 0)
15888 {
d9b3f62e
PA
15889 struct breakpoint *b;
15890
1042e4c0 15891 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15892 if (*args)
15893 error (_("Junk at end of arguments."));
1042e4c0 15894
d9b3f62e 15895 ALL_TRACEPOINTS (b)
197f0a60 15896 {
d9b3f62e 15897 t1 = (struct tracepoint *) b;
197f0a60
TT
15898 trace_pass_set_count (t1, count, from_tty);
15899 }
15900 }
15901 else if (*args == '\0')
1042e4c0 15902 {
5fa1d40e 15903 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15904 if (t1)
197f0a60
TT
15905 trace_pass_set_count (t1, count, from_tty);
15906 }
15907 else
15908 {
15909 struct get_number_or_range_state state;
15910
15911 init_number_or_range (&state, args);
15912 while (!state.finished)
1042e4c0 15913 {
5fa1d40e 15914 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15915 if (t1)
15916 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15917 }
15918 }
1042e4c0
SS
15919}
15920
d9b3f62e 15921struct tracepoint *
1042e4c0
SS
15922get_tracepoint (int num)
15923{
15924 struct breakpoint *t;
15925
15926 ALL_TRACEPOINTS (t)
15927 if (t->number == num)
d9b3f62e 15928 return (struct tracepoint *) t;
1042e4c0
SS
15929
15930 return NULL;
15931}
15932
d5551862
SS
15933/* Find the tracepoint with the given target-side number (which may be
15934 different from the tracepoint number after disconnecting and
15935 reconnecting). */
15936
d9b3f62e 15937struct tracepoint *
d5551862
SS
15938get_tracepoint_by_number_on_target (int num)
15939{
d9b3f62e 15940 struct breakpoint *b;
d5551862 15941
d9b3f62e
PA
15942 ALL_TRACEPOINTS (b)
15943 {
15944 struct tracepoint *t = (struct tracepoint *) b;
15945
15946 if (t->number_on_target == num)
15947 return t;
15948 }
d5551862
SS
15949
15950 return NULL;
15951}
15952
1042e4c0 15953/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15954 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15955 If the argument is missing, the most recent tracepoint
15956 (tracepoint_count) is returned. */
15957
d9b3f62e 15958struct tracepoint *
197f0a60 15959get_tracepoint_by_number (char **arg,
5fa1d40e 15960 struct get_number_or_range_state *state)
1042e4c0 15961{
1042e4c0
SS
15962 struct breakpoint *t;
15963 int tpnum;
15964 char *instring = arg == NULL ? NULL : *arg;
15965
197f0a60
TT
15966 if (state)
15967 {
15968 gdb_assert (!state->finished);
15969 tpnum = get_number_or_range (state);
15970 }
15971 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15972 tpnum = tracepoint_count;
1042e4c0 15973 else
197f0a60 15974 tpnum = get_number (arg);
1042e4c0
SS
15975
15976 if (tpnum <= 0)
15977 {
15978 if (instring && *instring)
15979 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15980 instring);
15981 else
5fa1d40e 15982 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15983 return NULL;
15984 }
15985
15986 ALL_TRACEPOINTS (t)
15987 if (t->number == tpnum)
15988 {
d9b3f62e 15989 return (struct tracepoint *) t;
1042e4c0
SS
15990 }
15991
1042e4c0
SS
15992 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15993 return NULL;
15994}
15995
d9b3f62e
PA
15996void
15997print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15998{
15999 if (b->thread != -1)
16000 fprintf_unfiltered (fp, " thread %d", b->thread);
16001
16002 if (b->task != 0)
16003 fprintf_unfiltered (fp, " task %d", b->task);
16004
16005 fprintf_unfiltered (fp, "\n");
16006}
16007
6149aea9
PA
16008/* Save information on user settable breakpoints (watchpoints, etc) to
16009 a new script file named FILENAME. If FILTER is non-NULL, call it
16010 on each breakpoint and only include the ones for which it returns
16011 non-zero. */
16012
1042e4c0 16013static void
6149aea9
PA
16014save_breakpoints (char *filename, int from_tty,
16015 int (*filter) (const struct breakpoint *))
1042e4c0
SS
16016{
16017 struct breakpoint *tp;
6149aea9 16018 int any = 0;
1042e4c0 16019 struct cleanup *cleanup;
a7bdde9e 16020 struct ui_file *fp;
6149aea9 16021 int extra_trace_bits = 0;
1042e4c0 16022
6149aea9
PA
16023 if (filename == 0 || *filename == 0)
16024 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
16025
16026 /* See if we have anything to save. */
6149aea9 16027 ALL_BREAKPOINTS (tp)
1042e4c0 16028 {
6149aea9 16029 /* Skip internal and momentary breakpoints. */
09d682a4 16030 if (!user_breakpoint_p (tp))
6149aea9
PA
16031 continue;
16032
16033 /* If we have a filter, only save the breakpoints it accepts. */
16034 if (filter && !filter (tp))
16035 continue;
16036
16037 any = 1;
16038
16039 if (is_tracepoint (tp))
16040 {
16041 extra_trace_bits = 1;
16042
16043 /* We can stop searching. */
16044 break;
16045 }
1042e4c0 16046 }
6149aea9
PA
16047
16048 if (!any)
1042e4c0 16049 {
6149aea9 16050 warning (_("Nothing to save."));
1042e4c0
SS
16051 return;
16052 }
16053
c718be47
PA
16054 filename = tilde_expand (filename);
16055 cleanup = make_cleanup (xfree, filename);
16056 fp = gdb_fopen (filename, "w");
059fb39f 16057 if (!fp)
6149aea9
PA
16058 error (_("Unable to open file '%s' for saving (%s)"),
16059 filename, safe_strerror (errno));
a7bdde9e 16060 make_cleanup_ui_file_delete (fp);
8bf6485c 16061
6149aea9
PA
16062 if (extra_trace_bits)
16063 save_trace_state_variables (fp);
8bf6485c 16064
6149aea9 16065 ALL_BREAKPOINTS (tp)
1042e4c0 16066 {
6149aea9 16067 /* Skip internal and momentary breakpoints. */
09d682a4 16068 if (!user_breakpoint_p (tp))
6149aea9 16069 continue;
8bf6485c 16070
6149aea9
PA
16071 /* If we have a filter, only save the breakpoints it accepts. */
16072 if (filter && !filter (tp))
16073 continue;
16074
348d480f 16075 tp->ops->print_recreate (tp, fp);
1042e4c0 16076
6149aea9
PA
16077 /* Note, we can't rely on tp->number for anything, as we can't
16078 assume the recreated breakpoint numbers will match. Use $bpnum
16079 instead. */
16080
16081 if (tp->cond_string)
16082 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
16083
16084 if (tp->ignore_count)
16085 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
16086
2d9442cc 16087 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 16088 {
a7bdde9e
VP
16089 volatile struct gdb_exception ex;
16090
6149aea9 16091 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 16092
79a45e25 16093 ui_out_redirect (current_uiout, fp);
14dba4b4 16094 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 16095 {
79a45e25 16096 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 16097 }
79a45e25 16098 ui_out_redirect (current_uiout, NULL);
1042e4c0 16099
a7bdde9e
VP
16100 if (ex.reason < 0)
16101 throw_exception (ex);
1042e4c0 16102
a7bdde9e 16103 fprintf_unfiltered (fp, " end\n");
1042e4c0 16104 }
6149aea9
PA
16105
16106 if (tp->enable_state == bp_disabled)
16107 fprintf_unfiltered (fp, "disable\n");
16108
16109 /* If this is a multi-location breakpoint, check if the locations
16110 should be individually disabled. Watchpoint locations are
16111 special, and not user visible. */
16112 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
16113 {
16114 struct bp_location *loc;
16115 int n = 1;
16116
16117 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
16118 if (!loc->enabled)
16119 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
16120 }
1042e4c0 16121 }
8bf6485c 16122
6149aea9 16123 if (extra_trace_bits && *default_collect)
8bf6485c
SS
16124 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
16125
1042e4c0 16126 if (from_tty)
6149aea9 16127 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 16128 do_cleanups (cleanup);
6149aea9
PA
16129}
16130
16131/* The `save breakpoints' command. */
16132
16133static void
16134save_breakpoints_command (char *args, int from_tty)
16135{
16136 save_breakpoints (args, from_tty, NULL);
16137}
16138
16139/* The `save tracepoints' command. */
16140
16141static void
16142save_tracepoints_command (char *args, int from_tty)
16143{
16144 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
16145}
16146
16147/* Create a vector of all tracepoints. */
16148
16149VEC(breakpoint_p) *
eeae04df 16150all_tracepoints (void)
1042e4c0
SS
16151{
16152 VEC(breakpoint_p) *tp_vec = 0;
16153 struct breakpoint *tp;
16154
16155 ALL_TRACEPOINTS (tp)
16156 {
16157 VEC_safe_push (breakpoint_p, tp_vec, tp);
16158 }
16159
16160 return tp_vec;
16161}
16162
c906108c 16163\f
4a64f543
MS
16164/* This help string is used for the break, hbreak, tbreak and thbreak
16165 commands. It is defined as a macro to prevent duplication.
16166 COMMAND should be a string constant containing the name of the
16167 command. */
31e2b00f 16168#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
16169command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
16170PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16171probe point. Accepted values are `-probe' (for a generic, automatically\n\
16172guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
16173LOCATION may be a line number, function name, or \"*\" and an address.\n\
16174If a line number is specified, break at start of code for that line.\n\
16175If a function is specified, break at start of code for that function.\n\
16176If an address is specified, break at that exact address.\n\
dc10affe
PA
16177With no LOCATION, uses current execution address of the selected\n\
16178stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
16179\n\
16180THREADNUM is the number from \"info threads\".\n\
16181CONDITION is a boolean expression.\n\
16182\n\
d41c0fc8
PA
16183Multiple breakpoints at one place are permitted, and useful if their\n\
16184conditions are different.\n\
31e2b00f
AS
16185\n\
16186Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16187
44feb3ce
TT
16188/* List of subcommands for "catch". */
16189static struct cmd_list_element *catch_cmdlist;
16190
16191/* List of subcommands for "tcatch". */
16192static struct cmd_list_element *tcatch_cmdlist;
16193
9ac4176b 16194void
44feb3ce 16195add_catch_command (char *name, char *docstring,
82ae6c8d 16196 cmd_sfunc_ftype *sfunc,
625e8578 16197 completer_ftype *completer,
44feb3ce
TT
16198 void *user_data_catch,
16199 void *user_data_tcatch)
16200{
16201 struct cmd_list_element *command;
16202
16203 command = add_cmd (name, class_breakpoint, NULL, docstring,
16204 &catch_cmdlist);
16205 set_cmd_sfunc (command, sfunc);
16206 set_cmd_context (command, user_data_catch);
a96d9b2e 16207 set_cmd_completer (command, completer);
44feb3ce
TT
16208
16209 command = add_cmd (name, class_breakpoint, NULL, docstring,
16210 &tcatch_cmdlist);
16211 set_cmd_sfunc (command, sfunc);
16212 set_cmd_context (command, user_data_tcatch);
a96d9b2e 16213 set_cmd_completer (command, completer);
44feb3ce
TT
16214}
16215
6c95b8df 16216static void
a79b8f6e 16217clear_syscall_counts (struct inferior *inf)
6c95b8df 16218{
fa3064dd
YQ
16219 struct catch_syscall_inferior_data *inf_data
16220 = get_catch_syscall_inferior_data (inf);
16221
16222 inf_data->total_syscalls_count = 0;
16223 inf_data->any_syscall_count = 0;
16224 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
16225}
16226
6149aea9
PA
16227static void
16228save_command (char *arg, int from_tty)
16229{
3e43a32a
MS
16230 printf_unfiltered (_("\"save\" must be followed by "
16231 "the name of a save subcommand.\n"));
635c7e8a 16232 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
16233}
16234
84f4c1fe
PM
16235struct breakpoint *
16236iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16237 void *data)
16238{
35df4500 16239 struct breakpoint *b, *b_tmp;
84f4c1fe 16240
35df4500 16241 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
16242 {
16243 if ((*callback) (b, data))
16244 return b;
16245 }
16246
16247 return NULL;
16248}
16249
0574c78f
GB
16250/* Zero if any of the breakpoint's locations could be a location where
16251 functions have been inlined, nonzero otherwise. */
16252
16253static int
16254is_non_inline_function (struct breakpoint *b)
16255{
16256 /* The shared library event breakpoint is set on the address of a
16257 non-inline function. */
16258 if (b->type == bp_shlib_event)
16259 return 1;
16260
16261 return 0;
16262}
16263
16264/* Nonzero if the specified PC cannot be a location where functions
16265 have been inlined. */
16266
16267int
09ac7c10
TT
16268pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16269 const struct target_waitstatus *ws)
0574c78f
GB
16270{
16271 struct breakpoint *b;
16272 struct bp_location *bl;
16273
16274 ALL_BREAKPOINTS (b)
16275 {
16276 if (!is_non_inline_function (b))
16277 continue;
16278
16279 for (bl = b->loc; bl != NULL; bl = bl->next)
16280 {
16281 if (!bl->shlib_disabled
09ac7c10 16282 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16283 return 1;
16284 }
16285 }
16286
16287 return 0;
16288}
16289
2f202fde
JK
16290/* Remove any references to OBJFILE which is going to be freed. */
16291
16292void
16293breakpoint_free_objfile (struct objfile *objfile)
16294{
16295 struct bp_location **locp, *loc;
16296
16297 ALL_BP_LOCATIONS (loc, locp)
16298 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
16299 loc->symtab = NULL;
16300}
16301
2060206e
PA
16302void
16303initialize_breakpoint_ops (void)
16304{
16305 static int initialized = 0;
16306
16307 struct breakpoint_ops *ops;
16308
16309 if (initialized)
16310 return;
16311 initialized = 1;
16312
16313 /* The breakpoint_ops structure to be inherit by all kinds of
16314 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16315 internal and momentary breakpoints, etc.). */
16316 ops = &bkpt_base_breakpoint_ops;
16317 *ops = base_breakpoint_ops;
16318 ops->re_set = bkpt_re_set;
16319 ops->insert_location = bkpt_insert_location;
16320 ops->remove_location = bkpt_remove_location;
16321 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16322 ops->create_sals_from_address = bkpt_create_sals_from_address;
16323 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16324 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16325
16326 /* The breakpoint_ops structure to be used in regular breakpoints. */
16327 ops = &bkpt_breakpoint_ops;
16328 *ops = bkpt_base_breakpoint_ops;
16329 ops->re_set = bkpt_re_set;
16330 ops->resources_needed = bkpt_resources_needed;
16331 ops->print_it = bkpt_print_it;
16332 ops->print_mention = bkpt_print_mention;
16333 ops->print_recreate = bkpt_print_recreate;
16334
16335 /* Ranged breakpoints. */
16336 ops = &ranged_breakpoint_ops;
16337 *ops = bkpt_breakpoint_ops;
16338 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16339 ops->resources_needed = resources_needed_ranged_breakpoint;
16340 ops->print_it = print_it_ranged_breakpoint;
16341 ops->print_one = print_one_ranged_breakpoint;
16342 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16343 ops->print_mention = print_mention_ranged_breakpoint;
16344 ops->print_recreate = print_recreate_ranged_breakpoint;
16345
16346 /* Internal breakpoints. */
16347 ops = &internal_breakpoint_ops;
16348 *ops = bkpt_base_breakpoint_ops;
16349 ops->re_set = internal_bkpt_re_set;
16350 ops->check_status = internal_bkpt_check_status;
16351 ops->print_it = internal_bkpt_print_it;
16352 ops->print_mention = internal_bkpt_print_mention;
16353
16354 /* Momentary breakpoints. */
16355 ops = &momentary_breakpoint_ops;
16356 *ops = bkpt_base_breakpoint_ops;
16357 ops->re_set = momentary_bkpt_re_set;
16358 ops->check_status = momentary_bkpt_check_status;
16359 ops->print_it = momentary_bkpt_print_it;
16360 ops->print_mention = momentary_bkpt_print_mention;
16361
e2e4d78b
JK
16362 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16363 ops = &longjmp_breakpoint_ops;
16364 *ops = momentary_breakpoint_ops;
16365 ops->dtor = longjmp_bkpt_dtor;
16366
55aa24fb
SDJ
16367 /* Probe breakpoints. */
16368 ops = &bkpt_probe_breakpoint_ops;
16369 *ops = bkpt_breakpoint_ops;
16370 ops->insert_location = bkpt_probe_insert_location;
16371 ops->remove_location = bkpt_probe_remove_location;
16372 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16373 ops->decode_linespec = bkpt_probe_decode_linespec;
16374
2060206e
PA
16375 /* Watchpoints. */
16376 ops = &watchpoint_breakpoint_ops;
16377 *ops = base_breakpoint_ops;
3a5c3e22 16378 ops->dtor = dtor_watchpoint;
2060206e
PA
16379 ops->re_set = re_set_watchpoint;
16380 ops->insert_location = insert_watchpoint;
16381 ops->remove_location = remove_watchpoint;
16382 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16383 ops->check_status = check_status_watchpoint;
16384 ops->resources_needed = resources_needed_watchpoint;
16385 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16386 ops->print_it = print_it_watchpoint;
16387 ops->print_mention = print_mention_watchpoint;
16388 ops->print_recreate = print_recreate_watchpoint;
427cd150 16389 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16390
16391 /* Masked watchpoints. */
16392 ops = &masked_watchpoint_breakpoint_ops;
16393 *ops = watchpoint_breakpoint_ops;
16394 ops->insert_location = insert_masked_watchpoint;
16395 ops->remove_location = remove_masked_watchpoint;
16396 ops->resources_needed = resources_needed_masked_watchpoint;
16397 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16398 ops->print_it = print_it_masked_watchpoint;
16399 ops->print_one_detail = print_one_detail_masked_watchpoint;
16400 ops->print_mention = print_mention_masked_watchpoint;
16401 ops->print_recreate = print_recreate_masked_watchpoint;
16402
16403 /* Tracepoints. */
16404 ops = &tracepoint_breakpoint_ops;
16405 *ops = base_breakpoint_ops;
16406 ops->re_set = tracepoint_re_set;
16407 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16408 ops->print_one_detail = tracepoint_print_one_detail;
16409 ops->print_mention = tracepoint_print_mention;
16410 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16411 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16412 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16413 ops->decode_linespec = tracepoint_decode_linespec;
16414
55aa24fb
SDJ
16415 /* Probe tracepoints. */
16416 ops = &tracepoint_probe_breakpoint_ops;
16417 *ops = tracepoint_breakpoint_ops;
16418 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16419 ops->decode_linespec = tracepoint_probe_decode_linespec;
16420
983af33b
SDJ
16421 /* Static tracepoints with marker (`-m'). */
16422 ops = &strace_marker_breakpoint_ops;
16423 *ops = tracepoint_breakpoint_ops;
16424 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16425 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16426 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16427
16428 /* Fork catchpoints. */
16429 ops = &catch_fork_breakpoint_ops;
16430 *ops = base_breakpoint_ops;
16431 ops->insert_location = insert_catch_fork;
16432 ops->remove_location = remove_catch_fork;
16433 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16434 ops->print_it = print_it_catch_fork;
16435 ops->print_one = print_one_catch_fork;
16436 ops->print_mention = print_mention_catch_fork;
16437 ops->print_recreate = print_recreate_catch_fork;
16438
16439 /* Vfork catchpoints. */
16440 ops = &catch_vfork_breakpoint_ops;
16441 *ops = base_breakpoint_ops;
16442 ops->insert_location = insert_catch_vfork;
16443 ops->remove_location = remove_catch_vfork;
16444 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16445 ops->print_it = print_it_catch_vfork;
16446 ops->print_one = print_one_catch_vfork;
16447 ops->print_mention = print_mention_catch_vfork;
16448 ops->print_recreate = print_recreate_catch_vfork;
16449
16450 /* Exec catchpoints. */
16451 ops = &catch_exec_breakpoint_ops;
16452 *ops = base_breakpoint_ops;
16453 ops->dtor = dtor_catch_exec;
16454 ops->insert_location = insert_catch_exec;
16455 ops->remove_location = remove_catch_exec;
16456 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16457 ops->print_it = print_it_catch_exec;
16458 ops->print_one = print_one_catch_exec;
16459 ops->print_mention = print_mention_catch_exec;
16460 ops->print_recreate = print_recreate_catch_exec;
16461
16462 /* Syscall catchpoints. */
16463 ops = &catch_syscall_breakpoint_ops;
16464 *ops = base_breakpoint_ops;
16465 ops->dtor = dtor_catch_syscall;
16466 ops->insert_location = insert_catch_syscall;
16467 ops->remove_location = remove_catch_syscall;
16468 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16469 ops->print_it = print_it_catch_syscall;
16470 ops->print_one = print_one_catch_syscall;
16471 ops->print_mention = print_mention_catch_syscall;
16472 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16473
16474 /* Solib-related catchpoints. */
16475 ops = &catch_solib_breakpoint_ops;
16476 *ops = base_breakpoint_ops;
16477 ops->dtor = dtor_catch_solib;
16478 ops->insert_location = insert_catch_solib;
16479 ops->remove_location = remove_catch_solib;
16480 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16481 ops->check_status = check_status_catch_solib;
16482 ops->print_it = print_it_catch_solib;
16483 ops->print_one = print_one_catch_solib;
16484 ops->print_mention = print_mention_catch_solib;
16485 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16486
16487 ops = &dprintf_breakpoint_ops;
16488 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16489 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16490 ops->resources_needed = bkpt_resources_needed;
16491 ops->print_it = bkpt_print_it;
16492 ops->print_mention = bkpt_print_mention;
2d9442cc 16493 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16494 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16495 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16496}
16497
8bfd80db
YQ
16498/* Chain containing all defined "enable breakpoint" subcommands. */
16499
16500static struct cmd_list_element *enablebreaklist = NULL;
16501
c906108c 16502void
fba45db2 16503_initialize_breakpoint (void)
c906108c
SS
16504{
16505 struct cmd_list_element *c;
16506
2060206e
PA
16507 initialize_breakpoint_ops ();
16508
84acb35a 16509 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16510 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16511 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16512 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16513
55aa24fb
SDJ
16514 breakpoint_objfile_key
16515 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16516
fa3064dd 16517 catch_syscall_inferior_data
8e260fc0
TT
16518 = register_inferior_data_with_cleanup (NULL,
16519 catch_syscall_inferior_data_cleanup);
fa3064dd 16520
c906108c
SS
16521 breakpoint_chain = 0;
16522 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16523 before a breakpoint is set. */
16524 breakpoint_count = 0;
16525
1042e4c0
SS
16526 tracepoint_count = 0;
16527
1bedd215
AC
16528 add_com ("ignore", class_breakpoint, ignore_command, _("\
16529Set ignore-count of breakpoint number N to COUNT.\n\
16530Usage is `ignore N COUNT'."));
c906108c 16531 if (xdb_commands)
c5aa993b 16532 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16533
1bedd215
AC
16534 add_com ("commands", class_breakpoint, commands_command, _("\
16535Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16536Give breakpoint number as argument after \"commands\".\n\
16537With no argument, the targeted breakpoint is the last one set.\n\
16538The commands themselves follow starting on the next line.\n\
16539Type a line containing \"end\" to indicate the end of them.\n\
16540Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16541then no output is printed when it is hit, except what the commands print."));
c906108c 16542
d55637df 16543 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16544Specify breakpoint number N to break only if COND is true.\n\
c906108c 16545Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16546expression to be evaluated whenever breakpoint N is reached."));
d55637df 16547 set_cmd_completer (c, condition_completer);
c906108c 16548
1bedd215 16549 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16550Set a temporary breakpoint.\n\
c906108c
SS
16551Like \"break\" except the breakpoint is only temporary,\n\
16552so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16553by using \"enable delete\" on the breakpoint number.\n\
16554\n"
16555BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16556 set_cmd_completer (c, location_completer);
c94fdfd0 16557
1bedd215 16558 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16559Set a hardware assisted breakpoint.\n\
c906108c 16560Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16561some target hardware may not have this support.\n\
16562\n"
16563BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16564 set_cmd_completer (c, location_completer);
c906108c 16565
1bedd215 16566 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16567Set a temporary hardware assisted breakpoint.\n\
c906108c 16568Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16569so it will be deleted when hit.\n\
16570\n"
16571BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16572 set_cmd_completer (c, location_completer);
c906108c 16573
1bedd215
AC
16574 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16575Enable some breakpoints.\n\
c906108c
SS
16576Give breakpoint numbers (separated by spaces) as arguments.\n\
16577With no subcommand, breakpoints are enabled until you command otherwise.\n\
16578This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16579With a subcommand you can enable temporarily."),
c906108c
SS
16580 &enablelist, "enable ", 1, &cmdlist);
16581 if (xdb_commands)
1bedd215
AC
16582 add_com ("ab", class_breakpoint, enable_command, _("\
16583Enable some breakpoints.\n\
c906108c
SS
16584Give breakpoint numbers (separated by spaces) as arguments.\n\
16585With no subcommand, breakpoints are enabled until you command otherwise.\n\
16586This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16587With a subcommand you can enable temporarily."));
c906108c
SS
16588
16589 add_com_alias ("en", "enable", class_breakpoint, 1);
16590
84951ab5 16591 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16592Enable some breakpoints.\n\
c906108c
SS
16593Give breakpoint numbers (separated by spaces) as arguments.\n\
16594This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16595May be abbreviated to simply \"enable\".\n"),
c5aa993b 16596 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16597
1a966eab
AC
16598 add_cmd ("once", no_class, enable_once_command, _("\
16599Enable breakpoints for one hit. Give breakpoint numbers.\n\
16600If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16601 &enablebreaklist);
16602
1a966eab
AC
16603 add_cmd ("delete", no_class, enable_delete_command, _("\
16604Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16605If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16606 &enablebreaklist);
16607
816338b5
SS
16608 add_cmd ("count", no_class, enable_count_command, _("\
16609Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16610If a breakpoint is hit while enabled in this fashion,\n\
16611the count is decremented; when it reaches zero, the breakpoint is disabled."),
16612 &enablebreaklist);
16613
1a966eab
AC
16614 add_cmd ("delete", no_class, enable_delete_command, _("\
16615Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16616If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16617 &enablelist);
16618
1a966eab
AC
16619 add_cmd ("once", no_class, enable_once_command, _("\
16620Enable breakpoints for one hit. Give breakpoint numbers.\n\
16621If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16622 &enablelist);
16623
16624 add_cmd ("count", no_class, enable_count_command, _("\
16625Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16626If a breakpoint is hit while enabled in this fashion,\n\
16627the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16628 &enablelist);
16629
1bedd215
AC
16630 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16631Disable some breakpoints.\n\
c906108c
SS
16632Arguments are breakpoint numbers with spaces in between.\n\
16633To disable all breakpoints, give no argument.\n\
64b9b334 16634A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16635 &disablelist, "disable ", 1, &cmdlist);
16636 add_com_alias ("dis", "disable", class_breakpoint, 1);
16637 add_com_alias ("disa", "disable", class_breakpoint, 1);
16638 if (xdb_commands)
1bedd215
AC
16639 add_com ("sb", class_breakpoint, disable_command, _("\
16640Disable some breakpoints.\n\
c906108c
SS
16641Arguments are breakpoint numbers with spaces in between.\n\
16642To disable all breakpoints, give no argument.\n\
64b9b334 16643A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16644
1a966eab
AC
16645 add_cmd ("breakpoints", class_alias, disable_command, _("\
16646Disable some breakpoints.\n\
c906108c
SS
16647Arguments are breakpoint numbers with spaces in between.\n\
16648To disable all breakpoints, give no argument.\n\
64b9b334 16649A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16650This command may be abbreviated \"disable\"."),
c906108c
SS
16651 &disablelist);
16652
1bedd215
AC
16653 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16654Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16655Arguments are breakpoint numbers with spaces in between.\n\
16656To delete all breakpoints, give no argument.\n\
16657\n\
16658Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16659The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16660 &deletelist, "delete ", 1, &cmdlist);
16661 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16662 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16663 if (xdb_commands)
1bedd215
AC
16664 add_com ("db", class_breakpoint, delete_command, _("\
16665Delete some breakpoints.\n\
c906108c 16666Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16667To delete all breakpoints, give no argument.\n"));
c906108c 16668
1a966eab
AC
16669 add_cmd ("breakpoints", class_alias, delete_command, _("\
16670Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16671Arguments are breakpoint numbers with spaces in between.\n\
16672To delete all breakpoints, give no argument.\n\
1a966eab 16673This command may be abbreviated \"delete\"."),
c906108c
SS
16674 &deletelist);
16675
1bedd215
AC
16676 add_com ("clear", class_breakpoint, clear_command, _("\
16677Clear breakpoint at specified line or function.\n\
c906108c
SS
16678Argument may be line number, function name, or \"*\" and an address.\n\
16679If line number is specified, all breakpoints in that line are cleared.\n\
16680If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16681If an address is specified, breakpoints at that address are cleared.\n\
16682\n\
16683With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16684is executing in.\n\
16685\n\
1bedd215 16686See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16687 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16688
1bedd215 16689 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16690Set breakpoint at specified line or function.\n"
16691BREAK_ARGS_HELP ("break")));
5ba2abeb 16692 set_cmd_completer (c, location_completer);
c94fdfd0 16693
c906108c
SS
16694 add_com_alias ("b", "break", class_run, 1);
16695 add_com_alias ("br", "break", class_run, 1);
16696 add_com_alias ("bre", "break", class_run, 1);
16697 add_com_alias ("brea", "break", class_run, 1);
16698
7681d515
PM
16699 if (xdb_commands)
16700 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16701
16702 if (dbx_commands)
16703 {
1bedd215
AC
16704 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16705Break in function/address or break at a line in the current file."),
c5aa993b
JM
16706 &stoplist, "stop ", 1, &cmdlist);
16707 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16708 _("Break in function or address."), &stoplist);
c5aa993b 16709 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16710 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16711 add_com ("status", class_info, breakpoints_info, _("\
16712Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16713The \"Type\" column indicates one of:\n\
16714\tbreakpoint - normal breakpoint\n\
16715\twatchpoint - watchpoint\n\
16716The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16717the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16718breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16719address and file/line number respectively.\n\
16720\n\
16721Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16722are set to the address of the last breakpoint listed unless the command\n\
16723is prefixed with \"server \".\n\n\
c906108c 16724Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16725breakpoint set."));
c906108c
SS
16726 }
16727
1bedd215 16728 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16729Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16730The \"Type\" column indicates one of:\n\
16731\tbreakpoint - normal breakpoint\n\
16732\twatchpoint - watchpoint\n\
16733The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16734the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16735breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16736address and file/line number respectively.\n\
16737\n\
16738Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16739are set to the address of the last breakpoint listed unless the command\n\
16740is prefixed with \"server \".\n\n\
c906108c 16741Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16742breakpoint set."));
c906108c 16743
6b04bdb7
MS
16744 add_info_alias ("b", "breakpoints", 1);
16745
c906108c 16746 if (xdb_commands)
1bedd215
AC
16747 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16748Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16749The \"Type\" column indicates one of:\n\
16750\tbreakpoint - normal breakpoint\n\
16751\twatchpoint - watchpoint\n\
16752The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16753the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16754breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16755address and file/line number respectively.\n\
16756\n\
16757Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16758are set to the address of the last breakpoint listed unless the command\n\
16759is prefixed with \"server \".\n\n\
c906108c 16760Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16761breakpoint set."));
c906108c 16762
1a966eab
AC
16763 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16764Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16765The \"Type\" column indicates one of:\n\
16766\tbreakpoint - normal breakpoint\n\
16767\twatchpoint - watchpoint\n\
16768\tlongjmp - internal breakpoint used to step through longjmp()\n\
16769\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16770\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16771\tfinish - internal breakpoint used by the \"finish\" command\n\
16772The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16773the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16774breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16775address and file/line number respectively.\n\
16776\n\
16777Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16778are set to the address of the last breakpoint listed unless the command\n\
16779is prefixed with \"server \".\n\n\
c906108c 16780Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16781breakpoint set."),
c906108c
SS
16782 &maintenanceinfolist);
16783
44feb3ce
TT
16784 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16785Set catchpoints to catch events."),
16786 &catch_cmdlist, "catch ",
16787 0/*allow-unknown*/, &cmdlist);
16788
16789 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16790Set temporary catchpoints to catch events."),
16791 &tcatch_cmdlist, "tcatch ",
16792 0/*allow-unknown*/, &cmdlist);
16793
44feb3ce
TT
16794 add_catch_command ("fork", _("Catch calls to fork."),
16795 catch_fork_command_1,
a96d9b2e 16796 NULL,
44feb3ce
TT
16797 (void *) (uintptr_t) catch_fork_permanent,
16798 (void *) (uintptr_t) catch_fork_temporary);
16799 add_catch_command ("vfork", _("Catch calls to vfork."),
16800 catch_fork_command_1,
a96d9b2e 16801 NULL,
44feb3ce
TT
16802 (void *) (uintptr_t) catch_vfork_permanent,
16803 (void *) (uintptr_t) catch_vfork_temporary);
16804 add_catch_command ("exec", _("Catch calls to exec."),
16805 catch_exec_command_1,
a96d9b2e
SDJ
16806 NULL,
16807 CATCH_PERMANENT,
16808 CATCH_TEMPORARY);
edcc5120
TT
16809 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16810Usage: catch load [REGEX]\n\
16811If REGEX is given, only stop for libraries matching the regular expression."),
16812 catch_load_command_1,
16813 NULL,
16814 CATCH_PERMANENT,
16815 CATCH_TEMPORARY);
16816 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16817Usage: catch unload [REGEX]\n\
16818If REGEX is given, only stop for libraries matching the regular expression."),
16819 catch_unload_command_1,
16820 NULL,
16821 CATCH_PERMANENT,
16822 CATCH_TEMPORARY);
a96d9b2e
SDJ
16823 add_catch_command ("syscall", _("\
16824Catch system calls by their names and/or numbers.\n\
16825Arguments say which system calls to catch. If no arguments\n\
16826are given, every system call will be caught.\n\
16827Arguments, if given, should be one or more system call names\n\
16828(if your system supports that), or system call numbers."),
16829 catch_syscall_command_1,
16830 catch_syscall_completer,
44feb3ce
TT
16831 CATCH_PERMANENT,
16832 CATCH_TEMPORARY);
c5aa993b 16833
1bedd215
AC
16834 c = add_com ("watch", class_breakpoint, watch_command, _("\
16835Set a watchpoint for an expression.\n\
06a64a0b 16836Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16837A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16838an expression changes.\n\
16839If -l or -location is given, this evaluates EXPRESSION and watches\n\
16840the memory to which it refers."));
65d12d83 16841 set_cmd_completer (c, expression_completer);
c906108c 16842
1bedd215
AC
16843 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16844Set a read watchpoint for an expression.\n\
06a64a0b 16845Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16846A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16847an expression is read.\n\
16848If -l or -location is given, this evaluates EXPRESSION and watches\n\
16849the memory to which it refers."));
65d12d83 16850 set_cmd_completer (c, expression_completer);
c906108c 16851
1bedd215
AC
16852 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16853Set a watchpoint for an expression.\n\
06a64a0b 16854Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16855A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16856an expression is either read or written.\n\
16857If -l or -location is given, this evaluates EXPRESSION and watches\n\
16858the memory to which it refers."));
65d12d83 16859 set_cmd_completer (c, expression_completer);
c906108c 16860
d77f58be 16861 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16862Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16863
920d2a44
AC
16864 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16865 respond to changes - contrary to the description. */
85c07804
AC
16866 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16867 &can_use_hw_watchpoints, _("\
16868Set debugger's willingness to use watchpoint hardware."), _("\
16869Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16870If zero, gdb will not use hardware for new watchpoints, even if\n\
16871such is available. (However, any hardware watchpoints that were\n\
16872created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16873hardware.)"),
16874 NULL,
920d2a44 16875 show_can_use_hw_watchpoints,
85c07804 16876 &setlist, &showlist);
c906108c
SS
16877
16878 can_use_hw_watchpoints = 1;
fa8d40ab 16879
1042e4c0
SS
16880 /* Tracepoint manipulation commands. */
16881
16882 c = add_com ("trace", class_breakpoint, trace_command, _("\
16883Set a tracepoint at specified line or function.\n\
16884\n"
16885BREAK_ARGS_HELP ("trace") "\n\
16886Do \"help tracepoints\" for info on other tracepoint commands."));
16887 set_cmd_completer (c, location_completer);
16888
16889 add_com_alias ("tp", "trace", class_alias, 0);
16890 add_com_alias ("tr", "trace", class_alias, 1);
16891 add_com_alias ("tra", "trace", class_alias, 1);
16892 add_com_alias ("trac", "trace", class_alias, 1);
16893
7a697b8d
SS
16894 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16895Set a fast tracepoint at specified line or function.\n\
16896\n"
16897BREAK_ARGS_HELP ("ftrace") "\n\
16898Do \"help tracepoints\" for info on other tracepoint commands."));
16899 set_cmd_completer (c, location_completer);
16900
0fb4aa4b
PA
16901 c = add_com ("strace", class_breakpoint, strace_command, _("\
16902Set a static tracepoint at specified line, function or marker.\n\
16903\n\
16904strace [LOCATION] [if CONDITION]\n\
16905LOCATION may be a line number, function name, \"*\" and an address,\n\
16906or -m MARKER_ID.\n\
16907If a line number is specified, probe the marker at start of code\n\
16908for that line. If a function is specified, probe the marker at start\n\
16909of code for that function. If an address is specified, probe the marker\n\
16910at that exact address. If a marker id is specified, probe the marker\n\
16911with that name. With no LOCATION, uses current execution address of\n\
16912the selected stack frame.\n\
16913Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16914This collects arbitrary user data passed in the probe point call to the\n\
16915tracing library. You can inspect it when analyzing the trace buffer,\n\
16916by printing the $_sdata variable like any other convenience variable.\n\
16917\n\
16918CONDITION is a boolean expression.\n\
16919\n\
d41c0fc8
PA
16920Multiple tracepoints at one place are permitted, and useful if their\n\
16921conditions are different.\n\
0fb4aa4b
PA
16922\n\
16923Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16924Do \"help tracepoints\" for info on other tracepoint commands."));
16925 set_cmd_completer (c, location_completer);
16926
1042e4c0 16927 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16928Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16929Convenience variable \"$tpnum\" contains the number of the\n\
16930last tracepoint set."));
16931
16932 add_info_alias ("tp", "tracepoints", 1);
16933
16934 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16935Delete specified tracepoints.\n\
16936Arguments are tracepoint numbers, separated by spaces.\n\
16937No argument means delete all tracepoints."),
16938 &deletelist);
7e20dfcd 16939 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16940
16941 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16942Disable specified tracepoints.\n\
16943Arguments are tracepoint numbers, separated by spaces.\n\
16944No argument means disable all tracepoints."),
16945 &disablelist);
16946 deprecate_cmd (c, "disable");
16947
16948 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16949Enable specified tracepoints.\n\
16950Arguments are tracepoint numbers, separated by spaces.\n\
16951No argument means enable all tracepoints."),
16952 &enablelist);
16953 deprecate_cmd (c, "enable");
16954
16955 add_com ("passcount", class_trace, trace_pass_command, _("\
16956Set the passcount for a tracepoint.\n\
16957The trace will end when the tracepoint has been passed 'count' times.\n\
16958Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16959if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16960
6149aea9
PA
16961 add_prefix_cmd ("save", class_breakpoint, save_command,
16962 _("Save breakpoint definitions as a script."),
16963 &save_cmdlist, "save ",
16964 0/*allow-unknown*/, &cmdlist);
16965
16966 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16967Save current breakpoint definitions as a script.\n\
cce7e648 16968This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16969catchpoints, tracepoints). Use the 'source' command in another debug\n\
16970session to restore them."),
16971 &save_cmdlist);
16972 set_cmd_completer (c, filename_completer);
16973
16974 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16975Save current tracepoint definitions as a script.\n\
6149aea9
PA
16976Use the 'source' command in another debug session to restore them."),
16977 &save_cmdlist);
1042e4c0
SS
16978 set_cmd_completer (c, filename_completer);
16979
6149aea9
PA
16980 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16981 deprecate_cmd (c, "save tracepoints");
16982
1bedd215 16983 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16984Breakpoint specific settings\n\
16985Configure various breakpoint-specific variables such as\n\
1bedd215 16986pending breakpoint behavior"),
fa8d40ab
JJ
16987 &breakpoint_set_cmdlist, "set breakpoint ",
16988 0/*allow-unknown*/, &setlist);
1bedd215 16989 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16990Breakpoint specific settings\n\
16991Configure various breakpoint-specific variables such as\n\
1bedd215 16992pending breakpoint behavior"),
fa8d40ab
JJ
16993 &breakpoint_show_cmdlist, "show breakpoint ",
16994 0/*allow-unknown*/, &showlist);
16995
7915a72c
AC
16996 add_setshow_auto_boolean_cmd ("pending", no_class,
16997 &pending_break_support, _("\
16998Set debugger's behavior regarding pending breakpoints."), _("\
16999Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
17000If on, an unrecognized breakpoint location will cause gdb to create a\n\
17001pending breakpoint. If off, an unrecognized breakpoint location results in\n\
17002an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 17003user-query to see if a pending breakpoint should be created."),
2c5b56ce 17004 NULL,
920d2a44 17005 show_pending_break_support,
6e1d7d6c
AC
17006 &breakpoint_set_cmdlist,
17007 &breakpoint_show_cmdlist);
fa8d40ab
JJ
17008
17009 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
17010
17011 add_setshow_boolean_cmd ("auto-hw", no_class,
17012 &automatic_hardware_breakpoints, _("\
17013Set automatic usage of hardware breakpoints."), _("\
17014Show automatic usage of hardware breakpoints."), _("\
17015If set, the debugger will automatically use hardware breakpoints for\n\
17016breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
17017a warning will be emitted for such breakpoints."),
17018 NULL,
17019 show_automatic_hardware_breakpoints,
17020 &breakpoint_set_cmdlist,
17021 &breakpoint_show_cmdlist);
74960c60 17022
72d0e2c5
YQ
17023 add_setshow_auto_boolean_cmd ("always-inserted", class_support,
17024 &always_inserted_mode, _("\
74960c60
VP
17025Set mode for inserting breakpoints."), _("\
17026Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
17027When this mode is off, breakpoints are inserted in inferior when it is\n\
17028resumed, and removed when execution stops. When this mode is on,\n\
17029breakpoints are inserted immediately and removed only when the user\n\
17030deletes the breakpoint. When this mode is auto (which is the default),\n\
17031the behaviour depends on the non-stop setting (see help set non-stop).\n\
17032In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
17033behaves as if always-inserted mode is on; if gdb is controlling the\n\
17034inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
72d0e2c5
YQ
17035 NULL,
17036 &show_always_inserted_mode,
17037 &breakpoint_set_cmdlist,
17038 &breakpoint_show_cmdlist);
f1310107 17039
b775012e
LM
17040 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
17041 condition_evaluation_enums,
17042 &condition_evaluation_mode_1, _("\
17043Set mode of breakpoint condition evaluation."), _("\
17044Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 17045When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
17046evaluated on the host's side by GDB. When it is set to \"target\",\n\
17047breakpoint conditions will be downloaded to the target (if the target\n\
17048supports such feature) and conditions will be evaluated on the target's side.\n\
17049If this is set to \"auto\" (default), this will be automatically set to\n\
17050\"target\" if it supports condition evaluation, otherwise it will\n\
17051be set to \"gdb\""),
17052 &set_condition_evaluation_mode,
17053 &show_condition_evaluation_mode,
17054 &breakpoint_set_cmdlist,
17055 &breakpoint_show_cmdlist);
17056
f1310107
TJB
17057 add_com ("break-range", class_breakpoint, break_range_command, _("\
17058Set a breakpoint for an address range.\n\
17059break-range START-LOCATION, END-LOCATION\n\
17060where START-LOCATION and END-LOCATION can be one of the following:\n\
17061 LINENUM, for that line in the current file,\n\
17062 FILE:LINENUM, for that line in that file,\n\
17063 +OFFSET, for that number of lines after the current line\n\
17064 or the start of the range\n\
17065 FUNCTION, for the first line in that function,\n\
17066 FILE:FUNCTION, to distinguish among like-named static functions.\n\
17067 *ADDRESS, for the instruction at that address.\n\
17068\n\
17069The breakpoint will stop execution of the inferior whenever it executes\n\
17070an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
17071range (including START-LOCATION and END-LOCATION)."));
17072
e7e0cddf
SS
17073 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
17074Set a dynamic printf at specified line or function.\n\
17075dprintf location,format string,arg1,arg2,...\n\
17076location may be a line number, function name, or \"*\" and an address.\n\
17077If a line number is specified, break at start of code for that line.\n\
0e4777df 17078If a function is specified, break at start of code for that function."));
e7e0cddf
SS
17079 set_cmd_completer (c, location_completer);
17080
17081 add_setshow_enum_cmd ("dprintf-style", class_support,
17082 dprintf_style_enums, &dprintf_style, _("\
17083Set the style of usage for dynamic printf."), _("\
17084Show the style of usage for dynamic printf."), _("\
17085This setting chooses how GDB will do a dynamic printf.\n\
17086If the value is \"gdb\", then the printing is done by GDB to its own\n\
17087console, as with the \"printf\" command.\n\
17088If the value is \"call\", the print is done by calling a function in your\n\
17089program; by default printf(), but you can choose a different function or\n\
17090output stream by setting dprintf-function and dprintf-channel."),
17091 update_dprintf_commands, NULL,
17092 &setlist, &showlist);
17093
17094 dprintf_function = xstrdup ("printf");
17095 add_setshow_string_cmd ("dprintf-function", class_support,
17096 &dprintf_function, _("\
17097Set the function to use for dynamic printf"), _("\
17098Show the function to use for dynamic printf"), NULL,
17099 update_dprintf_commands, NULL,
17100 &setlist, &showlist);
17101
17102 dprintf_channel = xstrdup ("");
17103 add_setshow_string_cmd ("dprintf-channel", class_support,
17104 &dprintf_channel, _("\
17105Set the channel to use for dynamic printf"), _("\
17106Show the channel to use for dynamic printf"), NULL,
17107 update_dprintf_commands, NULL,
17108 &setlist, &showlist);
17109
d3ce09f5
SS
17110 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
17111 &disconnected_dprintf, _("\
17112Set whether dprintf continues after GDB disconnects."), _("\
17113Show whether dprintf continues after GDB disconnects."), _("\
17114Use this to let dprintf commands continue to hit and produce output\n\
17115even if GDB disconnects or detaches from the target."),
17116 NULL,
17117 NULL,
17118 &setlist, &showlist);
17119
17120 add_com ("agent-printf", class_vars, agent_printf_command, _("\
17121agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
17122(target agent only) This is useful for formatted output in user-defined commands."));
17123
765dc015 17124 automatic_hardware_breakpoints = 1;
f3b1572e
PA
17125
17126 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 17127 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 17128}