]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
2011-07-25 Pedro Alves <pedro@codesourcery.com>
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1 4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7b6bb8da 5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
a6d9a66e 23#include "arch-utils.h"
c906108c 24#include <ctype.h>
776592bf 25#include "hashtab.h"
c906108c
SS
26#include "symtab.h"
27#include "frame.h"
28#include "breakpoint.h"
1042e4c0 29#include "tracepoint.h"
c906108c
SS
30#include "gdbtypes.h"
31#include "expression.h"
32#include "gdbcore.h"
33#include "gdbcmd.h"
34#include "value.h"
35#include "command.h"
36#include "inferior.h"
37#include "gdbthread.h"
38#include "target.h"
39#include "language.h"
40#include "gdb_string.h"
41#include "demangle.h"
0ba1096a 42#include "filenames.h"
c906108c
SS
43#include "annotate.h"
44#include "symfile.h"
45#include "objfiles.h"
0378c332 46#include "source.h"
c5f0f3d0 47#include "linespec.h"
c94fdfd0 48#include "completer.h"
5b7f31a4 49#include "gdb.h"
8b93c638 50#include "ui-out.h"
e1507482 51#include "cli/cli-script.h"
0225421b 52#include "gdb_assert.h"
fe898f56 53#include "block.h"
a77053c2 54#include "solib.h"
84acb35a
JJ
55#include "solist.h"
56#include "observer.h"
60250e8b 57#include "exceptions.h"
765dc015 58#include "memattr.h"
f7f9143b 59#include "ada-lang.h"
d1aa2f50 60#include "top.h"
fa4727a6 61#include "wrapper.h"
79a45b7d 62#include "valprint.h"
4efc6507 63#include "jit.h"
a96d9b2e 64#include "xml-syscall.h"
65d79d4b 65#include "parser-defs.h"
e9cafbcc 66#include "cli/cli-utils.h"
be34f849 67#include "continuations.h"
c906108c 68
1042e4c0
SS
69/* readline include files */
70#include "readline/readline.h"
71#include "readline/history.h"
72
73/* readline defines this. */
74#undef savestring
75
034dad6f 76#include "mi/mi-common.h"
7371cf6d 77#include "python/python.h"
104c1213 78
4a64f543 79/* Prototypes for local functions. */
c906108c 80
a14ed312 81static void enable_delete_command (char *, int);
c906108c 82
a14ed312 83static void enable_once_command (char *, int);
c906108c 84
a14ed312 85static void disable_command (char *, int);
c906108c 86
a14ed312 87static void enable_command (char *, int);
c906108c 88
95a42b64
TT
89static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
90 void *),
91 void *);
c906108c 92
a14ed312 93static void ignore_command (char *, int);
c906108c 94
4efb68b1 95static int breakpoint_re_set_one (void *);
c906108c 96
348d480f
PA
97static void breakpoint_re_set_default (struct breakpoint *);
98
a14ed312 99static void clear_command (char *, int);
c906108c 100
a14ed312 101static void catch_command (char *, int);
c906108c 102
a9634178 103static int can_use_hardware_watchpoint (struct value *);
c906108c 104
98deb0da 105static void break_command_1 (char *, int, int);
c906108c 106
a14ed312 107static void mention (struct breakpoint *);
c906108c 108
348d480f
PA
109static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
110 enum bptype,
111 struct breakpoint_ops *);
4a64f543
MS
112/* This function is used in gdbtk sources and thus can not be made
113 static. */
63c252f8 114struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f
PA
115 struct symtab_and_line,
116 enum bptype, struct breakpoint_ops *);
c906108c 117
06edf0c0
PA
118static struct breakpoint *
119 momentary_breakpoint_from_master (struct breakpoint *orig,
120 enum bptype type,
121 struct breakpoint_ops *ops);
122
76897487
KB
123static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
124
a6d9a66e
UW
125static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
126 CORE_ADDR bpaddr,
88f7da05 127 enum bptype bptype);
76897487 128
6c95b8df
PA
129static void describe_other_breakpoints (struct gdbarch *,
130 struct program_space *, CORE_ADDR,
5af949e3 131 struct obj_section *, int);
c906108c 132
6c95b8df
PA
133static int breakpoint_address_match (struct address_space *aspace1,
134 CORE_ADDR addr1,
135 struct address_space *aspace2,
136 CORE_ADDR addr2);
137
85d721b8
PA
138static int watchpoint_locations_match (struct bp_location *loc1,
139 struct bp_location *loc2);
140
f1310107
TJB
141static int breakpoint_location_address_match (struct bp_location *bl,
142 struct address_space *aspace,
143 CORE_ADDR addr);
144
a14ed312 145static void breakpoints_info (char *, int);
c906108c 146
d77f58be
SS
147static void watchpoints_info (char *, int);
148
e5a67952
MS
149static int breakpoint_1 (char *, int,
150 int (*) (const struct breakpoint *));
c906108c 151
4efb68b1 152static int breakpoint_cond_eval (void *);
c906108c 153
4efb68b1 154static void cleanup_executing_breakpoints (void *);
c906108c 155
a14ed312 156static void commands_command (char *, int);
c906108c 157
a14ed312 158static void condition_command (char *, int);
c906108c 159
c5aa993b
JM
160typedef enum
161 {
162 mark_inserted,
163 mark_uninserted
164 }
165insertion_state_t;
c906108c 166
0bde7532 167static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 168static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 169
e514a9d6 170static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 171
4efb68b1 172static int watchpoint_check (void *);
c906108c 173
a14ed312 174static void maintenance_info_breakpoints (char *, int);
c906108c 175
a14ed312 176static int hw_breakpoint_used_count (void);
c906108c 177
a14ed312 178static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 179
a14ed312 180static void hbreak_command (char *, int);
c906108c 181
a14ed312 182static void thbreak_command (char *, int);
c906108c 183
51be5b68 184static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp);
c906108c 185
a14ed312 186static void stop_command (char *arg, int from_tty);
7a292a7a 187
a14ed312 188static void stopin_command (char *arg, int from_tty);
7a292a7a 189
a14ed312 190static void stopat_command (char *arg, int from_tty);
7a292a7a 191
a14ed312 192static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 193
d85310f7
MS
194static void catch_exception_command_1 (enum exception_event_kind ex_event,
195 char *arg, int tempflag, int from_tty);
7a292a7a 196
a14ed312 197static void tcatch_command (char *arg, int from_tty);
7a292a7a 198
d03285ec
UW
199static void detach_single_step_breakpoints (void);
200
6c95b8df
PA
201static int single_step_breakpoint_inserted_here_p (struct address_space *,
202 CORE_ADDR pc);
1aafd4da 203
fe3f5fa8 204static void free_bp_location (struct bp_location *loc);
f431efe5
PA
205static void incref_bp_location (struct bp_location *loc);
206static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 207
39d61571 208static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 209
b60e7edf 210static void update_global_location_list (int);
a5606eee 211
b60e7edf 212static void update_global_location_list_nothrow (int);
74960c60 213
d77f58be 214static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60 215
d77f58be 216static int is_watchpoint (const struct breakpoint *bpt);
60e1c644 217
74960c60 218static void insert_breakpoint_locations (void);
a5606eee 219
a96d9b2e
SDJ
220static int syscall_catchpoint_p (struct breakpoint *b);
221
1042e4c0
SS
222static void tracepoints_info (char *, int);
223
224static void delete_trace_command (char *, int);
225
226static void enable_trace_command (char *, int);
227
228static void disable_trace_command (char *, int);
229
230static void trace_pass_command (char *, int);
231
9c06b0b4
TJB
232static int is_masked_watchpoint (const struct breakpoint *b);
233
0fb4aa4b
PA
234/* Assuming we're creating a static tracepoint, does S look like a
235 static tracepoint marker spec ("-m MARKER_ID")? */
236#define is_marker_spec(s) \
f5a8e22b 237 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 238
06edf0c0
PA
239/* Forward declarations. */
240static struct breakpoint_ops internal_breakpoint_ops;
241static struct breakpoint_ops momentary_breakpoint_ops;
242
5cea2a26
PA
243/* A reference-counted struct command_line. This lets multiple
244 breakpoints share a single command list. */
245struct counted_command_line
246{
247 /* The reference count. */
248 int refc;
249
250 /* The command list. */
251 struct command_line *commands;
252};
253
254struct command_line *
255breakpoint_commands (struct breakpoint *b)
256{
257 return b->commands ? b->commands->commands : NULL;
258}
3daf8fe5 259
f3b1572e
PA
260/* Flag indicating that a command has proceeded the inferior past the
261 current breakpoint. */
262
263static int breakpoint_proceeded;
264
956a9fb9 265const char *
2cec12e5
AR
266bpdisp_text (enum bpdisp disp)
267{
4a64f543
MS
268 /* NOTE: the following values are a part of MI protocol and
269 represent values of 'disp' field returned when inferior stops at
270 a breakpoint. */
bc043ef3 271 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 272
2cec12e5
AR
273 return bpdisps[(int) disp];
274}
c906108c 275
4a64f543 276/* Prototypes for exported functions. */
c906108c 277/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 278 if such is available. */
c906108c
SS
279static int can_use_hw_watchpoints;
280
920d2a44
AC
281static void
282show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
283 struct cmd_list_element *c,
284 const char *value)
285{
3e43a32a
MS
286 fprintf_filtered (file,
287 _("Debugger's willingness to use "
288 "watchpoint hardware is %s.\n"),
920d2a44
AC
289 value);
290}
291
fa8d40ab
JJ
292/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
293 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 294 for unrecognized breakpoint locations.
fa8d40ab
JJ
295 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
296static enum auto_boolean pending_break_support;
920d2a44
AC
297static void
298show_pending_break_support (struct ui_file *file, int from_tty,
299 struct cmd_list_element *c,
300 const char *value)
301{
3e43a32a
MS
302 fprintf_filtered (file,
303 _("Debugger's behavior regarding "
304 "pending breakpoints is %s.\n"),
920d2a44
AC
305 value);
306}
fa8d40ab 307
765dc015 308/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 309 set with "break" but falling in read-only memory.
765dc015
VP
310 If 0, gdb will warn about such breakpoints, but won't automatically
311 use hardware breakpoints. */
312static int automatic_hardware_breakpoints;
313static void
314show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
315 struct cmd_list_element *c,
316 const char *value)
317{
3e43a32a
MS
318 fprintf_filtered (file,
319 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
320 value);
321}
322
33e5cbd6
PA
323/* If on, gdb will keep breakpoints inserted even as inferior is
324 stopped, and immediately insert any new breakpoints. If off, gdb
325 will insert breakpoints into inferior only when resuming it, and
326 will remove breakpoints upon stop. If auto, GDB will behave as ON
327 if in non-stop mode, and as OFF if all-stop mode.*/
328
329static const char always_inserted_auto[] = "auto";
330static const char always_inserted_on[] = "on";
331static const char always_inserted_off[] = "off";
332static const char *always_inserted_enums[] = {
333 always_inserted_auto,
334 always_inserted_off,
335 always_inserted_on,
336 NULL
337};
338static const char *always_inserted_mode = always_inserted_auto;
339static void
74960c60 340show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 341 struct cmd_list_element *c, const char *value)
74960c60 342{
33e5cbd6 343 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
344 fprintf_filtered (file,
345 _("Always inserted breakpoint "
346 "mode is %s (currently %s).\n"),
33e5cbd6
PA
347 value,
348 breakpoints_always_inserted_mode () ? "on" : "off");
349 else
3e43a32a
MS
350 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
351 value);
74960c60
VP
352}
353
33e5cbd6
PA
354int
355breakpoints_always_inserted_mode (void)
356{
357 return (always_inserted_mode == always_inserted_on
358 || (always_inserted_mode == always_inserted_auto && non_stop));
359}
765dc015 360
a14ed312 361void _initialize_breakpoint (void);
c906108c 362
c906108c
SS
363/* Are we executing breakpoint commands? */
364static int executing_breakpoint_commands;
365
c02f5703
MS
366/* Are overlay event breakpoints enabled? */
367static int overlay_events_enabled;
368
e09342b5
TJB
369/* See description in breakpoint.h. */
370int target_exact_watchpoints = 0;
371
c906108c 372/* Walk the following statement or block through all breakpoints.
e5dd4106 373 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 374 current breakpoint. */
c906108c 375
5c44784c 376#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 377
5c44784c
JM
378#define ALL_BREAKPOINTS_SAFE(B,TMP) \
379 for (B = breakpoint_chain; \
380 B ? (TMP=B->next, 1): 0; \
381 B = TMP)
c906108c 382
4a64f543
MS
383/* Similar iterator for the low-level breakpoints. SAFE variant is
384 not provided so update_global_location_list must not be called
385 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 386
876fa593
JK
387#define ALL_BP_LOCATIONS(B,BP_TMP) \
388 for (BP_TMP = bp_location; \
389 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
390 BP_TMP++)
7cc221ef 391
1042e4c0
SS
392/* Iterator for tracepoints only. */
393
394#define ALL_TRACEPOINTS(B) \
395 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 396 if (is_tracepoint (B))
1042e4c0 397
7cc221ef 398/* Chains of all breakpoints defined. */
c906108c
SS
399
400struct breakpoint *breakpoint_chain;
401
876fa593
JK
402/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
403
404static struct bp_location **bp_location;
405
406/* Number of elements of BP_LOCATION. */
407
408static unsigned bp_location_count;
409
4a64f543
MS
410/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
411 ADDRESS for the current elements of BP_LOCATION which get a valid
412 result from bp_location_has_shadow. You can use it for roughly
413 limiting the subrange of BP_LOCATION to scan for shadow bytes for
414 an address you need to read. */
876fa593
JK
415
416static CORE_ADDR bp_location_placed_address_before_address_max;
417
4a64f543
MS
418/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
419 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
420 BP_LOCATION which get a valid result from bp_location_has_shadow.
421 You can use it for roughly limiting the subrange of BP_LOCATION to
422 scan for shadow bytes for an address you need to read. */
876fa593
JK
423
424static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 425
4a64f543
MS
426/* The locations that no longer correspond to any breakpoint, unlinked
427 from bp_location array, but for which a hit may still be reported
428 by a target. */
20874c92
VP
429VEC(bp_location_p) *moribund_locations = NULL;
430
c906108c
SS
431/* Number of last breakpoint made. */
432
95a42b64
TT
433static int breakpoint_count;
434
86b17b60
PA
435/* The value of `breakpoint_count' before the last command that
436 created breakpoints. If the last (break-like) command created more
437 than one breakpoint, then the difference between BREAKPOINT_COUNT
438 and PREV_BREAKPOINT_COUNT is more than one. */
439static int prev_breakpoint_count;
c906108c 440
1042e4c0
SS
441/* Number of last tracepoint made. */
442
95a42b64 443static int tracepoint_count;
1042e4c0 444
6149aea9
PA
445static struct cmd_list_element *breakpoint_set_cmdlist;
446static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 447struct cmd_list_element *save_cmdlist;
6149aea9 448
468d015d
JJ
449/* Return whether a breakpoint is an active enabled breakpoint. */
450static int
451breakpoint_enabled (struct breakpoint *b)
452{
0d381245 453 return (b->enable_state == bp_enabled);
468d015d
JJ
454}
455
c906108c
SS
456/* Set breakpoint count to NUM. */
457
95a42b64 458static void
fba45db2 459set_breakpoint_count (int num)
c906108c 460{
86b17b60 461 prev_breakpoint_count = breakpoint_count;
c906108c 462 breakpoint_count = num;
4fa62494 463 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
464}
465
86b17b60
PA
466/* Used by `start_rbreak_breakpoints' below, to record the current
467 breakpoint count before "rbreak" creates any breakpoint. */
468static int rbreak_start_breakpoint_count;
469
95a42b64
TT
470/* Called at the start an "rbreak" command to record the first
471 breakpoint made. */
86b17b60 472
95a42b64
TT
473void
474start_rbreak_breakpoints (void)
475{
86b17b60 476 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
477}
478
479/* Called at the end of an "rbreak" command to record the last
480 breakpoint made. */
86b17b60 481
95a42b64
TT
482void
483end_rbreak_breakpoints (void)
484{
86b17b60 485 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
486}
487
4a64f543 488/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
489
490void
fba45db2 491clear_breakpoint_hit_counts (void)
c906108c
SS
492{
493 struct breakpoint *b;
494
495 ALL_BREAKPOINTS (b)
496 b->hit_count = 0;
497}
498
9add0f1b
TT
499/* Allocate a new counted_command_line with reference count of 1.
500 The new structure owns COMMANDS. */
501
502static struct counted_command_line *
503alloc_counted_command_line (struct command_line *commands)
504{
505 struct counted_command_line *result
506 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 507
9add0f1b
TT
508 result->refc = 1;
509 result->commands = commands;
510 return result;
511}
512
513/* Increment reference count. This does nothing if CMD is NULL. */
514
515static void
516incref_counted_command_line (struct counted_command_line *cmd)
517{
518 if (cmd)
519 ++cmd->refc;
520}
521
522/* Decrement reference count. If the reference count reaches 0,
523 destroy the counted_command_line. Sets *CMDP to NULL. This does
524 nothing if *CMDP is NULL. */
525
526static void
527decref_counted_command_line (struct counted_command_line **cmdp)
528{
529 if (*cmdp)
530 {
531 if (--(*cmdp)->refc == 0)
532 {
533 free_command_lines (&(*cmdp)->commands);
534 xfree (*cmdp);
535 }
536 *cmdp = NULL;
537 }
538}
539
540/* A cleanup function that calls decref_counted_command_line. */
541
542static void
543do_cleanup_counted_command_line (void *arg)
544{
545 decref_counted_command_line (arg);
546}
547
548/* Create a cleanup that calls decref_counted_command_line on the
549 argument. */
550
551static struct cleanup *
552make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
553{
554 return make_cleanup (do_cleanup_counted_command_line, cmdp);
555}
556
c906108c
SS
557/* Default address, symtab and line to put a breakpoint at
558 for "break" command with no arg.
4a64f543 559 If default_breakpoint_valid is zero, the other three are
c906108c
SS
560 not valid, and "break" with no arg is an error.
561
562 This set by print_stack_frame, which calls set_default_breakpoint. */
563
564int default_breakpoint_valid;
565CORE_ADDR default_breakpoint_address;
566struct symtab *default_breakpoint_symtab;
567int default_breakpoint_line;
6c95b8df
PA
568struct program_space *default_breakpoint_pspace;
569
c906108c 570\f
48cb2d85
VP
571/* Return the breakpoint with the specified number, or NULL
572 if the number does not refer to an existing breakpoint. */
573
574struct breakpoint *
575get_breakpoint (int num)
576{
577 struct breakpoint *b;
578
579 ALL_BREAKPOINTS (b)
580 if (b->number == num)
581 return b;
582
583 return NULL;
584}
5c44784c 585
c906108c 586\f
adc36818
PM
587
588void
589set_breakpoint_condition (struct breakpoint *b, char *exp,
590 int from_tty)
591{
592 struct bp_location *loc = b->loc;
593
594 for (; loc; loc = loc->next)
595 {
596 xfree (loc->cond);
597 loc->cond = NULL;
598 }
599 xfree (b->cond_string);
600 b->cond_string = NULL;
601 xfree (b->cond_exp);
602 b->cond_exp = NULL;
603
604 if (*exp == 0)
605 {
606 if (from_tty)
607 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
608 }
609 else
610 {
611 char *arg = exp;
cc59ec59 612
adc36818
PM
613 /* I don't know if it matters whether this is the string the user
614 typed in or the decompiled expression. */
615 b->cond_string = xstrdup (arg);
616 b->condition_not_parsed = 0;
617
618 if (is_watchpoint (b))
619 {
620 innermost_block = NULL;
621 arg = exp;
622 b->cond_exp = parse_exp_1 (&arg, 0, 0);
623 if (*arg)
624 error (_("Junk at end of expression"));
625 b->cond_exp_valid_block = innermost_block;
626 }
627 else
628 {
629 for (loc = b->loc; loc; loc = loc->next)
630 {
631 arg = exp;
632 loc->cond =
633 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
634 if (*arg)
635 error (_("Junk at end of expression"));
636 }
637 }
638 }
639 breakpoints_changed ();
8d3788bd 640 observer_notify_breakpoint_modified (b);
adc36818
PM
641}
642
c906108c
SS
643/* condition N EXP -- set break condition of breakpoint N to EXP. */
644
645static void
fba45db2 646condition_command (char *arg, int from_tty)
c906108c 647{
52f0bd74 648 struct breakpoint *b;
c906108c 649 char *p;
52f0bd74 650 int bnum;
c906108c
SS
651
652 if (arg == 0)
e2e0b3e5 653 error_no_arg (_("breakpoint number"));
c906108c
SS
654
655 p = arg;
656 bnum = get_number (&p);
5c44784c 657 if (bnum == 0)
8a3fe4f8 658 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
659
660 ALL_BREAKPOINTS (b)
661 if (b->number == bnum)
2f069f6f 662 {
7371cf6d
PM
663 /* Check if this breakpoint has a Python object assigned to
664 it, and if it has a definition of the "stop"
665 method. This method and conditions entered into GDB from
666 the CLI are mutually exclusive. */
667 if (b->py_bp_object
668 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
669 error (_("Cannot set a condition where a Python 'stop' "
670 "method has been defined in the breakpoint."));
2566ad2d 671 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
672 return;
673 }
c906108c 674
8a3fe4f8 675 error (_("No breakpoint number %d."), bnum);
c906108c
SS
676}
677
a7bdde9e
VP
678/* Check that COMMAND do not contain commands that are suitable
679 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
680 Throw if any such commands is found. */
681
a7bdde9e
VP
682static void
683check_no_tracepoint_commands (struct command_line *commands)
684{
685 struct command_line *c;
cc59ec59 686
a7bdde9e
VP
687 for (c = commands; c; c = c->next)
688 {
689 int i;
690
691 if (c->control_type == while_stepping_control)
3e43a32a
MS
692 error (_("The 'while-stepping' command can "
693 "only be used for tracepoints"));
a7bdde9e
VP
694
695 for (i = 0; i < c->body_count; ++i)
696 check_no_tracepoint_commands ((c->body_list)[i]);
697
698 /* Not that command parsing removes leading whitespace and comment
4a64f543 699 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
700 command directly. */
701 if (strstr (c->line, "collect ") == c->line)
702 error (_("The 'collect' command can only be used for tracepoints"));
703
51661e93
VP
704 if (strstr (c->line, "teval ") == c->line)
705 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
706 }
707}
708
d77f58be
SS
709/* Encapsulate tests for different types of tracepoints. */
710
a7bdde9e 711int
d77f58be 712is_tracepoint (const struct breakpoint *b)
a7bdde9e 713{
0fb4aa4b
PA
714 return (b->type == bp_tracepoint
715 || b->type == bp_fast_tracepoint
716 || b->type == bp_static_tracepoint);
a7bdde9e 717}
d77f58be 718
e5dd4106 719/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
720 breakpoint. This function will throw an exception if a problem is
721 found. */
48cb2d85 722
95a42b64
TT
723static void
724validate_commands_for_breakpoint (struct breakpoint *b,
725 struct command_line *commands)
48cb2d85 726{
d77f58be 727 if (is_tracepoint (b))
a7bdde9e 728 {
4a64f543
MS
729 /* We need to verify that each top-level element of commands is
730 valid for tracepoints, that there's at most one
731 while-stepping element, and that while-stepping's body has
732 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
733 struct command_line *c;
734 struct command_line *while_stepping = 0;
735 for (c = commands; c; c = c->next)
736 {
a7bdde9e
VP
737 if (c->control_type == while_stepping_control)
738 {
739 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
740 error (_("The 'while-stepping' command "
741 "cannot be used for fast tracepoint"));
0fb4aa4b 742 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
743 error (_("The 'while-stepping' command "
744 "cannot be used for static tracepoint"));
a7bdde9e
VP
745
746 if (while_stepping)
3e43a32a
MS
747 error (_("The 'while-stepping' command "
748 "can be used only once"));
a7bdde9e
VP
749 else
750 while_stepping = c;
751 }
752 }
753 if (while_stepping)
754 {
755 struct command_line *c2;
756
757 gdb_assert (while_stepping->body_count == 1);
758 c2 = while_stepping->body_list[0];
759 for (; c2; c2 = c2->next)
760 {
a7bdde9e
VP
761 if (c2->control_type == while_stepping_control)
762 error (_("The 'while-stepping' command cannot be nested"));
763 }
764 }
765 }
766 else
767 {
768 check_no_tracepoint_commands (commands);
769 }
95a42b64
TT
770}
771
0fb4aa4b
PA
772/* Return a vector of all the static tracepoints set at ADDR. The
773 caller is responsible for releasing the vector. */
774
775VEC(breakpoint_p) *
776static_tracepoints_here (CORE_ADDR addr)
777{
778 struct breakpoint *b;
779 VEC(breakpoint_p) *found = 0;
780 struct bp_location *loc;
781
782 ALL_BREAKPOINTS (b)
783 if (b->type == bp_static_tracepoint)
784 {
785 for (loc = b->loc; loc; loc = loc->next)
786 if (loc->address == addr)
787 VEC_safe_push(breakpoint_p, found, b);
788 }
789
790 return found;
791}
792
95a42b64 793/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 794 validate that only allowed commands are included. */
95a42b64
TT
795
796void
4a64f543
MS
797breakpoint_set_commands (struct breakpoint *b,
798 struct command_line *commands)
95a42b64
TT
799{
800 validate_commands_for_breakpoint (b, commands);
a7bdde9e 801
9add0f1b
TT
802 decref_counted_command_line (&b->commands);
803 b->commands = alloc_counted_command_line (commands);
48cb2d85 804 breakpoints_changed ();
8d3788bd 805 observer_notify_breakpoint_modified (b);
48cb2d85
VP
806}
807
45a43567
TT
808/* Set the internal `silent' flag on the breakpoint. Note that this
809 is not the same as the "silent" that may appear in the breakpoint's
810 commands. */
811
812void
813breakpoint_set_silent (struct breakpoint *b, int silent)
814{
815 int old_silent = b->silent;
816
817 b->silent = silent;
818 if (old_silent != silent)
8d3788bd 819 observer_notify_breakpoint_modified (b);
45a43567
TT
820}
821
822/* Set the thread for this breakpoint. If THREAD is -1, make the
823 breakpoint work for any thread. */
824
825void
826breakpoint_set_thread (struct breakpoint *b, int thread)
827{
828 int old_thread = b->thread;
829
830 b->thread = thread;
831 if (old_thread != thread)
8d3788bd 832 observer_notify_breakpoint_modified (b);
45a43567
TT
833}
834
835/* Set the task for this breakpoint. If TASK is 0, make the
836 breakpoint work for any task. */
837
838void
839breakpoint_set_task (struct breakpoint *b, int task)
840{
841 int old_task = b->task;
842
843 b->task = task;
844 if (old_task != task)
8d3788bd 845 observer_notify_breakpoint_modified (b);
45a43567
TT
846}
847
95a42b64
TT
848void
849check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
850{
851 struct breakpoint *b = closure;
cc59ec59 852
a7bdde9e
VP
853 validate_actionline (&line, b);
854}
855
95a42b64
TT
856/* A structure used to pass information through
857 map_breakpoint_numbers. */
858
859struct commands_info
860{
861 /* True if the command was typed at a tty. */
862 int from_tty;
86b17b60
PA
863
864 /* The breakpoint range spec. */
865 char *arg;
866
95a42b64
TT
867 /* Non-NULL if the body of the commands are being read from this
868 already-parsed command. */
869 struct command_line *control;
86b17b60 870
95a42b64
TT
871 /* The command lines read from the user, or NULL if they have not
872 yet been read. */
873 struct counted_command_line *cmd;
874};
875
876/* A callback for map_breakpoint_numbers that sets the commands for
877 commands_command. */
878
c906108c 879static void
95a42b64 880do_map_commands_command (struct breakpoint *b, void *data)
c906108c 881{
95a42b64 882 struct commands_info *info = data;
c906108c 883
95a42b64
TT
884 if (info->cmd == NULL)
885 {
886 struct command_line *l;
5c44784c 887
95a42b64
TT
888 if (info->control != NULL)
889 l = copy_command_lines (info->control->body_list[0]);
890 else
86b17b60
PA
891 {
892 struct cleanup *old_chain;
893 char *str;
c5aa993b 894
3e43a32a
MS
895 str = xstrprintf (_("Type commands for breakpoint(s) "
896 "%s, one per line."),
86b17b60
PA
897 info->arg);
898
899 old_chain = make_cleanup (xfree, str);
900
901 l = read_command_lines (str,
902 info->from_tty, 1,
d77f58be 903 (is_tracepoint (b)
86b17b60
PA
904 ? check_tracepoint_command : 0),
905 b);
906
907 do_cleanups (old_chain);
908 }
a7bdde9e 909
95a42b64
TT
910 info->cmd = alloc_counted_command_line (l);
911 }
912
913 /* If a breakpoint was on the list more than once, we don't need to
914 do anything. */
915 if (b->commands != info->cmd)
916 {
917 validate_commands_for_breakpoint (b, info->cmd->commands);
918 incref_counted_command_line (info->cmd);
919 decref_counted_command_line (&b->commands);
920 b->commands = info->cmd;
921 breakpoints_changed ();
8d3788bd 922 observer_notify_breakpoint_modified (b);
c5aa993b 923 }
95a42b64
TT
924}
925
926static void
4a64f543
MS
927commands_command_1 (char *arg, int from_tty,
928 struct command_line *control)
95a42b64
TT
929{
930 struct cleanup *cleanups;
931 struct commands_info info;
932
933 info.from_tty = from_tty;
934 info.control = control;
935 info.cmd = NULL;
936 /* If we read command lines from the user, then `info' will hold an
937 extra reference to the commands that we must clean up. */
938 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
939
940 if (arg == NULL || !*arg)
941 {
86b17b60 942 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
943 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
944 breakpoint_count);
95a42b64
TT
945 else if (breakpoint_count > 0)
946 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
947 else
948 {
949 /* So that we don't try to free the incoming non-NULL
950 argument in the cleanup below. Mapping breakpoint
951 numbers will fail in this case. */
952 arg = NULL;
953 }
95a42b64 954 }
9766ced4
SS
955 else
956 /* The command loop has some static state, so we need to preserve
957 our argument. */
958 arg = xstrdup (arg);
86b17b60
PA
959
960 if (arg != NULL)
961 make_cleanup (xfree, arg);
962
963 info.arg = arg;
95a42b64
TT
964
965 map_breakpoint_numbers (arg, do_map_commands_command, &info);
966
967 if (info.cmd == NULL)
968 error (_("No breakpoints specified."));
969
970 do_cleanups (cleanups);
971}
972
973static void
974commands_command (char *arg, int from_tty)
975{
976 commands_command_1 (arg, from_tty, NULL);
c906108c 977}
40c03ae8
EZ
978
979/* Like commands_command, but instead of reading the commands from
980 input stream, takes them from an already parsed command structure.
981
982 This is used by cli-script.c to DTRT with breakpoint commands
983 that are part of if and while bodies. */
984enum command_control_type
985commands_from_control_command (char *arg, struct command_line *cmd)
986{
95a42b64
TT
987 commands_command_1 (arg, 0, cmd);
988 return simple_control;
40c03ae8 989}
876fa593
JK
990
991/* Return non-zero if BL->TARGET_INFO contains valid information. */
992
993static int
994bp_location_has_shadow (struct bp_location *bl)
995{
996 if (bl->loc_type != bp_loc_software_breakpoint)
997 return 0;
998 if (!bl->inserted)
999 return 0;
1000 if (bl->target_info.shadow_len == 0)
e5dd4106 1001 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1002 return 0;
1003 return 1;
1004}
1005
8defab1a 1006/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1007 by replacing any memory breakpoints with their shadowed contents.
1008
1009 The range of shadowed area by each bp_location is:
35df4500
TJB
1010 bl->address - bp_location_placed_address_before_address_max
1011 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1012 The range we were requested to resolve shadows for is:
1013 memaddr ... memaddr + len
1014 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1015 memaddr + len <= (bl->address
1016 - bp_location_placed_address_before_address_max)
876fa593 1017 and:
35df4500 1018 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1019
8defab1a
DJ
1020void
1021breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1022{
4a64f543
MS
1023 /* Left boundary, right boundary and median element of our binary
1024 search. */
876fa593
JK
1025 unsigned bc_l, bc_r, bc;
1026
4a64f543
MS
1027 /* Find BC_L which is a leftmost element which may affect BUF
1028 content. It is safe to report lower value but a failure to
1029 report higher one. */
876fa593
JK
1030
1031 bc_l = 0;
1032 bc_r = bp_location_count;
1033 while (bc_l + 1 < bc_r)
1034 {
35df4500 1035 struct bp_location *bl;
876fa593
JK
1036
1037 bc = (bc_l + bc_r) / 2;
35df4500 1038 bl = bp_location[bc];
876fa593 1039
4a64f543
MS
1040 /* Check first BL->ADDRESS will not overflow due to the added
1041 constant. Then advance the left boundary only if we are sure
1042 the BC element can in no way affect the BUF content (MEMADDR
1043 to MEMADDR + LEN range).
876fa593 1044
4a64f543
MS
1045 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1046 offset so that we cannot miss a breakpoint with its shadow
1047 range tail still reaching MEMADDR. */
c5aa993b 1048
35df4500
TJB
1049 if ((bl->address + bp_location_shadow_len_after_address_max
1050 >= bl->address)
1051 && (bl->address + bp_location_shadow_len_after_address_max
1052 <= memaddr))
876fa593
JK
1053 bc_l = bc;
1054 else
1055 bc_r = bc;
1056 }
1057
128070bb
PA
1058 /* Due to the binary search above, we need to make sure we pick the
1059 first location that's at BC_L's address. E.g., if there are
1060 multiple locations at the same address, BC_L may end up pointing
1061 at a duplicate location, and miss the "master"/"inserted"
1062 location. Say, given locations L1, L2 and L3 at addresses A and
1063 B:
1064
1065 L1@A, L2@A, L3@B, ...
1066
1067 BC_L could end up pointing at location L2, while the "master"
1068 location could be L1. Since the `loc->inserted' flag is only set
1069 on "master" locations, we'd forget to restore the shadow of L1
1070 and L2. */
1071 while (bc_l > 0
1072 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1073 bc_l--;
1074
876fa593
JK
1075 /* Now do full processing of the found relevant range of elements. */
1076
1077 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1078 {
35df4500 1079 struct bp_location *bl = bp_location[bc];
876fa593
JK
1080 CORE_ADDR bp_addr = 0;
1081 int bp_size = 0;
1082 int bptoffset = 0;
1083
35df4500
TJB
1084 /* bp_location array has BL->OWNER always non-NULL. */
1085 if (bl->owner->type == bp_none)
8a3fe4f8 1086 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1087 bl->owner->number);
ffce0d52 1088
e5dd4106 1089 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1090 content. */
1091
35df4500
TJB
1092 if (bl->address >= bp_location_placed_address_before_address_max
1093 && memaddr + len <= (bl->address
1094 - bp_location_placed_address_before_address_max))
876fa593
JK
1095 break;
1096
35df4500 1097 if (!bp_location_has_shadow (bl))
c5aa993b 1098 continue;
35df4500 1099 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1100 current_program_space->aspace, 0))
1101 continue;
1102
c5aa993b
JM
1103 /* Addresses and length of the part of the breakpoint that
1104 we need to copy. */
35df4500
TJB
1105 bp_addr = bl->target_info.placed_address;
1106 bp_size = bl->target_info.shadow_len;
8defab1a 1107
c5aa993b
JM
1108 if (bp_addr + bp_size <= memaddr)
1109 /* The breakpoint is entirely before the chunk of memory we
1110 are reading. */
1111 continue;
8defab1a 1112
c5aa993b
JM
1113 if (bp_addr >= memaddr + len)
1114 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1115 reading. */
c5aa993b 1116 continue;
c5aa993b 1117
8defab1a
DJ
1118 /* Offset within shadow_contents. */
1119 if (bp_addr < memaddr)
1120 {
1121 /* Only copy the second part of the breakpoint. */
1122 bp_size -= memaddr - bp_addr;
1123 bptoffset = memaddr - bp_addr;
1124 bp_addr = memaddr;
1125 }
c5aa993b 1126
8defab1a
DJ
1127 if (bp_addr + bp_size > memaddr + len)
1128 {
1129 /* Only copy the first part of the breakpoint. */
1130 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1131 }
c5aa993b 1132
8defab1a 1133 memcpy (buf + bp_addr - memaddr,
35df4500 1134 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1135 }
c906108c 1136}
c906108c 1137\f
c5aa993b 1138
60e1c644
PA
1139/* Return true if BPT is of any hardware watchpoint kind. */
1140
a5606eee 1141static int
d77f58be 1142is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1143{
1144 return (bpt->type == bp_hardware_watchpoint
1145 || bpt->type == bp_read_watchpoint
1146 || bpt->type == bp_access_watchpoint);
1147}
7270d8f2 1148
60e1c644
PA
1149/* Return true if BPT is of any watchpoint kind, hardware or
1150 software. */
1151
1152static int
d77f58be 1153is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1154{
1155 return (is_hardware_watchpoint (bpt)
1156 || bpt->type == bp_watchpoint);
1157}
1158
f6bc2008
PA
1159/* Assuming that B is a watchpoint: returns true if the current thread
1160 and its running state are safe to evaluate or update watchpoint B.
1161 Watchpoints on local expressions need to be evaluated in the
1162 context of the thread that was current when the watchpoint was
1163 created, and, that thread needs to be stopped to be able to select
1164 the correct frame context. Watchpoints on global expressions can
1165 be evaluated on any thread, and in any state. It is presently left
1166 to the target allowing memory accesses when threads are
1167 running. */
1168
1169static int
1170watchpoint_in_thread_scope (struct breakpoint *b)
1171{
1172 return (ptid_equal (b->watchpoint_thread, null_ptid)
1173 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1174 && !is_executing (inferior_ptid)));
1175}
1176
d0fb5eae
JK
1177/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1178 associated bp_watchpoint_scope breakpoint. */
1179
1180static void
1181watchpoint_del_at_next_stop (struct breakpoint *b)
1182{
1183 gdb_assert (is_watchpoint (b));
1184
1185 if (b->related_breakpoint != b)
1186 {
1187 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1188 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1189 b->related_breakpoint->disposition = disp_del_at_next_stop;
1190 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1191 b->related_breakpoint = b;
1192 }
1193 b->disposition = disp_del_at_next_stop;
1194}
1195
567e1b4e
JB
1196/* Assuming that B is a watchpoint:
1197 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1198 - Evaluate expression and store the result in B->val
567e1b4e
JB
1199 - Evaluate the condition if there is one, and store the result
1200 in b->loc->cond.
a5606eee
VP
1201 - Update the list of values that must be watched in B->loc.
1202
4a64f543
MS
1203 If the watchpoint disposition is disp_del_at_next_stop, then do
1204 nothing. If this is local watchpoint that is out of scope, delete
1205 it.
1206
1207 Even with `set breakpoint always-inserted on' the watchpoints are
1208 removed + inserted on each stop here. Normal breakpoints must
1209 never be removed because they might be missed by a running thread
1210 when debugging in non-stop mode. On the other hand, hardware
1211 watchpoints (is_hardware_watchpoint; processed here) are specific
1212 to each LWP since they are stored in each LWP's hardware debug
1213 registers. Therefore, such LWP must be stopped first in order to
1214 be able to modify its hardware watchpoints.
1215
1216 Hardware watchpoints must be reset exactly once after being
1217 presented to the user. It cannot be done sooner, because it would
1218 reset the data used to present the watchpoint hit to the user. And
1219 it must not be done later because it could display the same single
1220 watchpoint hit during multiple GDB stops. Note that the latter is
1221 relevant only to the hardware watchpoint types bp_read_watchpoint
1222 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1223 not user-visible - its hit is suppressed if the memory content has
1224 not changed.
1225
1226 The following constraints influence the location where we can reset
1227 hardware watchpoints:
1228
1229 * target_stopped_by_watchpoint and target_stopped_data_address are
1230 called several times when GDB stops.
1231
1232 [linux]
1233 * Multiple hardware watchpoints can be hit at the same time,
1234 causing GDB to stop. GDB only presents one hardware watchpoint
1235 hit at a time as the reason for stopping, and all the other hits
1236 are presented later, one after the other, each time the user
1237 requests the execution to be resumed. Execution is not resumed
1238 for the threads still having pending hit event stored in
1239 LWP_INFO->STATUS. While the watchpoint is already removed from
1240 the inferior on the first stop the thread hit event is kept being
1241 reported from its cached value by linux_nat_stopped_data_address
1242 until the real thread resume happens after the watchpoint gets
1243 presented and thus its LWP_INFO->STATUS gets reset.
1244
1245 Therefore the hardware watchpoint hit can get safely reset on the
1246 watchpoint removal from inferior. */
a79d3c27 1247
b40ce68a 1248static void
a5606eee 1249update_watchpoint (struct breakpoint *b, int reparse)
7270d8f2 1250{
a5606eee 1251 int within_current_scope;
a5606eee 1252 struct frame_id saved_frame_id;
66076460 1253 int frame_saved;
a5606eee 1254
d0fb5eae
JK
1255 gdb_assert (is_watchpoint (b));
1256
f6bc2008
PA
1257 /* If this is a local watchpoint, we only want to check if the
1258 watchpoint frame is in scope if the current thread is the thread
1259 that was used to create the watchpoint. */
1260 if (!watchpoint_in_thread_scope (b))
1261 return;
1262
a5606eee
VP
1263 if (b->disposition == disp_del_at_next_stop)
1264 return;
1265
66076460 1266 frame_saved = 0;
a5606eee
VP
1267
1268 /* Determine if the watchpoint is within scope. */
1269 if (b->exp_valid_block == NULL)
1270 within_current_scope = 1;
1271 else
1272 {
b5db5dfc
UW
1273 struct frame_info *fi = get_current_frame ();
1274 struct gdbarch *frame_arch = get_frame_arch (fi);
1275 CORE_ADDR frame_pc = get_frame_pc (fi);
1276
1277 /* If we're in a function epilogue, unwinding may not work
1278 properly, so do not attempt to recreate locations at this
1279 point. See similar comments in watchpoint_check. */
1280 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1281 return;
66076460
DJ
1282
1283 /* Save the current frame's ID so we can restore it after
1284 evaluating the watchpoint expression on its own frame. */
1285 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1286 took a frame parameter, so that we didn't have to change the
1287 selected frame. */
1288 frame_saved = 1;
1289 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1290
a5606eee
VP
1291 fi = frame_find_by_id (b->watchpoint_frame);
1292 within_current_scope = (fi != NULL);
1293 if (within_current_scope)
1294 select_frame (fi);
1295 }
1296
b5db5dfc
UW
1297 /* We don't free locations. They are stored in the bp_location array
1298 and update_global_location_list will eventually delete them and
1299 remove breakpoints if needed. */
1300 b->loc = NULL;
1301
a5606eee
VP
1302 if (within_current_scope && reparse)
1303 {
1304 char *s;
d63d0675 1305
a5606eee
VP
1306 if (b->exp)
1307 {
1308 xfree (b->exp);
1309 b->exp = NULL;
1310 }
d63d0675 1311 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1312 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1313 /* If the meaning of expression itself changed, the old value is
1314 no longer relevant. We don't want to report a watchpoint hit
1315 to the user when the old value and the new value may actually
1316 be completely different objects. */
1317 value_free (b->val);
fa4727a6
DJ
1318 b->val = NULL;
1319 b->val_valid = 0;
60e1c644
PA
1320
1321 /* Note that unlike with breakpoints, the watchpoint's condition
1322 expression is stored in the breakpoint object, not in the
1323 locations (re)created below. */
1324 if (b->cond_string != NULL)
1325 {
1326 if (b->cond_exp != NULL)
1327 {
1328 xfree (b->cond_exp);
1329 b->cond_exp = NULL;
1330 }
1331
1332 s = b->cond_string;
1333 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1334 }
a5606eee 1335 }
a5606eee
VP
1336
1337 /* If we failed to parse the expression, for example because
1338 it refers to a global variable in a not-yet-loaded shared library,
1339 don't try to insert watchpoint. We don't automatically delete
1340 such watchpoint, though, since failure to parse expression
1341 is different from out-of-scope watchpoint. */
2d134ed3
PA
1342 if ( !target_has_execution)
1343 {
1344 /* Without execution, memory can't change. No use to try and
1345 set watchpoint locations. The watchpoint will be reset when
1346 the target gains execution, through breakpoint_re_set. */
1347 }
1348 else if (within_current_scope && b->exp)
a5606eee 1349 {
0cf6dd15 1350 int pc = 0;
fa4727a6 1351 struct value *val_chain, *v, *result, *next;
2d134ed3 1352 struct program_space *frame_pspace;
a5606eee 1353
0cf6dd15 1354 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1355
a5606eee
VP
1356 /* Avoid setting b->val if it's already set. The meaning of
1357 b->val is 'the last value' user saw, and we should update
1358 it only if we reported that last value to user. As it
9c06b0b4
TJB
1359 happens, the code that reports it updates b->val directly.
1360 We don't keep track of the memory value for masked
1361 watchpoints. */
1362 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6
DJ
1363 {
1364 b->val = v;
1365 b->val_valid = 1;
1366 }
a5606eee 1367
2d134ed3
PA
1368 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1369
a5606eee 1370 /* Look at each value on the value chain. */
9fa40276 1371 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1372 {
1373 /* If it's a memory location, and GDB actually needed
1374 its contents to evaluate the expression, then we
fa4727a6
DJ
1375 must watch it. If the first value returned is
1376 still lazy, that means an error occurred reading it;
1377 watch it anyway in case it becomes readable. */
a5606eee 1378 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1379 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1380 {
1381 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1382
a5606eee
VP
1383 /* We only watch structs and arrays if user asked
1384 for it explicitly, never if they just happen to
1385 appear in the middle of some value chain. */
fa4727a6 1386 if (v == result
a5606eee
VP
1387 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1388 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1389 {
1390 CORE_ADDR addr;
1391 int len, type;
1392 struct bp_location *loc, **tmp;
1393
42ae5230 1394 addr = value_address (v);
a5606eee
VP
1395 len = TYPE_LENGTH (value_type (v));
1396 type = hw_write;
1397 if (b->type == bp_read_watchpoint)
1398 type = hw_read;
1399 else if (b->type == bp_access_watchpoint)
1400 type = hw_access;
1401
39d61571 1402 loc = allocate_bp_location (b);
a5606eee
VP
1403 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1404 ;
1405 *tmp = loc;
a6d9a66e 1406 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1407
1408 loc->pspace = frame_pspace;
a5606eee
VP
1409 loc->address = addr;
1410 loc->length = len;
1411 loc->watchpoint_type = type;
1412 }
1413 }
9fa40276
TJB
1414 }
1415
1416 /* Change the type of breakpoint between hardware assisted or
1417 an ordinary watchpoint depending on the hardware support
1418 and free hardware slots. REPARSE is set when the inferior
1419 is started. */
a9634178 1420 if (reparse)
9fa40276 1421 {
e09342b5 1422 int reg_cnt;
9fa40276
TJB
1423 enum bp_loc_type loc_type;
1424 struct bp_location *bl;
a5606eee 1425
a9634178 1426 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1427
1428 if (reg_cnt)
9fa40276
TJB
1429 {
1430 int i, target_resources_ok, other_type_used;
1431
a9634178
TJB
1432 /* Use an exact watchpoint when there's only one memory region to be
1433 watched, and only one debug register is needed to watch it. */
1434 b->exact = target_exact_watchpoints && reg_cnt == 1;
1435
9fa40276 1436 /* We need to determine how many resources are already
e09342b5
TJB
1437 used for all other hardware watchpoints plus this one
1438 to see if we still have enough resources to also fit
1439 this watchpoint in as well. To guarantee the
1440 hw_watchpoint_used_count call below counts this
1441 watchpoint, make sure that it is marked as a hardware
1442 watchpoint. */
a9634178
TJB
1443 if (b->type == bp_watchpoint)
1444 b->type = bp_hardware_watchpoint;
9fa40276 1445
a9634178 1446 i = hw_watchpoint_used_count (b->type, &other_type_used);
e09342b5 1447 target_resources_ok = target_can_use_hardware_watchpoint
a9634178 1448 (b->type, i, other_type_used);
e09342b5 1449 if (target_resources_ok <= 0)
a9634178 1450 {
348d480f 1451 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1452
1453 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1454 error (_("Target does not support this type of "
1455 "hardware watchpoint."));
9c06b0b4
TJB
1456 else if (target_resources_ok < 0 && !sw_mode)
1457 error (_("There are not enough available hardware "
1458 "resources for this watchpoint."));
a9634178
TJB
1459 else
1460 b->type = bp_watchpoint;
1461 }
9fa40276 1462 }
348d480f 1463 else if (!b->ops->works_in_software_mode (b))
a9634178
TJB
1464 error (_("Expression cannot be implemented with "
1465 "read/access watchpoint."));
9fa40276
TJB
1466 else
1467 b->type = bp_watchpoint;
1468
1469 loc_type = (b->type == bp_watchpoint? bp_loc_other
1470 : bp_loc_hardware_watchpoint);
1471 for (bl = b->loc; bl; bl = bl->next)
1472 bl->loc_type = loc_type;
1473 }
1474
1475 for (v = val_chain; v; v = next)
1476 {
a5606eee
VP
1477 next = value_next (v);
1478 if (v != b->val)
1479 value_free (v);
1480 }
1481
c7437ca6
PA
1482 /* If a software watchpoint is not watching any memory, then the
1483 above left it without any location set up. But,
1484 bpstat_stop_status requires a location to be able to report
1485 stops, so make sure there's at least a dummy one. */
1486 if (b->type == bp_watchpoint && b->loc == NULL)
1487 {
1488 b->loc = allocate_bp_location (b);
1489 b->loc->pspace = frame_pspace;
1490 b->loc->address = -1;
1491 b->loc->length = -1;
1492 b->loc->watchpoint_type = -1;
1493 }
a5606eee
VP
1494 }
1495 else if (!within_current_scope)
7270d8f2 1496 {
ac74f770
MS
1497 printf_filtered (_("\
1498Watchpoint %d deleted because the program has left the block\n\
1499in which its expression is valid.\n"),
a5606eee 1500 b->number);
d0fb5eae 1501 watchpoint_del_at_next_stop (b);
7270d8f2 1502 }
a5606eee
VP
1503
1504 /* Restore the selected frame. */
66076460
DJ
1505 if (frame_saved)
1506 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1507}
1508
a5606eee 1509
74960c60
VP
1510/* Returns 1 iff breakpoint location should be
1511 inserted in the inferior. */
1512static int
35df4500 1513should_be_inserted (struct bp_location *bl)
74960c60 1514{
35df4500 1515 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1516 return 0;
1517
35df4500 1518 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1519 return 0;
1520
35df4500 1521 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1522 return 0;
1523
56710373
PA
1524 /* This is set for example, when we're attached to the parent of a
1525 vfork, and have detached from the child. The child is running
1526 free, and we expect it to do an exec or exit, at which point the
1527 OS makes the parent schedulable again (and the target reports
1528 that the vfork is done). Until the child is done with the shared
1529 memory region, do not insert breakpoints in the parent, otherwise
1530 the child could still trip on the parent's breakpoints. Since
1531 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1532 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1533 return 0;
1534
1042e4c0
SS
1535 /* Tracepoints are inserted by the target at a time of its choosing,
1536 not by us. */
35df4500 1537 if (is_tracepoint (bl->owner))
1042e4c0
SS
1538 return 0;
1539
74960c60
VP
1540 return 1;
1541}
1542
35df4500
TJB
1543/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1544 location. Any error messages are printed to TMP_ERROR_STREAM; and
1545 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2 1546
4a64f543
MS
1547 NOTE drow/2003-09-09: This routine could be broken down to an
1548 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1549static int
35df4500 1550insert_bp_location (struct bp_location *bl,
26bb91f3 1551 struct ui_file *tmp_error_stream,
fa3a767f 1552 int *disabled_breaks,
26bb91f3 1553 int *hw_breakpoint_error)
879bfdc2
DJ
1554{
1555 int val = 0;
1556
35df4500 1557 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1558 return 0;
1559
8181d85f 1560 /* Initialize the target-specific information. */
35df4500
TJB
1561 memset (&bl->target_info, 0, sizeof (bl->target_info));
1562 bl->target_info.placed_address = bl->address;
1563 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1564 bl->target_info.length = bl->length;
8181d85f 1565
35df4500
TJB
1566 if (bl->loc_type == bp_loc_software_breakpoint
1567 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1568 {
35df4500 1569 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1570 {
1571 /* If the explicitly specified breakpoint type
1572 is not hardware breakpoint, check the memory map to see
1573 if the breakpoint address is in read only memory or not.
4a64f543 1574
765dc015
VP
1575 Two important cases are:
1576 - location type is not hardware breakpoint, memory
1577 is readonly. We change the type of the location to
1578 hardware breakpoint.
4a64f543
MS
1579 - location type is hardware breakpoint, memory is
1580 read-write. This means we've previously made the
1581 location hardware one, but then the memory map changed,
1582 so we undo.
765dc015 1583
4a64f543
MS
1584 When breakpoints are removed, remove_breakpoints will use
1585 location types we've just set here, the only possible
1586 problem is that memory map has changed during running
1587 program, but it's not going to work anyway with current
1588 gdb. */
765dc015 1589 struct mem_region *mr
35df4500 1590 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1591
1592 if (mr)
1593 {
1594 if (automatic_hardware_breakpoints)
1595 {
765dc015
VP
1596 enum bp_loc_type new_type;
1597
1598 if (mr->attrib.mode != MEM_RW)
1599 new_type = bp_loc_hardware_breakpoint;
1600 else
1601 new_type = bp_loc_software_breakpoint;
1602
35df4500 1603 if (new_type != bl->loc_type)
765dc015
VP
1604 {
1605 static int said = 0;
cc59ec59 1606
35df4500 1607 bl->loc_type = new_type;
765dc015
VP
1608 if (!said)
1609 {
3e43a32a
MS
1610 fprintf_filtered (gdb_stdout,
1611 _("Note: automatically using "
1612 "hardware breakpoints for "
1613 "read-only addresses.\n"));
765dc015
VP
1614 said = 1;
1615 }
1616 }
1617 }
35df4500 1618 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1619 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1620 warning (_("cannot set software breakpoint "
1621 "at readonly address %s"),
35df4500 1622 paddress (bl->gdbarch, bl->address));
765dc015
VP
1623 }
1624 }
1625
879bfdc2
DJ
1626 /* First check to see if we have to handle an overlay. */
1627 if (overlay_debugging == ovly_off
35df4500
TJB
1628 || bl->section == NULL
1629 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1630 {
1631 /* No overlay handling: just set the breakpoint. */
1632
348d480f 1633 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1634 }
1635 else
1636 {
4a64f543 1637 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1638 Shall we set a breakpoint at the LMA? */
1639 if (!overlay_events_enabled)
1640 {
1641 /* Yes -- overlay event support is not active,
1642 so we must try to set a breakpoint at the LMA.
1643 This will not work for a hardware breakpoint. */
35df4500 1644 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1645 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1646 bl->owner->number);
879bfdc2
DJ
1647 else
1648 {
35df4500
TJB
1649 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1650 bl->section);
879bfdc2 1651 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1652 bl->overlay_target_info = bl->target_info;
1653 bl->overlay_target_info.placed_address = addr;
1654 val = target_insert_breakpoint (bl->gdbarch,
1655 &bl->overlay_target_info);
879bfdc2 1656 if (val != 0)
99361f52 1657 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1658 "Overlay breakpoint %d "
1659 "failed: in ROM?\n",
35df4500 1660 bl->owner->number);
879bfdc2
DJ
1661 }
1662 }
1663 /* Shall we set a breakpoint at the VMA? */
35df4500 1664 if (section_is_mapped (bl->section))
879bfdc2
DJ
1665 {
1666 /* Yes. This overlay section is mapped into memory. */
348d480f 1667 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1668 }
1669 else
1670 {
1671 /* No. This breakpoint will not be inserted.
1672 No error, but do not mark the bp as 'inserted'. */
1673 return 0;
1674 }
1675 }
1676
1677 if (val)
1678 {
1679 /* Can't set the breakpoint. */
35df4500 1680 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1681 {
4a64f543 1682 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1683 val = 0;
35df4500 1684 bl->shlib_disabled = 1;
8d3788bd 1685 observer_notify_breakpoint_modified (bl->owner);
879bfdc2
DJ
1686 if (!*disabled_breaks)
1687 {
1688 fprintf_unfiltered (tmp_error_stream,
1689 "Cannot insert breakpoint %d.\n",
35df4500 1690 bl->owner->number);
879bfdc2 1691 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1692 "Temporarily disabling shared "
1693 "library breakpoints:\n");
879bfdc2
DJ
1694 }
1695 *disabled_breaks = 1;
1696 fprintf_unfiltered (tmp_error_stream,
35df4500 1697 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1698 }
1699 else
879bfdc2 1700 {
35df4500 1701 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1702 {
1703 *hw_breakpoint_error = 1;
3e43a32a
MS
1704 fprintf_unfiltered (tmp_error_stream,
1705 "Cannot insert hardware "
1706 "breakpoint %d.\n",
35df4500 1707 bl->owner->number);
879bfdc2
DJ
1708 }
1709 else
1710 {
1711 fprintf_unfiltered (tmp_error_stream,
1712 "Cannot insert breakpoint %d.\n",
35df4500 1713 bl->owner->number);
879bfdc2
DJ
1714 fprintf_filtered (tmp_error_stream,
1715 "Error accessing memory address ");
35df4500 1716 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1717 tmp_error_stream);
879bfdc2
DJ
1718 fprintf_filtered (tmp_error_stream, ": %s.\n",
1719 safe_strerror (val));
1720 }
1721
1722 }
1723 }
1724 else
35df4500 1725 bl->inserted = 1;
879bfdc2
DJ
1726
1727 return val;
1728 }
1729
35df4500 1730 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1731 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1732 watchpoints. It's not clear that it's necessary... */
35df4500 1733 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1734 {
77b06cd7
TJB
1735 gdb_assert (bl->owner->ops != NULL
1736 && bl->owner->ops->insert_location != NULL);
1737
1738 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1739
1740 /* If trying to set a read-watchpoint, and it turns out it's not
1741 supported, try emulating one with an access watchpoint. */
35df4500 1742 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1743 {
1744 struct bp_location *loc, **loc_temp;
1745
1746 /* But don't try to insert it, if there's already another
1747 hw_access location that would be considered a duplicate
1748 of this one. */
1749 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1750 if (loc != bl
85d721b8 1751 && loc->watchpoint_type == hw_access
35df4500 1752 && watchpoint_locations_match (bl, loc))
85d721b8 1753 {
35df4500
TJB
1754 bl->duplicate = 1;
1755 bl->inserted = 1;
1756 bl->target_info = loc->target_info;
1757 bl->watchpoint_type = hw_access;
85d721b8
PA
1758 val = 0;
1759 break;
1760 }
1761
1762 if (val == 1)
1763 {
77b06cd7
TJB
1764 bl->watchpoint_type = hw_access;
1765 val = bl->owner->ops->insert_location (bl);
1766
1767 if (val)
1768 /* Back to the original value. */
1769 bl->watchpoint_type = hw_read;
85d721b8
PA
1770 }
1771 }
1772
35df4500 1773 bl->inserted = (val == 0);
879bfdc2
DJ
1774 }
1775
35df4500 1776 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1777 {
77b06cd7
TJB
1778 gdb_assert (bl->owner->ops != NULL
1779 && bl->owner->ops->insert_location != NULL);
1780
1781 val = bl->owner->ops->insert_location (bl);
1782 if (val)
1783 {
1784 bl->owner->enable_state = bp_disabled;
1785
1786 if (val == 1)
1787 warning (_("\
1788Error inserting catchpoint %d: Your system does not support this type\n\
1789of catchpoint."), bl->owner->number);
1790 else
1791 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1792 }
1793
1794 bl->inserted = (val == 0);
1640b821
DJ
1795
1796 /* We've already printed an error message if there was a problem
1797 inserting this catchpoint, and we've disabled the catchpoint,
1798 so just return success. */
1799 return 0;
879bfdc2
DJ
1800 }
1801
1802 return 0;
1803}
1804
6c95b8df
PA
1805/* This function is called when program space PSPACE is about to be
1806 deleted. It takes care of updating breakpoints to not reference
1807 PSPACE anymore. */
1808
1809void
1810breakpoint_program_space_exit (struct program_space *pspace)
1811{
1812 struct breakpoint *b, *b_temp;
876fa593 1813 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1814
1815 /* Remove any breakpoint that was set through this program space. */
1816 ALL_BREAKPOINTS_SAFE (b, b_temp)
1817 {
1818 if (b->pspace == pspace)
1819 delete_breakpoint (b);
1820 }
1821
1822 /* Breakpoints set through other program spaces could have locations
1823 bound to PSPACE as well. Remove those. */
876fa593 1824 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1825 {
1826 struct bp_location *tmp;
1827
1828 if (loc->pspace == pspace)
1829 {
2bdf28a0 1830 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1831 if (loc->owner->loc == loc)
1832 loc->owner->loc = loc->next;
1833 else
1834 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1835 if (tmp->next == loc)
1836 {
1837 tmp->next = loc->next;
1838 break;
1839 }
1840 }
1841 }
1842
1843 /* Now update the global location list to permanently delete the
1844 removed locations above. */
1845 update_global_location_list (0);
1846}
1847
74960c60
VP
1848/* Make sure all breakpoints are inserted in inferior.
1849 Throws exception on any error.
1850 A breakpoint that is already inserted won't be inserted
1851 again, so calling this function twice is safe. */
1852void
1853insert_breakpoints (void)
1854{
1855 struct breakpoint *bpt;
1856
1857 ALL_BREAKPOINTS (bpt)
1858 if (is_hardware_watchpoint (bpt))
4a64f543 1859 update_watchpoint (bpt, 0 /* don't reparse. */);
74960c60 1860
b60e7edf 1861 update_global_location_list (1);
74960c60 1862
c35b1492
PA
1863 /* update_global_location_list does not insert breakpoints when
1864 always_inserted_mode is not enabled. Explicitly insert them
1865 now. */
1866 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1867 insert_breakpoint_locations ();
1868}
1869
c906108c
SS
1870/* insert_breakpoints is used when starting or continuing the program.
1871 remove_breakpoints is used when the program stops.
1872 Both return zero if successful,
1873 or an `errno' value if could not write the inferior. */
1874
74960c60
VP
1875static void
1876insert_breakpoint_locations (void)
c906108c 1877{
a5606eee 1878 struct breakpoint *bpt;
35df4500 1879 struct bp_location *bl, **blp_tmp;
e236ba44 1880 int error = 0;
c906108c
SS
1881 int val = 0;
1882 int disabled_breaks = 0;
81d0cc19 1883 int hw_breakpoint_error = 0;
c906108c 1884
81d0cc19 1885 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1886 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1887
81d0cc19
GS
1888 /* Explicitly mark the warning -- this will only be printed if
1889 there was an error. */
1890 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1891
1892 save_current_space_and_thread ();
1893
35df4500 1894 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1895 {
35df4500 1896 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1897 continue;
1898
4a64f543
MS
1899 /* There is no point inserting thread-specific breakpoints if
1900 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1901 has BL->OWNER always non-NULL. */
35df4500
TJB
1902 if (bl->owner->thread != -1
1903 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1904 continue;
1905
35df4500 1906 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1907
1908 /* For targets that support global breakpoints, there's no need
1909 to select an inferior to insert breakpoint to. In fact, even
1910 if we aren't attached to any process yet, we should still
1911 insert breakpoints. */
1912 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1913 && ptid_equal (inferior_ptid, null_ptid))
1914 continue;
1915
35df4500 1916 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
879bfdc2
DJ
1917 &hw_breakpoint_error);
1918 if (val)
e236ba44 1919 error = val;
879bfdc2 1920 }
c906108c 1921
4a64f543
MS
1922 /* If we failed to insert all locations of a watchpoint, remove
1923 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1924 ALL_BREAKPOINTS (bpt)
1925 {
1926 int some_failed = 0;
1927 struct bp_location *loc;
1928
1929 if (!is_hardware_watchpoint (bpt))
1930 continue;
1931
d6b74ac4 1932 if (!breakpoint_enabled (bpt))
a5606eee 1933 continue;
74960c60
VP
1934
1935 if (bpt->disposition == disp_del_at_next_stop)
1936 continue;
a5606eee
VP
1937
1938 for (loc = bpt->loc; loc; loc = loc->next)
56710373 1939 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
1940 {
1941 some_failed = 1;
1942 break;
1943 }
1944 if (some_failed)
1945 {
1946 for (loc = bpt->loc; loc; loc = loc->next)
1947 if (loc->inserted)
1948 remove_breakpoint (loc, mark_uninserted);
1949
1950 hw_breakpoint_error = 1;
1951 fprintf_unfiltered (tmp_error_stream,
1952 "Could not insert hardware watchpoint %d.\n",
1953 bpt->number);
1954 error = -1;
1955 }
1956 }
1957
e236ba44 1958 if (error)
81d0cc19
GS
1959 {
1960 /* If a hardware breakpoint or watchpoint was inserted, add a
1961 message about possibly exhausted resources. */
879bfdc2 1962 if (hw_breakpoint_error)
81d0cc19 1963 {
c6510018
MS
1964 fprintf_unfiltered (tmp_error_stream,
1965 "Could not insert hardware breakpoints:\n\
1966You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 1967 }
81d0cc19
GS
1968 target_terminal_ours_for_output ();
1969 error_stream (tmp_error_stream);
1970 }
f7545552
TT
1971
1972 do_cleanups (cleanups);
c906108c
SS
1973}
1974
c906108c 1975int
fba45db2 1976remove_breakpoints (void)
c906108c 1977{
35df4500 1978 struct bp_location *bl, **blp_tmp;
3a1bae8e 1979 int val = 0;
c906108c 1980
35df4500 1981 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 1982 {
35df4500
TJB
1983 if (bl->inserted)
1984 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 1985 }
3a1bae8e 1986 return val;
c906108c
SS
1987}
1988
6c95b8df
PA
1989/* Remove breakpoints of process PID. */
1990
1991int
1992remove_breakpoints_pid (int pid)
1993{
35df4500 1994 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
1995 int val;
1996 struct inferior *inf = find_inferior_pid (pid);
1997
35df4500 1998 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 1999 {
35df4500 2000 if (bl->pspace != inf->pspace)
6c95b8df
PA
2001 continue;
2002
35df4500 2003 if (bl->inserted)
6c95b8df 2004 {
35df4500 2005 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2006 if (val != 0)
2007 return val;
2008 }
2009 }
2010 return 0;
2011}
2012
c906108c 2013int
fba45db2 2014reattach_breakpoints (int pid)
c906108c 2015{
6c95b8df 2016 struct cleanup *old_chain;
35df4500 2017 struct bp_location *bl, **blp_tmp;
c906108c 2018 int val;
86b887df 2019 struct ui_file *tmp_error_stream;
fa3a767f 2020 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2021 struct inferior *inf;
2022 struct thread_info *tp;
2023
2024 tp = any_live_thread_of_process (pid);
2025 if (tp == NULL)
2026 return 1;
2027
2028 inf = find_inferior_pid (pid);
2029 old_chain = save_inferior_ptid ();
2030
2031 inferior_ptid = tp->ptid;
a4954f26 2032
86b887df 2033 tmp_error_stream = mem_fileopen ();
a4954f26 2034 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2035
35df4500 2036 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2037 {
35df4500 2038 if (bl->pspace != inf->pspace)
6c95b8df
PA
2039 continue;
2040
35df4500 2041 if (bl->inserted)
c5aa993b 2042 {
35df4500
TJB
2043 bl->inserted = 0;
2044 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2045 if (val != 0)
2046 {
ce696e05 2047 do_cleanups (old_chain);
c5aa993b
JM
2048 return val;
2049 }
2050 }
2051 }
ce696e05 2052 do_cleanups (old_chain);
c906108c
SS
2053 return 0;
2054}
2055
e58b0e63
PA
2056static int internal_breakpoint_number = -1;
2057
84f4c1fe
PM
2058/* Set the breakpoint number of B, depending on the value of INTERNAL.
2059 If INTERNAL is non-zero, the breakpoint number will be populated
2060 from internal_breakpoint_number and that variable decremented.
e5dd4106 2061 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2062 breakpoint_count and that value incremented. Internal breakpoints
2063 do not set the internal var bpnum. */
2064static void
2065set_breakpoint_number (int internal, struct breakpoint *b)
2066{
2067 if (internal)
2068 b->number = internal_breakpoint_number--;
2069 else
2070 {
2071 set_breakpoint_count (breakpoint_count + 1);
2072 b->number = breakpoint_count;
2073 }
2074}
2075
e62c965a 2076static struct breakpoint *
a6d9a66e 2077create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0
PA
2078 CORE_ADDR address, enum bptype type,
2079 struct breakpoint_ops *ops)
e62c965a 2080{
e62c965a
PP
2081 struct symtab_and_line sal;
2082 struct breakpoint *b;
2083
4a64f543 2084 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2085
2086 sal.pc = address;
2087 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2088 sal.pspace = current_program_space;
e62c965a 2089
06edf0c0 2090 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2091 b->number = internal_breakpoint_number--;
2092 b->disposition = disp_donttouch;
2093
2094 return b;
2095}
2096
17450429
PP
2097static const char *const longjmp_names[] =
2098 {
2099 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2100 };
2101#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2102
2103/* Per-objfile data private to breakpoint.c. */
2104struct breakpoint_objfile_data
2105{
2106 /* Minimal symbol for "_ovly_debug_event" (if any). */
2107 struct minimal_symbol *overlay_msym;
2108
2109 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2110 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2111
2112 /* Minimal symbol for "std::terminate()" (if any). */
2113 struct minimal_symbol *terminate_msym;
2114
2115 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2116 struct minimal_symbol *exception_msym;
2117};
2118
2119static const struct objfile_data *breakpoint_objfile_key;
2120
2121/* Minimal symbol not found sentinel. */
2122static struct minimal_symbol msym_not_found;
2123
2124/* Returns TRUE if MSYM point to the "not found" sentinel. */
2125
2126static int
2127msym_not_found_p (const struct minimal_symbol *msym)
2128{
2129 return msym == &msym_not_found;
2130}
2131
2132/* Return per-objfile data needed by breakpoint.c.
2133 Allocate the data if necessary. */
2134
2135static struct breakpoint_objfile_data *
2136get_breakpoint_objfile_data (struct objfile *objfile)
2137{
2138 struct breakpoint_objfile_data *bp_objfile_data;
2139
2140 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2141 if (bp_objfile_data == NULL)
2142 {
2143 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2144 sizeof (*bp_objfile_data));
2145
2146 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2147 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2148 }
2149 return bp_objfile_data;
2150}
2151
e62c965a 2152static void
af02033e 2153create_overlay_event_breakpoint (void)
e62c965a 2154{
69de3c6a 2155 struct objfile *objfile;
af02033e 2156 const char *const func_name = "_ovly_debug_event";
e62c965a 2157
69de3c6a
PP
2158 ALL_OBJFILES (objfile)
2159 {
2160 struct breakpoint *b;
17450429
PP
2161 struct breakpoint_objfile_data *bp_objfile_data;
2162 CORE_ADDR addr;
69de3c6a 2163
17450429
PP
2164 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2165
2166 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2167 continue;
2168
2169 if (bp_objfile_data->overlay_msym == NULL)
2170 {
2171 struct minimal_symbol *m;
2172
2173 m = lookup_minimal_symbol_text (func_name, objfile);
2174 if (m == NULL)
2175 {
2176 /* Avoid future lookups in this objfile. */
2177 bp_objfile_data->overlay_msym = &msym_not_found;
2178 continue;
2179 }
2180 bp_objfile_data->overlay_msym = m;
2181 }
e62c965a 2182
17450429
PP
2183 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2184 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2185 bp_overlay_event,
2186 &internal_breakpoint_ops);
69de3c6a 2187 b->addr_string = xstrdup (func_name);
e62c965a 2188
69de3c6a
PP
2189 if (overlay_debugging == ovly_auto)
2190 {
2191 b->enable_state = bp_enabled;
2192 overlay_events_enabled = 1;
2193 }
2194 else
2195 {
2196 b->enable_state = bp_disabled;
2197 overlay_events_enabled = 0;
2198 }
e62c965a
PP
2199 }
2200 update_global_location_list (1);
2201}
2202
0fd8e87f 2203static void
af02033e 2204create_longjmp_master_breakpoint (void)
0fd8e87f 2205{
6c95b8df 2206 struct program_space *pspace;
6c95b8df
PA
2207 struct cleanup *old_chain;
2208
2209 old_chain = save_current_program_space ();
0fd8e87f 2210
6c95b8df 2211 ALL_PSPACES (pspace)
af02033e
PP
2212 {
2213 struct objfile *objfile;
2214
2215 set_current_program_space (pspace);
2216
2217 ALL_OBJFILES (objfile)
0fd8e87f 2218 {
af02033e
PP
2219 int i;
2220 struct gdbarch *gdbarch;
17450429 2221 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2222
af02033e
PP
2223 gdbarch = get_objfile_arch (objfile);
2224 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2225 continue;
2226
17450429
PP
2227 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2228
2229 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2230 {
2231 struct breakpoint *b;
af02033e 2232 const char *func_name;
17450429 2233 CORE_ADDR addr;
6c95b8df 2234
17450429 2235 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2236 continue;
0fd8e87f 2237
17450429
PP
2238 func_name = longjmp_names[i];
2239 if (bp_objfile_data->longjmp_msym[i] == NULL)
2240 {
2241 struct minimal_symbol *m;
2242
2243 m = lookup_minimal_symbol_text (func_name, objfile);
2244 if (m == NULL)
2245 {
2246 /* Prevent future lookups in this objfile. */
2247 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2248 continue;
2249 }
2250 bp_objfile_data->longjmp_msym[i] = m;
2251 }
2252
2253 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2254 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2255 &internal_breakpoint_ops);
af02033e
PP
2256 b->addr_string = xstrdup (func_name);
2257 b->enable_state = bp_disabled;
2258 }
0fd8e87f 2259 }
af02033e 2260 }
0fd8e87f 2261 update_global_location_list (1);
6c95b8df
PA
2262
2263 do_cleanups (old_chain);
0fd8e87f
UW
2264}
2265
af02033e 2266/* Create a master std::terminate breakpoint. */
aa7d318d 2267static void
af02033e 2268create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2269{
2270 struct program_space *pspace;
aa7d318d 2271 struct cleanup *old_chain;
af02033e 2272 const char *const func_name = "std::terminate()";
aa7d318d
TT
2273
2274 old_chain = save_current_program_space ();
2275
2276 ALL_PSPACES (pspace)
17450429
PP
2277 {
2278 struct objfile *objfile;
2279 CORE_ADDR addr;
2280
2281 set_current_program_space (pspace);
2282
aa7d318d
TT
2283 ALL_OBJFILES (objfile)
2284 {
2285 struct breakpoint *b;
17450429 2286 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2287
17450429 2288 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2289
17450429
PP
2290 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2291 continue;
2292
2293 if (bp_objfile_data->terminate_msym == NULL)
2294 {
2295 struct minimal_symbol *m;
2296
2297 m = lookup_minimal_symbol (func_name, NULL, objfile);
2298 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2299 && MSYMBOL_TYPE (m) != mst_file_text))
2300 {
2301 /* Prevent future lookups in this objfile. */
2302 bp_objfile_data->terminate_msym = &msym_not_found;
2303 continue;
2304 }
2305 bp_objfile_data->terminate_msym = m;
2306 }
aa7d318d 2307
17450429
PP
2308 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2309 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2310 bp_std_terminate_master,
2311 &internal_breakpoint_ops);
aa7d318d
TT
2312 b->addr_string = xstrdup (func_name);
2313 b->enable_state = bp_disabled;
2314 }
17450429
PP
2315 }
2316
aa7d318d
TT
2317 update_global_location_list (1);
2318
2319 do_cleanups (old_chain);
2320}
2321
186c406b
TT
2322/* Install a master breakpoint on the unwinder's debug hook. */
2323
2324void
2325create_exception_master_breakpoint (void)
2326{
2327 struct objfile *objfile;
17450429 2328 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2329
2330 ALL_OBJFILES (objfile)
2331 {
17450429
PP
2332 struct breakpoint *b;
2333 struct gdbarch *gdbarch;
2334 struct breakpoint_objfile_data *bp_objfile_data;
2335 CORE_ADDR addr;
2336
2337 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2338
2339 if (msym_not_found_p (bp_objfile_data->exception_msym))
2340 continue;
2341
2342 gdbarch = get_objfile_arch (objfile);
186c406b 2343
17450429 2344 if (bp_objfile_data->exception_msym == NULL)
186c406b 2345 {
17450429 2346 struct minimal_symbol *debug_hook;
186c406b 2347
17450429
PP
2348 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2349 if (debug_hook == NULL)
2350 {
2351 bp_objfile_data->exception_msym = &msym_not_found;
2352 continue;
2353 }
2354
2355 bp_objfile_data->exception_msym = debug_hook;
186c406b 2356 }
17450429
PP
2357
2358 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2359 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2360 &current_target);
06edf0c0
PA
2361 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2362 &internal_breakpoint_ops);
17450429
PP
2363 b->addr_string = xstrdup (func_name);
2364 b->enable_state = bp_disabled;
186c406b
TT
2365 }
2366
2367 update_global_location_list (1);
2368}
2369
c906108c 2370void
fba45db2 2371update_breakpoints_after_exec (void)
c906108c 2372{
35df4500 2373 struct breakpoint *b, *b_tmp;
876fa593 2374 struct bp_location *bploc, **bplocp_tmp;
c906108c 2375
25b22b0a
PA
2376 /* We're about to delete breakpoints from GDB's lists. If the
2377 INSERTED flag is true, GDB will try to lift the breakpoints by
2378 writing the breakpoints' "shadow contents" back into memory. The
2379 "shadow contents" are NOT valid after an exec, so GDB should not
2380 do that. Instead, the target is responsible from marking
2381 breakpoints out as soon as it detects an exec. We don't do that
2382 here instead, because there may be other attempts to delete
2383 breakpoints after detecting an exec and before reaching here. */
876fa593 2384 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2385 if (bploc->pspace == current_program_space)
2386 gdb_assert (!bploc->inserted);
c906108c 2387
35df4500 2388 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2389 {
6c95b8df
PA
2390 if (b->pspace != current_program_space)
2391 continue;
2392
4a64f543 2393 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2394 if (b->type == bp_shlib_event)
2395 {
2396 delete_breakpoint (b);
2397 continue;
2398 }
c906108c 2399
4a64f543 2400 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2401 if (b->type == bp_jit_event)
2402 {
2403 delete_breakpoint (b);
2404 continue;
2405 }
2406
1900040c 2407 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2408 as must overlay event and longjmp master breakpoints. */
2409 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2410 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2411 || b->type == bp_exception_master)
c4093a6a
JM
2412 {
2413 delete_breakpoint (b);
2414 continue;
2415 }
2416
4a64f543 2417 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2418 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2419 {
2420 delete_breakpoint (b);
2421 continue;
2422 }
2423
611c83ae
PA
2424 /* Longjmp and longjmp-resume breakpoints are also meaningless
2425 after an exec. */
186c406b
TT
2426 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2427 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2428 {
2429 delete_breakpoint (b);
2430 continue;
2431 }
2432
ce78b96d
JB
2433 if (b->type == bp_catchpoint)
2434 {
2435 /* For now, none of the bp_catchpoint breakpoints need to
2436 do anything at this point. In the future, if some of
2437 the catchpoints need to something, we will need to add
2438 a new method, and call this method from here. */
2439 continue;
2440 }
2441
c5aa993b
JM
2442 /* bp_finish is a special case. The only way we ought to be able
2443 to see one of these when an exec() has happened, is if the user
2444 caught a vfork, and then said "finish". Ordinarily a finish just
2445 carries them to the call-site of the current callee, by setting
2446 a temporary bp there and resuming. But in this case, the finish
2447 will carry them entirely through the vfork & exec.
2448
2449 We don't want to allow a bp_finish to remain inserted now. But
2450 we can't safely delete it, 'cause finish_command has a handle to
2451 the bp on a bpstat, and will later want to delete it. There's a
2452 chance (and I've seen it happen) that if we delete the bp_finish
2453 here, that its storage will get reused by the time finish_command
2454 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2455 We really must allow finish_command to delete a bp_finish.
2456
e5dd4106 2457 In the absence of a general solution for the "how do we know
53a5351d
JM
2458 it's safe to delete something others may have handles to?"
2459 problem, what we'll do here is just uninsert the bp_finish, and
2460 let finish_command delete it.
2461
2462 (We know the bp_finish is "doomed" in the sense that it's
2463 momentary, and will be deleted as soon as finish_command sees
2464 the inferior stopped. So it doesn't matter that the bp's
2465 address is probably bogus in the new a.out, unlike e.g., the
2466 solib breakpoints.) */
c5aa993b 2467
c5aa993b
JM
2468 if (b->type == bp_finish)
2469 {
2470 continue;
2471 }
2472
2473 /* Without a symbolic address, we have little hope of the
2474 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2475 a.out. */
c5aa993b
JM
2476 if (b->addr_string == NULL)
2477 {
2478 delete_breakpoint (b);
2479 continue;
2480 }
c5aa993b 2481 }
1900040c 2482 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2483 create_overlay_event_breakpoint ();
2484 create_longjmp_master_breakpoint ();
2485 create_std_terminate_master_breakpoint ();
186c406b 2486 create_exception_master_breakpoint ();
c906108c
SS
2487}
2488
2489int
fba45db2 2490detach_breakpoints (int pid)
c906108c 2491{
35df4500 2492 struct bp_location *bl, **blp_tmp;
3a1bae8e 2493 int val = 0;
ce696e05 2494 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2495 struct inferior *inf = current_inferior ();
c5aa993b 2496
39f77062 2497 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2498 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2499
6c95b8df 2500 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2501 inferior_ptid = pid_to_ptid (pid);
35df4500 2502 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2503 {
35df4500 2504 if (bl->pspace != inf->pspace)
6c95b8df
PA
2505 continue;
2506
35df4500
TJB
2507 if (bl->inserted)
2508 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2509 }
d03285ec
UW
2510
2511 /* Detach single-step breakpoints as well. */
2512 detach_single_step_breakpoints ();
2513
ce696e05 2514 do_cleanups (old_chain);
3a1bae8e 2515 return val;
c906108c
SS
2516}
2517
35df4500 2518/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2519 Note that this is used to detach breakpoints from a child fork.
2520 When we get here, the child isn't in the inferior list, and neither
2521 do we have objects to represent its address space --- we should
35df4500 2522 *not* look at bl->pspace->aspace here. */
6c95b8df 2523
c906108c 2524static int
35df4500 2525remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2526{
2527 int val;
c5aa993b 2528
35df4500
TJB
2529 /* BL is never in moribund_locations by our callers. */
2530 gdb_assert (bl->owner != NULL);
2bdf28a0 2531
35df4500 2532 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2533 /* Permanent breakpoints cannot be inserted or removed. */
2534 return 0;
2535
74960c60
VP
2536 /* The type of none suggests that owner is actually deleted.
2537 This should not ever happen. */
35df4500 2538 gdb_assert (bl->owner->type != bp_none);
0bde7532 2539
35df4500
TJB
2540 if (bl->loc_type == bp_loc_software_breakpoint
2541 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2542 {
c02f5703
MS
2543 /* "Normal" instruction breakpoint: either the standard
2544 trap-instruction bp (bp_breakpoint), or a
2545 bp_hardware_breakpoint. */
2546
2547 /* First check to see if we have to handle an overlay. */
2548 if (overlay_debugging == ovly_off
35df4500
TJB
2549 || bl->section == NULL
2550 || !(section_is_overlay (bl->section)))
c02f5703
MS
2551 {
2552 /* No overlay handling: just remove the breakpoint. */
348d480f 2553 val = bl->owner->ops->remove_location (bl);
c02f5703 2554 }
c906108c
SS
2555 else
2556 {
4a64f543 2557 /* This breakpoint is in an overlay section.
c02f5703
MS
2558 Did we set a breakpoint at the LMA? */
2559 if (!overlay_events_enabled)
2560 {
2561 /* Yes -- overlay event support is not active, so we
2562 should have set a breakpoint at the LMA. Remove it.
2563 */
c02f5703
MS
2564 /* Ignore any failures: if the LMA is in ROM, we will
2565 have already warned when we failed to insert it. */
35df4500
TJB
2566 if (bl->loc_type == bp_loc_hardware_breakpoint)
2567 target_remove_hw_breakpoint (bl->gdbarch,
2568 &bl->overlay_target_info);
c02f5703 2569 else
35df4500
TJB
2570 target_remove_breakpoint (bl->gdbarch,
2571 &bl->overlay_target_info);
c02f5703
MS
2572 }
2573 /* Did we set a breakpoint at the VMA?
2574 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2575 if (bl->inserted)
c906108c 2576 {
c02f5703
MS
2577 /* Yes -- remove it. Previously we did not bother to
2578 remove the breakpoint if the section had been
2579 unmapped, but let's not rely on that being safe. We
2580 don't know what the overlay manager might do. */
aa67235e
UW
2581
2582 /* However, we should remove *software* breakpoints only
2583 if the section is still mapped, or else we overwrite
2584 wrong code with the saved shadow contents. */
348d480f
PA
2585 if (bl->loc_type == bp_loc_hardware_breakpoint
2586 || section_is_mapped (bl->section))
2587 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
2588 else
2589 val = 0;
c906108c 2590 }
c02f5703
MS
2591 else
2592 {
2593 /* No -- not inserted, so no need to remove. No error. */
2594 val = 0;
2595 }
c906108c 2596 }
879d1e6b
UW
2597
2598 /* In some cases, we might not be able to remove a breakpoint
2599 in a shared library that has already been removed, but we
2600 have not yet processed the shlib unload event. */
35df4500 2601 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2602 val = 0;
2603
c906108c
SS
2604 if (val)
2605 return val;
35df4500 2606 bl->inserted = (is == mark_inserted);
c906108c 2607 }
35df4500 2608 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2609 {
77b06cd7
TJB
2610 gdb_assert (bl->owner->ops != NULL
2611 && bl->owner->ops->remove_location != NULL);
2612
35df4500 2613 bl->inserted = (is == mark_inserted);
77b06cd7 2614 bl->owner->ops->remove_location (bl);
2e70b7b9 2615
c906108c 2616 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2617 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2618 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2619 bl->owner->number);
c906108c 2620 }
35df4500
TJB
2621 else if (bl->owner->type == bp_catchpoint
2622 && breakpoint_enabled (bl->owner)
2623 && !bl->duplicate)
ce78b96d 2624 {
77b06cd7
TJB
2625 gdb_assert (bl->owner->ops != NULL
2626 && bl->owner->ops->remove_location != NULL);
ce78b96d 2627
77b06cd7 2628 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2629 if (val)
2630 return val;
77b06cd7 2631
35df4500 2632 bl->inserted = (is == mark_inserted);
ce78b96d 2633 }
c906108c
SS
2634
2635 return 0;
2636}
2637
6c95b8df 2638static int
35df4500 2639remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2640{
2641 int ret;
2642 struct cleanup *old_chain;
2643
35df4500
TJB
2644 /* BL is never in moribund_locations by our callers. */
2645 gdb_assert (bl->owner != NULL);
2bdf28a0 2646
35df4500 2647 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2648 /* Permanent breakpoints cannot be inserted or removed. */
2649 return 0;
2650
2651 /* The type of none suggests that owner is actually deleted.
2652 This should not ever happen. */
35df4500 2653 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2654
2655 old_chain = save_current_space_and_thread ();
2656
35df4500 2657 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2658
35df4500 2659 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2660
2661 do_cleanups (old_chain);
2662 return ret;
2663}
2664
c906108c
SS
2665/* Clear the "inserted" flag in all breakpoints. */
2666
25b22b0a 2667void
fba45db2 2668mark_breakpoints_out (void)
c906108c 2669{
35df4500 2670 struct bp_location *bl, **blp_tmp;
c906108c 2671
35df4500
TJB
2672 ALL_BP_LOCATIONS (bl, blp_tmp)
2673 if (bl->pspace == current_program_space)
2674 bl->inserted = 0;
c906108c
SS
2675}
2676
53a5351d
JM
2677/* Clear the "inserted" flag in all breakpoints and delete any
2678 breakpoints which should go away between runs of the program.
c906108c
SS
2679
2680 Plus other such housekeeping that has to be done for breakpoints
2681 between runs.
2682
53a5351d
JM
2683 Note: this function gets called at the end of a run (by
2684 generic_mourn_inferior) and when a run begins (by
4a64f543 2685 init_wait_for_inferior). */
c906108c
SS
2686
2687
2688
2689void
fba45db2 2690breakpoint_init_inferior (enum inf_context context)
c906108c 2691{
35df4500
TJB
2692 struct breakpoint *b, *b_tmp;
2693 struct bp_location *bl, **blp_tmp;
1c5cfe86 2694 int ix;
6c95b8df 2695 struct program_space *pspace = current_program_space;
c906108c 2696
50c71eaf
PA
2697 /* If breakpoint locations are shared across processes, then there's
2698 nothing to do. */
2567c7d9 2699 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2700 return;
2701
35df4500 2702 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2703 {
35df4500
TJB
2704 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2705 if (bl->pspace == pspace
2706 && bl->owner->enable_state != bp_permanent)
2707 bl->inserted = 0;
6c95b8df 2708 }
075f6582 2709
35df4500 2710 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2711 {
6c95b8df
PA
2712 if (b->loc && b->loc->pspace != pspace)
2713 continue;
2714
c5aa993b
JM
2715 switch (b->type)
2716 {
2717 case bp_call_dummy:
c906108c 2718
c5aa993b 2719 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2720 cause problems when the inferior is rerun, so we better get
2721 rid of it. */
2722
2723 case bp_watchpoint_scope:
2724
2725 /* Also get rid of scope breakpoints. */
2726
2727 case bp_shlib_event:
2728
2729 /* Also remove solib event breakpoints. Their addresses may
2730 have changed since the last time we ran the program.
2731 Actually we may now be debugging against different target;
2732 and so the solib backend that installed this breakpoint may
2733 not be used in by the target. E.g.,
2734
2735 (gdb) file prog-linux
2736 (gdb) run # native linux target
2737 ...
2738 (gdb) kill
2739 (gdb) file prog-win.exe
2740 (gdb) tar rem :9999 # remote Windows gdbserver.
2741 */
c906108c 2742
c5aa993b
JM
2743 delete_breakpoint (b);
2744 break;
c906108c 2745
c5aa993b
JM
2746 case bp_watchpoint:
2747 case bp_hardware_watchpoint:
2748 case bp_read_watchpoint:
2749 case bp_access_watchpoint:
c906108c 2750
c5aa993b
JM
2751 /* Likewise for watchpoints on local expressions. */
2752 if (b->exp_valid_block != NULL)
2753 delete_breakpoint (b);
967af18d 2754 else if (context == inf_starting)
c860120c 2755 {
4a64f543
MS
2756 /* Reset val field to force reread of starting value in
2757 insert_breakpoints. */
c860120c
PM
2758 if (b->val)
2759 value_free (b->val);
2760 b->val = NULL;
fa4727a6 2761 b->val_valid = 0;
c860120c 2762 }
c5aa993b
JM
2763 break;
2764 default:
c5aa993b
JM
2765 break;
2766 }
2767 }
1c5cfe86
PA
2768
2769 /* Get rid of the moribund locations. */
35df4500
TJB
2770 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2771 decref_bp_location (&bl);
1c5cfe86 2772 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2773}
2774
6c95b8df
PA
2775/* These functions concern about actual breakpoints inserted in the
2776 target --- to e.g. check if we need to do decr_pc adjustment or if
2777 we need to hop over the bkpt --- so we check for address space
2778 match, not program space. */
2779
c2c6d25f
JM
2780/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2781 exists at PC. It returns ordinary_breakpoint_here if it's an
2782 ordinary breakpoint, or permanent_breakpoint_here if it's a
2783 permanent breakpoint.
2784 - When continuing from a location with an ordinary breakpoint, we
2785 actually single step once before calling insert_breakpoints.
e5dd4106 2786 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
2787 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2788 the target, to advance the PC past the breakpoint. */
c906108c 2789
c2c6d25f 2790enum breakpoint_here
6c95b8df 2791breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2792{
35df4500 2793 struct bp_location *bl, **blp_tmp;
c2c6d25f 2794 int any_breakpoint_here = 0;
c906108c 2795
35df4500 2796 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2797 {
35df4500
TJB
2798 if (bl->loc_type != bp_loc_software_breakpoint
2799 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2800 continue;
2801
f1310107 2802 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2803 if ((breakpoint_enabled (bl->owner)
2804 || bl->owner->enable_state == bp_permanent)
f1310107 2805 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2806 {
2807 if (overlay_debugging
35df4500
TJB
2808 && section_is_overlay (bl->section)
2809 && !section_is_mapped (bl->section))
075f6582 2810 continue; /* unmapped overlay -- can't be a match */
35df4500 2811 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2812 return permanent_breakpoint_here;
2813 else
2814 any_breakpoint_here = 1;
2815 }
2816 }
c906108c 2817
c2c6d25f 2818 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2819}
2820
1c5cfe86
PA
2821/* Return true if there's a moribund breakpoint at PC. */
2822
2823int
6c95b8df 2824moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2825{
2826 struct bp_location *loc;
2827 int ix;
2828
2829 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2830 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2831 return 1;
2832
2833 return 0;
2834}
c2c6d25f 2835
c36b740a 2836/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2837 inserted using regular breakpoint_chain / bp_location array
2838 mechanism. This does not check for single-step breakpoints, which
2839 are inserted and removed using direct target manipulation. */
c906108c
SS
2840
2841int
4a64f543
MS
2842regular_breakpoint_inserted_here_p (struct address_space *aspace,
2843 CORE_ADDR pc)
c906108c 2844{
35df4500 2845 struct bp_location *bl, **blp_tmp;
c906108c 2846
35df4500 2847 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2848 {
35df4500
TJB
2849 if (bl->loc_type != bp_loc_software_breakpoint
2850 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2851 continue;
2852
35df4500 2853 if (bl->inserted
f1310107 2854 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2855 {
2856 if (overlay_debugging
35df4500
TJB
2857 && section_is_overlay (bl->section)
2858 && !section_is_mapped (bl->section))
075f6582
DJ
2859 continue; /* unmapped overlay -- can't be a match */
2860 else
2861 return 1;
2862 }
c5aa993b 2863 }
c36b740a
VP
2864 return 0;
2865}
2866
2867/* Returns non-zero iff there's either regular breakpoint
2868 or a single step breakpoint inserted at PC. */
2869
2870int
6c95b8df 2871breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2872{
6c95b8df 2873 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2874 return 1;
c906108c 2875
6c95b8df 2876 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2877 return 1;
2878
c906108c
SS
2879 return 0;
2880}
2881
4fa8626c
DJ
2882/* This function returns non-zero iff there is a software breakpoint
2883 inserted at PC. */
2884
2885int
3e43a32a
MS
2886software_breakpoint_inserted_here_p (struct address_space *aspace,
2887 CORE_ADDR pc)
4fa8626c 2888{
35df4500 2889 struct bp_location *bl, **blp_tmp;
4fa8626c 2890
35df4500 2891 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2892 {
35df4500 2893 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2894 continue;
2895
35df4500
TJB
2896 if (bl->inserted
2897 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2898 aspace, pc))
4fa8626c
DJ
2899 {
2900 if (overlay_debugging
35df4500
TJB
2901 && section_is_overlay (bl->section)
2902 && !section_is_mapped (bl->section))
4fa8626c
DJ
2903 continue; /* unmapped overlay -- can't be a match */
2904 else
2905 return 1;
2906 }
2907 }
2908
1aafd4da 2909 /* Also check for software single-step breakpoints. */
6c95b8df 2910 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2911 return 1;
2912
4fa8626c
DJ
2913 return 0;
2914}
2915
9093389c
PA
2916int
2917hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2918 CORE_ADDR addr, ULONGEST len)
2919{
2920 struct breakpoint *bpt;
2921
2922 ALL_BREAKPOINTS (bpt)
2923 {
2924 struct bp_location *loc;
2925
2926 if (bpt->type != bp_hardware_watchpoint
2927 && bpt->type != bp_access_watchpoint)
2928 continue;
2929
2930 if (!breakpoint_enabled (bpt))
2931 continue;
2932
2933 for (loc = bpt->loc; loc; loc = loc->next)
2934 if (loc->pspace->aspace == aspace && loc->inserted)
2935 {
2936 CORE_ADDR l, h;
2937
2938 /* Check for intersection. */
2939 l = max (loc->address, addr);
2940 h = min (loc->address + loc->length, addr + len);
2941 if (l < h)
2942 return 1;
2943 }
2944 }
2945 return 0;
2946}
2947
075f6582
DJ
2948/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2949 PC is valid for process/thread PTID. */
c906108c
SS
2950
2951int
6c95b8df
PA
2952breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2953 ptid_t ptid)
c906108c 2954{
35df4500 2955 struct bp_location *bl, **blp_tmp;
4a306c9a 2956 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 2957 int thread = -1;
4a306c9a 2958 int task = 0;
a6f1cd96 2959
35df4500 2960 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2961 {
35df4500
TJB
2962 if (bl->loc_type != bp_loc_software_breakpoint
2963 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2964 continue;
2965
35df4500
TJB
2966 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2967 if (!breakpoint_enabled (bl->owner)
2968 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
2969 continue;
2970
f1310107 2971 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
2972 continue;
2973
35df4500 2974 if (bl->owner->thread != -1)
075f6582 2975 {
a6f1cd96
JB
2976 /* This is a thread-specific breakpoint. Check that ptid
2977 matches that thread. If thread hasn't been computed yet,
2978 it is now time to do so. */
2979 if (thread == -1)
2980 thread = pid_to_thread_id (ptid);
35df4500 2981 if (bl->owner->thread != thread)
a6f1cd96 2982 continue;
075f6582 2983 }
a6f1cd96 2984
35df4500 2985 if (bl->owner->task != 0)
4a306c9a
JB
2986 {
2987 /* This is a task-specific breakpoint. Check that ptid
2988 matches that task. If task hasn't been computed yet,
2989 it is now time to do so. */
2990 if (task == 0)
2991 task = ada_get_task_number (ptid);
35df4500 2992 if (bl->owner->task != task)
4a306c9a
JB
2993 continue;
2994 }
2995
a6f1cd96 2996 if (overlay_debugging
35df4500
TJB
2997 && section_is_overlay (bl->section)
2998 && !section_is_mapped (bl->section))
a6f1cd96
JB
2999 continue; /* unmapped overlay -- can't be a match */
3000
3001 return 1;
c5aa993b 3002 }
c906108c
SS
3003
3004 return 0;
3005}
c906108c 3006\f
c5aa993b 3007
c906108c
SS
3008/* bpstat stuff. External routines' interfaces are documented
3009 in breakpoint.h. */
3010
3011int
fba45db2 3012ep_is_catchpoint (struct breakpoint *ep)
c906108c 3013{
533be4dd 3014 return (ep->type == bp_catchpoint);
c906108c
SS
3015}
3016
f431efe5
PA
3017/* Frees any storage that is part of a bpstat. Does not walk the
3018 'next' chain. */
3019
3020static void
198757a8
VP
3021bpstat_free (bpstat bs)
3022{
3023 if (bs->old_val != NULL)
3024 value_free (bs->old_val);
9add0f1b 3025 decref_counted_command_line (&bs->commands);
f431efe5 3026 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3027 xfree (bs);
3028}
3029
c906108c
SS
3030/* Clear a bpstat so that it says we are not at any breakpoint.
3031 Also free any storage that is part of a bpstat. */
3032
3033void
fba45db2 3034bpstat_clear (bpstat *bsp)
c906108c
SS
3035{
3036 bpstat p;
3037 bpstat q;
3038
3039 if (bsp == 0)
3040 return;
3041 p = *bsp;
3042 while (p != NULL)
3043 {
3044 q = p->next;
198757a8 3045 bpstat_free (p);
c906108c
SS
3046 p = q;
3047 }
3048 *bsp = NULL;
3049}
3050
3051/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3052 is part of the bpstat is copied as well. */
3053
3054bpstat
fba45db2 3055bpstat_copy (bpstat bs)
c906108c
SS
3056{
3057 bpstat p = NULL;
3058 bpstat tmp;
3059 bpstat retval = NULL;
3060
3061 if (bs == NULL)
3062 return bs;
3063
3064 for (; bs != NULL; bs = bs->next)
3065 {
3066 tmp = (bpstat) xmalloc (sizeof (*tmp));
3067 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3068 incref_counted_command_line (tmp->commands);
f431efe5 3069 incref_bp_location (tmp->bp_location_at);
31cc81e9 3070 if (bs->old_val != NULL)
3c3185ac
JK
3071 {
3072 tmp->old_val = value_copy (bs->old_val);
3073 release_value (tmp->old_val);
3074 }
31cc81e9 3075
c906108c
SS
3076 if (p == NULL)
3077 /* This is the first thing in the chain. */
3078 retval = tmp;
3079 else
3080 p->next = tmp;
3081 p = tmp;
3082 }
3083 p->next = NULL;
3084 return retval;
3085}
3086
4a64f543 3087/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3088
3089bpstat
fba45db2 3090bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3091{
c5aa993b
JM
3092 if (bsp == NULL)
3093 return NULL;
c906108c 3094
c5aa993b
JM
3095 for (; bsp != NULL; bsp = bsp->next)
3096 {
f431efe5 3097 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3098 return bsp;
3099 }
c906108c
SS
3100 return NULL;
3101}
3102
4a64f543
MS
3103/* Put in *NUM the breakpoint number of the first breakpoint we are
3104 stopped at. *BSP upon return is a bpstat which points to the
3105 remaining breakpoints stopped at (but which is not guaranteed to be
3106 good for anything but further calls to bpstat_num).
3107
8671a17b
PA
3108 Return 0 if passed a bpstat which does not indicate any breakpoints.
3109 Return -1 if stopped at a breakpoint that has been deleted since
3110 we set it.
3111 Return 1 otherwise. */
c906108c
SS
3112
3113int
8671a17b 3114bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3115{
3116 struct breakpoint *b;
3117
3118 if ((*bsp) == NULL)
3119 return 0; /* No more breakpoint values */
8671a17b 3120
4a64f543
MS
3121 /* We assume we'll never have several bpstats that correspond to a
3122 single breakpoint -- otherwise, this function might return the
3123 same number more than once and this will look ugly. */
f431efe5 3124 b = (*bsp)->breakpoint_at;
8671a17b
PA
3125 *bsp = (*bsp)->next;
3126 if (b == NULL)
3127 return -1; /* breakpoint that's been deleted since */
3128
3129 *num = b->number; /* We have its number */
3130 return 1;
c906108c
SS
3131}
3132
3133/* Modify BS so that the actions will not be performed. */
3134
3135void
fba45db2 3136bpstat_clear_actions (bpstat bs)
c906108c
SS
3137{
3138 for (; bs != NULL; bs = bs->next)
3139 {
9add0f1b 3140 decref_counted_command_line (&bs->commands);
dde2d684 3141 bs->commands_left = NULL;
c906108c
SS
3142 if (bs->old_val != NULL)
3143 {
3144 value_free (bs->old_val);
3145 bs->old_val = NULL;
3146 }
3147 }
3148}
3149
f3b1572e
PA
3150/* Called when a command is about to proceed the inferior. */
3151
3152static void
3153breakpoint_about_to_proceed (void)
3154{
3155 if (!ptid_equal (inferior_ptid, null_ptid))
3156 {
3157 struct thread_info *tp = inferior_thread ();
3158
3159 /* Allow inferior function calls in breakpoint commands to not
3160 interrupt the command list. When the call finishes
3161 successfully, the inferior will be standing at the same
3162 breakpoint as if nothing happened. */
16c381f0 3163 if (tp->control.in_infcall)
f3b1572e
PA
3164 return;
3165 }
3166
3167 breakpoint_proceeded = 1;
3168}
3169
4a64f543
MS
3170/* Stub for cleaning up our state if we error-out of a breakpoint
3171 command. */
c906108c 3172static void
4efb68b1 3173cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3174{
3175 executing_breakpoint_commands = 0;
3176}
3177
4a64f543
MS
3178/* Execute all the commands associated with all the breakpoints at
3179 this location. Any of these commands could cause the process to
3180 proceed beyond this point, etc. We look out for such changes by
3181 checking the global "breakpoint_proceeded" after each command.
c906108c 3182
347bddb7
PA
3183 Returns true if a breakpoint command resumed the inferior. In that
3184 case, it is the caller's responsibility to recall it again with the
3185 bpstat of the current thread. */
3186
3187static int
3188bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3189{
3190 bpstat bs;
3191 struct cleanup *old_chain;
347bddb7 3192 int again = 0;
c906108c
SS
3193
3194 /* Avoid endless recursion if a `source' command is contained
3195 in bs->commands. */
3196 if (executing_breakpoint_commands)
347bddb7 3197 return 0;
c906108c
SS
3198
3199 executing_breakpoint_commands = 1;
3200 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3201
cf6c5ffb
TT
3202 prevent_dont_repeat ();
3203
4a64f543 3204 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3205 bs = *bsp;
3206
3207 breakpoint_proceeded = 0;
3208 for (; bs != NULL; bs = bs->next)
3209 {
9add0f1b 3210 struct counted_command_line *ccmd;
6c50ab1c
JB
3211 struct command_line *cmd;
3212 struct cleanup *this_cmd_tree_chain;
3213
3214 /* Take ownership of the BSP's command tree, if it has one.
3215
3216 The command tree could legitimately contain commands like
3217 'step' and 'next', which call clear_proceed_status, which
3218 frees stop_bpstat's command tree. To make sure this doesn't
3219 free the tree we're executing out from under us, we need to
3220 take ownership of the tree ourselves. Since a given bpstat's
3221 commands are only executed once, we don't need to copy it; we
3222 can clear the pointer in the bpstat, and make sure we free
3223 the tree when we're done. */
9add0f1b
TT
3224 ccmd = bs->commands;
3225 bs->commands = NULL;
3226 this_cmd_tree_chain
3227 = make_cleanup_decref_counted_command_line (&ccmd);
3228 cmd = bs->commands_left;
3229 bs->commands_left = NULL;
6c50ab1c 3230
c906108c
SS
3231 while (cmd != NULL)
3232 {
3233 execute_control_command (cmd);
3234
3235 if (breakpoint_proceeded)
3236 break;
3237 else
3238 cmd = cmd->next;
3239 }
6c50ab1c
JB
3240
3241 /* We can free this command tree now. */
3242 do_cleanups (this_cmd_tree_chain);
3243
c906108c 3244 if (breakpoint_proceeded)
32c1e744
VP
3245 {
3246 if (target_can_async_p ())
347bddb7
PA
3247 /* If we are in async mode, then the target might be still
3248 running, not stopped at any breakpoint, so nothing for
3249 us to do here -- just return to the event loop. */
3250 ;
32c1e744
VP
3251 else
3252 /* In sync mode, when execute_control_command returns
3253 we're already standing on the next breakpoint.
347bddb7
PA
3254 Breakpoint commands for that stop were not run, since
3255 execute_command does not run breakpoint commands --
3256 only command_line_handler does, but that one is not
3257 involved in execution of breakpoint commands. So, we
3258 can now execute breakpoint commands. It should be
3259 noted that making execute_command do bpstat actions is
3260 not an option -- in this case we'll have recursive
3261 invocation of bpstat for each breakpoint with a
3262 command, and can easily blow up GDB stack. Instead, we
3263 return true, which will trigger the caller to recall us
3264 with the new stop_bpstat. */
3265 again = 1;
3266 break;
32c1e744 3267 }
c906108c 3268 }
c2b8ed2c 3269 do_cleanups (old_chain);
347bddb7
PA
3270 return again;
3271}
3272
3273void
3274bpstat_do_actions (void)
3275{
3276 /* Do any commands attached to breakpoint we are stopped at. */
3277 while (!ptid_equal (inferior_ptid, null_ptid)
3278 && target_has_execution
3279 && !is_exited (inferior_ptid)
3280 && !is_executing (inferior_ptid))
3281 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3282 and only return when it is stopped at the next breakpoint, we
3283 keep doing breakpoint actions until it returns false to
3284 indicate the inferior was not resumed. */
16c381f0 3285 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3286 break;
c906108c
SS
3287}
3288
fa4727a6
DJ
3289/* Print out the (old or new) value associated with a watchpoint. */
3290
3291static void
3292watchpoint_value_print (struct value *val, struct ui_file *stream)
3293{
3294 if (val == NULL)
3295 fprintf_unfiltered (stream, _("<unreadable>"));
3296 else
79a45b7d
TT
3297 {
3298 struct value_print_options opts;
3299 get_user_print_options (&opts);
3300 value_print (val, stream, &opts);
3301 }
fa4727a6
DJ
3302}
3303
e514a9d6 3304/* Generic routine for printing messages indicating why we
4a64f543 3305 stopped. The behavior of this function depends on the value
e514a9d6
JM
3306 'print_it' in the bpstat structure. Under some circumstances we
3307 may decide not to print anything here and delegate the task to
4a64f543 3308 normal_stop(). */
e514a9d6
JM
3309
3310static enum print_stop_action
3311print_bp_stop_message (bpstat bs)
3312{
3313 switch (bs->print_it)
3314 {
3315 case print_it_noop:
4a64f543 3316 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3317 return PRINT_UNKNOWN;
3318 break;
3319
3320 case print_it_done:
3321 /* We still want to print the frame, but we already printed the
4a64f543 3322 relevant messages. */
e514a9d6
JM
3323 return PRINT_SRC_AND_LOC;
3324 break;
3325
3326 case print_it_normal:
4f8d1dc6 3327 {
f431efe5
PA
3328 struct breakpoint *b = bs->breakpoint_at;
3329
1a6a67de
TJB
3330 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3331 which has since been deleted. */
3332 if (b == NULL)
3333 return PRINT_UNKNOWN;
3334
348d480f
PA
3335 /* Normal case. Call the breakpoint's print_it method. */
3336 return b->ops->print_it (bs);
4f8d1dc6 3337 }
348d480f 3338 break;
3086aeae 3339
e514a9d6 3340 default:
8e65ff28 3341 internal_error (__FILE__, __LINE__,
e2e0b3e5 3342 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3343 break;
c906108c 3344 }
c906108c
SS
3345}
3346
e514a9d6
JM
3347/* Print a message indicating what happened. This is called from
3348 normal_stop(). The input to this routine is the head of the bpstat
3349 list - a list of the eventpoints that caused this stop. This
3350 routine calls the generic print routine for printing a message
3351 about reasons for stopping. This will print (for example) the
3352 "Breakpoint n," part of the output. The return value of this
3353 routine is one of:
c906108c 3354
4a64f543 3355 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3356 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3357 code to print the location. An example is
c5aa993b
JM
3358 "Breakpoint 1, " which should be followed by
3359 the location.
917317f4 3360 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3361 to also print the location part of the message.
3362 An example is the catch/throw messages, which
4a64f543 3363 don't require a location appended to the end.
917317f4 3364 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3365 further info to be printed. */
c906108c 3366
917317f4 3367enum print_stop_action
fba45db2 3368bpstat_print (bpstat bs)
c906108c
SS
3369{
3370 int val;
c5aa993b 3371
c906108c 3372 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3373 (Currently all watchpoints go on the bpstat whether hit or not.
3374 That probably could (should) be changed, provided care is taken
c906108c 3375 with respect to bpstat_explains_signal). */
e514a9d6
JM
3376 for (; bs; bs = bs->next)
3377 {
3378 val = print_bp_stop_message (bs);
3379 if (val == PRINT_SRC_ONLY
3380 || val == PRINT_SRC_AND_LOC
3381 || val == PRINT_NOTHING)
3382 return val;
3383 }
c906108c 3384
e514a9d6 3385 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3386 with and nothing was printed. */
917317f4 3387 return PRINT_UNKNOWN;
c906108c
SS
3388}
3389
4a64f543
MS
3390/* Evaluate the expression EXP and return 1 if value is zero. This is
3391 used inside a catch_errors to evaluate the breakpoint condition.
3392 The argument is a "struct expression *" that has been cast to a
3393 "char *" to make it pass through catch_errors. */
c906108c
SS
3394
3395static int
4efb68b1 3396breakpoint_cond_eval (void *exp)
c906108c 3397{
278cd55f 3398 struct value *mark = value_mark ();
c5aa993b 3399 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3400
c906108c
SS
3401 value_free_to_mark (mark);
3402 return i;
3403}
3404
5760d0ab 3405/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3406
3407static bpstat
5760d0ab 3408bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3409{
3410 bpstat bs;
3411
3412 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3413 bs->next = NULL;
3414 **bs_link_pointer = bs;
3415 *bs_link_pointer = &bs->next;
f431efe5
PA
3416 bs->breakpoint_at = bl->owner;
3417 bs->bp_location_at = bl;
3418 incref_bp_location (bl);
c906108c
SS
3419 /* If the condition is false, etc., don't do the commands. */
3420 bs->commands = NULL;
9add0f1b 3421 bs->commands_left = NULL;
c906108c
SS
3422 bs->old_val = NULL;
3423 bs->print_it = print_it_normal;
3424 return bs;
3425}
3426\f
d983da9c
DJ
3427/* The target has stopped with waitstatus WS. Check if any hardware
3428 watchpoints have triggered, according to the target. */
3429
3430int
3431watchpoints_triggered (struct target_waitstatus *ws)
3432{
d92524f1 3433 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3434 CORE_ADDR addr;
3435 struct breakpoint *b;
3436
3437 if (!stopped_by_watchpoint)
3438 {
3439 /* We were not stopped by a watchpoint. Mark all watchpoints
3440 as not triggered. */
3441 ALL_BREAKPOINTS (b)
cc60f2e3 3442 if (is_hardware_watchpoint (b))
d983da9c
DJ
3443 b->watchpoint_triggered = watch_triggered_no;
3444
3445 return 0;
3446 }
3447
3448 if (!target_stopped_data_address (&current_target, &addr))
3449 {
3450 /* We were stopped by a watchpoint, but we don't know where.
3451 Mark all watchpoints as unknown. */
3452 ALL_BREAKPOINTS (b)
cc60f2e3 3453 if (is_hardware_watchpoint (b))
d983da9c
DJ
3454 b->watchpoint_triggered = watch_triggered_unknown;
3455
3456 return stopped_by_watchpoint;
3457 }
3458
3459 /* The target could report the data address. Mark watchpoints
3460 affected by this data address as triggered, and all others as not
3461 triggered. */
3462
3463 ALL_BREAKPOINTS (b)
cc60f2e3 3464 if (is_hardware_watchpoint (b))
d983da9c 3465 {
a5606eee 3466 struct bp_location *loc;
d983da9c
DJ
3467
3468 b->watchpoint_triggered = watch_triggered_no;
a5606eee 3469 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4
TJB
3470 {
3471 if (is_masked_watchpoint (loc->owner))
3472 {
3473 CORE_ADDR newaddr = addr & loc->owner->hw_wp_mask;
3474 CORE_ADDR start = loc->address & loc->owner->hw_wp_mask;
3475
3476 if (newaddr == start)
3477 {
3478 b->watchpoint_triggered = watch_triggered_yes;
3479 break;
3480 }
3481 }
3482 /* Exact match not required. Within range is sufficient. */
3483 else if (target_watchpoint_addr_within_range (&current_target,
3484 addr, loc->address,
3485 loc->length))
3486 {
3487 b->watchpoint_triggered = watch_triggered_yes;
3488 break;
3489 }
3490 }
d983da9c
DJ
3491 }
3492
3493 return 1;
3494}
3495
c906108c
SS
3496/* Possible return values for watchpoint_check (this can't be an enum
3497 because of check_errors). */
3498/* The watchpoint has been deleted. */
3499#define WP_DELETED 1
3500/* The value has changed. */
3501#define WP_VALUE_CHANGED 2
3502/* The value has not changed. */
3503#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3504/* Ignore this watchpoint, no matter if the value changed or not. */
3505#define WP_IGNORE 4
c906108c
SS
3506
3507#define BP_TEMPFLAG 1
3508#define BP_HARDWAREFLAG 2
3509
4a64f543
MS
3510/* Evaluate watchpoint condition expression and check if its value
3511 changed.
553e4c11
JB
3512
3513 P should be a pointer to struct bpstat, but is defined as a void *
3514 in order for this function to be usable with catch_errors. */
c906108c
SS
3515
3516static int
4efb68b1 3517watchpoint_check (void *p)
c906108c
SS
3518{
3519 bpstat bs = (bpstat) p;
3520 struct breakpoint *b;
3521 struct frame_info *fr;
3522 int within_current_scope;
3523
f431efe5 3524 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3525 gdb_assert (bs->breakpoint_at != NULL);
f431efe5 3526 b = bs->breakpoint_at;
c906108c 3527
d0fb5eae
JK
3528 gdb_assert (is_watchpoint (b));
3529
f6bc2008
PA
3530 /* If this is a local watchpoint, we only want to check if the
3531 watchpoint frame is in scope if the current thread is the thread
3532 that was used to create the watchpoint. */
3533 if (!watchpoint_in_thread_scope (b))
60e1c644 3534 return WP_IGNORE;
f6bc2008 3535
c906108c
SS
3536 if (b->exp_valid_block == NULL)
3537 within_current_scope = 1;
3538 else
3539 {
edb3359d
DJ
3540 struct frame_info *frame = get_current_frame ();
3541 struct gdbarch *frame_arch = get_frame_arch (frame);
3542 CORE_ADDR frame_pc = get_frame_pc (frame);
3543
4a64f543
MS
3544 /* in_function_epilogue_p() returns a non-zero value if we're
3545 still in the function but the stack frame has already been
3546 invalidated. Since we can't rely on the values of local
3547 variables after the stack has been destroyed, we are treating
3548 the watchpoint in that state as `not changed' without further
3549 checking. Don't mark watchpoints as changed if the current
3550 frame is in an epilogue - even if they are in some other
3551 frame, our view of the stack is likely to be wrong and
3552 frame_find_by_id could error out. */
a0f49112 3553 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3554 return WP_IGNORE;
a0f49112 3555
101dcfbe 3556 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3557 within_current_scope = (fr != NULL);
69fbadd5
DJ
3558
3559 /* If we've gotten confused in the unwinder, we might have
3560 returned a frame that can't describe this variable. */
edb3359d
DJ
3561 if (within_current_scope)
3562 {
3563 struct symbol *function;
3564
3565 function = get_frame_function (fr);
3566 if (function == NULL
3567 || !contained_in (b->exp_valid_block,
3568 SYMBOL_BLOCK_VALUE (function)))
3569 within_current_scope = 0;
3570 }
69fbadd5 3571
edb3359d 3572 if (within_current_scope)
c906108c
SS
3573 /* If we end up stopping, the current frame will get selected
3574 in normal_stop. So this call to select_frame won't affect
3575 the user. */
0f7d239c 3576 select_frame (fr);
c906108c 3577 }
c5aa993b 3578
c906108c
SS
3579 if (within_current_scope)
3580 {
4a64f543
MS
3581 /* We use value_{,free_to_}mark because it could be a *long*
3582 time before we return to the command level and call
3583 free_all_values. We can't call free_all_values because we
3584 might be in the middle of evaluating a function call. */
c906108c 3585
0cf6dd15 3586 int pc = 0;
9c06b0b4 3587 struct value *mark;
fa4727a6
DJ
3588 struct value *new_val;
3589
9c06b0b4
TJB
3590 if (is_masked_watchpoint (b))
3591 /* Since we don't know the exact trigger address (from
3592 stopped_data_address), just tell the user we've triggered
3593 a mask watchpoint. */
3594 return WP_VALUE_CHANGED;
3595
3596 mark = value_mark ();
0cf6dd15 3597 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3598
4a64f543
MS
3599 /* We use value_equal_contents instead of value_equal because
3600 the latter coerces an array to a pointer, thus comparing just
3601 the address of the array instead of its contents. This is
3602 not what we want. */
fa4727a6 3603 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3604 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3605 {
fa4727a6
DJ
3606 if (new_val != NULL)
3607 {
3608 release_value (new_val);
3609 value_free_to_mark (mark);
3610 }
c906108c
SS
3611 bs->old_val = b->val;
3612 b->val = new_val;
fa4727a6 3613 b->val_valid = 1;
c906108c
SS
3614 return WP_VALUE_CHANGED;
3615 }
3616 else
3617 {
60e1c644 3618 /* Nothing changed. */
c906108c 3619 value_free_to_mark (mark);
c906108c
SS
3620 return WP_VALUE_NOT_CHANGED;
3621 }
3622 }
3623 else
3624 {
3625 /* This seems like the only logical thing to do because
c5aa993b
JM
3626 if we temporarily ignored the watchpoint, then when
3627 we reenter the block in which it is valid it contains
3628 garbage (in the case of a function, it may have two
3629 garbage values, one before and one after the prologue).
3630 So we can't even detect the first assignment to it and
3631 watch after that (since the garbage may or may not equal
3632 the first value assigned). */
348d480f
PA
3633 /* We print all the stop information in
3634 breakpoint_ops->print_it, but in this case, by the time we
3635 call breakpoint_ops->print_it this bp will be deleted
3636 already. So we have no choice but print the information
3637 here. */
9dc5e2a9 3638 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3639 ui_out_field_string
3640 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3641 ui_out_text (uiout, "\nWatchpoint ");
4f8d1dc6 3642 ui_out_field_int (uiout, "wpnum", b->number);
3e43a32a
MS
3643 ui_out_text (uiout,
3644 " deleted because the program has left the block in\n\
8b93c638 3645which its expression is valid.\n");
4ce44c66 3646
cdac0397
PA
3647 /* Make sure the watchpoint's commands aren't executed. */
3648 decref_counted_command_line (&b->commands);
d0fb5eae 3649 watchpoint_del_at_next_stop (b);
c906108c
SS
3650
3651 return WP_DELETED;
3652 }
3653}
3654
18a18393 3655/* Return true if it looks like target has stopped due to hitting
348d480f
PA
3656 breakpoint location BL. This function does not check if we should
3657 stop, only if BL explains the stop. */
3658
18a18393 3659static int
6c95b8df
PA
3660bpstat_check_location (const struct bp_location *bl,
3661 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3662{
3663 struct breakpoint *b = bl->owner;
3664
348d480f 3665 /* BL is from an existing breakpoint. */
2bdf28a0
JK
3666 gdb_assert (b != NULL);
3667
348d480f 3668 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
18a18393
VP
3669}
3670
3671/* If BS refers to a watchpoint, determine if the watched values
3672 has actually changed, and we should stop. If not, set BS->stop
3673 to 0. */
3674static void
3675bpstat_check_watchpoint (bpstat bs)
3676{
2bdf28a0
JK
3677 const struct bp_location *bl;
3678 struct breakpoint *b;
3679
3680 /* BS is built for existing struct breakpoint. */
f431efe5 3681 bl = bs->bp_location_at;
2bdf28a0 3682 gdb_assert (bl != NULL);
f431efe5 3683 b = bs->breakpoint_at;
2bdf28a0 3684 gdb_assert (b != NULL);
18a18393 3685
cc60f2e3 3686 if (is_watchpoint (b))
18a18393 3687 {
18a18393
VP
3688 int must_check_value = 0;
3689
3690 if (b->type == bp_watchpoint)
3691 /* For a software watchpoint, we must always check the
3692 watched value. */
3693 must_check_value = 1;
3694 else if (b->watchpoint_triggered == watch_triggered_yes)
3695 /* We have a hardware watchpoint (read, write, or access)
3696 and the target earlier reported an address watched by
3697 this watchpoint. */
3698 must_check_value = 1;
3699 else if (b->watchpoint_triggered == watch_triggered_unknown
3700 && b->type == bp_hardware_watchpoint)
3701 /* We were stopped by a hardware watchpoint, but the target could
3702 not report the data address. We must check the watchpoint's
3703 value. Access and read watchpoints are out of luck; without
3704 a data address, we can't figure it out. */
3705 must_check_value = 1;
3706
3707 if (must_check_value)
3708 {
3e43a32a
MS
3709 char *message
3710 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3711 b->number);
18a18393
VP
3712 struct cleanup *cleanups = make_cleanup (xfree, message);
3713 int e = catch_errors (watchpoint_check, bs, message,
3714 RETURN_MASK_ALL);
3715 do_cleanups (cleanups);
3716 switch (e)
3717 {
3718 case WP_DELETED:
3719 /* We've already printed what needs to be printed. */
3720 bs->print_it = print_it_done;
3721 /* Stop. */
3722 break;
60e1c644
PA
3723 case WP_IGNORE:
3724 bs->print_it = print_it_noop;
3725 bs->stop = 0;
3726 break;
18a18393
VP
3727 case WP_VALUE_CHANGED:
3728 if (b->type == bp_read_watchpoint)
3729 {
85d721b8
PA
3730 /* There are two cases to consider here:
3731
4a64f543 3732 1. We're watching the triggered memory for reads.
85d721b8
PA
3733 In that case, trust the target, and always report
3734 the watchpoint hit to the user. Even though
3735 reads don't cause value changes, the value may
3736 have changed since the last time it was read, and
3737 since we're not trapping writes, we will not see
3738 those, and as such we should ignore our notion of
3739 old value.
3740
4a64f543 3741 2. We're watching the triggered memory for both
85d721b8
PA
3742 reads and writes. There are two ways this may
3743 happen:
3744
4a64f543 3745 2.1. This is a target that can't break on data
85d721b8
PA
3746 reads only, but can break on accesses (reads or
3747 writes), such as e.g., x86. We detect this case
3748 at the time we try to insert read watchpoints.
3749
4a64f543 3750 2.2. Otherwise, the target supports read
85d721b8
PA
3751 watchpoints, but, the user set an access or write
3752 watchpoint watching the same memory as this read
3753 watchpoint.
3754
3755 If we're watching memory writes as well as reads,
3756 ignore watchpoint hits when we find that the
3757 value hasn't changed, as reads don't cause
3758 changes. This still gives false positives when
3759 the program writes the same value to memory as
3760 what there was already in memory (we will confuse
3761 it for a read), but it's much better than
3762 nothing. */
3763
3764 int other_write_watchpoint = 0;
3765
3766 if (bl->watchpoint_type == hw_read)
3767 {
3768 struct breakpoint *other_b;
3769
3770 ALL_BREAKPOINTS (other_b)
3771 if ((other_b->type == bp_hardware_watchpoint
3772 || other_b->type == bp_access_watchpoint)
3773 && (other_b->watchpoint_triggered
3774 == watch_triggered_yes))
3775 {
3776 other_write_watchpoint = 1;
3777 break;
3778 }
3779 }
3780
3781 if (other_write_watchpoint
3782 || bl->watchpoint_type == hw_access)
3783 {
3784 /* We're watching the same memory for writes,
3785 and the value changed since the last time we
3786 updated it, so this trap must be for a write.
3787 Ignore it. */
3788 bs->print_it = print_it_noop;
3789 bs->stop = 0;
3790 }
18a18393
VP
3791 }
3792 break;
3793 case WP_VALUE_NOT_CHANGED:
3794 if (b->type == bp_hardware_watchpoint
3795 || b->type == bp_watchpoint)
3796 {
3797 /* Don't stop: write watchpoints shouldn't fire if
3798 the value hasn't changed. */
3799 bs->print_it = print_it_noop;
3800 bs->stop = 0;
3801 }
3802 /* Stop. */
3803 break;
3804 default:
3805 /* Can't happen. */
3806 case 0:
3807 /* Error from catch_errors. */
3808 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
d0fb5eae 3809 watchpoint_del_at_next_stop (b);
18a18393
VP
3810 /* We've already printed what needs to be printed. */
3811 bs->print_it = print_it_done;
3812 break;
3813 }
3814 }
3815 else /* must_check_value == 0 */
3816 {
3817 /* This is a case where some watchpoint(s) triggered, but
3818 not at the address of this watchpoint, or else no
3819 watchpoint triggered after all. So don't print
3820 anything for this watchpoint. */
3821 bs->print_it = print_it_noop;
3822 bs->stop = 0;
3823 }
3824 }
3825}
3826
3827
3828/* Check conditions (condition proper, frame, thread and ignore count)
3829 of breakpoint referred to by BS. If we should not stop for this
3830 breakpoint, set BS->stop to 0. */
f431efe5 3831
18a18393
VP
3832static void
3833bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3834{
3835 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
3836 const struct bp_location *bl;
3837 struct breakpoint *b;
3838
3839 /* BS is built for existing struct breakpoint. */
f431efe5 3840 bl = bs->bp_location_at;
2bdf28a0 3841 gdb_assert (bl != NULL);
f431efe5 3842 b = bs->breakpoint_at;
2bdf28a0 3843 gdb_assert (b != NULL);
18a18393
VP
3844
3845 if (frame_id_p (b->frame_id)
edb3359d 3846 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
3847 bs->stop = 0;
3848 else if (bs->stop)
3849 {
3850 int value_is_zero = 0;
60e1c644
PA
3851 struct expression *cond;
3852
7371cf6d
PM
3853 /* Evaluate Python breakpoints that have a "stop"
3854 method implemented. */
3855 if (b->py_bp_object)
3856 bs->stop = gdbpy_should_stop (b->py_bp_object);
3857
60e1c644
PA
3858 if (is_watchpoint (b))
3859 cond = b->cond_exp;
3860 else
3861 cond = bl->cond;
3862
f431efe5 3863 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 3864 {
60e1c644
PA
3865 int within_current_scope = 1;
3866
c5bc3a77
DJ
3867 /* We use value_mark and value_free_to_mark because it could
3868 be a long time before we return to the command level and
3869 call free_all_values. We can't call free_all_values
3870 because we might be in the middle of evaluating a
3871 function call. */
3872 struct value *mark = value_mark ();
3873
edb3359d
DJ
3874 /* Need to select the frame, with all that implies so that
3875 the conditions will have the right context. Because we
3876 use the frame, we will not see an inlined function's
3877 variables when we arrive at a breakpoint at the start
3878 of the inlined function; the current frame will be the
3879 call site. */
60e1c644
PA
3880 if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
3881 select_frame (get_current_frame ());
3882 else
3883 {
3884 struct frame_info *frame;
3885
3886 /* For local watchpoint expressions, which particular
3887 instance of a local is being watched matters, so we
3888 keep track of the frame to evaluate the expression
3889 in. To evaluate the condition however, it doesn't
3890 really matter which instantiation of the function
3891 where the condition makes sense triggers the
3892 watchpoint. This allows an expression like "watch
3893 global if q > 10" set in `func', catch writes to
3894 global on all threads that call `func', or catch
3895 writes on all recursive calls of `func' by a single
3896 thread. We simply always evaluate the condition in
3897 the innermost frame that's executing where it makes
3898 sense to evaluate the condition. It seems
3899 intuitive. */
3900 frame = block_innermost_frame (b->cond_exp_valid_block);
3901 if (frame != NULL)
3902 select_frame (frame);
3903 else
3904 within_current_scope = 0;
3905 }
3906 if (within_current_scope)
3907 value_is_zero
3908 = catch_errors (breakpoint_cond_eval, cond,
3909 "Error in testing breakpoint condition:\n",
3910 RETURN_MASK_ALL);
3911 else
3912 {
3913 warning (_("Watchpoint condition cannot be tested "
3914 "in the current scope"));
3915 /* If we failed to set the right context for this
3916 watchpoint, unconditionally report it. */
3917 value_is_zero = 0;
3918 }
4a64f543 3919 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 3920 value_free_to_mark (mark);
18a18393 3921 }
60e1c644
PA
3922
3923 if (cond && value_is_zero)
18a18393
VP
3924 {
3925 bs->stop = 0;
3926 }
3927 else if (b->thread != -1 && b->thread != thread_id)
3928 {
3929 bs->stop = 0;
3930 }
3931 else if (b->ignore_count > 0)
3932 {
3933 b->ignore_count--;
3934 annotate_ignore_count_change ();
3935 bs->stop = 0;
4a64f543 3936 /* Increase the hit count even though we don't stop. */
18a18393 3937 ++(b->hit_count);
8d3788bd 3938 observer_notify_breakpoint_modified (b);
18a18393
VP
3939 }
3940 }
3941}
3942
3943
9709f61c 3944/* Get a bpstat associated with having just stopped at address
d983da9c 3945 BP_ADDR in thread PTID.
c906108c 3946
d983da9c 3947 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
3948 don't understand this stop. Result is a chain of bpstat's such
3949 that:
c906108c 3950
c5aa993b 3951 if we don't understand the stop, the result is a null pointer.
c906108c 3952
c5aa993b 3953 if we understand why we stopped, the result is not null.
c906108c 3954
c5aa993b
JM
3955 Each element of the chain refers to a particular breakpoint or
3956 watchpoint at which we have stopped. (We may have stopped for
3957 several reasons concurrently.)
c906108c 3958
c5aa993b
JM
3959 Each element of the chain has valid next, breakpoint_at,
3960 commands, FIXME??? fields. */
c906108c
SS
3961
3962bpstat
6c95b8df
PA
3963bpstat_stop_status (struct address_space *aspace,
3964 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 3965{
0d381245 3966 struct breakpoint *b = NULL;
afe38095 3967 struct bp_location *bl;
20874c92 3968 struct bp_location *loc;
5760d0ab
JK
3969 /* First item of allocated bpstat's. */
3970 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 3971 /* Pointer to the last thing in the chain currently. */
5760d0ab 3972 bpstat bs;
20874c92 3973 int ix;
429374b8 3974 int need_remove_insert;
f431efe5 3975 int removed_any;
c906108c 3976
f431efe5
PA
3977 /* First, build the bpstat chain with locations that explain a
3978 target stop, while being careful to not set the target running,
3979 as that may invalidate locations (in particular watchpoint
3980 locations are recreated). Resuming will happen here with
3981 breakpoint conditions or watchpoint expressions that include
3982 inferior function calls. */
c5aa993b 3983
429374b8
JK
3984 ALL_BREAKPOINTS (b)
3985 {
3986 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
3987 continue;
a5606eee 3988
429374b8
JK
3989 for (bl = b->loc; bl != NULL; bl = bl->next)
3990 {
4a64f543
MS
3991 /* For hardware watchpoints, we look only at the first
3992 location. The watchpoint_check function will work on the
3993 entire expression, not the individual locations. For
3994 read watchpoints, the watchpoints_triggered function has
3995 checked all locations already. */
429374b8
JK
3996 if (b->type == bp_hardware_watchpoint && bl != b->loc)
3997 break;
18a18393 3998
429374b8
JK
3999 if (bl->shlib_disabled)
4000 continue;
c5aa993b 4001
429374b8
JK
4002 if (!bpstat_check_location (bl, aspace, bp_addr))
4003 continue;
c5aa993b 4004
4a64f543
MS
4005 /* Come here if it's a watchpoint, or if the break address
4006 matches. */
c5aa993b 4007
4a64f543
MS
4008 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4009 explain stop. */
c5aa993b 4010
f431efe5
PA
4011 /* Assume we stop. Should we find a watchpoint that is not
4012 actually triggered, or if the condition of the breakpoint
4013 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4014 bs->stop = 1;
4015 bs->print = 1;
d983da9c 4016
f431efe5
PA
4017 /* If this is a scope breakpoint, mark the associated
4018 watchpoint as triggered so that we will handle the
4019 out-of-scope event. We'll get to the watchpoint next
4020 iteration. */
d0fb5eae 4021 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
f431efe5
PA
4022 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4023 }
4024 }
4025
4026 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4027 {
f1310107 4028 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4029 {
5760d0ab 4030 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4031 /* For hits of moribund locations, we should just proceed. */
4032 bs->stop = 0;
4033 bs->print = 0;
4034 bs->print_it = print_it_noop;
4035 }
4036 }
4037
f431efe5
PA
4038 /* Now go through the locations that caused the target to stop, and
4039 check whether we're interested in reporting this stop to higher
4040 layers, or whether we should resume the target transparently. */
4041
4042 removed_any = 0;
4043
5760d0ab 4044 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4045 {
4046 if (!bs->stop)
4047 continue;
4048
f431efe5 4049 b = bs->breakpoint_at;
348d480f
PA
4050 b->ops->check_status (bs);
4051 if (bs->stop)
28010a5d 4052 {
348d480f 4053 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4054
429374b8
JK
4055 if (bs->stop)
4056 {
4057 ++(b->hit_count);
8d3788bd 4058 observer_notify_breakpoint_modified (b);
c906108c 4059
4a64f543 4060 /* We will stop here. */
429374b8
JK
4061 if (b->disposition == disp_disable)
4062 {
4063 if (b->enable_state != bp_permanent)
4064 b->enable_state = bp_disabled;
f431efe5 4065 removed_any = 1;
429374b8
JK
4066 }
4067 if (b->silent)
4068 bs->print = 0;
4069 bs->commands = b->commands;
9add0f1b
TT
4070 incref_counted_command_line (bs->commands);
4071 bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4072 if (bs->commands_left
4073 && (strcmp ("silent", bs->commands_left->line) == 0
4074 || (xdb_commands
4075 && strcmp ("Q",
4076 bs->commands_left->line) == 0)))
429374b8 4077 {
9add0f1b 4078 bs->commands_left = bs->commands_left->next;
429374b8
JK
4079 bs->print = 0;
4080 }
429374b8
JK
4081 }
4082
e5dd4106 4083 /* Print nothing for this entry if we don't stop or don't print. */
429374b8
JK
4084 if (bs->stop == 0 || bs->print == 0)
4085 bs->print_it = print_it_noop;
348d480f 4086 }
429374b8 4087 }
876fa593 4088
d983da9c
DJ
4089 /* If we aren't stopping, the value of some hardware watchpoint may
4090 not have changed, but the intermediate memory locations we are
4091 watching may have. Don't bother if we're stopping; this will get
4092 done later. */
d832cb68 4093 need_remove_insert = 0;
5760d0ab
JK
4094 if (! bpstat_causes_stop (bs_head))
4095 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4096 if (!bs->stop
f431efe5
PA
4097 && bs->breakpoint_at
4098 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4099 {
4a64f543 4100 update_watchpoint (bs->breakpoint_at, 0 /* don't reparse. */);
d832cb68 4101 need_remove_insert = 1;
d983da9c
DJ
4102 }
4103
d832cb68 4104 if (need_remove_insert)
2d134ed3 4105 update_global_location_list (1);
f431efe5
PA
4106 else if (removed_any)
4107 update_global_location_list (0);
d832cb68 4108
5760d0ab 4109 return bs_head;
c906108c 4110}
628fe4e4
JK
4111
4112static void
4113handle_jit_event (void)
4114{
4115 struct frame_info *frame;
4116 struct gdbarch *gdbarch;
4117
4118 /* Switch terminal for any messages produced by
4119 breakpoint_re_set. */
4120 target_terminal_ours_for_output ();
4121
4122 frame = get_current_frame ();
4123 gdbarch = get_frame_arch (frame);
4124
4125 jit_event_handler (gdbarch);
4126
4127 target_terminal_inferior ();
4128}
4129
4130/* Prepare WHAT final decision for infrun. */
4131
4132/* Decide what infrun needs to do with this bpstat. */
4133
c906108c 4134struct bpstat_what
0e30163f 4135bpstat_what (bpstat bs_head)
c906108c 4136{
c906108c 4137 struct bpstat_what retval;
628fe4e4
JK
4138 /* We need to defer calling `solib_add', as adding new symbols
4139 resets breakpoints, which in turn deletes breakpoint locations,
4140 and hence may clear unprocessed entries in the BS chain. */
4141 int shlib_event = 0;
4142 int jit_event = 0;
0e30163f 4143 bpstat bs;
c906108c 4144
628fe4e4 4145 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4146 retval.call_dummy = STOP_NONE;
186c406b 4147 retval.is_longjmp = 0;
628fe4e4 4148
0e30163f 4149 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4150 {
628fe4e4
JK
4151 /* Extract this BS's action. After processing each BS, we check
4152 if its action overrides all we've seem so far. */
4153 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4154 enum bptype bptype;
4155
c906108c 4156 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4157 {
4158 /* I suspect this can happen if it was a momentary
4159 breakpoint which has since been deleted. */
4160 bptype = bp_none;
4161 }
20874c92 4162 else
f431efe5 4163 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4164
4165 switch (bptype)
c906108c
SS
4166 {
4167 case bp_none:
628fe4e4 4168 break;
c906108c
SS
4169 case bp_breakpoint:
4170 case bp_hardware_breakpoint:
4171 case bp_until:
4172 case bp_finish:
4173 if (bs->stop)
4174 {
4175 if (bs->print)
628fe4e4 4176 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4177 else
628fe4e4 4178 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4179 }
4180 else
628fe4e4 4181 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4182 break;
4183 case bp_watchpoint:
4184 case bp_hardware_watchpoint:
4185 case bp_read_watchpoint:
4186 case bp_access_watchpoint:
4187 if (bs->stop)
4188 {
4189 if (bs->print)
628fe4e4 4190 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4191 else
628fe4e4 4192 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4193 }
4194 else
628fe4e4
JK
4195 {
4196 /* There was a watchpoint, but we're not stopping.
4197 This requires no further action. */
4198 }
c906108c
SS
4199 break;
4200 case bp_longjmp:
186c406b 4201 case bp_exception:
628fe4e4 4202 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4203 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4204 break;
4205 case bp_longjmp_resume:
186c406b 4206 case bp_exception_resume:
628fe4e4 4207 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4208 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4209 break;
4210 case bp_step_resume:
4211 if (bs->stop)
628fe4e4
JK
4212 this_action = BPSTAT_WHAT_STEP_RESUME;
4213 else
c906108c 4214 {
628fe4e4
JK
4215 /* It is for the wrong frame. */
4216 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4217 }
c906108c 4218 break;
2c03e5be
PA
4219 case bp_hp_step_resume:
4220 if (bs->stop)
4221 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4222 else
4223 {
4224 /* It is for the wrong frame. */
4225 this_action = BPSTAT_WHAT_SINGLE;
4226 }
4227 break;
c906108c 4228 case bp_watchpoint_scope:
c4093a6a 4229 case bp_thread_event:
1900040c 4230 case bp_overlay_event:
0fd8e87f 4231 case bp_longjmp_master:
aa7d318d 4232 case bp_std_terminate_master:
186c406b 4233 case bp_exception_master:
628fe4e4 4234 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4235 break;
ce78b96d 4236 case bp_catchpoint:
c5aa993b
JM
4237 if (bs->stop)
4238 {
4239 if (bs->print)
628fe4e4 4240 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4241 else
628fe4e4 4242 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4243 }
4244 else
628fe4e4
JK
4245 {
4246 /* There was a catchpoint, but we're not stopping.
4247 This requires no further action. */
4248 }
4249 break;
4250 case bp_shlib_event:
4251 shlib_event = 1;
4252
4253 /* If requested, stop when the dynamic linker notifies GDB
4254 of events. This allows the user to get control and place
4255 breakpoints in initializer routines for dynamically
4256 loaded objects (among other things). */
4257 if (stop_on_solib_events)
4258 this_action = BPSTAT_WHAT_STOP_NOISY;
4259 else
4260 this_action = BPSTAT_WHAT_SINGLE;
4261 break;
4262 case bp_jit_event:
4263 jit_event = 1;
4264 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4265 break;
c906108c 4266 case bp_call_dummy:
53a5351d
JM
4267 /* Make sure the action is stop (silent or noisy),
4268 so infrun.c pops the dummy frame. */
aa7d318d 4269 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4270 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4271 break;
4272 case bp_std_terminate:
4273 /* Make sure the action is stop (silent or noisy),
4274 so infrun.c pops the dummy frame. */
aa7d318d 4275 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4276 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4277 break;
1042e4c0 4278 case bp_tracepoint:
7a697b8d 4279 case bp_fast_tracepoint:
0fb4aa4b 4280 case bp_static_tracepoint:
1042e4c0
SS
4281 /* Tracepoint hits should not be reported back to GDB, and
4282 if one got through somehow, it should have been filtered
4283 out already. */
4284 internal_error (__FILE__, __LINE__,
7a697b8d 4285 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4286 break;
4287 case bp_gnu_ifunc_resolver:
4288 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4289 this_action = BPSTAT_WHAT_SINGLE;
4290 break;
4291 case bp_gnu_ifunc_resolver_return:
4292 /* The breakpoint will be removed, execution will restart from the
4293 PC of the former breakpoint. */
4294 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4295 break;
628fe4e4
JK
4296 default:
4297 internal_error (__FILE__, __LINE__,
4298 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4299 }
628fe4e4
JK
4300
4301 retval.main_action = max (retval.main_action, this_action);
c906108c 4302 }
628fe4e4 4303
0e30163f
JK
4304 /* These operations may affect the bs->breakpoint_at state so they are
4305 delayed after MAIN_ACTION is decided above. */
4306
628fe4e4
JK
4307 if (shlib_event)
4308 {
4309 if (debug_infrun)
4310 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4311
4312 /* Check for any newly added shared libraries if we're supposed
4313 to be adding them automatically. */
4314
4315 /* Switch terminal for any messages produced by
4316 breakpoint_re_set. */
4317 target_terminal_ours_for_output ();
4318
4319#ifdef SOLIB_ADD
4320 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4321#else
4322 solib_add (NULL, 0, &current_target, auto_solib_add);
4323#endif
4324
4325 target_terminal_inferior ();
4326 }
4327
4328 if (jit_event)
4329 {
4330 if (debug_infrun)
4331 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4332
4333 handle_jit_event ();
4334 }
4335
0e30163f
JK
4336 for (bs = bs_head; bs != NULL; bs = bs->next)
4337 {
4338 struct breakpoint *b = bs->breakpoint_at;
4339
4340 if (b == NULL)
4341 continue;
4342 switch (b->type)
4343 {
4344 case bp_gnu_ifunc_resolver:
4345 gnu_ifunc_resolver_stop (b);
4346 break;
4347 case bp_gnu_ifunc_resolver_return:
4348 gnu_ifunc_resolver_return_stop (b);
4349 break;
4350 }
4351 }
4352
c906108c
SS
4353 return retval;
4354}
4355
4356/* Nonzero if we should step constantly (e.g. watchpoints on machines
4357 without hardware support). This isn't related to a specific bpstat,
4358 just to things like whether watchpoints are set. */
4359
c5aa993b 4360int
fba45db2 4361bpstat_should_step (void)
c906108c
SS
4362{
4363 struct breakpoint *b;
cc59ec59 4364
c906108c 4365 ALL_BREAKPOINTS (b)
717a8278 4366 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4367 return 1;
c906108c
SS
4368 return 0;
4369}
4370
67822962
PA
4371int
4372bpstat_causes_stop (bpstat bs)
4373{
4374 for (; bs != NULL; bs = bs->next)
4375 if (bs->stop)
4376 return 1;
4377
4378 return 0;
4379}
4380
c906108c 4381\f
c5aa993b 4382
170b53b2
UW
4383/* Compute a string of spaces suitable to indent the next line
4384 so it starts at the position corresponding to the table column
4385 named COL_NAME in the currently active table of UIOUT. */
4386
4387static char *
4388wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4389{
4390 static char wrap_indent[80];
4391 int i, total_width, width, align;
4392 char *text;
4393
4394 total_width = 0;
4395 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4396 {
4397 if (strcmp (text, col_name) == 0)
4398 {
4399 gdb_assert (total_width < sizeof wrap_indent);
4400 memset (wrap_indent, ' ', total_width);
4401 wrap_indent[total_width] = 0;
4402
4403 return wrap_indent;
4404 }
4405
4406 total_width += width + 1;
4407 }
4408
4409 return NULL;
4410}
4411
859825b8
JK
4412/* Print the LOC location out of the list of B->LOC locations. */
4413
170b53b2
UW
4414static void
4415print_breakpoint_location (struct breakpoint *b,
4416 struct bp_location *loc)
0d381245 4417{
6c95b8df
PA
4418 struct cleanup *old_chain = save_current_program_space ();
4419
859825b8
JK
4420 if (loc != NULL && loc->shlib_disabled)
4421 loc = NULL;
4422
6c95b8df
PA
4423 if (loc != NULL)
4424 set_current_program_space (loc->pspace);
4425
56435ebe
TT
4426 if (b->display_canonical)
4427 ui_out_field_string (uiout, "what", b->addr_string);
4428 else if (b->source_file && loc)
0d381245
VP
4429 {
4430 struct symbol *sym
4431 = find_pc_sect_function (loc->address, loc->section);
4432 if (sym)
4433 {
4434 ui_out_text (uiout, "in ");
4435 ui_out_field_string (uiout, "func",
4436 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4437 ui_out_text (uiout, " ");
4438 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4439 ui_out_text (uiout, "at ");
0d381245
VP
4440 }
4441 ui_out_field_string (uiout, "file", b->source_file);
4442 ui_out_text (uiout, ":");
4443
4444 if (ui_out_is_mi_like_p (uiout))
4445 {
4446 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4447 char *fullname = symtab_to_fullname (sal.symtab);
4448
4449 if (fullname)
4450 ui_out_field_string (uiout, "fullname", fullname);
4451 }
4452
4453 ui_out_field_int (uiout, "line", b->line_number);
4454 }
859825b8 4455 else if (loc)
0d381245 4456 {
170b53b2
UW
4457 struct ui_stream *stb = ui_out_stream_new (uiout);
4458 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4459
22e722e1
DJ
4460 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4461 demangle, "");
0d381245 4462 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4463
4464 do_cleanups (stb_chain);
0d381245 4465 }
859825b8
JK
4466 else
4467 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4468
4469 do_cleanups (old_chain);
0d381245
VP
4470}
4471
269b11a2
PA
4472static const char *
4473bptype_string (enum bptype type)
c906108c 4474{
c4093a6a
JM
4475 struct ep_type_description
4476 {
4477 enum bptype type;
4478 char *description;
4479 };
4480 static struct ep_type_description bptypes[] =
c906108c 4481 {
c5aa993b
JM
4482 {bp_none, "?deleted?"},
4483 {bp_breakpoint, "breakpoint"},
c906108c 4484 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4485 {bp_until, "until"},
4486 {bp_finish, "finish"},
4487 {bp_watchpoint, "watchpoint"},
c906108c 4488 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4489 {bp_read_watchpoint, "read watchpoint"},
4490 {bp_access_watchpoint, "acc watchpoint"},
4491 {bp_longjmp, "longjmp"},
4492 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4493 {bp_exception, "exception"},
4494 {bp_exception_resume, "exception resume"},
c5aa993b 4495 {bp_step_resume, "step resume"},
2c03e5be 4496 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4497 {bp_watchpoint_scope, "watchpoint scope"},
4498 {bp_call_dummy, "call dummy"},
aa7d318d 4499 {bp_std_terminate, "std::terminate"},
c5aa993b 4500 {bp_shlib_event, "shlib events"},
c4093a6a 4501 {bp_thread_event, "thread events"},
1900040c 4502 {bp_overlay_event, "overlay events"},
0fd8e87f 4503 {bp_longjmp_master, "longjmp master"},
aa7d318d 4504 {bp_std_terminate_master, "std::terminate master"},
186c406b 4505 {bp_exception_master, "exception master"},
ce78b96d 4506 {bp_catchpoint, "catchpoint"},
1042e4c0 4507 {bp_tracepoint, "tracepoint"},
7a697b8d 4508 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4509 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4510 {bp_jit_event, "jit events"},
0e30163f
JK
4511 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4512 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4513 };
269b11a2
PA
4514
4515 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4516 || ((int) type != bptypes[(int) type].type))
4517 internal_error (__FILE__, __LINE__,
4518 _("bptypes table does not describe type #%d."),
4519 (int) type);
4520
4521 return bptypes[(int) type].description;
4522}
4523
4524/* Print B to gdb_stdout. */
4525
4526static void
4527print_one_breakpoint_location (struct breakpoint *b,
4528 struct bp_location *loc,
4529 int loc_number,
4530 struct bp_location **last_loc,
269b11a2
PA
4531 int allflag)
4532{
4533 struct command_line *l;
c2c6d25f 4534 static char bpenables[] = "nynny";
c906108c 4535
0d381245
VP
4536 int header_of_multiple = 0;
4537 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4538 struct value_print_options opts;
4539
4540 get_user_print_options (&opts);
0d381245
VP
4541
4542 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4543 /* See comment in print_one_breakpoint concerning treatment of
4544 breakpoints with single disabled location. */
0d381245
VP
4545 if (loc == NULL
4546 && (b->loc != NULL
4547 && (b->loc->next != NULL || !b->loc->enabled)))
4548 header_of_multiple = 1;
4549 if (loc == NULL)
4550 loc = b->loc;
4551
c4093a6a
JM
4552 annotate_record ();
4553
4554 /* 1 */
4555 annotate_field (0);
0d381245
VP
4556 if (part_of_multiple)
4557 {
4558 char *formatted;
0c6773c1 4559 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4560 ui_out_field_string (uiout, "number", formatted);
4561 xfree (formatted);
4562 }
4563 else
4564 {
4565 ui_out_field_int (uiout, "number", b->number);
4566 }
c4093a6a
JM
4567
4568 /* 2 */
4569 annotate_field (1);
0d381245
VP
4570 if (part_of_multiple)
4571 ui_out_field_skip (uiout, "type");
269b11a2
PA
4572 else
4573 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4574
4575 /* 3 */
4576 annotate_field (2);
0d381245
VP
4577 if (part_of_multiple)
4578 ui_out_field_skip (uiout, "disp");
4579 else
2cec12e5 4580 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4581
c4093a6a
JM
4582
4583 /* 4 */
4584 annotate_field (3);
0d381245 4585 if (part_of_multiple)
54e52265 4586 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4587 else
4a64f543
MS
4588 ui_out_field_fmt (uiout, "enabled", "%c",
4589 bpenables[(int) b->enable_state]);
54e52265 4590 ui_out_spaces (uiout, 2);
0d381245 4591
c4093a6a
JM
4592
4593 /* 5 and 6 */
3086aeae 4594 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4595 {
4a64f543
MS
4596 /* Although the print_one can possibly print all locations,
4597 calling it here is not likely to get any nice result. So,
4598 make sure there's just one location. */
0d381245 4599 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4600 b->ops->print_one (b, last_loc);
0d381245 4601 }
3086aeae
DJ
4602 else
4603 switch (b->type)
4604 {
4605 case bp_none:
4606 internal_error (__FILE__, __LINE__,
e2e0b3e5 4607 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4608 break;
c906108c 4609
3086aeae
DJ
4610 case bp_watchpoint:
4611 case bp_hardware_watchpoint:
4612 case bp_read_watchpoint:
4613 case bp_access_watchpoint:
4614 /* Field 4, the address, is omitted (which makes the columns
4615 not line up too nicely with the headers, but the effect
4616 is relatively readable). */
79a45b7d 4617 if (opts.addressprint)
3086aeae
DJ
4618 ui_out_field_skip (uiout, "addr");
4619 annotate_field (5);
fa8a61dc 4620 ui_out_field_string (uiout, "what", b->exp_string);
3086aeae
DJ
4621 break;
4622
3086aeae
DJ
4623 case bp_breakpoint:
4624 case bp_hardware_breakpoint:
4625 case bp_until:
4626 case bp_finish:
4627 case bp_longjmp:
4628 case bp_longjmp_resume:
186c406b
TT
4629 case bp_exception:
4630 case bp_exception_resume:
3086aeae 4631 case bp_step_resume:
2c03e5be 4632 case bp_hp_step_resume:
3086aeae
DJ
4633 case bp_watchpoint_scope:
4634 case bp_call_dummy:
aa7d318d 4635 case bp_std_terminate:
3086aeae
DJ
4636 case bp_shlib_event:
4637 case bp_thread_event:
4638 case bp_overlay_event:
0fd8e87f 4639 case bp_longjmp_master:
aa7d318d 4640 case bp_std_terminate_master:
186c406b 4641 case bp_exception_master:
1042e4c0 4642 case bp_tracepoint:
7a697b8d 4643 case bp_fast_tracepoint:
0fb4aa4b 4644 case bp_static_tracepoint:
4efc6507 4645 case bp_jit_event:
0e30163f
JK
4646 case bp_gnu_ifunc_resolver:
4647 case bp_gnu_ifunc_resolver_return:
79a45b7d 4648 if (opts.addressprint)
3086aeae
DJ
4649 {
4650 annotate_field (4);
54e52265 4651 if (header_of_multiple)
0d381245 4652 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4653 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4654 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4655 else
5af949e3
UW
4656 ui_out_field_core_addr (uiout, "addr",
4657 loc->gdbarch, loc->address);
3086aeae
DJ
4658 }
4659 annotate_field (5);
0d381245 4660 if (!header_of_multiple)
170b53b2 4661 print_breakpoint_location (b, loc);
0d381245 4662 if (b->loc)
a6d9a66e 4663 *last_loc = b->loc;
3086aeae
DJ
4664 break;
4665 }
c906108c 4666
6c95b8df
PA
4667
4668 /* For backward compatibility, don't display inferiors unless there
4669 are several. */
4670 if (loc != NULL
4671 && !header_of_multiple
4672 && (allflag
4673 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4674 && (number_of_program_spaces () > 1
4675 || number_of_inferiors () > 1)
4a64f543
MS
4676 /* LOC is for existing B, it cannot be in
4677 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4678 && loc->owner->type != bp_catchpoint)))
4679 {
4680 struct inferior *inf;
4681 int first = 1;
4682
4683 for (inf = inferior_list; inf != NULL; inf = inf->next)
4684 {
4685 if (inf->pspace == loc->pspace)
4686 {
4687 if (first)
4688 {
4689 first = 0;
4690 ui_out_text (uiout, " inf ");
4691 }
4692 else
4693 ui_out_text (uiout, ", ");
4694 ui_out_text (uiout, plongest (inf->num));
4695 }
4696 }
4697 }
4698
4a306c9a 4699 if (!part_of_multiple)
c4093a6a 4700 {
4a306c9a
JB
4701 if (b->thread != -1)
4702 {
4703 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4704 "stop only in" line a little further down. */
4a306c9a
JB
4705 ui_out_text (uiout, " thread ");
4706 ui_out_field_int (uiout, "thread", b->thread);
4707 }
4708 else if (b->task != 0)
4709 {
4710 ui_out_text (uiout, " task ");
4711 ui_out_field_int (uiout, "task", b->task);
4712 }
c4093a6a 4713 }
f1310107 4714
8b93c638 4715 ui_out_text (uiout, "\n");
f1310107 4716
348d480f 4717 if (!part_of_multiple)
f1310107
TJB
4718 b->ops->print_one_detail (b, uiout);
4719
0d381245 4720 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4721 {
4722 annotate_field (6);
8b93c638 4723 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 4724 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 4725 the frame ID. */
5af949e3
UW
4726 ui_out_field_core_addr (uiout, "frame",
4727 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4728 ui_out_text (uiout, "\n");
c4093a6a
JM
4729 }
4730
28010a5d 4731 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
4732 {
4733 annotate_field (7);
d77f58be 4734 if (is_tracepoint (b))
1042e4c0
SS
4735 ui_out_text (uiout, "\ttrace only if ");
4736 else
4737 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4738 ui_out_field_string (uiout, "cond", b->cond_string);
4739 ui_out_text (uiout, "\n");
4740 }
4741
0d381245 4742 if (!part_of_multiple && b->thread != -1)
c4093a6a 4743 {
4a64f543 4744 /* FIXME should make an annotation for this. */
8b93c638
JM
4745 ui_out_text (uiout, "\tstop only in thread ");
4746 ui_out_field_int (uiout, "thread", b->thread);
4747 ui_out_text (uiout, "\n");
c4093a6a
JM
4748 }
4749
63c715c6 4750 if (!part_of_multiple && b->hit_count)
c4093a6a 4751 {
4a64f543 4752 /* FIXME should make an annotation for this. */
8b93c638
JM
4753 if (ep_is_catchpoint (b))
4754 ui_out_text (uiout, "\tcatchpoint");
4755 else
4756 ui_out_text (uiout, "\tbreakpoint");
4757 ui_out_text (uiout, " already hit ");
4758 ui_out_field_int (uiout, "times", b->hit_count);
4759 if (b->hit_count == 1)
4760 ui_out_text (uiout, " time\n");
4761 else
4762 ui_out_text (uiout, " times\n");
c4093a6a
JM
4763 }
4764
4a64f543
MS
4765 /* Output the count also if it is zero, but only if this is mi.
4766 FIXME: Should have a better test for this. */
9dc5e2a9 4767 if (ui_out_is_mi_like_p (uiout))
63c715c6 4768 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4769 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4770
0d381245 4771 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4772 {
4773 annotate_field (8);
8b93c638
JM
4774 ui_out_text (uiout, "\tignore next ");
4775 ui_out_field_int (uiout, "ignore", b->ignore_count);
4776 ui_out_text (uiout, " hits\n");
c4093a6a 4777 }
059fb39f 4778
9add0f1b 4779 l = b->commands ? b->commands->commands : NULL;
059fb39f 4780 if (!part_of_multiple && l)
c4093a6a 4781 {
3b31d625
EZ
4782 struct cleanup *script_chain;
4783
c4093a6a 4784 annotate_field (9);
3b31d625 4785 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4786 print_command_lines (uiout, l, 4);
3b31d625 4787 do_cleanups (script_chain);
c4093a6a 4788 }
d24317b4 4789
1042e4c0
SS
4790 if (!part_of_multiple && b->pass_count)
4791 {
4792 annotate_field (10);
4793 ui_out_text (uiout, "\tpass count ");
4794 ui_out_field_int (uiout, "pass", b->pass_count);
4795 ui_out_text (uiout, " \n");
4796 }
4797
d24317b4
VP
4798 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4799 {
4800 if (b->addr_string)
4801 ui_out_field_string (uiout, "original-location", b->addr_string);
4802 else if (b->exp_string)
4803 ui_out_field_string (uiout, "original-location", b->exp_string);
4804 }
c4093a6a 4805}
c5aa993b 4806
0d381245
VP
4807static void
4808print_one_breakpoint (struct breakpoint *b,
4a64f543 4809 struct bp_location **last_loc,
6c95b8df 4810 int allflag)
0d381245 4811{
8d3788bd
VP
4812 struct cleanup *bkpt_chain;
4813
4814 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4815
12c5a436 4816 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 4817 do_cleanups (bkpt_chain);
0d381245
VP
4818
4819 /* If this breakpoint has custom print function,
4820 it's already printed. Otherwise, print individual
4821 locations, if any. */
4822 if (b->ops == NULL || b->ops->print_one == NULL)
4823 {
4a64f543
MS
4824 /* If breakpoint has a single location that is disabled, we
4825 print it as if it had several locations, since otherwise it's
4826 hard to represent "breakpoint enabled, location disabled"
4827 situation.
4828
4829 Note that while hardware watchpoints have several locations
a3be7890 4830 internally, that's not a property exposed to user. */
0d381245 4831 if (b->loc
a5606eee 4832 && !is_hardware_watchpoint (b)
8d3788bd 4833 && (b->loc->next || !b->loc->enabled))
0d381245
VP
4834 {
4835 struct bp_location *loc;
4836 int n = 1;
8d3788bd 4837
0d381245 4838 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
4839 {
4840 struct cleanup *inner2 =
4841 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
4842 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
4843 do_cleanups (inner2);
4844 }
0d381245
VP
4845 }
4846 }
4847}
4848
a6d9a66e
UW
4849static int
4850breakpoint_address_bits (struct breakpoint *b)
4851{
4852 int print_address_bits = 0;
4853 struct bp_location *loc;
4854
4855 for (loc = b->loc; loc; loc = loc->next)
4856 {
c7437ca6
PA
4857 int addr_bit;
4858
4859 /* Software watchpoints that aren't watching memory don't have
4860 an address to print. */
4861 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
4862 continue;
4863
4864 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
4865 if (addr_bit > print_address_bits)
4866 print_address_bits = addr_bit;
4867 }
4868
4869 return print_address_bits;
4870}
0d381245 4871
c4093a6a
JM
4872struct captured_breakpoint_query_args
4873 {
4874 int bnum;
4875 };
c5aa993b 4876
c4093a6a 4877static int
2b65245e 4878do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
4879{
4880 struct captured_breakpoint_query_args *args = data;
52f0bd74 4881 struct breakpoint *b;
a6d9a66e 4882 struct bp_location *dummy_loc = NULL;
cc59ec59 4883
c4093a6a
JM
4884 ALL_BREAKPOINTS (b)
4885 {
4886 if (args->bnum == b->number)
c5aa993b 4887 {
12c5a436 4888 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 4889 return GDB_RC_OK;
c5aa993b 4890 }
c4093a6a
JM
4891 }
4892 return GDB_RC_NONE;
4893}
c5aa993b 4894
c4093a6a 4895enum gdb_rc
4a64f543
MS
4896gdb_breakpoint_query (struct ui_out *uiout, int bnum,
4897 char **error_message)
c4093a6a
JM
4898{
4899 struct captured_breakpoint_query_args args;
cc59ec59 4900
c4093a6a
JM
4901 args.bnum = bnum;
4902 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 4903 an error. */
b0b13bb4
DJ
4904 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
4905 error_message, RETURN_MASK_ALL) < 0)
4906 return GDB_RC_FAIL;
4907 else
4908 return GDB_RC_OK;
c4093a6a 4909}
c5aa993b 4910
09d682a4
TT
4911/* Return true if this breakpoint was set by the user, false if it is
4912 internal or momentary. */
4913
4914int
4915user_breakpoint_p (struct breakpoint *b)
4916{
46c6471b 4917 return b->number > 0;
09d682a4
TT
4918}
4919
7f3b0473 4920/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
4921 number BNUM. If BNUM is -1 print all user-settable breakpoints.
4922 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
4923 FILTER is non-NULL, call it on each breakpoint and only include the
4924 ones for which it returns non-zero. Return the total number of
4925 breakpoints listed. */
c906108c 4926
d77f58be 4927static int
e5a67952 4928breakpoint_1 (char *args, int allflag,
4a64f543 4929 int (*filter) (const struct breakpoint *))
c4093a6a 4930{
52f0bd74 4931 struct breakpoint *b;
a6d9a66e 4932 struct bp_location *last_loc = NULL;
7f3b0473 4933 int nr_printable_breakpoints;
3b31d625 4934 struct cleanup *bkpttbl_chain;
79a45b7d 4935 struct value_print_options opts;
a6d9a66e 4936 int print_address_bits = 0;
269b11a2
PA
4937 int print_type_col_width = 14;
4938
79a45b7d
TT
4939 get_user_print_options (&opts);
4940
4a64f543
MS
4941 /* Compute the number of rows in the table, as well as the size
4942 required for address fields. */
7f3b0473
AC
4943 nr_printable_breakpoints = 0;
4944 ALL_BREAKPOINTS (b)
e5a67952
MS
4945 {
4946 /* If we have a filter, only list the breakpoints it accepts. */
4947 if (filter && !filter (b))
4948 continue;
4949
4950 /* If we have an "args" string, it is a list of breakpoints to
4951 accept. Skip the others. */
4952 if (args != NULL && *args != '\0')
4953 {
4954 if (allflag && parse_and_eval_long (args) != b->number)
4955 continue;
4956 if (!allflag && !number_is_in_list (args, b->number))
4957 continue;
4958 }
269b11a2 4959
e5a67952
MS
4960 if (allflag || user_breakpoint_p (b))
4961 {
4962 int addr_bit, type_len;
a6d9a66e 4963
e5a67952
MS
4964 addr_bit = breakpoint_address_bits (b);
4965 if (addr_bit > print_address_bits)
4966 print_address_bits = addr_bit;
269b11a2 4967
e5a67952
MS
4968 type_len = strlen (bptype_string (b->type));
4969 if (type_len > print_type_col_width)
4970 print_type_col_width = type_len;
4971
4972 nr_printable_breakpoints++;
4973 }
4974 }
7f3b0473 4975
79a45b7d 4976 if (opts.addressprint)
3b31d625 4977 bkpttbl_chain
3e43a32a
MS
4978 = make_cleanup_ui_out_table_begin_end (uiout, 6,
4979 nr_printable_breakpoints,
3b31d625 4980 "BreakpointTable");
8b93c638 4981 else
3b31d625 4982 bkpttbl_chain
3e43a32a
MS
4983 = make_cleanup_ui_out_table_begin_end (uiout, 5,
4984 nr_printable_breakpoints,
3b31d625 4985 "BreakpointTable");
8b93c638 4986
7f3b0473 4987 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
4988 annotate_breakpoints_headers ();
4989 if (nr_printable_breakpoints > 0)
4990 annotate_field (0);
4a64f543 4991 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
4992 if (nr_printable_breakpoints > 0)
4993 annotate_field (1);
269b11a2 4994 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 4995 "type", "Type"); /* 2 */
d7faa9e7
AC
4996 if (nr_printable_breakpoints > 0)
4997 annotate_field (2);
4a64f543 4998 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
4999 if (nr_printable_breakpoints > 0)
5000 annotate_field (3);
54e52265 5001 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5002 if (opts.addressprint)
e5a67952
MS
5003 {
5004 if (nr_printable_breakpoints > 0)
5005 annotate_field (4);
5006 if (print_address_bits <= 32)
5007 ui_out_table_header (uiout, 10, ui_left,
5008 "addr", "Address"); /* 5 */
5009 else
5010 ui_out_table_header (uiout, 18, ui_left,
5011 "addr", "Address"); /* 5 */
5012 }
d7faa9e7
AC
5013 if (nr_printable_breakpoints > 0)
5014 annotate_field (5);
5015 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5016 ui_out_table_body (uiout);
5017 if (nr_printable_breakpoints > 0)
5018 annotate_breakpoints_table ();
7f3b0473 5019
c4093a6a 5020 ALL_BREAKPOINTS (b)
e5a67952
MS
5021 {
5022 QUIT;
5023 /* If we have a filter, only list the breakpoints it accepts. */
5024 if (filter && !filter (b))
5025 continue;
5026
5027 /* If we have an "args" string, it is a list of breakpoints to
5028 accept. Skip the others. */
5029
5030 if (args != NULL && *args != '\0')
5031 {
5032 if (allflag) /* maintenance info breakpoint */
5033 {
5034 if (parse_and_eval_long (args) != b->number)
5035 continue;
5036 }
5037 else /* all others */
5038 {
5039 if (!number_is_in_list (args, b->number))
5040 continue;
5041 }
5042 }
5043 /* We only print out user settable breakpoints unless the
5044 allflag is set. */
5045 if (allflag || user_breakpoint_p (b))
12c5a436 5046 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5047 }
5048
3b31d625 5049 do_cleanups (bkpttbl_chain);
698384cd 5050
7f3b0473 5051 if (nr_printable_breakpoints == 0)
c906108c 5052 {
4a64f543
MS
5053 /* If there's a filter, let the caller decide how to report
5054 empty list. */
d77f58be
SS
5055 if (!filter)
5056 {
e5a67952 5057 if (args == NULL || *args == '\0')
d77f58be
SS
5058 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5059 else
4a64f543 5060 ui_out_message (uiout, 0,
e5a67952
MS
5061 "No breakpoint or watchpoint matching '%s'.\n",
5062 args);
d77f58be 5063 }
c906108c
SS
5064 }
5065 else
c4093a6a 5066 {
a6d9a66e
UW
5067 if (last_loc && !server_command)
5068 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5069 }
c906108c 5070
4a64f543 5071 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5072 there have been breakpoints? */
c906108c 5073 annotate_breakpoints_table_end ();
d77f58be
SS
5074
5075 return nr_printable_breakpoints;
c906108c
SS
5076}
5077
ad443146
SS
5078/* Display the value of default-collect in a way that is generally
5079 compatible with the breakpoint list. */
5080
5081static void
5082default_collect_info (void)
5083{
5084 /* If it has no value (which is frequently the case), say nothing; a
5085 message like "No default-collect." gets in user's face when it's
5086 not wanted. */
5087 if (!*default_collect)
5088 return;
5089
5090 /* The following phrase lines up nicely with per-tracepoint collect
5091 actions. */
5092 ui_out_text (uiout, "default collect ");
5093 ui_out_field_string (uiout, "default-collect", default_collect);
5094 ui_out_text (uiout, " \n");
5095}
5096
c906108c 5097static void
e5a67952 5098breakpoints_info (char *args, int from_tty)
c906108c 5099{
e5a67952 5100 breakpoint_1 (args, 0, NULL);
ad443146
SS
5101
5102 default_collect_info ();
d77f58be
SS
5103}
5104
5105static void
e5a67952 5106watchpoints_info (char *args, int from_tty)
d77f58be 5107{
e5a67952 5108 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
d77f58be
SS
5109
5110 if (num_printed == 0)
5111 {
e5a67952 5112 if (args == NULL || *args == '\0')
d77f58be
SS
5113 ui_out_message (uiout, 0, "No watchpoints.\n");
5114 else
e5a67952 5115 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5116 }
c906108c
SS
5117}
5118
7a292a7a 5119static void
e5a67952 5120maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5121{
e5a67952 5122 breakpoint_1 (args, 1, NULL);
ad443146
SS
5123
5124 default_collect_info ();
c906108c
SS
5125}
5126
0d381245 5127static int
714835d5 5128breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5129 struct program_space *pspace,
714835d5 5130 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5131{
5132 struct bp_location *bl = b->loc;
cc59ec59 5133
0d381245
VP
5134 for (; bl; bl = bl->next)
5135 {
6c95b8df
PA
5136 if (bl->pspace == pspace
5137 && bl->address == pc
0d381245
VP
5138 && (!overlay_debugging || bl->section == section))
5139 return 1;
5140 }
5141 return 0;
5142}
5143
6c95b8df
PA
5144/* Print a message describing any breakpoints set at PC. This
5145 concerns with logical breakpoints, so we match program spaces, not
5146 address spaces. */
c906108c
SS
5147
5148static void
6c95b8df
PA
5149describe_other_breakpoints (struct gdbarch *gdbarch,
5150 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5151 struct obj_section *section, int thread)
c906108c 5152{
52f0bd74
AC
5153 int others = 0;
5154 struct breakpoint *b;
c906108c
SS
5155
5156 ALL_BREAKPOINTS (b)
6c95b8df 5157 others += breakpoint_has_pc (b, pspace, pc, section);
c906108c
SS
5158 if (others > 0)
5159 {
a3f17187
AC
5160 if (others == 1)
5161 printf_filtered (_("Note: breakpoint "));
5162 else /* if (others == ???) */
5163 printf_filtered (_("Note: breakpoints "));
c906108c 5164 ALL_BREAKPOINTS (b)
6c95b8df 5165 if (breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5166 {
5167 others--;
5168 printf_filtered ("%d", b->number);
5169 if (b->thread == -1 && thread != -1)
5170 printf_filtered (" (all threads)");
5171 else if (b->thread != -1)
5172 printf_filtered (" (thread %d)", b->thread);
5173 printf_filtered ("%s%s ",
059fb39f 5174 ((b->enable_state == bp_disabled
8bea4e01
UW
5175 || b->enable_state == bp_call_disabled
5176 || b->enable_state == bp_startup_disabled)
0d381245
VP
5177 ? " (disabled)"
5178 : b->enable_state == bp_permanent
5179 ? " (permanent)"
5180 : ""),
5181 (others > 1) ? ","
5182 : ((others == 1) ? " and" : ""));
5183 }
a3f17187 5184 printf_filtered (_("also set at pc "));
5af949e3 5185 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5186 printf_filtered (".\n");
5187 }
5188}
5189\f
5190/* Set the default place to put a breakpoint
5191 for the `break' command with no arguments. */
5192
5193void
6c95b8df
PA
5194set_default_breakpoint (int valid, struct program_space *pspace,
5195 CORE_ADDR addr, struct symtab *symtab,
fba45db2 5196 int line)
c906108c
SS
5197{
5198 default_breakpoint_valid = valid;
6c95b8df 5199 default_breakpoint_pspace = pspace;
c906108c
SS
5200 default_breakpoint_address = addr;
5201 default_breakpoint_symtab = symtab;
5202 default_breakpoint_line = line;
5203}
5204
e4f237da
KB
5205/* Return true iff it is meaningful to use the address member of
5206 BPT. For some breakpoint types, the address member is irrelevant
5207 and it makes no sense to attempt to compare it to other addresses
5208 (or use it for any other purpose either).
5209
4a64f543
MS
5210 More specifically, each of the following breakpoint types will
5211 always have a zero valued address and we don't want to mark
5212 breakpoints of any of these types to be a duplicate of an actual
5213 breakpoint at address zero:
e4f237da
KB
5214
5215 bp_watchpoint
2d134ed3
PA
5216 bp_catchpoint
5217
5218*/
e4f237da
KB
5219
5220static int
5221breakpoint_address_is_meaningful (struct breakpoint *bpt)
5222{
5223 enum bptype type = bpt->type;
5224
2d134ed3
PA
5225 return (type != bp_watchpoint && type != bp_catchpoint);
5226}
5227
5228/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5229 true if LOC1 and LOC2 represent the same watchpoint location. */
5230
5231static int
4a64f543
MS
5232watchpoint_locations_match (struct bp_location *loc1,
5233 struct bp_location *loc2)
2d134ed3 5234{
2bdf28a0
JK
5235 /* Both of them must not be in moribund_locations. */
5236 gdb_assert (loc1->owner != NULL);
5237 gdb_assert (loc2->owner != NULL);
5238
4a64f543
MS
5239 /* If the target can evaluate the condition expression in hardware,
5240 then we we need to insert both watchpoints even if they are at
5241 the same place. Otherwise the watchpoint will only trigger when
5242 the condition of whichever watchpoint was inserted evaluates to
5243 true, not giving a chance for GDB to check the condition of the
5244 other watchpoint. */
0cf6dd15 5245 if ((loc1->owner->cond_exp
4a64f543
MS
5246 && target_can_accel_watchpoint_condition (loc1->address,
5247 loc1->length,
0cf6dd15
TJB
5248 loc1->watchpoint_type,
5249 loc1->owner->cond_exp))
5250 || (loc2->owner->cond_exp
4a64f543
MS
5251 && target_can_accel_watchpoint_condition (loc2->address,
5252 loc2->length,
0cf6dd15
TJB
5253 loc2->watchpoint_type,
5254 loc2->owner->cond_exp)))
5255 return 0;
5256
85d721b8
PA
5257 /* Note that this checks the owner's type, not the location's. In
5258 case the target does not support read watchpoints, but does
5259 support access watchpoints, we'll have bp_read_watchpoint
5260 watchpoints with hw_access locations. Those should be considered
5261 duplicates of hw_read locations. The hw_read locations will
5262 become hw_access locations later. */
2d134ed3
PA
5263 return (loc1->owner->type == loc2->owner->type
5264 && loc1->pspace->aspace == loc2->pspace->aspace
5265 && loc1->address == loc2->address
5266 && loc1->length == loc2->length);
e4f237da
KB
5267}
5268
6c95b8df
PA
5269/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5270 same breakpoint location. In most targets, this can only be true
5271 if ASPACE1 matches ASPACE2. On targets that have global
5272 breakpoints, the address space doesn't really matter. */
5273
5274static int
5275breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5276 struct address_space *aspace2, CORE_ADDR addr2)
5277{
5278 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5279 || aspace1 == aspace2)
5280 && addr1 == addr2);
5281}
5282
f1310107
TJB
5283/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5284 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5285 matches ASPACE2. On targets that have global breakpoints, the address
5286 space doesn't really matter. */
5287
5288static int
5289breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5290 int len1, struct address_space *aspace2,
5291 CORE_ADDR addr2)
5292{
5293 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5294 || aspace1 == aspace2)
5295 && addr2 >= addr1 && addr2 < addr1 + len1);
5296}
5297
5298/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5299 a ranged breakpoint. In most targets, a match happens only if ASPACE
5300 matches the breakpoint's address space. On targets that have global
5301 breakpoints, the address space doesn't really matter. */
5302
5303static int
5304breakpoint_location_address_match (struct bp_location *bl,
5305 struct address_space *aspace,
5306 CORE_ADDR addr)
5307{
5308 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5309 aspace, addr)
5310 || (bl->length
5311 && breakpoint_address_match_range (bl->pspace->aspace,
5312 bl->address, bl->length,
5313 aspace, addr)));
5314}
5315
2d134ed3
PA
5316/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5317 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5318 represent the same location. */
5319
5320static int
4a64f543
MS
5321breakpoint_locations_match (struct bp_location *loc1,
5322 struct bp_location *loc2)
2d134ed3 5323{
2bdf28a0
JK
5324 int hw_point1, hw_point2;
5325
5326 /* Both of them must not be in moribund_locations. */
5327 gdb_assert (loc1->owner != NULL);
5328 gdb_assert (loc2->owner != NULL);
5329
5330 hw_point1 = is_hardware_watchpoint (loc1->owner);
5331 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5332
5333 if (hw_point1 != hw_point2)
5334 return 0;
5335 else if (hw_point1)
5336 return watchpoint_locations_match (loc1, loc2);
5337 else
f1310107
TJB
5338 /* We compare bp_location.length in order to cover ranged breakpoints. */
5339 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5340 loc2->pspace->aspace, loc2->address)
5341 && loc1->length == loc2->length);
2d134ed3
PA
5342}
5343
76897487
KB
5344static void
5345breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5346 int bnum, int have_bnum)
5347{
f63fbe86
MS
5348 /* The longest string possibly returned by hex_string_custom
5349 is 50 chars. These must be at least that big for safety. */
5350 char astr1[64];
5351 char astr2[64];
76897487 5352
bb599908
PH
5353 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5354 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5355 if (have_bnum)
8a3fe4f8 5356 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5357 bnum, astr1, astr2);
5358 else
8a3fe4f8 5359 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5360}
5361
4a64f543
MS
5362/* Adjust a breakpoint's address to account for architectural
5363 constraints on breakpoint placement. Return the adjusted address.
5364 Note: Very few targets require this kind of adjustment. For most
5365 targets, this function is simply the identity function. */
76897487
KB
5366
5367static CORE_ADDR
a6d9a66e
UW
5368adjust_breakpoint_address (struct gdbarch *gdbarch,
5369 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5370{
a6d9a66e 5371 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5372 {
5373 /* Very few targets need any kind of breakpoint adjustment. */
5374 return bpaddr;
5375 }
88f7da05
KB
5376 else if (bptype == bp_watchpoint
5377 || bptype == bp_hardware_watchpoint
5378 || bptype == bp_read_watchpoint
5379 || bptype == bp_access_watchpoint
fe798b75 5380 || bptype == bp_catchpoint)
88f7da05
KB
5381 {
5382 /* Watchpoints and the various bp_catch_* eventpoints should not
5383 have their addresses modified. */
5384 return bpaddr;
5385 }
76897487
KB
5386 else
5387 {
5388 CORE_ADDR adjusted_bpaddr;
5389
5390 /* Some targets have architectural constraints on the placement
5391 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5392 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5393
5394 /* An adjusted breakpoint address can significantly alter
5395 a user's expectations. Print a warning if an adjustment
5396 is required. */
5397 if (adjusted_bpaddr != bpaddr)
5398 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5399
5400 return adjusted_bpaddr;
5401 }
5402}
5403
28010a5d
PA
5404void
5405init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5406 struct breakpoint *owner)
7cc221ef 5407{
7cc221ef
DJ
5408 memset (loc, 0, sizeof (*loc));
5409
348d480f
PA
5410 gdb_assert (ops != NULL);
5411
28010a5d
PA
5412 loc->ops = ops;
5413 loc->owner = owner;
511a6cd4 5414 loc->cond = NULL;
0d381245
VP
5415 loc->shlib_disabled = 0;
5416 loc->enabled = 1;
e049a4b5 5417
28010a5d 5418 switch (owner->type)
e049a4b5
DJ
5419 {
5420 case bp_breakpoint:
5421 case bp_until:
5422 case bp_finish:
5423 case bp_longjmp:
5424 case bp_longjmp_resume:
186c406b
TT
5425 case bp_exception:
5426 case bp_exception_resume:
e049a4b5 5427 case bp_step_resume:
2c03e5be 5428 case bp_hp_step_resume:
e049a4b5
DJ
5429 case bp_watchpoint_scope:
5430 case bp_call_dummy:
aa7d318d 5431 case bp_std_terminate:
e049a4b5
DJ
5432 case bp_shlib_event:
5433 case bp_thread_event:
5434 case bp_overlay_event:
4efc6507 5435 case bp_jit_event:
0fd8e87f 5436 case bp_longjmp_master:
aa7d318d 5437 case bp_std_terminate_master:
186c406b 5438 case bp_exception_master:
0e30163f
JK
5439 case bp_gnu_ifunc_resolver:
5440 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5441 loc->loc_type = bp_loc_software_breakpoint;
5442 break;
5443 case bp_hardware_breakpoint:
5444 loc->loc_type = bp_loc_hardware_breakpoint;
5445 break;
5446 case bp_hardware_watchpoint:
5447 case bp_read_watchpoint:
5448 case bp_access_watchpoint:
5449 loc->loc_type = bp_loc_hardware_watchpoint;
5450 break;
5451 case bp_watchpoint:
ce78b96d 5452 case bp_catchpoint:
15c3d785
PA
5453 case bp_tracepoint:
5454 case bp_fast_tracepoint:
0fb4aa4b 5455 case bp_static_tracepoint:
e049a4b5
DJ
5456 loc->loc_type = bp_loc_other;
5457 break;
5458 default:
e2e0b3e5 5459 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5460 }
5461
f431efe5 5462 loc->refc = 1;
28010a5d
PA
5463}
5464
5465/* Allocate a struct bp_location. */
5466
5467static struct bp_location *
5468allocate_bp_location (struct breakpoint *bpt)
5469{
348d480f
PA
5470 return bpt->ops->allocate_location (bpt);
5471}
7cc221ef 5472
f431efe5
PA
5473static void
5474free_bp_location (struct bp_location *loc)
fe3f5fa8 5475{
348d480f 5476 loc->ops->dtor (loc);
fe3f5fa8
VP
5477 xfree (loc);
5478}
5479
f431efe5
PA
5480/* Increment reference count. */
5481
5482static void
5483incref_bp_location (struct bp_location *bl)
5484{
5485 ++bl->refc;
5486}
5487
5488/* Decrement reference count. If the reference count reaches 0,
5489 destroy the bp_location. Sets *BLP to NULL. */
5490
5491static void
5492decref_bp_location (struct bp_location **blp)
5493{
0807b50c
PA
5494 gdb_assert ((*blp)->refc > 0);
5495
f431efe5
PA
5496 if (--(*blp)->refc == 0)
5497 free_bp_location (*blp);
5498 *blp = NULL;
5499}
5500
346774a9 5501/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5502
346774a9
PA
5503static void
5504add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5505{
346774a9 5506 struct breakpoint *b1;
c906108c 5507
346774a9
PA
5508 /* Add this breakpoint to the end of the chain so that a list of
5509 breakpoints will come out in order of increasing numbers. */
5510
5511 b1 = breakpoint_chain;
5512 if (b1 == 0)
5513 breakpoint_chain = b;
5514 else
5515 {
5516 while (b1->next)
5517 b1 = b1->next;
5518 b1->next = b;
5519 }
5520}
5521
5522/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5523
5524static void
5525init_raw_breakpoint_without_location (struct breakpoint *b,
5526 struct gdbarch *gdbarch,
28010a5d
PA
5527 enum bptype bptype,
5528 struct breakpoint_ops *ops)
346774a9 5529{
c906108c 5530 memset (b, 0, sizeof (*b));
2219d63c 5531
348d480f
PA
5532 gdb_assert (ops != NULL);
5533
28010a5d 5534 b->ops = ops;
4d28f7a8 5535 b->type = bptype;
a6d9a66e 5536 b->gdbarch = gdbarch;
c906108c
SS
5537 b->language = current_language->la_language;
5538 b->input_radix = input_radix;
5539 b->thread = -1;
b5de0fa7 5540 b->enable_state = bp_enabled;
c906108c
SS
5541 b->next = 0;
5542 b->silent = 0;
5543 b->ignore_count = 0;
5544 b->commands = NULL;
818dd999 5545 b->frame_id = null_frame_id;
0d381245 5546 b->condition_not_parsed = 0;
84f4c1fe 5547 b->py_bp_object = NULL;
d0fb5eae 5548 b->related_breakpoint = b;
346774a9
PA
5549}
5550
5551/* Helper to set_raw_breakpoint below. Creates a breakpoint
5552 that has type BPTYPE and has no locations as yet. */
346774a9
PA
5553
5554static struct breakpoint *
5555set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f
PA
5556 enum bptype bptype,
5557 struct breakpoint_ops *ops)
346774a9
PA
5558{
5559 struct breakpoint *b = XNEW (struct breakpoint);
5560
348d480f 5561 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 5562 add_to_breakpoint_chain (b);
0d381245
VP
5563 return b;
5564}
5565
0e30163f
JK
5566/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5567 resolutions should be made as the user specified the location explicitly
5568 enough. */
5569
0d381245 5570static void
0e30163f 5571set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5572{
2bdf28a0
JK
5573 gdb_assert (loc->owner != NULL);
5574
0d381245 5575 if (loc->owner->type == bp_breakpoint
1042e4c0 5576 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5577 || is_tracepoint (loc->owner))
0d381245 5578 {
0e30163f
JK
5579 int is_gnu_ifunc;
5580
5581 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5582 NULL, NULL, &is_gnu_ifunc);
5583
5584 if (is_gnu_ifunc && !explicit_loc)
5585 {
5586 struct breakpoint *b = loc->owner;
5587
5588 gdb_assert (loc->pspace == current_program_space);
5589 if (gnu_ifunc_resolve_name (loc->function_name,
5590 &loc->requested_address))
5591 {
5592 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5593 loc->address = adjust_breakpoint_address (loc->gdbarch,
5594 loc->requested_address,
5595 b->type);
5596 }
5597 else if (b->type == bp_breakpoint && b->loc == loc
5598 && loc->next == NULL && b->related_breakpoint == b)
5599 {
5600 /* Create only the whole new breakpoint of this type but do not
5601 mess more complicated breakpoints with multiple locations. */
5602 b->type = bp_gnu_ifunc_resolver;
5603 }
5604 }
5605
0d381245
VP
5606 if (loc->function_name)
5607 loc->function_name = xstrdup (loc->function_name);
5608 }
5609}
5610
a6d9a66e
UW
5611/* Attempt to determine architecture of location identified by SAL. */
5612static struct gdbarch *
5613get_sal_arch (struct symtab_and_line sal)
5614{
5615 if (sal.section)
5616 return get_objfile_arch (sal.section->objfile);
5617 if (sal.symtab)
5618 return get_objfile_arch (sal.symtab->objfile);
5619
5620 return NULL;
5621}
5622
346774a9
PA
5623/* Low level routine for partially initializing a breakpoint of type
5624 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5625 file name, and line number are provided by SAL.
0d381245
VP
5626
5627 It is expected that the caller will complete the initialization of
5628 the newly created breakpoint struct as well as output any status
c56053d2 5629 information regarding the creation of a new breakpoint. */
0d381245 5630
346774a9
PA
5631static void
5632init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d
PA
5633 struct symtab_and_line sal, enum bptype bptype,
5634 struct breakpoint_ops *ops)
0d381245 5635{
0d381245 5636 CORE_ADDR adjusted_address;
a6d9a66e
UW
5637 struct gdbarch *loc_gdbarch;
5638
28010a5d 5639 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5640
a6d9a66e
UW
5641 loc_gdbarch = get_sal_arch (sal);
5642 if (!loc_gdbarch)
5643 loc_gdbarch = b->gdbarch;
0d381245 5644
6c95b8df
PA
5645 if (bptype != bp_catchpoint)
5646 gdb_assert (sal.pspace != NULL);
5647
0d381245
VP
5648 /* Adjust the breakpoint's address prior to allocating a location.
5649 Once we call allocate_bp_location(), that mostly uninitialized
5650 location will be placed on the location chain. Adjustment of the
8defab1a 5651 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
5652 not want its scan of the location chain to find a breakpoint and
5653 location that's only been partially initialized. */
4a64f543
MS
5654 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5655 sal.pc, b->type);
0d381245 5656
39d61571 5657 b->loc = allocate_bp_location (b);
a6d9a66e 5658 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
5659 b->loc->requested_address = sal.pc;
5660 b->loc->address = adjusted_address;
6c95b8df
PA
5661 b->loc->pspace = sal.pspace;
5662
5663 /* Store the program space that was used to set the breakpoint, for
5664 breakpoint resetting. */
5665 b->pspace = sal.pspace;
0d381245
VP
5666
5667 if (sal.symtab == NULL)
5668 b->source_file = NULL;
5669 else
1b36a34b 5670 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5671 b->loc->section = sal.section;
5672 b->line_number = sal.line;
5673
0e30163f
JK
5674 set_breakpoint_location_function (b->loc,
5675 sal.explicit_pc || sal.explicit_line);
c906108c 5676
c906108c 5677 breakpoints_changed ();
346774a9 5678}
c906108c 5679
346774a9
PA
5680/* set_raw_breakpoint is a low level routine for allocating and
5681 partially initializing a breakpoint of type BPTYPE. The newly
5682 created breakpoint's address, section, source file name, and line
5683 number are provided by SAL. The newly created and partially
5684 initialized breakpoint is added to the breakpoint chain and
5685 is also returned as the value of this function.
5686
5687 It is expected that the caller will complete the initialization of
5688 the newly created breakpoint struct as well as output any status
5689 information regarding the creation of a new breakpoint. In
5690 particular, set_raw_breakpoint does NOT set the breakpoint
5691 number! Care should be taken to not allow an error to occur
5692 prior to completing the initialization of the breakpoint. If this
5693 should happen, a bogus breakpoint will be left on the chain. */
5694
5695struct breakpoint *
5696set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f
PA
5697 struct symtab_and_line sal, enum bptype bptype,
5698 struct breakpoint_ops *ops)
346774a9
PA
5699{
5700 struct breakpoint *b = XNEW (struct breakpoint);
5701
348d480f 5702 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 5703 add_to_breakpoint_chain (b);
c906108c
SS
5704 return b;
5705}
5706
c2c6d25f
JM
5707
5708/* Note that the breakpoint object B describes a permanent breakpoint
5709 instruction, hard-wired into the inferior's code. */
5710void
5711make_breakpoint_permanent (struct breakpoint *b)
5712{
0d381245 5713 struct bp_location *bl;
cc59ec59 5714
b5de0fa7 5715 b->enable_state = bp_permanent;
c2c6d25f 5716
4a64f543
MS
5717 /* By definition, permanent breakpoints are already present in the
5718 code. Mark all locations as inserted. For now,
5719 make_breakpoint_permanent is called in just one place, so it's
5720 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 5721 multiple locations or not, but it's easy to implement. */
0d381245
VP
5722 for (bl = b->loc; bl; bl = bl->next)
5723 bl->inserted = 1;
c2c6d25f
JM
5724}
5725
53a5351d 5726/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5727 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5728 initiated the operation. */
c906108c
SS
5729
5730void
186c406b 5731set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5732{
35df4500 5733 struct breakpoint *b, *b_tmp;
186c406b 5734 int thread = tp->num;
0fd8e87f
UW
5735
5736 /* To avoid having to rescan all objfile symbols at every step,
5737 we maintain a list of continually-inserted but always disabled
5738 longjmp "master" breakpoints. Here, we simply create momentary
5739 clones of those and enable them for the requested thread. */
35df4500 5740 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5741 if (b->pspace == current_program_space
186c406b
TT
5742 && (b->type == bp_longjmp_master
5743 || b->type == bp_exception_master))
0fd8e87f 5744 {
06edf0c0
PA
5745 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5746 struct breakpoint *clone;
cc59ec59 5747
06edf0c0
PA
5748 clone = momentary_breakpoint_from_master (b, type,
5749 &momentary_breakpoint_ops);
0fd8e87f
UW
5750 clone->thread = thread;
5751 }
186c406b
TT
5752
5753 tp->initiating_frame = frame;
c906108c
SS
5754}
5755
611c83ae 5756/* Delete all longjmp breakpoints from THREAD. */
c906108c 5757void
611c83ae 5758delete_longjmp_breakpoint (int thread)
c906108c 5759{
35df4500 5760 struct breakpoint *b, *b_tmp;
c906108c 5761
35df4500 5762 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5763 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5764 {
5765 if (b->thread == thread)
5766 delete_breakpoint (b);
5767 }
c906108c
SS
5768}
5769
1900040c
MS
5770void
5771enable_overlay_breakpoints (void)
5772{
52f0bd74 5773 struct breakpoint *b;
1900040c
MS
5774
5775 ALL_BREAKPOINTS (b)
5776 if (b->type == bp_overlay_event)
5777 {
5778 b->enable_state = bp_enabled;
b60e7edf 5779 update_global_location_list (1);
c02f5703 5780 overlay_events_enabled = 1;
1900040c
MS
5781 }
5782}
5783
5784void
5785disable_overlay_breakpoints (void)
5786{
52f0bd74 5787 struct breakpoint *b;
1900040c
MS
5788
5789 ALL_BREAKPOINTS (b)
5790 if (b->type == bp_overlay_event)
5791 {
5792 b->enable_state = bp_disabled;
b60e7edf 5793 update_global_location_list (0);
c02f5703 5794 overlay_events_enabled = 0;
1900040c
MS
5795 }
5796}
5797
aa7d318d
TT
5798/* Set an active std::terminate breakpoint for each std::terminate
5799 master breakpoint. */
5800void
5801set_std_terminate_breakpoint (void)
5802{
35df4500 5803 struct breakpoint *b, *b_tmp;
aa7d318d 5804
35df4500 5805 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5806 if (b->pspace == current_program_space
5807 && b->type == bp_std_terminate_master)
5808 {
06edf0c0
PA
5809 momentary_breakpoint_from_master (b, bp_std_terminate,
5810 &momentary_breakpoint_ops);
aa7d318d
TT
5811 }
5812}
5813
5814/* Delete all the std::terminate breakpoints. */
5815void
5816delete_std_terminate_breakpoint (void)
5817{
35df4500 5818 struct breakpoint *b, *b_tmp;
aa7d318d 5819
35df4500 5820 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5821 if (b->type == bp_std_terminate)
5822 delete_breakpoint (b);
5823}
5824
c4093a6a 5825struct breakpoint *
a6d9a66e 5826create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
5827{
5828 struct breakpoint *b;
c4093a6a 5829
06edf0c0
PA
5830 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
5831 &internal_breakpoint_ops);
5832
b5de0fa7 5833 b->enable_state = bp_enabled;
c4093a6a 5834 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
5835 b->addr_string
5836 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 5837
b60e7edf 5838 update_global_location_list_nothrow (1);
74960c60 5839
c4093a6a
JM
5840 return b;
5841}
5842
5843void
5844remove_thread_event_breakpoints (void)
5845{
35df4500 5846 struct breakpoint *b, *b_tmp;
c4093a6a 5847
35df4500 5848 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5849 if (b->type == bp_thread_event
5850 && b->loc->pspace == current_program_space)
c4093a6a
JM
5851 delete_breakpoint (b);
5852}
5853
0101ce28
JJ
5854struct lang_and_radix
5855 {
5856 enum language lang;
5857 int radix;
5858 };
5859
4efc6507
DE
5860/* Create a breakpoint for JIT code registration and unregistration. */
5861
5862struct breakpoint *
5863create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5864{
5865 struct breakpoint *b;
5866
06edf0c0
PA
5867 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
5868 &internal_breakpoint_ops);
4efc6507
DE
5869 update_global_location_list_nothrow (1);
5870 return b;
5871}
0101ce28 5872
03673fc7
PP
5873/* Remove JIT code registration and unregistration breakpoint(s). */
5874
5875void
5876remove_jit_event_breakpoints (void)
5877{
5878 struct breakpoint *b, *b_tmp;
5879
5880 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5881 if (b->type == bp_jit_event
5882 && b->loc->pspace == current_program_space)
5883 delete_breakpoint (b);
5884}
5885
cae688ec
JJ
5886void
5887remove_solib_event_breakpoints (void)
5888{
35df4500 5889 struct breakpoint *b, *b_tmp;
cae688ec 5890
35df4500 5891 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5892 if (b->type == bp_shlib_event
5893 && b->loc->pspace == current_program_space)
cae688ec
JJ
5894 delete_breakpoint (b);
5895}
5896
5897struct breakpoint *
a6d9a66e 5898create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
5899{
5900 struct breakpoint *b;
5901
06edf0c0
PA
5902 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
5903 &internal_breakpoint_ops);
b60e7edf 5904 update_global_location_list_nothrow (1);
cae688ec
JJ
5905 return b;
5906}
5907
5908/* Disable any breakpoints that are on code in shared libraries. Only
5909 apply to enabled breakpoints, disabled ones can just stay disabled. */
5910
5911void
cb851954 5912disable_breakpoints_in_shlibs (void)
cae688ec 5913{
876fa593 5914 struct bp_location *loc, **locp_tmp;
cae688ec 5915
876fa593 5916 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 5917 {
2bdf28a0 5918 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 5919 struct breakpoint *b = loc->owner;
2bdf28a0 5920
4a64f543
MS
5921 /* We apply the check to all breakpoints, including disabled for
5922 those with loc->duplicate set. This is so that when breakpoint
5923 becomes enabled, or the duplicate is removed, gdb will try to
5924 insert all breakpoints. If we don't set shlib_disabled here,
5925 we'll try to insert those breakpoints and fail. */
1042e4c0 5926 if (((b->type == bp_breakpoint)
508ccb1f 5927 || (b->type == bp_jit_event)
1042e4c0 5928 || (b->type == bp_hardware_breakpoint)
d77f58be 5929 || (is_tracepoint (b)))
6c95b8df 5930 && loc->pspace == current_program_space
0d381245 5931 && !loc->shlib_disabled
a77053c2 5932#ifdef PC_SOLIB
0d381245 5933 && PC_SOLIB (loc->address)
a77053c2 5934#else
6c95b8df 5935 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
5936#endif
5937 )
0d381245
VP
5938 {
5939 loc->shlib_disabled = 1;
5940 }
cae688ec
JJ
5941 }
5942}
5943
7a9dd1b2 5944/* Disable any breakpoints that are in an unloaded shared library.
4a64f543
MS
5945 Only apply to enabled breakpoints, disabled ones can just stay
5946 disabled. */
84acb35a 5947
75149521 5948static void
84acb35a
JJ
5949disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
5950{
876fa593 5951 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
5952 int disabled_shlib_breaks = 0;
5953
c86cf029
VP
5954 /* SunOS a.out shared libraries are always mapped, so do not
5955 disable breakpoints; they will only be reported as unloaded
5956 through clear_solib when GDB discards its shared library
5957 list. See clear_solib for more information. */
5958 if (exec_bfd != NULL
5959 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
5960 return;
5961
876fa593 5962 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 5963 {
2bdf28a0 5964 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 5965 struct breakpoint *b = loc->owner;
cc59ec59 5966
0d381245
VP
5967 if ((loc->loc_type == bp_loc_hardware_breakpoint
5968 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 5969 && solib->pspace == loc->pspace
e2dd7057 5970 && !loc->shlib_disabled
508ccb1f
TT
5971 && (b->type == bp_breakpoint
5972 || b->type == bp_jit_event
5973 || b->type == bp_hardware_breakpoint)
e2dd7057 5974 && solib_contains_address_p (solib, loc->address))
84acb35a 5975 {
e2dd7057
PP
5976 loc->shlib_disabled = 1;
5977 /* At this point, we cannot rely on remove_breakpoint
5978 succeeding so we must mark the breakpoint as not inserted
5979 to prevent future errors occurring in remove_breakpoints. */
5980 loc->inserted = 0;
8d3788bd
VP
5981
5982 /* This may cause duplicate notifications for the same breakpoint. */
5983 observer_notify_breakpoint_modified (b);
5984
e2dd7057
PP
5985 if (!disabled_shlib_breaks)
5986 {
5987 target_terminal_ours_for_output ();
3e43a32a
MS
5988 warning (_("Temporarily disabling breakpoints "
5989 "for unloaded shared library \"%s\""),
e2dd7057 5990 solib->so_name);
84acb35a 5991 }
e2dd7057 5992 disabled_shlib_breaks = 1;
84acb35a
JJ
5993 }
5994 }
84acb35a
JJ
5995}
5996
ce78b96d
JB
5997/* FORK & VFORK catchpoints. */
5998
e29a4733
PA
5999/* An instance of this type is used to represent a fork or vfork
6000 catchpoint. It includes a "struct breakpoint" as a kind of base
6001 class; users downcast to "struct breakpoint *" when needed. A
6002 breakpoint is really of this type iff its ops pointer points to
6003 CATCH_FORK_BREAKPOINT_OPS. */
6004
6005struct fork_catchpoint
6006{
6007 /* The base class. */
6008 struct breakpoint base;
6009
6010 /* Process id of a child process whose forking triggered this
6011 catchpoint. This field is only valid immediately after this
6012 catchpoint has triggered. */
6013 ptid_t forked_inferior_pid;
6014};
6015
4a64f543
MS
6016/* Implement the "insert" breakpoint_ops method for fork
6017 catchpoints. */
ce78b96d 6018
77b06cd7
TJB
6019static int
6020insert_catch_fork (struct bp_location *bl)
ce78b96d 6021{
77b06cd7 6022 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6023}
6024
4a64f543
MS
6025/* Implement the "remove" breakpoint_ops method for fork
6026 catchpoints. */
ce78b96d
JB
6027
6028static int
77b06cd7 6029remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6030{
6031 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6032}
6033
6034/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6035 catchpoints. */
6036
6037static int
f1310107
TJB
6038breakpoint_hit_catch_fork (const struct bp_location *bl,
6039 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6040{
e29a4733
PA
6041 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6042
6043 return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6044}
6045
4a64f543
MS
6046/* Implement the "print_it" breakpoint_ops method for fork
6047 catchpoints. */
ce78b96d
JB
6048
6049static enum print_stop_action
348d480f 6050print_it_catch_fork (bpstat bs)
ce78b96d 6051{
348d480f
PA
6052 struct breakpoint *b = bs->breakpoint_at;
6053 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 6054
ce78b96d
JB
6055 annotate_catchpoint (b->number);
6056 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
e29a4733 6057 b->number, ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6058 return PRINT_SRC_AND_LOC;
6059}
6060
4a64f543
MS
6061/* Implement the "print_one" breakpoint_ops method for fork
6062 catchpoints. */
ce78b96d
JB
6063
6064static void
a6d9a66e 6065print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6066{
e29a4733 6067 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d
TT
6068 struct value_print_options opts;
6069
6070 get_user_print_options (&opts);
6071
4a64f543
MS
6072 /* Field 4, the address, is omitted (which makes the columns not
6073 line up too nicely with the headers, but the effect is relatively
6074 readable). */
79a45b7d 6075 if (opts.addressprint)
ce78b96d
JB
6076 ui_out_field_skip (uiout, "addr");
6077 annotate_field (5);
6078 ui_out_text (uiout, "fork");
e29a4733 6079 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6080 {
6081 ui_out_text (uiout, ", process ");
6082 ui_out_field_int (uiout, "what",
e29a4733 6083 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6084 ui_out_spaces (uiout, 1);
6085 }
6086}
6087
6088/* Implement the "print_mention" breakpoint_ops method for fork
6089 catchpoints. */
6090
6091static void
6092print_mention_catch_fork (struct breakpoint *b)
6093{
6094 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6095}
6096
6149aea9
PA
6097/* Implement the "print_recreate" breakpoint_ops method for fork
6098 catchpoints. */
6099
6100static void
6101print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6102{
6103 fprintf_unfiltered (fp, "catch fork");
6104}
6105
ce78b96d
JB
6106/* The breakpoint_ops structure to be used in fork catchpoints. */
6107
6108static struct breakpoint_ops catch_fork_breakpoint_ops =
6109{
348d480f
PA
6110 bkpt_dtor,
6111 bkpt_allocate_location,
6112 null_re_set,
ce78b96d
JB
6113 insert_catch_fork,
6114 remove_catch_fork,
6115 breakpoint_hit_catch_fork,
348d480f
PA
6116 null_check_status,
6117 null_resources_needed,
6118 null_works_in_software_mode,
ce78b96d
JB
6119 print_it_catch_fork,
6120 print_one_catch_fork,
348d480f 6121 null_print_one_detail,
6149aea9
PA
6122 print_mention_catch_fork,
6123 print_recreate_catch_fork
ce78b96d
JB
6124};
6125
4a64f543
MS
6126/* Implement the "insert" breakpoint_ops method for vfork
6127 catchpoints. */
ce78b96d 6128
77b06cd7
TJB
6129static int
6130insert_catch_vfork (struct bp_location *bl)
ce78b96d 6131{
77b06cd7 6132 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6133}
6134
4a64f543
MS
6135/* Implement the "remove" breakpoint_ops method for vfork
6136 catchpoints. */
ce78b96d
JB
6137
6138static int
77b06cd7 6139remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6140{
6141 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6142}
6143
6144/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6145 catchpoints. */
6146
6147static int
f1310107
TJB
6148breakpoint_hit_catch_vfork (const struct bp_location *bl,
6149 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6150{
e29a4733
PA
6151 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6152
6153 return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6154}
6155
4a64f543
MS
6156/* Implement the "print_it" breakpoint_ops method for vfork
6157 catchpoints. */
ce78b96d
JB
6158
6159static enum print_stop_action
348d480f 6160print_it_catch_vfork (bpstat bs)
ce78b96d 6161{
348d480f 6162 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
6163 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6164
ce78b96d
JB
6165 annotate_catchpoint (b->number);
6166 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
e29a4733 6167 b->number, ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6168 return PRINT_SRC_AND_LOC;
6169}
6170
4a64f543
MS
6171/* Implement the "print_one" breakpoint_ops method for vfork
6172 catchpoints. */
ce78b96d
JB
6173
6174static void
a6d9a66e 6175print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6176{
e29a4733 6177 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d
TT
6178 struct value_print_options opts;
6179
6180 get_user_print_options (&opts);
4a64f543
MS
6181 /* Field 4, the address, is omitted (which makes the columns not
6182 line up too nicely with the headers, but the effect is relatively
6183 readable). */
79a45b7d 6184 if (opts.addressprint)
ce78b96d
JB
6185 ui_out_field_skip (uiout, "addr");
6186 annotate_field (5);
6187 ui_out_text (uiout, "vfork");
e29a4733 6188 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6189 {
6190 ui_out_text (uiout, ", process ");
6191 ui_out_field_int (uiout, "what",
e29a4733 6192 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6193 ui_out_spaces (uiout, 1);
6194 }
6195}
6196
6197/* Implement the "print_mention" breakpoint_ops method for vfork
6198 catchpoints. */
6199
6200static void
6201print_mention_catch_vfork (struct breakpoint *b)
6202{
6203 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6204}
6205
6149aea9
PA
6206/* Implement the "print_recreate" breakpoint_ops method for vfork
6207 catchpoints. */
6208
6209static void
6210print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6211{
6212 fprintf_unfiltered (fp, "catch vfork");
6213}
6214
ce78b96d
JB
6215/* The breakpoint_ops structure to be used in vfork catchpoints. */
6216
6217static struct breakpoint_ops catch_vfork_breakpoint_ops =
6218{
348d480f
PA
6219 bkpt_dtor,
6220 bkpt_allocate_location,
6221 null_re_set,
ce78b96d
JB
6222 insert_catch_vfork,
6223 remove_catch_vfork,
6224 breakpoint_hit_catch_vfork,
348d480f
PA
6225 null_check_status,
6226 null_resources_needed,
6227 null_works_in_software_mode,
ce78b96d
JB
6228 print_it_catch_vfork,
6229 print_one_catch_vfork,
348d480f 6230 null_print_one_detail,
6149aea9
PA
6231 print_mention_catch_vfork,
6232 print_recreate_catch_vfork
ce78b96d
JB
6233};
6234
be5c67c1
PA
6235/* An instance of this type is used to represent a syscall catchpoint.
6236 It includes a "struct breakpoint" as a kind of base class; users
6237 downcast to "struct breakpoint *" when needed. A breakpoint is
6238 really of this type iff its ops pointer points to
6239 CATCH_SYSCALL_BREAKPOINT_OPS. */
6240
6241struct syscall_catchpoint
6242{
6243 /* The base class. */
6244 struct breakpoint base;
6245
6246 /* Syscall numbers used for the 'catch syscall' feature. If no
6247 syscall has been specified for filtering, its value is NULL.
6248 Otherwise, it holds a list of all syscalls to be caught. The
6249 list elements are allocated with xmalloc. */
6250 VEC(int) *syscalls_to_be_caught;
6251};
6252
6253/* Implement the "dtor" breakpoint_ops method for syscall
6254 catchpoints. */
6255
6256static void
6257dtor_catch_syscall (struct breakpoint *b)
6258{
6259 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6260
6261 VEC_free (int, c->syscalls_to_be_caught);
348d480f
PA
6262
6263 bkpt_dtor (b);
be5c67c1
PA
6264}
6265
a96d9b2e
SDJ
6266/* Implement the "insert" breakpoint_ops method for syscall
6267 catchpoints. */
6268
77b06cd7
TJB
6269static int
6270insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6271{
be5c67c1 6272 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6273 struct inferior *inf = current_inferior ();
6274
6275 ++inf->total_syscalls_count;
be5c67c1 6276 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6277 ++inf->any_syscall_count;
6278 else
6279 {
6280 int i, iter;
cc59ec59 6281
a96d9b2e 6282 for (i = 0;
be5c67c1 6283 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6284 i++)
6285 {
6286 int elem;
cc59ec59 6287
a96d9b2e
SDJ
6288 if (iter >= VEC_length (int, inf->syscalls_counts))
6289 {
6290 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6291 uintptr_t vec_addr_offset
6292 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6293 uintptr_t vec_addr;
6294 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6295 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6296 vec_addr_offset;
6297 memset ((void *) vec_addr, 0,
6298 (iter + 1 - old_size) * sizeof (int));
6299 }
6300 elem = VEC_index (int, inf->syscalls_counts, iter);
6301 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6302 }
6303 }
6304
77b06cd7
TJB
6305 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6306 inf->total_syscalls_count != 0,
6307 inf->any_syscall_count,
6308 VEC_length (int, inf->syscalls_counts),
6309 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6310}
6311
6312/* Implement the "remove" breakpoint_ops method for syscall
6313 catchpoints. */
6314
6315static int
77b06cd7 6316remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6317{
be5c67c1 6318 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6319 struct inferior *inf = current_inferior ();
6320
6321 --inf->total_syscalls_count;
be5c67c1 6322 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6323 --inf->any_syscall_count;
6324 else
6325 {
6326 int i, iter;
cc59ec59 6327
a96d9b2e 6328 for (i = 0;
be5c67c1 6329 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6330 i++)
6331 {
6332 int elem;
6333 if (iter >= VEC_length (int, inf->syscalls_counts))
6334 /* Shouldn't happen. */
6335 continue;
6336 elem = VEC_index (int, inf->syscalls_counts, iter);
6337 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6338 }
6339 }
6340
6341 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6342 inf->total_syscalls_count != 0,
6343 inf->any_syscall_count,
6344 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6345 VEC_address (int,
6346 inf->syscalls_counts));
a96d9b2e
SDJ
6347}
6348
6349/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6350 catchpoints. */
6351
6352static int
f1310107
TJB
6353breakpoint_hit_catch_syscall (const struct bp_location *bl,
6354 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6355{
4a64f543
MS
6356 /* We must check if we are catching specific syscalls in this
6357 breakpoint. If we are, then we must guarantee that the called
6358 syscall is the same syscall we are catching. */
a96d9b2e 6359 int syscall_number = 0;
be5c67c1
PA
6360 const struct syscall_catchpoint *c
6361 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6362
6363 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6364 return 0;
6365
6366 /* Now, checking if the syscall is the same. */
be5c67c1 6367 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6368 {
6369 int i, iter;
cc59ec59 6370
a96d9b2e 6371 for (i = 0;
be5c67c1 6372 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6373 i++)
6374 if (syscall_number == iter)
6375 break;
6376 /* Not the same. */
6377 if (!iter)
6378 return 0;
6379 }
6380
6381 return 1;
6382}
6383
6384/* Implement the "print_it" breakpoint_ops method for syscall
6385 catchpoints. */
6386
6387static enum print_stop_action
348d480f 6388print_it_catch_syscall (bpstat bs)
a96d9b2e 6389{
348d480f 6390 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
6391 /* These are needed because we want to know in which state a
6392 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6393 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6394 must print "called syscall" or "returned from syscall". */
6395 ptid_t ptid;
6396 struct target_waitstatus last;
6397 struct syscall s;
6398 struct cleanup *old_chain;
6399 char *syscall_id;
6400
6401 get_last_target_status (&ptid, &last);
6402
6403 get_syscall_by_number (last.value.syscall_number, &s);
6404
6405 annotate_catchpoint (b->number);
6406
6407 if (s.name == NULL)
6408 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6409 else
6410 syscall_id = xstrprintf ("'%s'", s.name);
6411
6412 old_chain = make_cleanup (xfree, syscall_id);
6413
6414 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6415 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6416 b->number, syscall_id);
6417 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6418 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6419 b->number, syscall_id);
6420
6421 do_cleanups (old_chain);
6422
6423 return PRINT_SRC_AND_LOC;
6424}
6425
6426/* Implement the "print_one" breakpoint_ops method for syscall
6427 catchpoints. */
6428
6429static void
6430print_one_catch_syscall (struct breakpoint *b,
f1310107 6431 struct bp_location **last_loc)
a96d9b2e 6432{
be5c67c1 6433 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e
SDJ
6434 struct value_print_options opts;
6435
6436 get_user_print_options (&opts);
4a64f543
MS
6437 /* Field 4, the address, is omitted (which makes the columns not
6438 line up too nicely with the headers, but the effect is relatively
6439 readable). */
a96d9b2e
SDJ
6440 if (opts.addressprint)
6441 ui_out_field_skip (uiout, "addr");
6442 annotate_field (5);
6443
be5c67c1
PA
6444 if (c->syscalls_to_be_caught
6445 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6446 ui_out_text (uiout, "syscalls \"");
6447 else
6448 ui_out_text (uiout, "syscall \"");
6449
be5c67c1 6450 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6451 {
6452 int i, iter;
6453 char *text = xstrprintf ("%s", "");
cc59ec59 6454
a96d9b2e 6455 for (i = 0;
be5c67c1 6456 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6457 i++)
6458 {
6459 char *x = text;
6460 struct syscall s;
6461 get_syscall_by_number (iter, &s);
6462
6463 if (s.name != NULL)
6464 text = xstrprintf ("%s%s, ", text, s.name);
6465 else
6466 text = xstrprintf ("%s%d, ", text, iter);
6467
6468 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 6469 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
6470 on every call. */
6471 xfree (x);
6472 }
6473 /* Remove the last comma. */
6474 text[strlen (text) - 2] = '\0';
6475 ui_out_field_string (uiout, "what", text);
6476 }
6477 else
6478 ui_out_field_string (uiout, "what", "<any syscall>");
6479 ui_out_text (uiout, "\" ");
6480}
6481
6482/* Implement the "print_mention" breakpoint_ops method for syscall
6483 catchpoints. */
6484
6485static void
6486print_mention_catch_syscall (struct breakpoint *b)
6487{
be5c67c1
PA
6488 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6489
6490 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6491 {
6492 int i, iter;
6493
be5c67c1 6494 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6495 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6496 else
6497 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6498
6499 for (i = 0;
be5c67c1 6500 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6501 i++)
6502 {
6503 struct syscall s;
6504 get_syscall_by_number (iter, &s);
6505
6506 if (s.name)
6507 printf_filtered (" '%s' [%d]", s.name, s.number);
6508 else
6509 printf_filtered (" %d", s.number);
6510 }
6511 printf_filtered (")");
6512 }
6513 else
6514 printf_filtered (_("Catchpoint %d (any syscall)"),
6515 b->number);
6516}
6517
6149aea9
PA
6518/* Implement the "print_recreate" breakpoint_ops method for syscall
6519 catchpoints. */
6520
6521static void
6522print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6523{
be5c67c1
PA
6524 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6525
6149aea9
PA
6526 fprintf_unfiltered (fp, "catch syscall");
6527
be5c67c1 6528 if (c->syscalls_to_be_caught)
6149aea9
PA
6529 {
6530 int i, iter;
6531
6532 for (i = 0;
be5c67c1 6533 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
6534 i++)
6535 {
6536 struct syscall s;
6537
6538 get_syscall_by_number (iter, &s);
6539 if (s.name)
6540 fprintf_unfiltered (fp, " %s", s.name);
6541 else
6542 fprintf_unfiltered (fp, " %d", s.number);
6543 }
6544 }
6545}
6546
a96d9b2e
SDJ
6547/* The breakpoint_ops structure to be used in syscall catchpoints. */
6548
6549static struct breakpoint_ops catch_syscall_breakpoint_ops =
6550{
be5c67c1 6551 dtor_catch_syscall,
348d480f
PA
6552 bkpt_allocate_location,
6553 null_re_set,
a96d9b2e
SDJ
6554 insert_catch_syscall,
6555 remove_catch_syscall,
6556 breakpoint_hit_catch_syscall,
348d480f
PA
6557 null_check_status,
6558 null_resources_needed,
6559 null_works_in_software_mode,
a96d9b2e
SDJ
6560 print_it_catch_syscall,
6561 print_one_catch_syscall,
348d480f 6562 null_print_one_detail,
6149aea9
PA
6563 print_mention_catch_syscall,
6564 print_recreate_catch_syscall
a96d9b2e
SDJ
6565};
6566
6567/* Returns non-zero if 'b' is a syscall catchpoint. */
6568
6569static int
6570syscall_catchpoint_p (struct breakpoint *b)
6571{
6572 return (b->ops == &catch_syscall_breakpoint_ops);
6573}
6574
346774a9
PA
6575/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
6576 is non-zero, then make the breakpoint temporary. If COND_STRING is
6577 not NULL, then store it in the breakpoint. OPS, if not NULL, is
6578 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 6579
346774a9
PA
6580static void
6581init_catchpoint (struct breakpoint *b,
6582 struct gdbarch *gdbarch, int tempflag,
6583 char *cond_string,
6584 struct breakpoint_ops *ops)
c906108c 6585{
c5aa993b 6586 struct symtab_and_line sal;
346774a9 6587
fe39c653 6588 init_sal (&sal);
6c95b8df 6589 sal.pspace = current_program_space;
c5aa993b 6590
28010a5d 6591 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 6592
1b36a34b 6593 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 6594 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
6595}
6596
28010a5d
PA
6597void
6598install_breakpoint (struct breakpoint *b)
c56053d2
PA
6599{
6600 add_to_breakpoint_chain (b);
6601 set_breakpoint_count (breakpoint_count + 1);
6602 b->number = breakpoint_count;
6603 mention (b);
6604 observer_notify_breakpoint_created (b);
6605 update_global_location_list (1);
6606}
6607
9b70b993 6608static void
a6d9a66e
UW
6609create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6610 int tempflag, char *cond_string,
ce78b96d 6611 struct breakpoint_ops *ops)
c906108c 6612{
e29a4733 6613 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 6614
e29a4733
PA
6615 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6616
6617 c->forked_inferior_pid = null_ptid;
6618
28010a5d 6619 install_breakpoint (&c->base);
c906108c
SS
6620}
6621
fe798b75
JB
6622/* Exec catchpoints. */
6623
b4d90040
PA
6624/* An instance of this type is used to represent an exec catchpoint.
6625 It includes a "struct breakpoint" as a kind of base class; users
6626 downcast to "struct breakpoint *" when needed. A breakpoint is
6627 really of this type iff its ops pointer points to
6628 CATCH_EXEC_BREAKPOINT_OPS. */
6629
6630struct exec_catchpoint
6631{
6632 /* The base class. */
6633 struct breakpoint base;
6634
6635 /* Filename of a program whose exec triggered this catchpoint.
6636 This field is only valid immediately after this catchpoint has
6637 triggered. */
6638 char *exec_pathname;
6639};
6640
6641/* Implement the "dtor" breakpoint_ops method for exec
6642 catchpoints. */
6643
6644static void
6645dtor_catch_exec (struct breakpoint *b)
6646{
6647 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6648
6649 xfree (c->exec_pathname);
348d480f
PA
6650
6651 bkpt_dtor (b);
b4d90040
PA
6652}
6653
77b06cd7
TJB
6654static int
6655insert_catch_exec (struct bp_location *bl)
c906108c 6656{
77b06cd7 6657 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6658}
c906108c 6659
fe798b75 6660static int
77b06cd7 6661remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6662{
6663 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6664}
c906108c 6665
fe798b75 6666static int
f1310107
TJB
6667breakpoint_hit_catch_exec (const struct bp_location *bl,
6668 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6669{
b4d90040
PA
6670 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6671
6672 return inferior_has_execd (inferior_ptid, &c->exec_pathname);
fe798b75 6673}
c906108c 6674
fe798b75 6675static enum print_stop_action
348d480f 6676print_it_catch_exec (bpstat bs)
fe798b75 6677{
348d480f 6678 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
6679 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6680
fe798b75
JB
6681 annotate_catchpoint (b->number);
6682 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
b4d90040 6683 c->exec_pathname);
fe798b75 6684 return PRINT_SRC_AND_LOC;
c906108c
SS
6685}
6686
fe798b75 6687static void
a6d9a66e 6688print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 6689{
b4d90040 6690 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75
JB
6691 struct value_print_options opts;
6692
6693 get_user_print_options (&opts);
6694
6695 /* Field 4, the address, is omitted (which makes the columns
6696 not line up too nicely with the headers, but the effect
6697 is relatively readable). */
6698 if (opts.addressprint)
6699 ui_out_field_skip (uiout, "addr");
6700 annotate_field (5);
6701 ui_out_text (uiout, "exec");
b4d90040 6702 if (c->exec_pathname != NULL)
fe798b75
JB
6703 {
6704 ui_out_text (uiout, ", program \"");
b4d90040 6705 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
6706 ui_out_text (uiout, "\" ");
6707 }
6708}
6709
6710static void
6711print_mention_catch_exec (struct breakpoint *b)
6712{
6713 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6714}
6715
6149aea9
PA
6716/* Implement the "print_recreate" breakpoint_ops method for exec
6717 catchpoints. */
6718
6719static void
6720print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6721{
6722 fprintf_unfiltered (fp, "catch exec");
6723}
6724
fe798b75
JB
6725static struct breakpoint_ops catch_exec_breakpoint_ops =
6726{
b4d90040 6727 dtor_catch_exec,
348d480f
PA
6728 bkpt_allocate_location,
6729 null_re_set,
fe798b75
JB
6730 insert_catch_exec,
6731 remove_catch_exec,
6732 breakpoint_hit_catch_exec,
348d480f
PA
6733 null_check_status,
6734 null_resources_needed,
6735 null_works_in_software_mode,
fe798b75
JB
6736 print_it_catch_exec,
6737 print_one_catch_exec,
348d480f 6738 null_print_one_detail,
6149aea9
PA
6739 print_mention_catch_exec,
6740 print_recreate_catch_exec
fe798b75
JB
6741};
6742
a96d9b2e
SDJ
6743static void
6744create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6745 struct breakpoint_ops *ops)
6746{
be5c67c1 6747 struct syscall_catchpoint *c;
a96d9b2e 6748 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 6749
be5c67c1
PA
6750 c = XNEW (struct syscall_catchpoint);
6751 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
6752 c->syscalls_to_be_caught = filter;
a96d9b2e 6753
28010a5d 6754 install_breakpoint (&c->base);
a96d9b2e
SDJ
6755}
6756
c906108c 6757static int
fba45db2 6758hw_breakpoint_used_count (void)
c906108c 6759{
c906108c 6760 int i = 0;
f1310107
TJB
6761 struct breakpoint *b;
6762 struct bp_location *bl;
c906108c
SS
6763
6764 ALL_BREAKPOINTS (b)
c5aa993b 6765 {
d6b74ac4 6766 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6767 for (bl = b->loc; bl; bl = bl->next)
6768 {
6769 /* Special types of hardware breakpoints may use more than
6770 one register. */
348d480f 6771 i += b->ops->resources_needed (bl);
f1310107 6772 }
c5aa993b 6773 }
c906108c
SS
6774
6775 return i;
6776}
6777
6778static int
fba45db2 6779hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 6780{
c906108c 6781 int i = 0;
e09342b5
TJB
6782 struct breakpoint *b;
6783 struct bp_location *bl;
c906108c
SS
6784
6785 *other_type_used = 0;
6786 ALL_BREAKPOINTS (b)
e09342b5
TJB
6787 {
6788 if (!breakpoint_enabled (b))
6789 continue;
6790
c5aa993b 6791 if (b->type == type)
e09342b5
TJB
6792 for (bl = b->loc; bl; bl = bl->next)
6793 {
6794 /* Special types of hardware watchpoints may use more than
6795 one register. */
348d480f 6796 i += b->ops->resources_needed (bl);
e09342b5 6797 }
cc60f2e3 6798 else if (is_hardware_watchpoint (b))
c5aa993b 6799 *other_type_used = 1;
e09342b5
TJB
6800 }
6801
c906108c
SS
6802 return i;
6803}
6804
c906108c 6805void
fba45db2 6806disable_watchpoints_before_interactive_call_start (void)
c906108c 6807{
c5aa993b 6808 struct breakpoint *b;
c906108c
SS
6809
6810 ALL_BREAKPOINTS (b)
c5aa993b 6811 {
cc60f2e3 6812 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6813 {
b5de0fa7 6814 b->enable_state = bp_call_disabled;
b60e7edf 6815 update_global_location_list (0);
c5aa993b
JM
6816 }
6817 }
c906108c
SS
6818}
6819
6820void
fba45db2 6821enable_watchpoints_after_interactive_call_stop (void)
c906108c 6822{
c5aa993b 6823 struct breakpoint *b;
c906108c
SS
6824
6825 ALL_BREAKPOINTS (b)
c5aa993b 6826 {
cc60f2e3 6827 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 6828 {
b5de0fa7 6829 b->enable_state = bp_enabled;
b60e7edf 6830 update_global_location_list (1);
c5aa993b
JM
6831 }
6832 }
c906108c
SS
6833}
6834
8bea4e01
UW
6835void
6836disable_breakpoints_before_startup (void)
6837{
6838 struct breakpoint *b;
6839 int found = 0;
6840
6841 ALL_BREAKPOINTS (b)
6842 {
6c95b8df
PA
6843 if (b->pspace != current_program_space)
6844 continue;
6845
8bea4e01
UW
6846 if ((b->type == bp_breakpoint
6847 || b->type == bp_hardware_breakpoint)
6848 && breakpoint_enabled (b))
6849 {
6850 b->enable_state = bp_startup_disabled;
6851 found = 1;
6852 }
6853 }
6854
6855 if (found)
6856 update_global_location_list (0);
6857
6c95b8df 6858 current_program_space->executing_startup = 1;
8bea4e01
UW
6859}
6860
6861void
6862enable_breakpoints_after_startup (void)
6863{
6864 struct breakpoint *b;
6865 int found = 0;
6866
6c95b8df 6867 current_program_space->executing_startup = 0;
8bea4e01
UW
6868
6869 ALL_BREAKPOINTS (b)
6870 {
6c95b8df
PA
6871 if (b->pspace != current_program_space)
6872 continue;
6873
8bea4e01
UW
6874 if ((b->type == bp_breakpoint
6875 || b->type == bp_hardware_breakpoint)
6876 && b->enable_state == bp_startup_disabled)
6877 {
6878 b->enable_state = bp_enabled;
6879 found = 1;
6880 }
6881 }
6882
6883 if (found)
6884 breakpoint_re_set ();
6885}
6886
c906108c
SS
6887
6888/* Set a breakpoint that will evaporate an end of command
6889 at address specified by SAL.
6890 Restrict it to frame FRAME if FRAME is nonzero. */
6891
6892struct breakpoint *
a6d9a66e
UW
6893set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6894 struct frame_id frame_id, enum bptype type)
c906108c 6895{
52f0bd74 6896 struct breakpoint *b;
edb3359d
DJ
6897
6898 /* If FRAME_ID is valid, it should be a real frame, not an inlined
6899 one. */
6900 gdb_assert (!frame_id_inlined_p (frame_id));
6901
06edf0c0 6902 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
6903 b->enable_state = bp_enabled;
6904 b->disposition = disp_donttouch;
818dd999 6905 b->frame_id = frame_id;
c906108c 6906
4a64f543
MS
6907 /* If we're debugging a multi-threaded program, then we want
6908 momentary breakpoints to be active in only a single thread of
6909 control. */
39f77062
KB
6910 if (in_thread_list (inferior_ptid))
6911 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 6912
b60e7edf 6913 update_global_location_list_nothrow (1);
74960c60 6914
c906108c
SS
6915 return b;
6916}
611c83ae 6917
06edf0c0
PA
6918/* Make a momentary breakpoint based on the master breakpoint ORIG.
6919 The new breakpoint will have type TYPE, and use OPS as it
6920 breakpoint_ops. */
e58b0e63 6921
06edf0c0
PA
6922static struct breakpoint *
6923momentary_breakpoint_from_master (struct breakpoint *orig,
6924 enum bptype type,
6925 struct breakpoint_ops *ops)
e58b0e63
PA
6926{
6927 struct breakpoint *copy;
6928
06edf0c0 6929 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 6930 copy->loc = allocate_bp_location (copy);
0e30163f 6931 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 6932
a6d9a66e 6933 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
6934 copy->loc->requested_address = orig->loc->requested_address;
6935 copy->loc->address = orig->loc->address;
6936 copy->loc->section = orig->loc->section;
6c95b8df 6937 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
6938
6939 if (orig->source_file == NULL)
6940 copy->source_file = NULL;
6941 else
6942 copy->source_file = xstrdup (orig->source_file);
6943
6944 copy->line_number = orig->line_number;
6945 copy->frame_id = orig->frame_id;
6946 copy->thread = orig->thread;
6c95b8df 6947 copy->pspace = orig->pspace;
e58b0e63
PA
6948
6949 copy->enable_state = bp_enabled;
6950 copy->disposition = disp_donttouch;
6951 copy->number = internal_breakpoint_number--;
6952
6953 update_global_location_list_nothrow (0);
6954 return copy;
6955}
6956
06edf0c0
PA
6957/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
6958 ORIG is NULL. */
6959
6960struct breakpoint *
6961clone_momentary_breakpoint (struct breakpoint *orig)
6962{
6963 /* If there's nothing to clone, then return nothing. */
6964 if (orig == NULL)
6965 return NULL;
6966
6967 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
6968}
6969
611c83ae 6970struct breakpoint *
a6d9a66e
UW
6971set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
6972 enum bptype type)
611c83ae
PA
6973{
6974 struct symtab_and_line sal;
6975
6976 sal = find_pc_line (pc, 0);
6977 sal.pc = pc;
6978 sal.section = find_pc_overlay (pc);
6979 sal.explicit_pc = 1;
6980
a6d9a66e 6981 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 6982}
c906108c 6983\f
c5aa993b 6984
c906108c
SS
6985/* Tell the user we have just set a breakpoint B. */
6986
6987static void
fba45db2 6988mention (struct breakpoint *b)
c906108c 6989{
348d480f 6990 b->ops->print_mention (b);
9dc5e2a9 6991 if (ui_out_is_mi_like_p (uiout))
fb40c209 6992 return;
c906108c
SS
6993 printf_filtered ("\n");
6994}
c906108c 6995\f
c5aa993b 6996
0d381245 6997static struct bp_location *
39d61571 6998add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
6999 const struct symtab_and_line *sal)
7000{
7001 struct bp_location *loc, **tmp;
7002
39d61571 7003 loc = allocate_bp_location (b);
0d381245
VP
7004 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7005 ;
7006 *tmp = loc;
a6d9a66e
UW
7007 loc->gdbarch = get_sal_arch (*sal);
7008 if (!loc->gdbarch)
7009 loc->gdbarch = b->gdbarch;
0d381245 7010 loc->requested_address = sal->pc;
a6d9a66e
UW
7011 loc->address = adjust_breakpoint_address (loc->gdbarch,
7012 loc->requested_address, b->type);
6c95b8df
PA
7013 loc->pspace = sal->pspace;
7014 gdb_assert (loc->pspace != NULL);
0d381245
VP
7015 loc->section = sal->section;
7016
0e30163f
JK
7017 set_breakpoint_location_function (loc,
7018 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7019 return loc;
7020}
514f746b
AR
7021\f
7022
7023/* Return 1 if LOC is pointing to a permanent breakpoint,
7024 return 0 otherwise. */
7025
7026static int
7027bp_loc_is_permanent (struct bp_location *loc)
7028{
7029 int len;
7030 CORE_ADDR addr;
7031 const gdb_byte *brk;
7032 gdb_byte *target_mem;
939c61fa
JK
7033 struct cleanup *cleanup;
7034 int retval = 0;
514f746b
AR
7035
7036 gdb_assert (loc != NULL);
7037
7038 addr = loc->address;
a6d9a66e 7039 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7040
939c61fa
JK
7041 /* Software breakpoints unsupported? */
7042 if (brk == NULL)
7043 return 0;
7044
514f746b
AR
7045 target_mem = alloca (len);
7046
939c61fa
JK
7047 /* Enable the automatic memory restoration from breakpoints while
7048 we read the memory. Otherwise we could say about our temporary
7049 breakpoints they are permanent. */
6c95b8df
PA
7050 cleanup = save_current_space_and_thread ();
7051
7052 switch_to_program_space_and_thread (loc->pspace);
7053 make_show_memory_breakpoints_cleanup (0);
939c61fa 7054
514f746b
AR
7055 if (target_read_memory (loc->address, target_mem, len) == 0
7056 && memcmp (target_mem, brk, len) == 0)
939c61fa 7057 retval = 1;
514f746b 7058
939c61fa
JK
7059 do_cleanups (cleanup);
7060
7061 return retval;
514f746b
AR
7062}
7063
7064
c3f6f71d 7065
018d34a4
VP
7066/* Create a breakpoint with SAL as location. Use ADDR_STRING
7067 as textual description of the location, and COND_STRING
db107f19 7068 as condition expression. */
018d34a4
VP
7069
7070static void
8cdf0e15
VP
7071create_breakpoint_sal (struct gdbarch *gdbarch,
7072 struct symtabs_and_lines sals, char *addr_string,
7073 char *cond_string,
7074 enum bptype type, enum bpdisp disposition,
7075 int thread, int task, int ignore_count,
84f4c1fe 7076 struct breakpoint_ops *ops, int from_tty,
56435ebe 7077 int enabled, int internal, int display_canonical)
018d34a4 7078{
0d381245
VP
7079 struct breakpoint *b = NULL;
7080 int i;
018d34a4
VP
7081
7082 if (type == bp_hardware_breakpoint)
7083 {
7084 int i = hw_breakpoint_used_count ();
7085 int target_resources_ok =
d92524f1 7086 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7087 i + 1, 0);
7088 if (target_resources_ok == 0)
7089 error (_("No hardware breakpoint support in the target."));
7090 else if (target_resources_ok < 0)
7091 error (_("Hardware breakpoints used exceeds limit."));
7092 }
7093
6c95b8df
PA
7094 gdb_assert (sals.nelts > 0);
7095
0d381245
VP
7096 for (i = 0; i < sals.nelts; ++i)
7097 {
7098 struct symtab_and_line sal = sals.sals[i];
7099 struct bp_location *loc;
7100
7101 if (from_tty)
5af949e3
UW
7102 {
7103 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7104 if (!loc_gdbarch)
7105 loc_gdbarch = gdbarch;
7106
7107 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7108 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7109 }
0d381245
VP
7110
7111 if (i == 0)
7112 {
348d480f 7113 b = set_raw_breakpoint (gdbarch, sal, type, ops);
84f4c1fe 7114 set_breakpoint_number (internal, b);
0d381245 7115 b->thread = thread;
4a306c9a 7116 b->task = task;
018d34a4 7117
0d381245
VP
7118 b->cond_string = cond_string;
7119 b->ignore_count = ignore_count;
41447f92 7120 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7121 b->disposition = disposition;
6c95b8df
PA
7122 b->pspace = sals.sals[0].pspace;
7123
0fb4aa4b
PA
7124 if (type == bp_static_tracepoint)
7125 {
7126 struct static_tracepoint_marker marker;
7127
7128 if (is_marker_spec (addr_string))
7129 {
7130 /* We already know the marker exists, otherwise, we
7131 wouldn't see a sal for it. */
7132 char *p = &addr_string[3];
7133 char *endp;
7134 char *marker_str;
7135 int i;
7136
e9cafbcc 7137 p = skip_spaces (p);
0fb4aa4b 7138
e9cafbcc 7139 endp = skip_to_space (p);
0fb4aa4b
PA
7140
7141 marker_str = savestring (p, endp - p);
7142 b->static_trace_marker_id = marker_str;
7143
3e43a32a
MS
7144 printf_filtered (_("Probed static tracepoint "
7145 "marker \"%s\"\n"),
0fb4aa4b
PA
7146 b->static_trace_marker_id);
7147 }
7148 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7149 {
7150 b->static_trace_marker_id = xstrdup (marker.str_id);
7151 release_static_tracepoint_marker (&marker);
7152
3e43a32a
MS
7153 printf_filtered (_("Probed static tracepoint "
7154 "marker \"%s\"\n"),
0fb4aa4b
PA
7155 b->static_trace_marker_id);
7156 }
7157 else
3e43a32a
MS
7158 warning (_("Couldn't determine the static "
7159 "tracepoint marker to probe"));
0fb4aa4b
PA
7160 }
7161
6c95b8df 7162 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7163 && (b->type == bp_breakpoint
7164 || b->type == bp_hardware_breakpoint))
7165 b->enable_state = bp_startup_disabled;
7166
0d381245
VP
7167 loc = b->loc;
7168 }
7169 else
018d34a4 7170 {
39d61571 7171 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7172 }
7173
514f746b
AR
7174 if (bp_loc_is_permanent (loc))
7175 make_breakpoint_permanent (b);
7176
0d381245
VP
7177 if (b->cond_string)
7178 {
7179 char *arg = b->cond_string;
d32a6982 7180 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7181 if (*arg)
db107f19 7182 error (_("Garbage %s follows condition"), arg);
018d34a4 7183 }
0d381245 7184 }
018d34a4 7185
56435ebe 7186 b->display_canonical = display_canonical;
018d34a4
VP
7187 if (addr_string)
7188 b->addr_string = addr_string;
7189 else
7190 /* addr_string has to be used or breakpoint_re_set will delete
7191 me. */
5af949e3
UW
7192 b->addr_string
7193 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
018d34a4 7194
8d3788bd
VP
7195 /* Do not mention breakpoints with a negative number, but do
7196 notify observers. */
7197 if (!internal)
84f4c1fe 7198 mention (b);
8d3788bd 7199 observer_notify_breakpoint_created (b);
018d34a4
VP
7200}
7201
ed0616c6
VP
7202/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7203 elements to fill the void space. */
2c0b251b
PA
7204static void
7205remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7206{
7207 int i = index_to_remove+1;
7208 int last_index = sal->nelts-1;
7209
7210 for (;i <= last_index; ++i)
7211 sal->sals[i-1] = sal->sals[i];
7212
7213 --(sal->nelts);
7214}
7215
6c95b8df
PA
7216/* If appropriate, obtains all sals that correspond to the same file
7217 and line as SAL, in all program spaces. Users debugging with IDEs,
7218 will want to set a breakpoint at foo.c:line, and not really care
7219 about program spaces. This is done only if SAL does not have
7220 explicit PC and has line and file information. If we got just a
7221 single expanded sal, return the original.
ed0616c6 7222
6c95b8df
PA
7223 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7224 which the name of enclosing function is different from SAL. This
7225 makes sure that if we have breakpoint originally set in template
7226 instantiation, say foo<int>(), we won't expand SAL to locations at
7227 the same line in all existing instantiations of 'foo'. */
ed0616c6 7228
2c0b251b 7229static struct symtabs_and_lines
ed0616c6
VP
7230expand_line_sal_maybe (struct symtab_and_line sal)
7231{
7232 struct symtabs_and_lines expanded;
7233 CORE_ADDR original_pc = sal.pc;
7234 char *original_function = NULL;
7235 int found;
7236 int i;
6c95b8df 7237 struct cleanup *old_chain;
ed0616c6
VP
7238
7239 /* If we have explicit pc, don't expand.
7240 If we have no line number, we can't expand. */
7241 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7242 {
7243 expanded.nelts = 1;
7244 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7245 expanded.sals[0] = sal;
7246 return expanded;
7247 }
7248
7249 sal.pc = 0;
6c95b8df
PA
7250
7251 old_chain = save_current_space_and_thread ();
7252
7253 switch_to_program_space_and_thread (sal.pspace);
7254
ed0616c6 7255 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7256
7257 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7258 expanded = expand_line_sal (sal);
6c95b8df 7259
ed0616c6
VP
7260 if (expanded.nelts == 1)
7261 {
3dba1c98
JB
7262 /* We had one sal, we got one sal. Return that sal, adjusting it
7263 past the function prologue if necessary. */
ed0616c6
VP
7264 xfree (expanded.sals);
7265 expanded.nelts = 1;
7266 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7267 sal.pc = original_pc;
7268 expanded.sals[0] = sal;
3dba1c98 7269 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7270 do_cleanups (old_chain);
ed0616c6
VP
7271 return expanded;
7272 }
7273
7274 if (!sal.explicit_line)
7275 {
7276 CORE_ADDR func_addr, func_end;
7277 for (i = 0; i < expanded.nelts; ++i)
7278 {
7279 CORE_ADDR pc = expanded.sals[i].pc;
7280 char *this_function;
6c95b8df
PA
7281
7282 /* We need to switch threads as well since we're about to
7283 read memory. */
7284 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7285
ed0616c6
VP
7286 if (find_pc_partial_function (pc, &this_function,
7287 &func_addr, &func_end))
7288 {
059fb39f
PM
7289 if (this_function
7290 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7291 {
7292 remove_sal (&expanded, i);
7293 --i;
7294 }
ed0616c6
VP
7295 }
7296 }
7297 }
059acae7
UW
7298
7299 /* Skip the function prologue if necessary. */
7300 for (i = 0; i < expanded.nelts; ++i)
7301 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7302
6c95b8df
PA
7303 do_cleanups (old_chain);
7304
ed0616c6
VP
7305 if (expanded.nelts <= 1)
7306 {
e5dd4106 7307 /* This is an ugly workaround. If we get zero expanded sals
4a64f543
MS
7308 then something is really wrong. Fix that by returning the
7309 original sal. */
7310
ed0616c6
VP
7311 xfree (expanded.sals);
7312 expanded.nelts = 1;
7313 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7314 sal.pc = original_pc;
7315 expanded.sals[0] = sal;
7316 return expanded;
7317 }
7318
7319 if (original_pc)
7320 {
7321 found = 0;
7322 for (i = 0; i < expanded.nelts; ++i)
7323 if (expanded.sals[i].pc == original_pc)
7324 {
7325 found = 1;
7326 break;
7327 }
7328 gdb_assert (found);
7329 }
7330
7331 return expanded;
7332}
7333
018d34a4
VP
7334/* Add SALS.nelts breakpoints to the breakpoint table. For each
7335 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7336 value. COND_STRING, if not NULL, specified the condition to be
7337 used for all breakpoints. Essentially the only case where
7338 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7339 function. In that case, it's still not possible to specify
7340 separate conditions for different overloaded functions, so
7341 we take just a single condition string.
7342
c3f6f71d 7343 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7344 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7345 array contents). If the function fails (error() is called), the
7346 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7347 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7348
7349static void
8cdf0e15 7350create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2
TT
7351 struct symtabs_and_lines sals,
7352 struct linespec_result *canonical,
8cdf0e15
VP
7353 char *cond_string,
7354 enum bptype type, enum bpdisp disposition,
7355 int thread, int task, int ignore_count,
7356 struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7357 int enabled, int internal)
c906108c 7358{
018d34a4 7359 int i;
cc59ec59 7360
018d34a4 7361 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7362 {
ed0616c6
VP
7363 struct symtabs_and_lines expanded =
7364 expand_line_sal_maybe (sals.sals[i]);
0d381245 7365
7efd8fc2 7366 create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
8cdf0e15 7367 cond_string, type, disposition,
84f4c1fe 7368 thread, task, ignore_count, ops,
56435ebe
TT
7369 from_tty, enabled, internal,
7370 canonical->special_display);
c3f6f71d 7371 }
c3f6f71d 7372}
c906108c 7373
9998af43 7374/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7375 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7376 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7377 address strings. ADDRESS points to the end of the SAL.
7378
7379 The array and the line spec strings are allocated on the heap, it is
7380 the caller's responsibility to free them. */
c906108c 7381
b9362cc7 7382static void
c3f6f71d
JM
7383parse_breakpoint_sals (char **address,
7384 struct symtabs_and_lines *sals,
58438ac1 7385 struct linespec_result *canonical)
c3f6f71d
JM
7386{
7387 char *addr_start = *address;
cc59ec59 7388
c3f6f71d 7389 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7390 breakpoint. */
c3f6f71d
JM
7391 if ((*address) == NULL
7392 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
7393 {
7394 if (default_breakpoint_valid)
7395 {
c3f6f71d 7396 struct symtab_and_line sal;
cc59ec59 7397
4a64f543 7398 init_sal (&sal); /* Initialize to zeroes. */
c3f6f71d 7399 sals->sals = (struct symtab_and_line *)
c906108c
SS
7400 xmalloc (sizeof (struct symtab_and_line));
7401 sal.pc = default_breakpoint_address;
7402 sal.line = default_breakpoint_line;
7403 sal.symtab = default_breakpoint_symtab;
6c95b8df 7404 sal.pspace = default_breakpoint_pspace;
c5aa993b 7405 sal.section = find_pc_overlay (sal.pc);
00903456 7406
4a64f543
MS
7407 /* "break" without arguments is equivalent to "break *PC"
7408 where PC is the default_breakpoint_address. So make sure
7409 to set sal.explicit_pc to prevent GDB from trying to
7410 expand the list of sals to include all other instances
7411 with the same symtab and line. */
00903456
JK
7412 sal.explicit_pc = 1;
7413
c3f6f71d
JM
7414 sals->sals[0] = sal;
7415 sals->nelts = 1;
c906108c
SS
7416 }
7417 else
8a3fe4f8 7418 error (_("No default breakpoint address now."));
c906108c
SS
7419 }
7420 else
7421 {
c906108c 7422 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7423 current_source_symtab (which is decode_line_1's default).
7424 This should produce the results we want almost all of the
7425 time while leaving default_breakpoint_* alone.
7426
1aeae86e
AF
7427 ObjC: However, don't match an Objective-C method name which
7428 may have a '+' or '-' succeeded by a '[' */
0378c332 7429
c214a6fd 7430 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7431
c906108c 7432 if (default_breakpoint_valid
0378c332 7433 && (!cursal.symtab
1aeae86e
AF
7434 || ((strchr ("+-", (*address)[0]) != NULL)
7435 && ((*address)[1] != '['))))
c3f6f71d 7436 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
58438ac1 7437 default_breakpoint_line, canonical);
c906108c 7438 else
0101ce28 7439 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
58438ac1 7440 canonical);
c906108c 7441 }
4a64f543 7442 /* For any SAL that didn't have a canonical string, fill one in. */
7efd8fc2 7443 if (sals->nelts > 0 && canonical->canonical == NULL)
38a714bb 7444 canonical->canonical = xcalloc (sals->nelts, sizeof (char *));
c3f6f71d 7445 if (addr_start != (*address))
c906108c 7446 {
c3f6f71d 7447 int i;
cc59ec59 7448
c3f6f71d 7449 for (i = 0; i < sals->nelts; i++)
c906108c 7450 {
4a64f543 7451 /* Add the string if not present. */
7efd8fc2
TT
7452 if (canonical->canonical[i] == NULL)
7453 canonical->canonical[i] = savestring (addr_start,
7454 (*address) - addr_start);
c906108c
SS
7455 }
7456 }
c3f6f71d 7457}
c906108c 7458
c906108c 7459
c3f6f71d 7460/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7461 inserted as a breakpoint. If it can't throw an error. */
c906108c 7462
b9362cc7 7463static void
23e7acfb 7464breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7465{
7466 int i;
cc59ec59 7467
c3f6f71d 7468 for (i = 0; i < sals->nelts; i++)
ee53e872 7469 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7470}
7471
7a697b8d
SS
7472/* Fast tracepoints may have restrictions on valid locations. For
7473 instance, a fast tracepoint using a jump instead of a trap will
7474 likely have to overwrite more bytes than a trap would, and so can
7475 only be placed where the instruction is longer than the jump, or a
7476 multi-instruction sequence does not have a jump into the middle of
7477 it, etc. */
7478
7479static void
7480check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7481 struct symtabs_and_lines *sals)
7482{
7483 int i, rslt;
7484 struct symtab_and_line *sal;
7485 char *msg;
7486 struct cleanup *old_chain;
7487
7488 for (i = 0; i < sals->nelts; i++)
7489 {
7490 sal = &sals->sals[i];
7491
7492 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7493 NULL, &msg);
7494 old_chain = make_cleanup (xfree, msg);
7495
7496 if (!rslt)
7497 error (_("May not have a fast tracepoint at 0x%s%s"),
7498 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7499
7500 do_cleanups (old_chain);
7501 }
7502}
7503
018d34a4
VP
7504/* Given TOK, a string specification of condition and thread, as
7505 accepted by the 'break' command, extract the condition
7506 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7507 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7508 If no condition is found, *COND_STRING is set to NULL.
7509 If no thread is found, *THREAD is set to -1. */
7510static void
7511find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7512 char **cond_string, int *thread, int *task)
018d34a4
VP
7513{
7514 *cond_string = NULL;
7515 *thread = -1;
7516 while (tok && *tok)
7517 {
7518 char *end_tok;
7519 int toklen;
7520 char *cond_start = NULL;
7521 char *cond_end = NULL;
cc59ec59 7522
e9cafbcc 7523 tok = skip_spaces (tok);
018d34a4 7524
e9cafbcc 7525 end_tok = skip_to_space (tok);
018d34a4
VP
7526
7527 toklen = end_tok - tok;
7528
7529 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7530 {
f7545552
TT
7531 struct expression *expr;
7532
018d34a4 7533 tok = cond_start = end_tok + 1;
f7545552
TT
7534 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7535 xfree (expr);
018d34a4
VP
7536 cond_end = tok;
7537 *cond_string = savestring (cond_start,
7538 cond_end - cond_start);
7539 }
7540 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7541 {
7542 char *tmptok;
7543
7544 tok = end_tok + 1;
7545 tmptok = tok;
7546 *thread = strtol (tok, &tok, 0);
7547 if (tok == tmptok)
7548 error (_("Junk after thread keyword."));
7549 if (!valid_thread_id (*thread))
7550 error (_("Unknown thread %d."), *thread);
7551 }
4a306c9a
JB
7552 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7553 {
7554 char *tmptok;
7555
7556 tok = end_tok + 1;
7557 tmptok = tok;
7558 *task = strtol (tok, &tok, 0);
7559 if (tok == tmptok)
7560 error (_("Junk after task keyword."));
7561 if (!valid_task_id (*task))
b6199126 7562 error (_("Unknown task %d."), *task);
4a306c9a 7563 }
018d34a4
VP
7564 else
7565 error (_("Junk at end of arguments."));
7566 }
7567}
7568
0fb4aa4b
PA
7569/* Decode a static tracepoint marker spec. */
7570
7571static struct symtabs_and_lines
7572decode_static_tracepoint_spec (char **arg_p)
7573{
7574 VEC(static_tracepoint_marker_p) *markers = NULL;
7575 struct symtabs_and_lines sals;
7576 struct symtab_and_line sal;
7577 struct symbol *sym;
7578 struct cleanup *old_chain;
7579 char *p = &(*arg_p)[3];
7580 char *endp;
7581 char *marker_str;
7582 int i;
7583
e9cafbcc 7584 p = skip_spaces (p);
0fb4aa4b 7585
e9cafbcc 7586 endp = skip_to_space (p);
0fb4aa4b
PA
7587
7588 marker_str = savestring (p, endp - p);
7589 old_chain = make_cleanup (xfree, marker_str);
7590
7591 markers = target_static_tracepoint_markers_by_strid (marker_str);
7592 if (VEC_empty(static_tracepoint_marker_p, markers))
7593 error (_("No known static tracepoint marker named %s"), marker_str);
7594
7595 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7596 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7597
7598 for (i = 0; i < sals.nelts; i++)
7599 {
7600 struct static_tracepoint_marker *marker;
7601
7602 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7603
7604 init_sal (&sals.sals[i]);
7605
7606 sals.sals[i] = find_pc_line (marker->address, 0);
7607 sals.sals[i].pc = marker->address;
7608
7609 release_static_tracepoint_marker (marker);
7610 }
7611
7612 do_cleanups (old_chain);
7613
7614 *arg_p = endp;
7615 return sals;
7616}
7617
fd9b8c24
PA
7618/* Set a breakpoint. This function is shared between CLI and MI
7619 functions for setting a breakpoint. This function has two major
7620 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7621 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7622 breakpoint location, address and thread. Otherwise, ARG is just
7623 the location of breakpoint, with condition and thread specified by
7624 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7625 the breakpoint number will be allocated from the internal
7626 breakpoint count. Returns true if any breakpoint was created;
7627 false otherwise. */
0101ce28 7628
8cdf0e15
VP
7629int
7630create_breakpoint (struct gdbarch *gdbarch,
7631 char *arg, char *cond_string, int thread,
7632 int parse_condition_and_thread,
0fb4aa4b 7633 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7634 int ignore_count,
7635 enum auto_boolean pending_break_support,
7636 struct breakpoint_ops *ops,
84f4c1fe 7637 int from_tty, int enabled, int internal)
c3f6f71d 7638{
b78a6381 7639 volatile struct gdb_exception e;
c3f6f71d 7640 struct symtabs_and_lines sals;
0101ce28 7641 struct symtab_and_line pending_sal;
0101ce28 7642 char *copy_arg;
c3f6f71d 7643 char *addr_start = arg;
7efd8fc2 7644 struct linespec_result canonical;
c3f6f71d 7645 struct cleanup *old_chain;
80c99de1 7646 struct cleanup *bkpt_chain = NULL;
05ff989b 7647 int i;
0101ce28 7648 int pending = 0;
4a306c9a 7649 int task = 0;
86b17b60 7650 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7651
348d480f
PA
7652 gdb_assert (ops != NULL);
7653
c3f6f71d
JM
7654 sals.sals = NULL;
7655 sals.nelts = 0;
7efd8fc2 7656 init_linespec_result (&canonical);
c3f6f71d 7657
0fb4aa4b
PA
7658 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7659 {
7660 int i;
7661
7662 sals = decode_static_tracepoint_spec (&arg);
7663
7664 copy_arg = savestring (addr_start, arg - addr_start);
38a714bb 7665 canonical.canonical = xcalloc (sals.nelts, sizeof (char *));
0fb4aa4b 7666 for (i = 0; i < sals.nelts; i++)
7efd8fc2 7667 canonical.canonical[i] = xstrdup (copy_arg);
0fb4aa4b
PA
7668 goto done;
7669 }
7670
b78a6381
TT
7671 TRY_CATCH (e, RETURN_MASK_ALL)
7672 {
58438ac1 7673 parse_breakpoint_sals (&arg, &sals, &canonical);
b78a6381 7674 }
0101ce28
JJ
7675
7676 /* If caller is interested in rc value from parse, set value. */
05ff989b 7677 switch (e.reason)
0101ce28 7678 {
05ff989b 7679 case RETURN_QUIT:
98deb0da 7680 throw_exception (e);
05ff989b
AC
7681 case RETURN_ERROR:
7682 switch (e.error)
0101ce28 7683 {
05ff989b 7684 case NOT_FOUND_ERROR:
0101ce28 7685
05ff989b
AC
7686 /* If pending breakpoint support is turned off, throw
7687 error. */
fa8d40ab
JJ
7688
7689 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7690 throw_exception (e);
7691
7692 exception_print (gdb_stderr, e);
fa8d40ab 7693
05ff989b
AC
7694 /* If pending breakpoint support is auto query and the user
7695 selects no, then simply return the error code. */
059fb39f 7696 if (pending_break_support == AUTO_BOOLEAN_AUTO
3e43a32a
MS
7697 && !nquery (_("Make breakpoint pending on "
7698 "future shared library load? ")))
fd9b8c24 7699 return 0;
fa8d40ab 7700
05ff989b
AC
7701 /* At this point, either the user was queried about setting
7702 a pending breakpoint and selected yes, or pending
7703 breakpoint behavior is on and thus a pending breakpoint
7704 is defaulted on behalf of the user. */
0101ce28 7705 copy_arg = xstrdup (addr_start);
7efd8fc2 7706 canonical.canonical = &copy_arg;
0101ce28
JJ
7707 sals.nelts = 1;
7708 sals.sals = &pending_sal;
7709 pending_sal.pc = 0;
7710 pending = 1;
05ff989b
AC
7711 break;
7712 default:
98deb0da 7713 throw_exception (e);
0101ce28 7714 }
2abae994 7715 break;
05ff989b
AC
7716 default:
7717 if (!sals.nelts)
fd9b8c24 7718 return 0;
0101ce28 7719 }
c3f6f71d 7720
0fb4aa4b
PA
7721 done:
7722
4a64f543 7723 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
7724 old_chain = make_cleanup (null_cleanup, 0);
7725
0101ce28
JJ
7726 if (!pending)
7727 {
7728 /* Make sure that all storage allocated to SALS gets freed. */
7729 make_cleanup (xfree, sals.sals);
7730
7efd8fc2
TT
7731 /* Cleanup the canonical array but not its contents. */
7732 make_cleanup (xfree, canonical.canonical);
0101ce28 7733 }
c3f6f71d 7734
c3f6f71d
JM
7735 /* ----------------------------- SNIP -----------------------------
7736 Anything added to the cleanup chain beyond this point is assumed
7737 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7738 then the memory is not reclaimed. */
7739 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7740
7efd8fc2 7741 /* Mark the contents of the canonical for cleanup. These go on
80c99de1 7742 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
7743 for (i = 0; i < sals.nelts; i++)
7744 {
7efd8fc2
TT
7745 if (canonical.canonical[i] != NULL)
7746 make_cleanup (xfree, canonical.canonical[i]);
c3f6f71d
JM
7747 }
7748
7749 /* Resolve all line numbers to PC's and verify that the addresses
7750 are ok for the target. */
0101ce28 7751 if (!pending)
23e7acfb 7752 breakpoint_sals_to_pc (&sals);
c3f6f71d 7753
7a697b8d
SS
7754 /* Fast tracepoints may have additional restrictions on location. */
7755 if (type_wanted == bp_fast_tracepoint)
7756 check_fast_tracepoint_sals (gdbarch, &sals);
7757
c3f6f71d
JM
7758 /* Verify that condition can be parsed, before setting any
7759 breakpoints. Allocate a separate condition expression for each
4a64f543 7760 breakpoint. */
0101ce28 7761 if (!pending)
c3f6f71d 7762 {
2f069f6f 7763 if (parse_condition_and_thread)
72b2ff0e
VP
7764 {
7765 /* Here we only parse 'arg' to separate condition
7766 from thread number, so parsing in context of first
7767 sal is OK. When setting the breakpoint we'll
7768 re-parse it in context of each sal. */
7769 cond_string = NULL;
7770 thread = -1;
4a306c9a
JB
7771 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7772 &thread, &task);
72b2ff0e
VP
7773 if (cond_string)
7774 make_cleanup (xfree, cond_string);
7775 }
2f069f6f 7776 else
72b2ff0e
VP
7777 {
7778 /* Create a private copy of condition string. */
7779 if (cond_string)
7780 {
7781 cond_string = xstrdup (cond_string);
7782 make_cleanup (xfree, cond_string);
7783 }
7784 }
0fb4aa4b
PA
7785
7786 /* If the user is creating a static tracepoint by marker id
7787 (strace -m MARKER_ID), then store the sals index, so that
7788 breakpoint_re_set can try to match up which of the newly
7789 found markers corresponds to this one, and, don't try to
7790 expand multiple locations for each sal, given than SALS
7791 already should contain all sals for MARKER_ID. */
7792 if (type_wanted == bp_static_tracepoint
7efd8fc2 7793 && is_marker_spec (canonical.canonical[0]))
0fb4aa4b
PA
7794 {
7795 int i;
7796
7797 for (i = 0; i < sals.nelts; ++i)
7798 {
7799 struct symtabs_and_lines expanded;
7800 struct breakpoint *tp;
7801 struct cleanup *old_chain;
7802
7803 expanded.nelts = 1;
7804 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7805 expanded.sals[0] = sals.sals[i];
7806 old_chain = make_cleanup (xfree, expanded.sals);
7807
7efd8fc2 7808 create_breakpoint_sal (gdbarch, expanded, canonical.canonical[i],
0fb4aa4b
PA
7809 cond_string, type_wanted,
7810 tempflag ? disp_del : disp_donttouch,
7811 thread, task, ignore_count, ops,
56435ebe
TT
7812 from_tty, enabled, internal,
7813 canonical.special_display);
0fb4aa4b
PA
7814
7815 do_cleanups (old_chain);
7816
7817 /* Get the tracepoint we just created. */
84f4c1fe
PM
7818 if (internal)
7819 tp = get_breakpoint (internal_breakpoint_number);
7820 else
7821 tp = get_breakpoint (breakpoint_count);
0fb4aa4b
PA
7822 gdb_assert (tp != NULL);
7823
7824 /* Given that its possible to have multiple markers with
7825 the same string id, if the user is creating a static
7826 tracepoint by marker id ("strace -m MARKER_ID"), then
7827 store the sals index, so that breakpoint_re_set can
7828 try to match up which of the newly found markers
7829 corresponds to this one */
7830 tp->static_trace_marker_id_idx = i;
7831 }
7832 }
7833 else
7efd8fc2 7834 create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
3e43a32a
MS
7835 type_wanted,
7836 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 7837 thread, task, ignore_count, ops, from_tty,
84f4c1fe 7838 enabled, internal);
c906108c 7839 }
0101ce28
JJ
7840 else
7841 {
0101ce28
JJ
7842 struct breakpoint *b;
7843
0101ce28
JJ
7844 make_cleanup (xfree, copy_arg);
7845
348d480f 7846 b = set_raw_breakpoint_without_location (gdbarch, type_wanted, ops);
84f4c1fe 7847 set_breakpoint_number (internal, b);
72b2ff0e 7848 b->thread = -1;
7efd8fc2 7849 b->addr_string = canonical.canonical[0];
72b2ff0e 7850 b->cond_string = NULL;
0101ce28 7851 b->ignore_count = ignore_count;
0101ce28 7852 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 7853 b->condition_not_parsed = 1;
41447f92 7854 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 7855 b->pspace = current_program_space;
84f4c1fe 7856 b->py_bp_object = NULL;
74960c60 7857
6c95b8df 7858 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7859 && (b->type == bp_breakpoint
7860 || b->type == bp_hardware_breakpoint))
7861 b->enable_state = bp_startup_disabled;
7862
8d3788bd 7863 if (!internal)
84f4c1fe
PM
7864 /* Do not mention breakpoints with a negative number,
7865 but do notify observers. */
8d3788bd
VP
7866 mention (b);
7867 observer_notify_breakpoint_created (b);
0101ce28
JJ
7868 }
7869
c3f6f71d 7870 if (sals.nelts > 1)
95a42b64 7871 {
3e43a32a
MS
7872 warning (_("Multiple breakpoints were set.\nUse the "
7873 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 7874 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
7875 }
7876
80c99de1
PA
7877 /* That's it. Discard the cleanups for data inserted into the
7878 breakpoint. */
7879 discard_cleanups (bkpt_chain);
7880 /* But cleanup everything else. */
c3f6f71d 7881 do_cleanups (old_chain);
217dc9e2 7882
80c99de1 7883 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 7884 update_global_location_list (1);
fd9b8c24
PA
7885
7886 return 1;
c3f6f71d 7887}
c906108c 7888
348d480f 7889/* Set a breakpoint.
72b2ff0e
VP
7890 ARG is a string describing breakpoint address,
7891 condition, and thread.
7892 FLAG specifies if a breakpoint is hardware on,
7893 and if breakpoint is temporary, using BP_HARDWARE_FLAG
7894 and BP_TEMPFLAG. */
348d480f 7895
98deb0da 7896static void
72b2ff0e 7897break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 7898{
72b2ff0e 7899 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
7900 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
7901 ? bp_hardware_breakpoint
7902 : bp_breakpoint);
c3f6f71d 7903
8cdf0e15
VP
7904 create_breakpoint (get_current_arch (),
7905 arg,
7906 NULL, 0, 1 /* parse arg */,
0fb4aa4b 7907 tempflag, type_wanted,
8cdf0e15
VP
7908 0 /* Ignore count */,
7909 pending_break_support,
348d480f 7910 &bkpt_breakpoint_ops,
8cdf0e15 7911 from_tty,
84f4c1fe
PM
7912 1 /* enabled */,
7913 0 /* internal */);
c906108c
SS
7914}
7915
c906108c
SS
7916/* Helper function for break_command_1 and disassemble_command. */
7917
7918void
fba45db2 7919resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
7920{
7921 CORE_ADDR pc;
7922
7923 if (sal->pc == 0 && sal->symtab != NULL)
7924 {
7925 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 7926 error (_("No line %d in file \"%s\"."),
c906108c
SS
7927 sal->line, sal->symtab->filename);
7928 sal->pc = pc;
6a048695 7929
4a64f543
MS
7930 /* If this SAL corresponds to a breakpoint inserted using a line
7931 number, then skip the function prologue if necessary. */
6a048695 7932 if (sal->explicit_line)
059acae7 7933 skip_prologue_sal (sal);
c906108c
SS
7934 }
7935
7936 if (sal->section == 0 && sal->symtab != NULL)
7937 {
7938 struct blockvector *bv;
c5aa993b
JM
7939 struct block *b;
7940 struct symbol *sym;
c906108c 7941
801e3a5b 7942 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
7943 if (bv != NULL)
7944 {
7f0df278 7945 sym = block_linkage_function (b);
c906108c
SS
7946 if (sym != NULL)
7947 {
7948 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 7949 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
7950 }
7951 else
7952 {
4a64f543
MS
7953 /* It really is worthwhile to have the section, so we'll
7954 just have to look harder. This case can be executed
7955 if we have line numbers but no functions (as can
7956 happen in assembly source). */
c906108c 7957
c5aa993b 7958 struct minimal_symbol *msym;
6c95b8df
PA
7959 struct cleanup *old_chain = save_current_space_and_thread ();
7960
7961 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
7962
7963 msym = lookup_minimal_symbol_by_pc (sal->pc);
7964 if (msym)
714835d5 7965 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
7966
7967 do_cleanups (old_chain);
c906108c
SS
7968 }
7969 }
7970 }
7971}
7972
7973void
fba45db2 7974break_command (char *arg, int from_tty)
c906108c 7975{
db107f19 7976 break_command_1 (arg, 0, from_tty);
c906108c
SS
7977}
7978
c906108c 7979void
fba45db2 7980tbreak_command (char *arg, int from_tty)
c906108c 7981{
db107f19 7982 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
7983}
7984
c906108c 7985static void
fba45db2 7986hbreak_command (char *arg, int from_tty)
c906108c 7987{
db107f19 7988 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
7989}
7990
7991static void
fba45db2 7992thbreak_command (char *arg, int from_tty)
c906108c 7993{
db107f19 7994 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
7995}
7996
7997static void
fba45db2 7998stop_command (char *arg, int from_tty)
c906108c 7999{
a3f17187 8000 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8001Usage: stop in <function | address>\n\
a3f17187 8002 stop at <line>\n"));
c906108c
SS
8003}
8004
8005static void
fba45db2 8006stopin_command (char *arg, int from_tty)
c906108c
SS
8007{
8008 int badInput = 0;
8009
c5aa993b 8010 if (arg == (char *) NULL)
c906108c
SS
8011 badInput = 1;
8012 else if (*arg != '*')
8013 {
8014 char *argptr = arg;
8015 int hasColon = 0;
8016
4a64f543 8017 /* Look for a ':'. If this is a line number specification, then
53a5351d 8018 say it is bad, otherwise, it should be an address or
4a64f543 8019 function/method name. */
c906108c 8020 while (*argptr && !hasColon)
c5aa993b
JM
8021 {
8022 hasColon = (*argptr == ':');
8023 argptr++;
8024 }
c906108c
SS
8025
8026 if (hasColon)
c5aa993b 8027 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8028 else
c5aa993b 8029 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8030 }
8031
8032 if (badInput)
a3f17187 8033 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8034 else
db107f19 8035 break_command_1 (arg, 0, from_tty);
c906108c
SS
8036}
8037
8038static void
fba45db2 8039stopat_command (char *arg, int from_tty)
c906108c
SS
8040{
8041 int badInput = 0;
8042
c5aa993b 8043 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8044 badInput = 1;
8045 else
8046 {
8047 char *argptr = arg;
8048 int hasColon = 0;
8049
4a64f543
MS
8050 /* Look for a ':'. If there is a '::' then get out, otherwise
8051 it is probably a line number. */
c906108c 8052 while (*argptr && !hasColon)
c5aa993b
JM
8053 {
8054 hasColon = (*argptr == ':');
8055 argptr++;
8056 }
c906108c
SS
8057
8058 if (hasColon)
c5aa993b 8059 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8060 else
c5aa993b 8061 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8062 }
8063
8064 if (badInput)
a3f17187 8065 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8066 else
db107f19 8067 break_command_1 (arg, 0, from_tty);
c906108c
SS
8068}
8069
f1310107
TJB
8070/* Implement the "breakpoint_hit" breakpoint_ops method for
8071 ranged breakpoints. */
8072
8073static int
8074breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8075 struct address_space *aspace,
8076 CORE_ADDR bp_addr)
8077{
8078 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8079 bl->length, aspace, bp_addr);
8080}
8081
8082/* Implement the "resources_needed" breakpoint_ops method for
8083 ranged breakpoints. */
8084
8085static int
8086resources_needed_ranged_breakpoint (const struct bp_location *bl)
8087{
8088 return target_ranged_break_num_registers ();
8089}
8090
8091/* Implement the "print_it" breakpoint_ops method for
8092 ranged breakpoints. */
8093
8094static enum print_stop_action
348d480f 8095print_it_ranged_breakpoint (bpstat bs)
f1310107 8096{
348d480f 8097 struct breakpoint *b = bs->breakpoint_at;
f1310107
TJB
8098 struct bp_location *bl = b->loc;
8099
8100 gdb_assert (b->type == bp_hardware_breakpoint);
8101
8102 /* Ranged breakpoints have only one location. */
8103 gdb_assert (bl && bl->next == NULL);
8104
8105 annotate_breakpoint (b->number);
8106 if (b->disposition == disp_del)
8107 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8108 else
8109 ui_out_text (uiout, "\nRanged breakpoint ");
8110 if (ui_out_is_mi_like_p (uiout))
8111 {
8112 ui_out_field_string (uiout, "reason",
8113 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8114 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8115 }
8116 ui_out_field_int (uiout, "bkptno", b->number);
8117 ui_out_text (uiout, ", ");
8118
8119 return PRINT_SRC_AND_LOC;
8120}
8121
8122/* Implement the "print_one" breakpoint_ops method for
8123 ranged breakpoints. */
8124
8125static void
8126print_one_ranged_breakpoint (struct breakpoint *b,
8127 struct bp_location **last_loc)
8128{
8129 struct bp_location *bl = b->loc;
8130 struct value_print_options opts;
8131
8132 /* Ranged breakpoints have only one location. */
8133 gdb_assert (bl && bl->next == NULL);
8134
8135 get_user_print_options (&opts);
8136
8137 if (opts.addressprint)
8138 /* We don't print the address range here, it will be printed later
8139 by print_one_detail_ranged_breakpoint. */
8140 ui_out_field_skip (uiout, "addr");
8141 annotate_field (5);
8142 print_breakpoint_location (b, bl);
8143 *last_loc = bl;
8144}
8145
8146/* Implement the "print_one_detail" breakpoint_ops method for
8147 ranged breakpoints. */
8148
8149static void
8150print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8151 struct ui_out *uiout)
8152{
8153 CORE_ADDR address_start, address_end;
8154 struct bp_location *bl = b->loc;
8155 struct ui_stream *stb = ui_out_stream_new (uiout);
8156 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8157
8158 gdb_assert (bl);
8159
8160 address_start = bl->address;
8161 address_end = address_start + bl->length - 1;
8162
8163 ui_out_text (uiout, "\taddress range: ");
8164 fprintf_unfiltered (stb->stream, "[%s, %s]",
8165 print_core_address (bl->gdbarch, address_start),
8166 print_core_address (bl->gdbarch, address_end));
8167 ui_out_field_stream (uiout, "addr", stb);
8168 ui_out_text (uiout, "\n");
8169
8170 do_cleanups (cleanup);
8171}
8172
8173/* Implement the "print_mention" breakpoint_ops method for
8174 ranged breakpoints. */
8175
8176static void
8177print_mention_ranged_breakpoint (struct breakpoint *b)
8178{
8179 struct bp_location *bl = b->loc;
8180
8181 gdb_assert (bl);
8182 gdb_assert (b->type == bp_hardware_breakpoint);
8183
8184 if (ui_out_is_mi_like_p (uiout))
8185 return;
8186
8187 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8188 b->number, paddress (bl->gdbarch, bl->address),
8189 paddress (bl->gdbarch, bl->address + bl->length - 1));
8190}
8191
8192/* Implement the "print_recreate" breakpoint_ops method for
8193 ranged breakpoints. */
8194
8195static void
8196print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8197{
8198 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8199 b->addr_string_range_end);
8200}
8201
8202/* The breakpoint_ops structure to be used in ranged breakpoints. */
8203
8204static struct breakpoint_ops ranged_breakpoint_ops =
8205{
348d480f
PA
8206 bkpt_dtor,
8207 bkpt_allocate_location,
8208 bkpt_re_set,
8209 bkpt_insert_location,
8210 bkpt_remove_location,
f1310107 8211 breakpoint_hit_ranged_breakpoint,
348d480f 8212 null_check_status,
f1310107 8213 resources_needed_ranged_breakpoint,
348d480f 8214 null_works_in_software_mode,
f1310107
TJB
8215 print_it_ranged_breakpoint,
8216 print_one_ranged_breakpoint,
8217 print_one_detail_ranged_breakpoint,
8218 print_mention_ranged_breakpoint,
8219 print_recreate_ranged_breakpoint
8220};
8221
8222/* Find the address where the end of the breakpoint range should be
8223 placed, given the SAL of the end of the range. This is so that if
8224 the user provides a line number, the end of the range is set to the
8225 last instruction of the given line. */
8226
8227static CORE_ADDR
8228find_breakpoint_range_end (struct symtab_and_line sal)
8229{
8230 CORE_ADDR end;
8231
8232 /* If the user provided a PC value, use it. Otherwise,
8233 find the address of the end of the given location. */
8234 if (sal.explicit_pc)
8235 end = sal.pc;
8236 else
8237 {
8238 int ret;
8239 CORE_ADDR start;
8240
8241 ret = find_line_pc_range (sal, &start, &end);
8242 if (!ret)
8243 error (_("Could not find location of the end of the range."));
8244
8245 /* find_line_pc_range returns the start of the next line. */
8246 end--;
8247 }
8248
8249 return end;
8250}
8251
8252/* Implement the "break-range" CLI command. */
8253
8254static void
8255break_range_command (char *arg, int from_tty)
8256{
8257 char *arg_start, *addr_string_start, *addr_string_end;
8258 struct linespec_result canonical_start, canonical_end;
8259 int bp_count, can_use_bp, length;
8260 CORE_ADDR end;
8261 struct breakpoint *b;
8262 struct symtab_and_line sal_start, sal_end;
8263 struct symtabs_and_lines sals_start, sals_end;
8264 struct cleanup *cleanup_bkpt;
8265
8266 /* We don't support software ranged breakpoints. */
8267 if (target_ranged_break_num_registers () < 0)
8268 error (_("This target does not support hardware ranged breakpoints."));
8269
8270 bp_count = hw_breakpoint_used_count ();
8271 bp_count += target_ranged_break_num_registers ();
8272 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8273 bp_count, 0);
8274 if (can_use_bp < 0)
8275 error (_("Hardware breakpoints used exceeds limit."));
8276
8277 if (arg == NULL || arg[0] == '\0')
8278 error(_("No address range specified."));
8279
8280 sals_start.sals = NULL;
8281 sals_start.nelts = 0;
8282 init_linespec_result (&canonical_start);
8283
8284 while (*arg == ' ' || *arg == '\t')
8285 arg++;
8286
58438ac1 8287 parse_breakpoint_sals (&arg, &sals_start, &canonical_start);
f1310107
TJB
8288
8289 sal_start = sals_start.sals[0];
8290 addr_string_start = canonical_start.canonical[0];
8291 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
8292 xfree (sals_start.sals);
8293 xfree (canonical_start.canonical);
8294
8295 if (arg[0] != ',')
8296 error (_("Too few arguments."));
8297 else if (sals_start.nelts == 0)
8298 error (_("Could not find location of the beginning of the range."));
8299 else if (sals_start.nelts != 1)
8300 error (_("Cannot create a ranged breakpoint with multiple locations."));
8301
8302 resolve_sal_pc (&sal_start);
8303
8304 arg++; /* Skip the comma. */
8305 while (*arg == ' ' || *arg == '\t')
8306 arg++;
8307
8308 /* Parse the end location. */
8309
8310 sals_end.sals = NULL;
8311 sals_end.nelts = 0;
8312 init_linespec_result (&canonical_end);
8313 arg_start = arg;
8314
423f41a5 8315 /* We call decode_line_1 directly here instead of using
f1310107
TJB
8316 parse_breakpoint_sals because we need to specify the start location's
8317 symtab and line as the default symtab and line for the end of the
8318 range. This makes it possible to have ranges like "foo.c:27, +14",
8319 where +14 means 14 lines from the start location. */
8320 sals_end = decode_line_1 (&arg, 1, sal_start.symtab, sal_start.line,
58438ac1 8321 &canonical_end);
f1310107
TJB
8322
8323 /* canonical_end can be NULL if it was of the form "*0xdeadbeef". */
8324 if (canonical_end.canonical == NULL)
38a714bb 8325 canonical_end.canonical = xcalloc (1, sizeof (char *));
f1310107
TJB
8326 /* Add the string if not present. */
8327 if (arg_start != arg && canonical_end.canonical[0] == NULL)
8328 canonical_end.canonical[0] = savestring (arg_start, arg - arg_start);
8329
8330 sal_end = sals_end.sals[0];
8331 addr_string_end = canonical_end.canonical[0];
8332 make_cleanup (xfree, addr_string_end);
8333 xfree (sals_end.sals);
8334 xfree (canonical_end.canonical);
8335
8336 if (sals_end.nelts == 0)
8337 error (_("Could not find location of the end of the range."));
8338 else if (sals_end.nelts != 1)
8339 error (_("Cannot create a ranged breakpoint with multiple locations."));
8340
8341 resolve_sal_pc (&sal_end);
8342
8343 end = find_breakpoint_range_end (sal_end);
8344 if (sal_start.pc > end)
177b42fe 8345 error (_("Invalid address range, end precedes start."));
f1310107
TJB
8346
8347 length = end - sal_start.pc + 1;
8348 if (length < 0)
8349 /* Length overflowed. */
8350 error (_("Address range too large."));
8351 else if (length == 1)
8352 {
8353 /* This range is simple enough to be handled by
8354 the `hbreak' command. */
8355 hbreak_command (addr_string_start, 1);
8356
8357 do_cleanups (cleanup_bkpt);
8358
8359 return;
8360 }
8361
8362 /* Now set up the breakpoint. */
8363 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 8364 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
8365 set_breakpoint_count (breakpoint_count + 1);
8366 b->number = breakpoint_count;
8367 b->disposition = disp_donttouch;
8368 b->addr_string = addr_string_start;
8369 b->addr_string_range_end = addr_string_end;
f1310107
TJB
8370 b->loc->length = length;
8371
8372 discard_cleanups (cleanup_bkpt);
8373
8374 mention (b);
8d3788bd 8375 observer_notify_breakpoint_created (b);
f1310107
TJB
8376 update_global_location_list (1);
8377}
8378
4a64f543
MS
8379/* Return non-zero if EXP is verified as constant. Returned zero
8380 means EXP is variable. Also the constant detection may fail for
8381 some constant expressions and in such case still falsely return
8382 zero. */
65d79d4b
SDJ
8383static int
8384watchpoint_exp_is_const (const struct expression *exp)
8385{
8386 int i = exp->nelts;
8387
8388 while (i > 0)
8389 {
8390 int oplenp, argsp;
8391
8392 /* We are only interested in the descriptor of each element. */
8393 operator_length (exp, i, &oplenp, &argsp);
8394 i -= oplenp;
8395
8396 switch (exp->elts[i].opcode)
8397 {
8398 case BINOP_ADD:
8399 case BINOP_SUB:
8400 case BINOP_MUL:
8401 case BINOP_DIV:
8402 case BINOP_REM:
8403 case BINOP_MOD:
8404 case BINOP_LSH:
8405 case BINOP_RSH:
8406 case BINOP_LOGICAL_AND:
8407 case BINOP_LOGICAL_OR:
8408 case BINOP_BITWISE_AND:
8409 case BINOP_BITWISE_IOR:
8410 case BINOP_BITWISE_XOR:
8411 case BINOP_EQUAL:
8412 case BINOP_NOTEQUAL:
8413 case BINOP_LESS:
8414 case BINOP_GTR:
8415 case BINOP_LEQ:
8416 case BINOP_GEQ:
8417 case BINOP_REPEAT:
8418 case BINOP_COMMA:
8419 case BINOP_EXP:
8420 case BINOP_MIN:
8421 case BINOP_MAX:
8422 case BINOP_INTDIV:
8423 case BINOP_CONCAT:
8424 case BINOP_IN:
8425 case BINOP_RANGE:
8426 case TERNOP_COND:
8427 case TERNOP_SLICE:
8428 case TERNOP_SLICE_COUNT:
8429
8430 case OP_LONG:
8431 case OP_DOUBLE:
8432 case OP_DECFLOAT:
8433 case OP_LAST:
8434 case OP_COMPLEX:
8435 case OP_STRING:
8436 case OP_BITSTRING:
8437 case OP_ARRAY:
8438 case OP_TYPE:
8439 case OP_NAME:
8440 case OP_OBJC_NSSTRING:
8441
8442 case UNOP_NEG:
8443 case UNOP_LOGICAL_NOT:
8444 case UNOP_COMPLEMENT:
8445 case UNOP_ADDR:
8446 case UNOP_HIGH:
4a64f543
MS
8447 /* Unary, binary and ternary operators: We have to check
8448 their operands. If they are constant, then so is the
8449 result of that operation. For instance, if A and B are
8450 determined to be constants, then so is "A + B".
8451
8452 UNOP_IND is one exception to the rule above, because the
8453 value of *ADDR is not necessarily a constant, even when
8454 ADDR is. */
65d79d4b
SDJ
8455 break;
8456
8457 case OP_VAR_VALUE:
8458 /* Check whether the associated symbol is a constant.
4a64f543 8459
65d79d4b 8460 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8461 possible that a buggy compiler could mark a variable as
8462 constant even when it is not, and TYPE_CONST would return
8463 true in this case, while SYMBOL_CLASS wouldn't.
8464
8465 We also have to check for function symbols because they
8466 are always constant. */
65d79d4b
SDJ
8467 {
8468 struct symbol *s = exp->elts[i + 2].symbol;
8469
8470 if (SYMBOL_CLASS (s) != LOC_BLOCK
8471 && SYMBOL_CLASS (s) != LOC_CONST
8472 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8473 return 0;
8474 break;
8475 }
8476
8477 /* The default action is to return 0 because we are using
8478 the optimistic approach here: If we don't know something,
8479 then it is not a constant. */
8480 default:
8481 return 0;
8482 }
8483 }
8484
8485 return 1;
8486}
8487
348d480f
PA
8488/* Implement the "re_set" breakpoint_ops method for watchpoints. */
8489
8490static void
8491re_set_watchpoint (struct breakpoint *b)
8492{
8493 /* Watchpoint can be either on expression using entirely global
8494 variables, or it can be on local variables.
8495
8496 Watchpoints of the first kind are never auto-deleted, and even
8497 persist across program restarts. Since they can use variables
8498 from shared libraries, we need to reparse expression as libraries
8499 are loaded and unloaded.
8500
8501 Watchpoints on local variables can also change meaning as result
8502 of solib event. For example, if a watchpoint uses both a local
8503 and a global variables in expression, it's a local watchpoint,
8504 but unloading of a shared library will make the expression
8505 invalid. This is not a very common use case, but we still
8506 re-evaluate expression, to avoid surprises to the user.
8507
8508 Note that for local watchpoints, we re-evaluate it only if
8509 watchpoints frame id is still valid. If it's not, it means the
8510 watchpoint is out of scope and will be deleted soon. In fact,
8511 I'm not sure we'll ever be called in this case.
8512
8513 If a local watchpoint's frame id is still valid, then
8514 b->exp_valid_block is likewise valid, and we can safely use it.
8515
8516 Don't do anything about disabled watchpoints, since they will
8517 be reevaluated again when enabled. */
8518 update_watchpoint (b, 1 /* reparse */);
8519}
8520
77b06cd7
TJB
8521/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8522
8523static int
8524insert_watchpoint (struct bp_location *bl)
8525{
e09342b5
TJB
8526 int length = bl->owner->exact? 1 : bl->length;
8527
8528 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8529 bl->owner->cond_exp);
77b06cd7
TJB
8530}
8531
8532/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8533
8534static int
8535remove_watchpoint (struct bp_location *bl)
8536{
e09342b5
TJB
8537 int length = bl->owner->exact? 1 : bl->length;
8538
8539 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8540 bl->owner->cond_exp);
8541}
8542
e09342b5 8543static int
348d480f
PA
8544breakpoint_hit_watchpoint (const struct bp_location *bl,
8545 struct address_space *aspace, CORE_ADDR bp_addr)
e09342b5 8546{
348d480f 8547 struct breakpoint *b = bl->owner;
77b06cd7 8548
348d480f
PA
8549 /* Continuable hardware watchpoints are treated as non-existent if the
8550 reason we stopped wasn't a hardware watchpoint (we didn't stop on
8551 some data address). Otherwise gdb won't stop on a break instruction
8552 in the code (not from a breakpoint) when a hardware watchpoint has
8553 been defined. Also skip watchpoints which we know did not trigger
8554 (did not match the data address). */
8555 if (is_hardware_watchpoint (b)
8556 && b->watchpoint_triggered == watch_triggered_no)
8557 return 0;
9c06b0b4 8558
348d480f 8559 return 1;
9c06b0b4
TJB
8560}
8561
348d480f
PA
8562static void
8563check_status_watchpoint (bpstat bs)
9c06b0b4 8564{
348d480f 8565 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 8566
348d480f 8567 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
8568}
8569
8570/* Implement the "resources_needed" breakpoint_ops method for
348d480f 8571 hardware watchpoints. */
9c06b0b4
TJB
8572
8573static int
348d480f 8574resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 8575{
348d480f
PA
8576 int length = bl->owner->exact? 1 : bl->length;
8577
8578 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
8579}
8580
8581/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 8582 hardware watchpoints. */
9c06b0b4
TJB
8583
8584static int
348d480f 8585works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 8586{
348d480f 8587 return b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
8588}
8589
9c06b0b4 8590static enum print_stop_action
348d480f 8591print_it_watchpoint (bpstat bs)
9c06b0b4 8592{
348d480f
PA
8593 struct cleanup *old_chain;
8594 struct breakpoint *b;
8595 const struct bp_location *bl;
8596 struct ui_stream *stb;
8597 enum print_stop_action result;
8598
8599 gdb_assert (bs->bp_location_at != NULL);
8600
8601 bl = bs->bp_location_at;
8602 b = bs->breakpoint_at;
8603
8604 stb = ui_out_stream_new (uiout);
8605 old_chain = make_cleanup_ui_out_stream_delete (stb);
9c06b0b4
TJB
8606
8607 switch (b->type)
8608 {
348d480f 8609 case bp_watchpoint:
9c06b0b4
TJB
8610 case bp_hardware_watchpoint:
8611 annotate_watchpoint (b->number);
8612 if (ui_out_is_mi_like_p (uiout))
8613 ui_out_field_string
8614 (uiout, "reason",
8615 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
8616 mention (b);
8617 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8618 ui_out_text (uiout, "\nOld value = ");
8619 watchpoint_value_print (bs->old_val, stb->stream);
8620 ui_out_field_stream (uiout, "old", stb);
8621 ui_out_text (uiout, "\nNew value = ");
8622 watchpoint_value_print (b->val, stb->stream);
8623 ui_out_field_stream (uiout, "new", stb);
8624 ui_out_text (uiout, "\n");
8625 /* More than one watchpoint may have been triggered. */
8626 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8627 break;
8628
8629 case bp_read_watchpoint:
8630 if (ui_out_is_mi_like_p (uiout))
8631 ui_out_field_string
8632 (uiout, "reason",
8633 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
8634 mention (b);
8635 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8636 ui_out_text (uiout, "\nValue = ");
8637 watchpoint_value_print (b->val, stb->stream);
8638 ui_out_field_stream (uiout, "value", stb);
8639 ui_out_text (uiout, "\n");
8640 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8641 break;
8642
8643 case bp_access_watchpoint:
348d480f
PA
8644 if (bs->old_val != NULL)
8645 {
8646 annotate_watchpoint (b->number);
8647 if (ui_out_is_mi_like_p (uiout))
8648 ui_out_field_string
8649 (uiout, "reason",
8650 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8651 mention (b);
8652 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8653 ui_out_text (uiout, "\nOld value = ");
8654 watchpoint_value_print (bs->old_val, stb->stream);
8655 ui_out_field_stream (uiout, "old", stb);
8656 ui_out_text (uiout, "\nNew value = ");
8657 }
8658 else
8659 {
8660 mention (b);
8661 if (ui_out_is_mi_like_p (uiout))
8662 ui_out_field_string
8663 (uiout, "reason",
8664 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8665 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8666 ui_out_text (uiout, "\nValue = ");
8667 }
8668 watchpoint_value_print (b->val, stb->stream);
8669 ui_out_field_stream (uiout, "new", stb);
8670 ui_out_text (uiout, "\n");
8671 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8672 break;
8673 default:
348d480f 8674 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8675 }
8676
348d480f
PA
8677 do_cleanups (old_chain);
8678 return result;
8679}
8680
8681/* Implement the "print_mention" breakpoint_ops method for hardware
8682 watchpoints. */
8683
8684static void
8685print_mention_watchpoint (struct breakpoint *b)
8686{
8687 struct cleanup *ui_out_chain;
8688
8689 switch (b->type)
8690 {
8691 case bp_watchpoint:
8692 ui_out_text (uiout, "Watchpoint ");
8693 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8694 break;
8695 case bp_hardware_watchpoint:
8696 ui_out_text (uiout, "Hardware watchpoint ");
8697 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8698 break;
8699 case bp_read_watchpoint:
8700 ui_out_text (uiout, "Hardware read watchpoint ");
8701 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8702 break;
8703 case bp_access_watchpoint:
8704 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
8705 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8706 break;
8707 default:
8708 internal_error (__FILE__, __LINE__,
8709 _("Invalid hardware watchpoint type."));
8710 }
8711
8712 ui_out_field_int (uiout, "number", b->number);
8713 ui_out_text (uiout, ": ");
8714 ui_out_field_string (uiout, "exp", b->exp_string);
8715 do_cleanups (ui_out_chain);
8716}
8717
8718/* Implement the "print_recreate" breakpoint_ops method for
8719 watchpoints. */
8720
8721static void
8722print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
8723{
8724 switch (b->type)
8725 {
8726 case bp_watchpoint:
8727 case bp_hardware_watchpoint:
8728 fprintf_unfiltered (fp, "watch");
8729 break;
8730 case bp_read_watchpoint:
8731 fprintf_unfiltered (fp, "rwatch");
8732 break;
8733 case bp_access_watchpoint:
8734 fprintf_unfiltered (fp, "awatch");
8735 break;
8736 default:
8737 internal_error (__FILE__, __LINE__,
8738 _("Invalid watchpoint type."));
8739 }
8740
8741 fprintf_unfiltered (fp, " %s", b->exp_string);
8742}
8743
8744/* The breakpoint_ops structure to be used in hardware watchpoints. */
8745
8746static struct breakpoint_ops watchpoint_breakpoint_ops =
8747{
8748 bkpt_dtor,
8749 bkpt_allocate_location,
8750 re_set_watchpoint,
8751 insert_watchpoint,
8752 remove_watchpoint,
8753 breakpoint_hit_watchpoint,
8754 check_status_watchpoint,
8755 resources_needed_watchpoint,
8756 works_in_software_mode_watchpoint,
8757 print_it_watchpoint,
8758 NULL, /* print_one */
8759 null_print_one_detail,
8760 print_mention_watchpoint,
8761 print_recreate_watchpoint
8762};
8763
8764/* Implement the "insert" breakpoint_ops method for
8765 masked hardware watchpoints. */
8766
8767static int
8768insert_masked_watchpoint (struct bp_location *bl)
8769{
8770 return target_insert_mask_watchpoint (bl->address, bl->owner->hw_wp_mask,
8771 bl->watchpoint_type);
8772}
8773
8774/* Implement the "remove" breakpoint_ops method for
8775 masked hardware watchpoints. */
8776
8777static int
8778remove_masked_watchpoint (struct bp_location *bl)
8779{
8780 return target_remove_mask_watchpoint (bl->address, bl->owner->hw_wp_mask,
8781 bl->watchpoint_type);
8782}
8783
8784/* Implement the "resources_needed" breakpoint_ops method for
8785 masked hardware watchpoints. */
8786
8787static int
8788resources_needed_masked_watchpoint (const struct bp_location *bl)
8789{
8790 return target_masked_watch_num_registers (bl->address,
8791 bl->owner->hw_wp_mask);
8792}
8793
8794/* Implement the "works_in_software_mode" breakpoint_ops method for
8795 masked hardware watchpoints. */
8796
8797static int
8798works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
8799{
8800 return 0;
8801}
8802
8803/* Implement the "print_it" breakpoint_ops method for
8804 masked hardware watchpoints. */
8805
8806static enum print_stop_action
8807print_it_masked_watchpoint (bpstat bs)
8808{
8809 struct breakpoint *b = bs->breakpoint_at;
8810
8811 /* Masked watchpoints have only one location. */
8812 gdb_assert (b->loc && b->loc->next == NULL);
8813
8814 switch (b->type)
8815 {
8816 case bp_hardware_watchpoint:
8817 annotate_watchpoint (b->number);
8818 if (ui_out_is_mi_like_p (uiout))
8819 ui_out_field_string
8820 (uiout, "reason",
8821 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
8822 break;
8823
8824 case bp_read_watchpoint:
8825 if (ui_out_is_mi_like_p (uiout))
8826 ui_out_field_string
8827 (uiout, "reason",
8828 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
8829 break;
8830
8831 case bp_access_watchpoint:
8832 if (ui_out_is_mi_like_p (uiout))
8833 ui_out_field_string
8834 (uiout, "reason",
8835 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8836 break;
8837 default:
8838 internal_error (__FILE__, __LINE__,
8839 _("Invalid hardware watchpoint type."));
8840 }
8841
8842 mention (b);
9c06b0b4
TJB
8843 ui_out_text (uiout, _("\n\
8844Check the underlying instruction at PC for the memory\n\
8845address and value which triggered this watchpoint.\n"));
8846 ui_out_text (uiout, "\n");
8847
8848 /* More than one watchpoint may have been triggered. */
8849 return PRINT_UNKNOWN;
8850}
8851
8852/* Implement the "print_one_detail" breakpoint_ops method for
8853 masked hardware watchpoints. */
8854
8855static void
8856print_one_detail_masked_watchpoint (const struct breakpoint *b,
8857 struct ui_out *uiout)
8858{
8859 /* Masked watchpoints have only one location. */
8860 gdb_assert (b->loc && b->loc->next == NULL);
8861
8862 ui_out_text (uiout, "\tmask ");
8863 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, b->hw_wp_mask);
8864 ui_out_text (uiout, "\n");
8865}
8866
8867/* Implement the "print_mention" breakpoint_ops method for
8868 masked hardware watchpoints. */
8869
8870static void
8871print_mention_masked_watchpoint (struct breakpoint *b)
8872{
8873 struct cleanup *ui_out_chain;
8874
8875 switch (b->type)
8876 {
8877 case bp_hardware_watchpoint:
8878 ui_out_text (uiout, "Masked hardware watchpoint ");
8879 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8880 break;
8881 case bp_read_watchpoint:
8882 ui_out_text (uiout, "Masked hardware read watchpoint ");
8883 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8884 break;
8885 case bp_access_watchpoint:
8886 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
8887 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8888 break;
8889 default:
8890 internal_error (__FILE__, __LINE__,
8891 _("Invalid hardware watchpoint type."));
8892 }
8893
8894 ui_out_field_int (uiout, "number", b->number);
8895 ui_out_text (uiout, ": ");
8896 ui_out_field_string (uiout, "exp", b->exp_string);
8897 do_cleanups (ui_out_chain);
8898}
8899
8900/* Implement the "print_recreate" breakpoint_ops method for
8901 masked hardware watchpoints. */
8902
8903static void
8904print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
8905{
8906 char tmp[40];
8907
8908 switch (b->type)
8909 {
8910 case bp_hardware_watchpoint:
8911 fprintf_unfiltered (fp, "watch");
8912 break;
8913 case bp_read_watchpoint:
8914 fprintf_unfiltered (fp, "rwatch");
8915 break;
8916 case bp_access_watchpoint:
8917 fprintf_unfiltered (fp, "awatch");
8918 break;
8919 default:
8920 internal_error (__FILE__, __LINE__,
8921 _("Invalid hardware watchpoint type."));
8922 }
8923
8924 sprintf_vma (tmp, b->hw_wp_mask);
8925 fprintf_unfiltered (fp, " %s mask 0x%s", b->exp_string, tmp);
8926}
8927
8928/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
8929
8930static struct breakpoint_ops masked_watchpoint_breakpoint_ops =
8931{
348d480f
PA
8932 bkpt_dtor,
8933 bkpt_allocate_location,
8934 re_set_watchpoint,
9c06b0b4
TJB
8935 insert_masked_watchpoint,
8936 remove_masked_watchpoint,
348d480f
PA
8937 breakpoint_hit_watchpoint,
8938 check_status_watchpoint,
9c06b0b4
TJB
8939 resources_needed_masked_watchpoint,
8940 works_in_software_mode_masked_watchpoint,
8941 print_it_masked_watchpoint,
8942 NULL, /* print_one */
8943 print_one_detail_masked_watchpoint,
8944 print_mention_masked_watchpoint,
8945 print_recreate_masked_watchpoint
8946};
8947
8948/* Tell whether the given watchpoint is a masked hardware watchpoint. */
8949
8950static int
8951is_masked_watchpoint (const struct breakpoint *b)
8952{
8953 return b->ops == &masked_watchpoint_breakpoint_ops;
8954}
8955
53a5351d
JM
8956/* accessflag: hw_write: watch write,
8957 hw_read: watch read,
8958 hw_access: watch access (read or write) */
c906108c 8959static void
84f4c1fe
PM
8960watch_command_1 (char *arg, int accessflag, int from_tty,
8961 int just_location, int internal)
c906108c 8962{
a9634178 8963 volatile struct gdb_exception e;
d983da9c 8964 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 8965 struct expression *exp;
60e1c644 8966 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 8967 struct value *val, *mark, *result;
c906108c 8968 struct frame_info *frame;
c906108c
SS
8969 char *exp_start = NULL;
8970 char *exp_end = NULL;
9c06b0b4
TJB
8971 char *tok, *end_tok;
8972 int toklen = -1;
c906108c
SS
8973 char *cond_start = NULL;
8974 char *cond_end = NULL;
c906108c 8975 enum bptype bp_type;
37e4754d 8976 int thread = -1;
0cf6dd15 8977 int pc = 0;
9c06b0b4
TJB
8978 /* Flag to indicate whether we are going to use masks for
8979 the hardware watchpoint. */
8980 int use_mask = 0;
8981 CORE_ADDR mask = 0;
c906108c 8982
37e4754d
LM
8983 /* Make sure that we actually have parameters to parse. */
8984 if (arg != NULL && arg[0] != '\0')
8985 {
9c06b0b4 8986 char *value_start;
37e4754d 8987
9c06b0b4
TJB
8988 /* Look for "parameter value" pairs at the end
8989 of the arguments string. */
8990 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
8991 {
8992 /* Skip whitespace at the end of the argument list. */
8993 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8994 tok--;
8995
8996 /* Find the beginning of the last token.
8997 This is the value of the parameter. */
8998 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8999 tok--;
9000 value_start = tok + 1;
9001
9002 /* Skip whitespace. */
9003 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9004 tok--;
9005
9006 end_tok = tok;
9007
9008 /* Find the beginning of the second to last token.
9009 This is the parameter itself. */
9010 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9011 tok--;
9012 tok++;
9013 toklen = end_tok - tok + 1;
9014
9015 if (toklen == 6 && !strncmp (tok, "thread", 6))
9016 {
9017 /* At this point we've found a "thread" token, which means
9018 the user is trying to set a watchpoint that triggers
9019 only in a specific thread. */
9020 char *endp;
37e4754d 9021
9c06b0b4
TJB
9022 if (thread != -1)
9023 error(_("You can specify only one thread."));
37e4754d 9024
9c06b0b4
TJB
9025 /* Extract the thread ID from the next token. */
9026 thread = strtol (value_start, &endp, 0);
37e4754d 9027
9c06b0b4
TJB
9028 /* Check if the user provided a valid numeric value for the
9029 thread ID. */
9030 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9031 error (_("Invalid thread ID specification %s."), value_start);
9032
9033 /* Check if the thread actually exists. */
9034 if (!valid_thread_id (thread))
9035 error (_("Unknown thread %d."), thread);
9036 }
9037 else if (toklen == 4 && !strncmp (tok, "mask", 4))
9038 {
9039 /* We've found a "mask" token, which means the user wants to
9040 create a hardware watchpoint that is going to have the mask
9041 facility. */
9042 struct value *mask_value, *mark;
37e4754d 9043
9c06b0b4
TJB
9044 if (use_mask)
9045 error(_("You can specify only one mask."));
37e4754d 9046
9c06b0b4 9047 use_mask = just_location = 1;
37e4754d 9048
9c06b0b4
TJB
9049 mark = value_mark ();
9050 mask_value = parse_to_comma_and_eval (&value_start);
9051 mask = value_as_address (mask_value);
9052 value_free_to_mark (mark);
9053 }
9054 else
9055 /* We didn't recognize what we found. We should stop here. */
9056 break;
37e4754d 9057
9c06b0b4
TJB
9058 /* Truncate the string and get rid of the "parameter value" pair before
9059 the arguments string is parsed by the parse_exp_1 function. */
9060 *tok = '\0';
9061 }
37e4754d
LM
9062 }
9063
9064 /* Parse the rest of the arguments. */
c906108c
SS
9065 innermost_block = NULL;
9066 exp_start = arg;
9067 exp = parse_exp_1 (&arg, 0, 0);
9068 exp_end = arg;
fa8a61dc
TT
9069 /* Remove trailing whitespace from the expression before saving it.
9070 This makes the eventual display of the expression string a bit
9071 prettier. */
9072 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9073 --exp_end;
9074
65d79d4b
SDJ
9075 /* Checking if the expression is not constant. */
9076 if (watchpoint_exp_is_const (exp))
9077 {
9078 int len;
9079
9080 len = exp_end - exp_start;
9081 while (len > 0 && isspace (exp_start[len - 1]))
9082 len--;
9083 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9084 }
9085
c906108c
SS
9086 exp_valid_block = innermost_block;
9087 mark = value_mark ();
a1442452 9088 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
9089
9090 if (just_location)
9091 {
9c06b0b4
TJB
9092 int ret;
9093
06a64a0b 9094 exp_valid_block = NULL;
a1442452 9095 val = value_addr (result);
06a64a0b
TT
9096 release_value (val);
9097 value_free_to_mark (mark);
9c06b0b4
TJB
9098
9099 if (use_mask)
9100 {
9101 ret = target_masked_watch_num_registers (value_as_address (val),
9102 mask);
9103 if (ret == -1)
9104 error (_("This target does not support masked watchpoints."));
9105 else if (ret == -2)
9106 error (_("Invalid mask or memory region."));
9107 }
06a64a0b
TT
9108 }
9109 else if (val != NULL)
fa4727a6 9110 release_value (val);
c906108c 9111
e9cafbcc
TT
9112 tok = skip_spaces (arg);
9113 end_tok = skip_to_space (tok);
c906108c
SS
9114
9115 toklen = end_tok - tok;
9116 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9117 {
2d134ed3
PA
9118 struct expression *cond;
9119
60e1c644 9120 innermost_block = NULL;
c906108c
SS
9121 tok = cond_start = end_tok + 1;
9122 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
9123
9124 /* The watchpoint expression may not be local, but the condition
9125 may still be. E.g.: `watch global if local > 0'. */
9126 cond_exp_valid_block = innermost_block;
9127
2d134ed3 9128 xfree (cond);
c906108c
SS
9129 cond_end = tok;
9130 }
9131 if (*tok)
8a3fe4f8 9132 error (_("Junk at end of command."));
c906108c 9133
53a5351d 9134 if (accessflag == hw_read)
c5aa993b 9135 bp_type = bp_read_watchpoint;
53a5351d 9136 else if (accessflag == hw_access)
c5aa993b
JM
9137 bp_type = bp_access_watchpoint;
9138 else
9139 bp_type = bp_hardware_watchpoint;
c906108c 9140
d983da9c 9141 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
9142
9143 /* If the expression is "local", then set up a "watchpoint scope"
9144 breakpoint at the point where we've left the scope of the watchpoint
9145 expression. Create the scope breakpoint before the watchpoint, so
9146 that we will encounter it first in bpstat_stop_status. */
60e1c644 9147 if (exp_valid_block && frame)
d983da9c 9148 {
edb3359d
DJ
9149 if (frame_id_p (frame_unwind_caller_id (frame)))
9150 {
9151 scope_breakpoint
a6d9a66e
UW
9152 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9153 frame_unwind_caller_pc (frame),
06edf0c0
PA
9154 bp_watchpoint_scope,
9155 &momentary_breakpoint_ops);
d983da9c 9156
edb3359d 9157 scope_breakpoint->enable_state = bp_enabled;
d983da9c 9158
edb3359d
DJ
9159 /* Automatically delete the breakpoint when it hits. */
9160 scope_breakpoint->disposition = disp_del;
d983da9c 9161
edb3359d
DJ
9162 /* Only break in the proper frame (help with recursion). */
9163 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 9164
edb3359d 9165 /* Set the address at which we will stop. */
a6d9a66e
UW
9166 scope_breakpoint->loc->gdbarch
9167 = frame_unwind_caller_arch (frame);
edb3359d
DJ
9168 scope_breakpoint->loc->requested_address
9169 = frame_unwind_caller_pc (frame);
9170 scope_breakpoint->loc->address
a6d9a66e
UW
9171 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9172 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9173 scope_breakpoint->type);
9174 }
d983da9c
DJ
9175 }
9176
c906108c 9177 /* Now set up the breakpoint. */
348d480f
PA
9178 if (use_mask)
9179 b = set_raw_breakpoint_without_location (NULL, bp_type,
9180 &masked_watchpoint_breakpoint_ops);
9181 else
9182 b = set_raw_breakpoint_without_location (NULL, bp_type,
9183 &watchpoint_breakpoint_ops);
37e4754d 9184 b->thread = thread;
b5de0fa7 9185 b->disposition = disp_donttouch;
c906108c
SS
9186 b->exp = exp;
9187 b->exp_valid_block = exp_valid_block;
60e1c644 9188 b->cond_exp_valid_block = cond_exp_valid_block;
348d480f 9189 b->pspace = current_program_space;
06a64a0b
TT
9190 if (just_location)
9191 {
9192 struct type *t = value_type (val);
9193 CORE_ADDR addr = value_as_address (val);
9194 char *name;
9195
9196 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9197 name = type_to_string (t);
9198
d63d0675
JK
9199 b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
9200 core_addr_to_string (addr));
06a64a0b
TT
9201 xfree (name);
9202
4a4106ca 9203 b->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9204 (int) (exp_end - exp_start), exp_start);
9205
06a64a0b
TT
9206 /* The above expression is in C. */
9207 b->language = language_c;
9208 }
9209 else
9210 b->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
9211
9212 if (use_mask)
9213 {
9214 b->hw_wp_mask = mask;
9c06b0b4
TJB
9215 }
9216 else
9217 {
9218 b->val = val;
9219 b->val_valid = 1;
9c06b0b4 9220 }
77b06cd7 9221
c906108c
SS
9222 if (cond_start)
9223 b->cond_string = savestring (cond_start, cond_end - cond_start);
9224 else
9225 b->cond_string = 0;
c5aa993b 9226
c906108c 9227 if (frame)
f6bc2008
PA
9228 {
9229 b->watchpoint_frame = get_frame_id (frame);
9230 b->watchpoint_thread = inferior_ptid;
9231 }
c906108c 9232 else
f6bc2008
PA
9233 {
9234 b->watchpoint_frame = null_frame_id;
9235 b->watchpoint_thread = null_ptid;
9236 }
c906108c 9237
d983da9c 9238 if (scope_breakpoint != NULL)
c906108c 9239 {
d983da9c
DJ
9240 /* The scope breakpoint is related to the watchpoint. We will
9241 need to act on them together. */
9242 b->related_breakpoint = scope_breakpoint;
9243 scope_breakpoint->related_breakpoint = b;
c906108c 9244 }
d983da9c 9245
06a64a0b
TT
9246 if (!just_location)
9247 value_free_to_mark (mark);
2d134ed3 9248
a9634178
TJB
9249 TRY_CATCH (e, RETURN_MASK_ALL)
9250 {
9251 /* Finally update the new watchpoint. This creates the locations
9252 that should be inserted. */
9253 update_watchpoint (b, 1);
9254 }
9255 if (e.reason < 0)
9256 {
9257 delete_breakpoint (b);
9258 throw_exception (e);
9259 }
9260
9261 set_breakpoint_number (internal, b);
8d3788bd
VP
9262
9263 /* Do not mention breakpoints with a negative number, but do
a9634178 9264 notify observers. */
8d3788bd 9265 if (!internal)
84f4c1fe 9266 mention (b);
8d3788bd
VP
9267 observer_notify_breakpoint_created (b);
9268
b60e7edf 9269 update_global_location_list (1);
c906108c
SS
9270}
9271
e09342b5 9272/* Return count of debug registers needed to watch the given expression.
e09342b5 9273 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9274
c906108c 9275static int
a9634178 9276can_use_hardware_watchpoint (struct value *v)
c906108c
SS
9277{
9278 int found_memory_cnt = 0;
2e70b7b9 9279 struct value *head = v;
c906108c
SS
9280
9281 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9282 if (!can_use_hw_watchpoints)
c906108c 9283 return 0;
c5aa993b 9284
5c44784c
JM
9285 /* Make sure that the value of the expression depends only upon
9286 memory contents, and values computed from them within GDB. If we
9287 find any register references or function calls, we can't use a
9288 hardware watchpoint.
9289
9290 The idea here is that evaluating an expression generates a series
9291 of values, one holding the value of every subexpression. (The
9292 expression a*b+c has five subexpressions: a, b, a*b, c, and
9293 a*b+c.) GDB's values hold almost enough information to establish
9294 the criteria given above --- they identify memory lvalues,
9295 register lvalues, computed values, etcetera. So we can evaluate
9296 the expression, and then scan the chain of values that leaves
9297 behind to decide whether we can detect any possible change to the
9298 expression's final value using only hardware watchpoints.
9299
9300 However, I don't think that the values returned by inferior
9301 function calls are special in any way. So this function may not
9302 notice that an expression involving an inferior function call
9303 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9304 for (; v; v = value_next (v))
c906108c 9305 {
5c44784c 9306 if (VALUE_LVAL (v) == lval_memory)
c906108c 9307 {
8464be76
DJ
9308 if (v != head && value_lazy (v))
9309 /* A lazy memory lvalue in the chain is one that GDB never
9310 needed to fetch; we either just used its address (e.g.,
9311 `a' in `a.b') or we never needed it at all (e.g., `a'
9312 in `a,b'). This doesn't apply to HEAD; if that is
9313 lazy then it was not readable, but watch it anyway. */
5c44784c 9314 ;
53a5351d 9315 else
5c44784c
JM
9316 {
9317 /* Ahh, memory we actually used! Check if we can cover
9318 it with hardware watchpoints. */
df407dfe 9319 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9320
9321 /* We only watch structs and arrays if user asked for it
9322 explicitly, never if they just happen to appear in a
9323 middle of some value chain. */
9324 if (v == head
9325 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9326 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9327 {
42ae5230 9328 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9329 int len;
9330 int num_regs;
9331
a9634178 9332 len = (target_exact_watchpoints
e09342b5
TJB
9333 && is_scalar_type_recursive (vtype))?
9334 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9335
e09342b5
TJB
9336 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9337 if (!num_regs)
2e70b7b9
MS
9338 return 0;
9339 else
e09342b5 9340 found_memory_cnt += num_regs;
2e70b7b9 9341 }
5c44784c 9342 }
c5aa993b 9343 }
5086187c
AC
9344 else if (VALUE_LVAL (v) != not_lval
9345 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9346 return 0; /* These are values from the history (e.g., $1). */
5086187c 9347 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9348 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9349 }
9350
9351 /* The expression itself looks suitable for using a hardware
9352 watchpoint, but give the target machine a chance to reject it. */
9353 return found_memory_cnt;
9354}
9355
8b93c638 9356void
84f4c1fe 9357watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9358{
84f4c1fe 9359 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9360}
9361
9362/* A helper function that looks for an argument at the start of a
9363 string. The argument must also either be at the end of the string,
9364 or be followed by whitespace. Returns 1 if it finds the argument,
9365 0 otherwise. If the argument is found, it updates *STR. */
9366
9367static int
9368check_for_argument (char **str, char *arg, int arg_len)
9369{
9370 if (strncmp (*str, arg, arg_len) == 0
9371 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9372 {
9373 *str += arg_len;
9374 return 1;
9375 }
9376 return 0;
9377}
9378
9379/* A helper function that looks for the "-location" argument and then
9380 calls watch_command_1. */
9381
9382static void
9383watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9384{
9385 int just_location = 0;
9386
9387 if (arg
9388 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9389 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9390 {
e9cafbcc 9391 arg = skip_spaces (arg);
06a64a0b
TT
9392 just_location = 1;
9393 }
9394
84f4c1fe 9395 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9396}
8926118c 9397
c5aa993b 9398static void
fba45db2 9399watch_command (char *arg, int from_tty)
c906108c 9400{
06a64a0b 9401 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9402}
9403
8b93c638 9404void
84f4c1fe 9405rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9406{
84f4c1fe 9407 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9408}
8926118c 9409
c5aa993b 9410static void
fba45db2 9411rwatch_command (char *arg, int from_tty)
c906108c 9412{
06a64a0b 9413 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9414}
9415
8b93c638 9416void
84f4c1fe 9417awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9418{
84f4c1fe 9419 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9420}
8926118c 9421
c5aa993b 9422static void
fba45db2 9423awatch_command (char *arg, int from_tty)
c906108c 9424{
06a64a0b 9425 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9426}
c906108c 9427\f
c5aa993b 9428
43ff13b4 9429/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9430 because it uses the mechanisms of breakpoints. */
9431
bfec99b2
PA
9432struct until_break_command_continuation_args
9433{
9434 struct breakpoint *breakpoint;
9435 struct breakpoint *breakpoint2;
186c406b 9436 int thread_num;
bfec99b2
PA
9437};
9438
43ff13b4 9439/* This function is called by fetch_inferior_event via the
4a64f543 9440 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9441 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9442 command. */
c2c6d25f 9443static void
fa4cd53f 9444until_break_command_continuation (void *arg, int err)
43ff13b4 9445{
bfec99b2
PA
9446 struct until_break_command_continuation_args *a = arg;
9447
9448 delete_breakpoint (a->breakpoint);
9449 if (a->breakpoint2)
9450 delete_breakpoint (a->breakpoint2);
186c406b 9451 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9452}
9453
c906108c 9454void
ae66c1fc 9455until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9456{
9457 struct symtabs_and_lines sals;
9458 struct symtab_and_line sal;
206415a3 9459 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9460 struct breakpoint *breakpoint;
f107f563 9461 struct breakpoint *breakpoint2 = NULL;
c906108c 9462 struct cleanup *old_chain;
186c406b
TT
9463 int thread;
9464 struct thread_info *tp;
c906108c
SS
9465
9466 clear_proceed_status ();
9467
9468 /* Set a breakpoint where the user wants it and at return from
4a64f543 9469 this function. */
c5aa993b 9470
c906108c
SS
9471 if (default_breakpoint_valid)
9472 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
58438ac1 9473 default_breakpoint_line, NULL);
c906108c 9474 else
58438ac1 9475 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL);
c5aa993b 9476
c906108c 9477 if (sals.nelts != 1)
8a3fe4f8 9478 error (_("Couldn't get information on specified line."));
c5aa993b 9479
c906108c 9480 sal = sals.sals[0];
4a64f543 9481 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9482
c906108c 9483 if (*arg)
8a3fe4f8 9484 error (_("Junk at end of arguments."));
c5aa993b 9485
c906108c 9486 resolve_sal_pc (&sal);
c5aa993b 9487
ae66c1fc
EZ
9488 if (anywhere)
9489 /* If the user told us to continue until a specified location,
9490 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9491 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9492 null_frame_id, bp_until);
ae66c1fc 9493 else
4a64f543
MS
9494 /* Otherwise, specify the selected frame, because we want to stop
9495 only at the very same frame. */
a6d9a66e
UW
9496 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9497 get_stack_frame_id (frame),
ae66c1fc 9498 bp_until);
c5aa993b 9499
f107f563 9500 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9501
186c406b
TT
9502 tp = inferior_thread ();
9503 thread = tp->num;
9504
ae66c1fc
EZ
9505 /* Keep within the current frame, or in frames called by the current
9506 one. */
edb3359d
DJ
9507
9508 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9509 {
edb3359d
DJ
9510 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9511 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9512 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9513 sal,
edb3359d 9514 frame_unwind_caller_id (frame),
f107f563
VP
9515 bp_until);
9516 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9517
9518 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9519 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9520 }
c5aa993b 9521
c906108c 9522 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9523
4a64f543
MS
9524 /* If we are running asynchronously, and proceed call above has
9525 actually managed to start the target, arrange for breakpoints to
9526 be deleted when the target stops. Otherwise, we're already
9527 stopped and delete breakpoints via cleanup chain. */
f107f563 9528
8ea051c5 9529 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9530 {
bfec99b2
PA
9531 struct until_break_command_continuation_args *args;
9532 args = xmalloc (sizeof (*args));
f107f563 9533
bfec99b2
PA
9534 args->breakpoint = breakpoint;
9535 args->breakpoint2 = breakpoint2;
186c406b 9536 args->thread_num = thread;
f107f563
VP
9537
9538 discard_cleanups (old_chain);
95e54da7
PA
9539 add_continuation (inferior_thread (),
9540 until_break_command_continuation, args,
604ead4a 9541 xfree);
f107f563
VP
9542 }
9543 else
c5aa993b 9544 do_cleanups (old_chain);
c906108c 9545}
ae66c1fc 9546
c906108c
SS
9547/* This function attempts to parse an optional "if <cond>" clause
9548 from the arg string. If one is not found, it returns NULL.
c5aa993b 9549
c906108c
SS
9550 Else, it returns a pointer to the condition string. (It does not
9551 attempt to evaluate the string against a particular block.) And,
9552 it updates arg to point to the first character following the parsed
4a64f543 9553 if clause in the arg string. */
53a5351d 9554
c906108c 9555static char *
fba45db2 9556ep_parse_optional_if_clause (char **arg)
c906108c 9557{
c5aa993b
JM
9558 char *cond_string;
9559
9560 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9561 return NULL;
c5aa993b 9562
4a64f543 9563 /* Skip the "if" keyword. */
c906108c 9564 (*arg) += 2;
c5aa993b 9565
c906108c 9566 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9567 condition string. */
e9cafbcc 9568 *arg = skip_spaces (*arg);
c906108c 9569 cond_string = *arg;
c5aa993b 9570
4a64f543
MS
9571 /* Assume that the condition occupies the remainder of the arg
9572 string. */
c906108c 9573 (*arg) += strlen (cond_string);
c5aa993b 9574
c906108c
SS
9575 return cond_string;
9576}
c5aa993b 9577
c906108c
SS
9578/* Commands to deal with catching events, such as signals, exceptions,
9579 process start/exit, etc. */
c5aa993b
JM
9580
9581typedef enum
9582{
44feb3ce
TT
9583 catch_fork_temporary, catch_vfork_temporary,
9584 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9585}
9586catch_fork_kind;
9587
c906108c 9588static void
cc59ec59
MS
9589catch_fork_command_1 (char *arg, int from_tty,
9590 struct cmd_list_element *command)
c906108c 9591{
a6d9a66e 9592 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9593 char *cond_string = NULL;
44feb3ce
TT
9594 catch_fork_kind fork_kind;
9595 int tempflag;
9596
9597 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9598 tempflag = (fork_kind == catch_fork_temporary
9599 || fork_kind == catch_vfork_temporary);
c5aa993b 9600
44feb3ce
TT
9601 if (!arg)
9602 arg = "";
e9cafbcc 9603 arg = skip_spaces (arg);
c5aa993b 9604
c906108c 9605 /* The allowed syntax is:
c5aa993b
JM
9606 catch [v]fork
9607 catch [v]fork if <cond>
9608
4a64f543 9609 First, check if there's an if clause. */
c906108c 9610 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9611
c906108c 9612 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9613 error (_("Junk at end of arguments."));
c5aa993b 9614
c906108c 9615 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9616 and enable reporting of such events. */
c5aa993b
JM
9617 switch (fork_kind)
9618 {
44feb3ce
TT
9619 case catch_fork_temporary:
9620 case catch_fork_permanent:
a6d9a66e 9621 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9622 &catch_fork_breakpoint_ops);
c906108c 9623 break;
44feb3ce
TT
9624 case catch_vfork_temporary:
9625 case catch_vfork_permanent:
a6d9a66e 9626 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9627 &catch_vfork_breakpoint_ops);
c906108c 9628 break;
c5aa993b 9629 default:
8a3fe4f8 9630 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9631 break;
c5aa993b 9632 }
c906108c
SS
9633}
9634
9635static void
cc59ec59
MS
9636catch_exec_command_1 (char *arg, int from_tty,
9637 struct cmd_list_element *command)
c906108c 9638{
b4d90040 9639 struct exec_catchpoint *c;
a6d9a66e 9640 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9641 int tempflag;
c5aa993b 9642 char *cond_string = NULL;
c906108c 9643
44feb3ce
TT
9644 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9645
9646 if (!arg)
9647 arg = "";
e9cafbcc 9648 arg = skip_spaces (arg);
c906108c
SS
9649
9650 /* The allowed syntax is:
c5aa993b
JM
9651 catch exec
9652 catch exec if <cond>
c906108c 9653
4a64f543 9654 First, check if there's an if clause. */
c906108c
SS
9655 cond_string = ep_parse_optional_if_clause (&arg);
9656
9657 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9658 error (_("Junk at end of arguments."));
c906108c 9659
b4d90040
PA
9660 c = XNEW (struct exec_catchpoint);
9661 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9662 &catch_exec_breakpoint_ops);
9663 c->exec_pathname = NULL;
9664
28010a5d 9665 install_breakpoint (&c->base);
c906108c 9666}
c5aa993b 9667
3086aeae 9668static enum print_stop_action
348d480f 9669print_it_exception_catchpoint (bpstat bs)
3086aeae 9670{
348d480f 9671 struct breakpoint *b = bs->breakpoint_at;
ade92717 9672 int bp_temp, bp_throw;
3086aeae 9673
ade92717 9674 annotate_catchpoint (b->number);
3086aeae 9675
ade92717
AR
9676 bp_throw = strstr (b->addr_string, "throw") != NULL;
9677 if (b->loc->address != b->loc->requested_address)
9678 breakpoint_adjustment_warning (b->loc->requested_address,
9679 b->loc->address,
9680 b->number, 1);
df2b6d2d 9681 bp_temp = b->disposition == disp_del;
ade92717
AR
9682 ui_out_text (uiout,
9683 bp_temp ? "Temporary catchpoint "
9684 : "Catchpoint ");
9685 if (!ui_out_is_mi_like_p (uiout))
9686 ui_out_field_int (uiout, "bkptno", b->number);
9687 ui_out_text (uiout,
c0b37c48
AR
9688 bp_throw ? " (exception thrown), "
9689 : " (exception caught), ");
ade92717
AR
9690 if (ui_out_is_mi_like_p (uiout))
9691 {
9692 ui_out_field_string (uiout, "reason",
9693 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9694 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9695 ui_out_field_int (uiout, "bkptno", b->number);
9696 }
3086aeae
DJ
9697 return PRINT_SRC_AND_LOC;
9698}
9699
9700static void
cc59ec59
MS
9701print_one_exception_catchpoint (struct breakpoint *b,
9702 struct bp_location **last_loc)
3086aeae 9703{
79a45b7d 9704 struct value_print_options opts;
cc59ec59 9705
79a45b7d
TT
9706 get_user_print_options (&opts);
9707 if (opts.addressprint)
3086aeae
DJ
9708 {
9709 annotate_field (4);
604133b5
AR
9710 if (b->loc == NULL || b->loc->shlib_disabled)
9711 ui_out_field_string (uiout, "addr", "<PENDING>");
9712 else
5af949e3
UW
9713 ui_out_field_core_addr (uiout, "addr",
9714 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9715 }
9716 annotate_field (5);
604133b5 9717 if (b->loc)
a6d9a66e 9718 *last_loc = b->loc;
3086aeae
DJ
9719 if (strstr (b->addr_string, "throw") != NULL)
9720 ui_out_field_string (uiout, "what", "exception throw");
9721 else
9722 ui_out_field_string (uiout, "what", "exception catch");
9723}
9724
9725static void
9726print_mention_exception_catchpoint (struct breakpoint *b)
9727{
ade92717
AR
9728 int bp_temp;
9729 int bp_throw;
9730
df2b6d2d 9731 bp_temp = b->disposition == disp_del;
ade92717
AR
9732 bp_throw = strstr (b->addr_string, "throw") != NULL;
9733 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9734 : _("Catchpoint "));
9735 ui_out_field_int (uiout, "bkptno", b->number);
9736 ui_out_text (uiout, bp_throw ? _(" (throw)")
9737 : _(" (catch)"));
3086aeae
DJ
9738}
9739
6149aea9
PA
9740/* Implement the "print_recreate" breakpoint_ops method for throw and
9741 catch catchpoints. */
9742
9743static void
4a64f543
MS
9744print_recreate_exception_catchpoint (struct breakpoint *b,
9745 struct ui_file *fp)
6149aea9
PA
9746{
9747 int bp_temp;
9748 int bp_throw;
9749
9750 bp_temp = b->disposition == disp_del;
9751 bp_throw = strstr (b->addr_string, "throw") != NULL;
9752 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9753 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9754}
9755
3086aeae 9756static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
348d480f
PA
9757 bkpt_dtor,
9758 bkpt_allocate_location,
9759 bkpt_re_set,
9760 bkpt_insert_location,
9761 bkpt_remove_location,
9762 bkpt_breakpoint_hit,
9763 bkpt_check_status,
9764 bkpt_resources_needed,
9765 null_works_in_software_mode,
ba770c9c 9766 print_it_exception_catchpoint,
3086aeae 9767 print_one_exception_catchpoint,
348d480f 9768 null_print_one_detail,
6149aea9
PA
9769 print_mention_exception_catchpoint,
9770 print_recreate_exception_catchpoint
3086aeae
DJ
9771};
9772
9773static int
9774handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9775 enum exception_event_kind ex_event, int from_tty)
9776{
604133b5
AR
9777 char *trigger_func_name;
9778
3086aeae 9779 if (ex_event == EX_EVENT_CATCH)
604133b5 9780 trigger_func_name = "__cxa_begin_catch";
3086aeae 9781 else
604133b5 9782 trigger_func_name = "__cxa_throw";
3086aeae 9783
8cdf0e15
VP
9784 create_breakpoint (get_current_arch (),
9785 trigger_func_name, cond_string, -1,
9786 0 /* condition and thread are valid. */,
0fb4aa4b 9787 tempflag, bp_breakpoint,
8cdf0e15
VP
9788 0,
9789 AUTO_BOOLEAN_TRUE /* pending */,
9790 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9791 1 /* enabled */,
9792 0 /* internal */);
3086aeae 9793
3086aeae
DJ
9794 return 1;
9795}
9796
4a64f543 9797/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9798
9799static void
fba45db2
KB
9800catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9801 int tempflag, int from_tty)
c906108c 9802{
c5aa993b 9803 char *cond_string = NULL;
c5aa993b 9804
44feb3ce
TT
9805 if (!arg)
9806 arg = "";
e9cafbcc 9807 arg = skip_spaces (arg);
c5aa993b 9808
c906108c
SS
9809 cond_string = ep_parse_optional_if_clause (&arg);
9810
9811 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9812 error (_("Junk at end of arguments."));
c906108c 9813
059fb39f
PM
9814 if (ex_event != EX_EVENT_THROW
9815 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9816 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9817
3086aeae
DJ
9818 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9819 return;
9820
8a3fe4f8 9821 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9822}
9823
44feb3ce
TT
9824/* Implementation of "catch catch" command. */
9825
9826static void
9827catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9828{
9829 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9830
44feb3ce
TT
9831 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9832}
9833
9834/* Implementation of "catch throw" command. */
9835
9836static void
9837catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9838{
9839 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9840
44feb3ce
TT
9841 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9842}
9843
9ac4176b 9844void
28010a5d
PA
9845init_ada_exception_breakpoint (struct breakpoint *b,
9846 struct gdbarch *gdbarch,
9847 struct symtab_and_line sal,
9848 char *addr_string,
9849 struct breakpoint_ops *ops,
9850 int tempflag,
9851 int from_tty)
f7f9143b 9852{
f7f9143b
JB
9853 if (from_tty)
9854 {
5af949e3
UW
9855 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9856 if (!loc_gdbarch)
9857 loc_gdbarch = gdbarch;
9858
6c95b8df
PA
9859 describe_other_breakpoints (loc_gdbarch,
9860 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9861 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9862 version for exception catchpoints, because two catchpoints
9863 used for different exception names will use the same address.
9864 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9865 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 9866 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
9867 the user what type of catchpoint it is. The above is good
9868 enough for now, though. */
9869 }
9870
28010a5d 9871 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
9872
9873 b->enable_state = bp_enabled;
9874 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
9875 b->addr_string = addr_string;
9876 b->language = language_ada;
f7f9143b
JB
9877}
9878
a96d9b2e
SDJ
9879/* Cleanup function for a syscall filter list. */
9880static void
9881clean_up_filters (void *arg)
9882{
9883 VEC(int) *iter = *(VEC(int) **) arg;
9884 VEC_free (int, iter);
9885}
9886
9887/* Splits the argument using space as delimiter. Returns an xmalloc'd
9888 filter list, or NULL if no filtering is required. */
9889static VEC(int) *
9890catch_syscall_split_args (char *arg)
9891{
9892 VEC(int) *result = NULL;
9893 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9894
9895 while (*arg != '\0')
9896 {
9897 int i, syscall_number;
9898 char *endptr;
9899 char cur_name[128];
9900 struct syscall s;
9901
9902 /* Skip whitespace. */
9903 while (isspace (*arg))
9904 arg++;
9905
9906 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9907 cur_name[i] = arg[i];
9908 cur_name[i] = '\0';
9909 arg += i;
9910
9911 /* Check if the user provided a syscall name or a number. */
9912 syscall_number = (int) strtol (cur_name, &endptr, 0);
9913 if (*endptr == '\0')
bccd0dd2 9914 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9915 else
9916 {
9917 /* We have a name. Let's check if it's valid and convert it
9918 to a number. */
9919 get_syscall_by_name (cur_name, &s);
9920
9921 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9922 /* Here we have to issue an error instead of a warning,
9923 because GDB cannot do anything useful if there's no
9924 syscall number to be caught. */
a96d9b2e
SDJ
9925 error (_("Unknown syscall name '%s'."), cur_name);
9926 }
9927
9928 /* Ok, it's valid. */
9929 VEC_safe_push (int, result, s.number);
9930 }
9931
9932 discard_cleanups (cleanup);
9933 return result;
9934}
9935
9936/* Implement the "catch syscall" command. */
9937
9938static void
cc59ec59
MS
9939catch_syscall_command_1 (char *arg, int from_tty,
9940 struct cmd_list_element *command)
a96d9b2e
SDJ
9941{
9942 int tempflag;
9943 VEC(int) *filter;
9944 struct syscall s;
9945 struct gdbarch *gdbarch = get_current_arch ();
9946
9947 /* Checking if the feature if supported. */
9948 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9949 error (_("The feature 'catch syscall' is not supported on \
ea666128 9950this architecture yet."));
a96d9b2e
SDJ
9951
9952 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9953
e9cafbcc 9954 arg = skip_spaces (arg);
a96d9b2e
SDJ
9955
9956 /* We need to do this first "dummy" translation in order
9957 to get the syscall XML file loaded or, most important,
9958 to display a warning to the user if there's no XML file
9959 for his/her architecture. */
9960 get_syscall_by_number (0, &s);
9961
9962 /* The allowed syntax is:
9963 catch syscall
9964 catch syscall <name | number> [<name | number> ... <name | number>]
9965
9966 Let's check if there's a syscall name. */
9967
9968 if (arg != NULL)
9969 filter = catch_syscall_split_args (arg);
9970 else
9971 filter = NULL;
9972
9973 create_syscall_event_catchpoint (tempflag, filter,
9974 &catch_syscall_breakpoint_ops);
9975}
9976
c906108c 9977static void
fba45db2 9978catch_command (char *arg, int from_tty)
c906108c 9979{
44feb3ce 9980 error (_("Catch requires an event name."));
c906108c
SS
9981}
9982\f
9983
9984static void
fba45db2 9985tcatch_command (char *arg, int from_tty)
c906108c 9986{
44feb3ce 9987 error (_("Catch requires an event name."));
c906108c
SS
9988}
9989
80f8a6eb 9990/* Delete breakpoints by address or line. */
c906108c
SS
9991
9992static void
fba45db2 9993clear_command (char *arg, int from_tty)
c906108c 9994{
d6e956e5
VP
9995 struct breakpoint *b;
9996 VEC(breakpoint_p) *found = 0;
9997 int ix;
c906108c
SS
9998 int default_match;
9999 struct symtabs_and_lines sals;
10000 struct symtab_and_line sal;
c906108c
SS
10001 int i;
10002
10003 if (arg)
10004 {
10005 sals = decode_line_spec (arg, 1);
10006 default_match = 0;
10007 }
10008 else
10009 {
c5aa993b 10010 sals.sals = (struct symtab_and_line *)
c906108c 10011 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 10012 make_cleanup (xfree, sals.sals);
4a64f543 10013 init_sal (&sal); /* Initialize to zeroes. */
c906108c
SS
10014 sal.line = default_breakpoint_line;
10015 sal.symtab = default_breakpoint_symtab;
10016 sal.pc = default_breakpoint_address;
6c95b8df 10017 sal.pspace = default_breakpoint_pspace;
c906108c 10018 if (sal.symtab == 0)
8a3fe4f8 10019 error (_("No source file specified."));
c906108c
SS
10020
10021 sals.sals[0] = sal;
10022 sals.nelts = 1;
10023
10024 default_match = 1;
10025 }
10026
4a64f543
MS
10027 /* We don't call resolve_sal_pc here. That's not as bad as it
10028 seems, because all existing breakpoints typically have both
10029 file/line and pc set. So, if clear is given file/line, we can
10030 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10031
10032 We only support clearing given the address explicitly
10033 present in breakpoint table. Say, we've set breakpoint
4a64f543 10034 at file:line. There were several PC values for that file:line,
ed0616c6 10035 due to optimization, all in one block.
4a64f543
MS
10036
10037 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10038 PC corresponding to the same file:line, the breakpoint won't
10039 be cleared. We probably can still clear the breakpoint, but
10040 since the other PC value is never presented to user, user
10041 can only find it by guessing, and it does not seem important
10042 to support that. */
10043
4a64f543
MS
10044 /* For each line spec given, delete bps which correspond to it. Do
10045 it in two passes, solely to preserve the current behavior that
10046 from_tty is forced true if we delete more than one
10047 breakpoint. */
c906108c 10048
80f8a6eb 10049 found = NULL;
c906108c
SS
10050 for (i = 0; i < sals.nelts; i++)
10051 {
10052 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
10053 If line given (pc == 0), clear all bpts on specified line.
10054 If defaulting, clear all bpts on default line
c906108c 10055 or at default pc.
c5aa993b
JM
10056
10057 defaulting sal.pc != 0 tests to do
10058
10059 0 1 pc
10060 1 1 pc _and_ line
10061 0 0 line
10062 1 0 <can't happen> */
c906108c
SS
10063
10064 sal = sals.sals[i];
c906108c 10065
4a64f543 10066 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 10067 ALL_BREAKPOINTS (b)
c5aa993b 10068 {
0d381245 10069 int match = 0;
4a64f543 10070 /* Are we going to delete b? */
cc60f2e3 10071 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
10072 {
10073 struct bp_location *loc = b->loc;
10074 for (; loc; loc = loc->next)
10075 {
6c95b8df
PA
10076 int pc_match = sal.pc
10077 && (loc->pspace == sal.pspace)
0d381245
VP
10078 && (loc->address == sal.pc)
10079 && (!section_is_overlay (loc->section)
10080 || loc->section == sal.section);
10081 int line_match = ((default_match || (0 == sal.pc))
10082 && b->source_file != NULL
10083 && sal.symtab != NULL
6c95b8df 10084 && sal.pspace == loc->pspace
0ba1096a
KT
10085 && filename_cmp (b->source_file,
10086 sal.symtab->filename) == 0
0d381245
VP
10087 && b->line_number == sal.line);
10088 if (pc_match || line_match)
10089 {
10090 match = 1;
10091 break;
10092 }
10093 }
10094 }
10095
10096 if (match)
d6e956e5 10097 VEC_safe_push(breakpoint_p, found, b);
c906108c 10098 }
80f8a6eb
MS
10099 }
10100 /* Now go thru the 'found' chain and delete them. */
d6e956e5 10101 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
10102 {
10103 if (arg)
8a3fe4f8 10104 error (_("No breakpoint at %s."), arg);
80f8a6eb 10105 else
8a3fe4f8 10106 error (_("No breakpoint at this line."));
80f8a6eb 10107 }
c906108c 10108
d6e956e5 10109 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 10110 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10111 if (from_tty)
a3f17187 10112 {
d6e956e5 10113 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10114 printf_unfiltered (_("Deleted breakpoint "));
10115 else
10116 printf_unfiltered (_("Deleted breakpoints "));
10117 }
80f8a6eb 10118 breakpoints_changed ();
d6e956e5
VP
10119
10120 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10121 {
c5aa993b 10122 if (from_tty)
d6e956e5
VP
10123 printf_unfiltered ("%d ", b->number);
10124 delete_breakpoint (b);
c906108c 10125 }
80f8a6eb
MS
10126 if (from_tty)
10127 putchar_unfiltered ('\n');
c906108c
SS
10128}
10129\f
10130/* Delete breakpoint in BS if they are `delete' breakpoints and
10131 all breakpoints that are marked for deletion, whether hit or not.
10132 This is called after any breakpoint is hit, or after errors. */
10133
10134void
fba45db2 10135breakpoint_auto_delete (bpstat bs)
c906108c 10136{
35df4500 10137 struct breakpoint *b, *b_tmp;
c906108c
SS
10138
10139 for (; bs; bs = bs->next)
f431efe5
PA
10140 if (bs->breakpoint_at
10141 && bs->breakpoint_at->disposition == disp_del
c906108c 10142 && bs->stop)
f431efe5 10143 delete_breakpoint (bs->breakpoint_at);
c906108c 10144
35df4500 10145 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10146 {
b5de0fa7 10147 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10148 delete_breakpoint (b);
10149 }
c906108c
SS
10150}
10151
4a64f543
MS
10152/* A comparison function for bp_location AP and BP being interfaced to
10153 qsort. Sort elements primarily by their ADDRESS (no matter what
10154 does breakpoint_address_is_meaningful say for its OWNER),
10155 secondarily by ordering first bp_permanent OWNERed elements and
10156 terciarily just ensuring the array is sorted stable way despite
e5dd4106 10157 qsort being an unstable algorithm. */
876fa593
JK
10158
10159static int
494cfb0f 10160bp_location_compare (const void *ap, const void *bp)
876fa593 10161{
494cfb0f
JK
10162 struct bp_location *a = *(void **) ap;
10163 struct bp_location *b = *(void **) bp;
2bdf28a0 10164 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10165 int a_perm = a->owner->enable_state == bp_permanent;
10166 int b_perm = b->owner->enable_state == bp_permanent;
10167
10168 if (a->address != b->address)
10169 return (a->address > b->address) - (a->address < b->address);
10170
10171 /* Sort permanent breakpoints first. */
10172 if (a_perm != b_perm)
10173 return (a_perm < b_perm) - (a_perm > b_perm);
10174
4a64f543
MS
10175 /* Make the user-visible order stable across GDB runs. Locations of
10176 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10177
10178 if (a->owner->number != b->owner->number)
10179 return (a->owner->number > b->owner->number)
10180 - (a->owner->number < b->owner->number);
10181
10182 return (a > b) - (a < b);
10183}
10184
876fa593 10185/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10186 bp_location_shadow_len_after_address_max according to the current
10187 content of the bp_location array. */
f7545552
TT
10188
10189static void
876fa593 10190bp_location_target_extensions_update (void)
f7545552 10191{
876fa593
JK
10192 struct bp_location *bl, **blp_tmp;
10193
10194 bp_location_placed_address_before_address_max = 0;
10195 bp_location_shadow_len_after_address_max = 0;
10196
10197 ALL_BP_LOCATIONS (bl, blp_tmp)
10198 {
10199 CORE_ADDR start, end, addr;
10200
10201 if (!bp_location_has_shadow (bl))
10202 continue;
10203
10204 start = bl->target_info.placed_address;
10205 end = start + bl->target_info.shadow_len;
10206
10207 gdb_assert (bl->address >= start);
10208 addr = bl->address - start;
10209 if (addr > bp_location_placed_address_before_address_max)
10210 bp_location_placed_address_before_address_max = addr;
10211
10212 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10213
10214 gdb_assert (bl->address < end);
10215 addr = end - bl->address;
10216 if (addr > bp_location_shadow_len_after_address_max)
10217 bp_location_shadow_len_after_address_max = addr;
10218 }
f7545552
TT
10219}
10220
4cd9bd08 10221/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10222 into the inferior, only remove already-inserted locations that no
10223 longer should be inserted. Functions that delete a breakpoint or
10224 breakpoints should pass false, so that deleting a breakpoint
10225 doesn't have the side effect of inserting the locations of other
10226 breakpoints that are marked not-inserted, but should_be_inserted
10227 returns true on them.
10228
10229 This behaviour is useful is situations close to tear-down -- e.g.,
10230 after an exec, while the target still has execution, but breakpoint
10231 shadows of the previous executable image should *NOT* be restored
10232 to the new image; or before detaching, where the target still has
10233 execution and wants to delete breakpoints from GDB's lists, and all
10234 breakpoints had already been removed from the inferior. */
10235
0d381245 10236static void
b60e7edf 10237update_global_location_list (int should_insert)
0d381245 10238{
74960c60 10239 struct breakpoint *b;
876fa593 10240 struct bp_location **locp, *loc;
f7545552
TT
10241 struct cleanup *cleanups;
10242
2d134ed3
PA
10243 /* Used in the duplicates detection below. When iterating over all
10244 bp_locations, points to the first bp_location of a given address.
10245 Breakpoints and watchpoints of different types are never
10246 duplicates of each other. Keep one pointer for each type of
10247 breakpoint/watchpoint, so we only need to loop over all locations
10248 once. */
10249 struct bp_location *bp_loc_first; /* breakpoint */
10250 struct bp_location *wp_loc_first; /* hardware watchpoint */
10251 struct bp_location *awp_loc_first; /* access watchpoint */
10252 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10253
4a64f543
MS
10254 /* Saved former bp_location array which we compare against the newly
10255 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10256 struct bp_location **old_location, **old_locp;
10257 unsigned old_location_count;
10258
10259 old_location = bp_location;
10260 old_location_count = bp_location_count;
10261 bp_location = NULL;
10262 bp_location_count = 0;
10263 cleanups = make_cleanup (xfree, old_location);
0d381245 10264
74960c60 10265 ALL_BREAKPOINTS (b)
876fa593
JK
10266 for (loc = b->loc; loc; loc = loc->next)
10267 bp_location_count++;
10268
10269 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10270 locp = bp_location;
10271 ALL_BREAKPOINTS (b)
10272 for (loc = b->loc; loc; loc = loc->next)
10273 *locp++ = loc;
10274 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10275 bp_location_compare);
876fa593
JK
10276
10277 bp_location_target_extensions_update ();
74960c60 10278
4a64f543
MS
10279 /* Identify bp_location instances that are no longer present in the
10280 new list, and therefore should be freed. Note that it's not
10281 necessary that those locations should be removed from inferior --
10282 if there's another location at the same address (previously
10283 marked as duplicate), we don't need to remove/insert the
10284 location.
876fa593 10285
4a64f543
MS
10286 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10287 and former bp_location array state respectively. */
876fa593
JK
10288
10289 locp = bp_location;
10290 for (old_locp = old_location; old_locp < old_location + old_location_count;
10291 old_locp++)
74960c60 10292 {
876fa593 10293 struct bp_location *old_loc = *old_locp;
c7d46a38 10294 struct bp_location **loc2p;
876fa593 10295
e5dd4106 10296 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10297 not, we have to free it. */
c7d46a38 10298 int found_object = 0;
20874c92
VP
10299 /* Tells if the location should remain inserted in the target. */
10300 int keep_in_target = 0;
10301 int removed = 0;
876fa593 10302
4a64f543
MS
10303 /* Skip LOCP entries which will definitely never be needed.
10304 Stop either at or being the one matching OLD_LOC. */
876fa593 10305 while (locp < bp_location + bp_location_count
c7d46a38 10306 && (*locp)->address < old_loc->address)
876fa593 10307 locp++;
c7d46a38
PA
10308
10309 for (loc2p = locp;
10310 (loc2p < bp_location + bp_location_count
10311 && (*loc2p)->address == old_loc->address);
10312 loc2p++)
10313 {
10314 if (*loc2p == old_loc)
10315 {
10316 found_object = 1;
10317 break;
10318 }
10319 }
74960c60 10320
4a64f543
MS
10321 /* If this location is no longer present, and inserted, look if
10322 there's maybe a new location at the same address. If so,
10323 mark that one inserted, and don't remove this one. This is
10324 needed so that we don't have a time window where a breakpoint
10325 at certain location is not inserted. */
74960c60 10326
876fa593 10327 if (old_loc->inserted)
0d381245 10328 {
4a64f543
MS
10329 /* If the location is inserted now, we might have to remove
10330 it. */
74960c60 10331
876fa593 10332 if (found_object && should_be_inserted (old_loc))
74960c60 10333 {
4a64f543
MS
10334 /* The location is still present in the location list,
10335 and still should be inserted. Don't do anything. */
20874c92 10336 keep_in_target = 1;
74960c60
VP
10337 }
10338 else
10339 {
4a64f543
MS
10340 /* The location is either no longer present, or got
10341 disabled. See if there's another location at the
10342 same address, in which case we don't need to remove
10343 this one from the target. */
876fa593 10344
2bdf28a0 10345 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10346 if (breakpoint_address_is_meaningful (old_loc->owner))
10347 {
876fa593 10348 for (loc2p = locp;
c7d46a38
PA
10349 (loc2p < bp_location + bp_location_count
10350 && (*loc2p)->address == old_loc->address);
876fa593
JK
10351 loc2p++)
10352 {
10353 struct bp_location *loc2 = *loc2p;
10354
2d134ed3 10355 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38
PA
10356 {
10357 /* For the sake of should_be_inserted.
4a64f543
MS
10358 Duplicates check below will fix up this
10359 later. */
c7d46a38 10360 loc2->duplicate = 0;
85d721b8
PA
10361
10362 /* Read watchpoint locations are switched to
10363 access watchpoints, if the former are not
10364 supported, but the latter are. */
10365 if (is_hardware_watchpoint (old_loc->owner))
10366 {
10367 gdb_assert (is_hardware_watchpoint (loc2->owner));
10368 loc2->watchpoint_type = old_loc->watchpoint_type;
10369 }
10370
c7d46a38
PA
10371 if (loc2 != old_loc && should_be_inserted (loc2))
10372 {
10373 loc2->inserted = 1;
10374 loc2->target_info = old_loc->target_info;
10375 keep_in_target = 1;
10376 break;
10377 }
876fa593
JK
10378 }
10379 }
10380 }
74960c60
VP
10381 }
10382
20874c92
VP
10383 if (!keep_in_target)
10384 {
876fa593 10385 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10386 {
4a64f543
MS
10387 /* This is just about all we can do. We could keep
10388 this location on the global list, and try to
10389 remove it next time, but there's no particular
10390 reason why we will succeed next time.
20874c92 10391
4a64f543
MS
10392 Note that at this point, old_loc->owner is still
10393 valid, as delete_breakpoint frees the breakpoint
10394 only after calling us. */
3e43a32a
MS
10395 printf_filtered (_("warning: Error removing "
10396 "breakpoint %d\n"),
876fa593 10397 old_loc->owner->number);
20874c92
VP
10398 }
10399 removed = 1;
10400 }
0d381245 10401 }
74960c60
VP
10402
10403 if (!found_object)
1c5cfe86 10404 {
db82e815
PA
10405 if (removed && non_stop
10406 && breakpoint_address_is_meaningful (old_loc->owner)
10407 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10408 {
db82e815
PA
10409 /* This location was removed from the target. In
10410 non-stop mode, a race condition is possible where
10411 we've removed a breakpoint, but stop events for that
10412 breakpoint are already queued and will arrive later.
10413 We apply an heuristic to be able to distinguish such
10414 SIGTRAPs from other random SIGTRAPs: we keep this
10415 breakpoint location for a bit, and will retire it
10416 after we see some number of events. The theory here
10417 is that reporting of events should, "on the average",
10418 be fair, so after a while we'll see events from all
10419 threads that have anything of interest, and no longer
10420 need to keep this breakpoint location around. We
10421 don't hold locations forever so to reduce chances of
10422 mistaking a non-breakpoint SIGTRAP for a breakpoint
10423 SIGTRAP.
10424
10425 The heuristic failing can be disastrous on
10426 decr_pc_after_break targets.
10427
10428 On decr_pc_after_break targets, like e.g., x86-linux,
10429 if we fail to recognize a late breakpoint SIGTRAP,
10430 because events_till_retirement has reached 0 too
10431 soon, we'll fail to do the PC adjustment, and report
10432 a random SIGTRAP to the user. When the user resumes
10433 the inferior, it will most likely immediately crash
2dec564e 10434 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10435 corrupted, because of being resumed e.g., in the
10436 middle of a multi-byte instruction, or skipped a
10437 one-byte instruction. This was actually seen happen
10438 on native x86-linux, and should be less rare on
10439 targets that do not support new thread events, like
10440 remote, due to the heuristic depending on
10441 thread_count.
10442
10443 Mistaking a random SIGTRAP for a breakpoint trap
10444 causes similar symptoms (PC adjustment applied when
10445 it shouldn't), but then again, playing with SIGTRAPs
10446 behind the debugger's back is asking for trouble.
10447
10448 Since hardware watchpoint traps are always
10449 distinguishable from other traps, so we don't need to
10450 apply keep hardware watchpoint moribund locations
10451 around. We simply always ignore hardware watchpoint
10452 traps we can no longer explain. */
10453
876fa593
JK
10454 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10455 old_loc->owner = NULL;
20874c92 10456
876fa593 10457 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10458 }
10459 else
f431efe5
PA
10460 {
10461 old_loc->owner = NULL;
10462 decref_bp_location (&old_loc);
10463 }
20874c92 10464 }
74960c60 10465 }
1c5cfe86 10466
348d480f
PA
10467 /* Rescan breakpoints at the same address and section, marking the
10468 first one as "first" and any others as "duplicates". This is so
10469 that the bpt instruction is only inserted once. If we have a
10470 permanent breakpoint at the same place as BPT, make that one the
10471 official one, and the rest as duplicates. Permanent breakpoints
10472 are sorted first for the same address.
10473
10474 Do the same for hardware watchpoints, but also considering the
10475 watchpoint's type (regular/access/read) and length. */
10476
10477 bp_loc_first = NULL;
10478 wp_loc_first = NULL;
10479 awp_loc_first = NULL;
10480 rwp_loc_first = NULL;
10481 ALL_BP_LOCATIONS (loc, locp)
10482 {
10483 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10484 non-NULL. */
10485 struct breakpoint *b = loc->owner;
10486 struct bp_location **loc_first_p;
10487
10488 if (b->enable_state == bp_disabled
10489 || b->enable_state == bp_call_disabled
10490 || b->enable_state == bp_startup_disabled
10491 || !loc->enabled
10492 || loc->shlib_disabled
10493 || !breakpoint_address_is_meaningful (b)
10494 || is_tracepoint (b))
10495 continue;
10496
10497 /* Permanent breakpoint should always be inserted. */
10498 if (b->enable_state == bp_permanent && ! loc->inserted)
10499 internal_error (__FILE__, __LINE__,
10500 _("allegedly permanent breakpoint is not "
10501 "actually inserted"));
10502
10503 if (b->type == bp_hardware_watchpoint)
10504 loc_first_p = &wp_loc_first;
10505 else if (b->type == bp_read_watchpoint)
10506 loc_first_p = &rwp_loc_first;
10507 else if (b->type == bp_access_watchpoint)
10508 loc_first_p = &awp_loc_first;
10509 else
10510 loc_first_p = &bp_loc_first;
10511
10512 if (*loc_first_p == NULL
10513 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10514 || !breakpoint_locations_match (loc, *loc_first_p))
10515 {
10516 *loc_first_p = loc;
10517 loc->duplicate = 0;
10518 continue;
10519 }
10520
10521 loc->duplicate = 1;
10522
10523 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10524 && b->enable_state != bp_permanent)
10525 internal_error (__FILE__, __LINE__,
10526 _("another breakpoint was inserted on top of "
10527 "a permanent breakpoint"));
10528 }
10529
10530 if (breakpoints_always_inserted_mode () && should_insert
10531 && (have_live_inferiors ()
10532 || (gdbarch_has_global_breakpoints (target_gdbarch))))
10533 insert_breakpoint_locations ();
10534
10535 do_cleanups (cleanups);
10536}
10537
10538void
10539breakpoint_retire_moribund (void)
10540{
10541 struct bp_location *loc;
10542 int ix;
10543
10544 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10545 if (--(loc->events_till_retirement) == 0)
10546 {
10547 decref_bp_location (&loc);
10548 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10549 --ix;
10550 }
10551}
10552
10553static void
10554update_global_location_list_nothrow (int inserting)
10555{
10556 struct gdb_exception e;
10557
10558 TRY_CATCH (e, RETURN_MASK_ERROR)
10559 update_global_location_list (inserting);
10560}
10561
10562/* Clear BKP from a BPS. */
10563
10564static void
10565bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10566{
10567 bpstat bs;
10568
10569 for (bs = bps; bs; bs = bs->next)
10570 if (bs->breakpoint_at == bpt)
10571 {
10572 bs->breakpoint_at = NULL;
10573 bs->old_val = NULL;
10574 /* bs->commands will be freed later. */
10575 }
10576}
10577
10578/* Callback for iterate_over_threads. */
10579static int
10580bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10581{
10582 struct breakpoint *bpt = data;
10583
10584 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10585 return 0;
10586}
10587
10588/* Helper for breakpoint and tracepoint breakpoint_ops->mention
10589 callbacks. */
10590
10591static void
10592say_where (struct breakpoint *b)
10593{
10594 struct value_print_options opts;
10595
10596 get_user_print_options (&opts);
10597
10598 /* i18n: cagney/2005-02-11: Below needs to be merged into a
10599 single string. */
10600 if (b->loc == NULL)
10601 {
10602 printf_filtered (_(" (%s) pending."), b->addr_string);
10603 }
10604 else
10605 {
10606 if (opts.addressprint || b->source_file == NULL)
10607 {
10608 printf_filtered (" at ");
10609 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10610 gdb_stdout);
10611 }
10612 if (b->source_file)
10613 printf_filtered (": file %s, line %d.",
10614 b->source_file, b->line_number);
10615
10616 if (b->loc->next)
10617 {
10618 struct bp_location *loc = b->loc;
10619 int n = 0;
10620 for (; loc; loc = loc->next)
10621 ++n;
10622 printf_filtered (" (%d locations)", n);
10623 }
10624 }
10625}
10626
10627/* Default breakpoint_ops methods that do nothing. */
10628
10629void
10630null_re_set (struct breakpoint *b)
10631{
10632 /* Nothing to re-set. */
10633}
10634
10635void
10636null_check_status (bpstat bs)
10637{
10638 /* nothing */
10639}
10640
10641/* A "works_in_software_mode" breakpoint_ops method that just internal
10642 errors. */
10643
10644int
10645null_works_in_software_mode (const struct breakpoint *b)
10646{
10647 gdb_assert_not_reached ("null_works_in_software_mode called");
10648}
10649
10650/* A "resources_needed" breakpoint_ops method that just internal
10651 errors. */
10652
10653int
10654null_resources_needed (const struct bp_location *bl)
10655{
10656 gdb_assert_not_reached ("null_resources_needed");
10657}
10658
10659void
10660null_print_one_detail (const struct breakpoint *self,
10661 struct ui_out *uiout)
10662{
10663 /* nothing */
10664}
10665
10666/* Default bp_location_ops methods. */
10667
10668static void
10669bp_location_dtor (struct bp_location *self)
10670{
10671 xfree (self->cond);
10672 xfree (self->function_name);
10673}
10674
10675static const struct bp_location_ops bp_location_ops =
10676{
10677 bp_location_dtor
10678};
10679
10680/* Default breakpoint_ops methods. */
10681
10682void
10683bkpt_dtor (struct breakpoint *self)
10684{
10685 decref_counted_command_line (&self->commands);
10686 xfree (self->cond_string);
10687 xfree (self->cond_exp);
10688 xfree (self->addr_string);
10689 xfree (self->addr_string_range_end);
10690 xfree (self->exp);
10691 xfree (self->exp_string);
10692 xfree (self->exp_string_reparse);
10693 value_free (self->val);
10694 xfree (self->source_file);
10695}
10696
10697struct bp_location *
10698bkpt_allocate_location (struct breakpoint *self)
10699{
10700 struct bp_location *loc;
10701
10702 loc = XNEW (struct bp_location);
10703 init_bp_location (loc, &bp_location_ops, self);
10704 return loc;
10705}
10706
10707void
10708bkpt_re_set (struct breakpoint *b)
10709{
06edf0c0
PA
10710 /* Do not attempt to re-set breakpoints disabled during startup. */
10711 if (b->enable_state == bp_startup_disabled)
10712 return;
348d480f 10713
06edf0c0
PA
10714 /* FIXME: is this still reachable? */
10715 if (b->addr_string == NULL)
10716 {
10717 /* Anything without a string can't be re-set. */
348d480f 10718 delete_breakpoint (b);
06edf0c0 10719 return;
348d480f 10720 }
06edf0c0
PA
10721
10722 breakpoint_re_set_default (b);
348d480f
PA
10723}
10724
10725int
10726bkpt_insert_location (struct bp_location *bl)
10727{
10728 if (bl->loc_type == bp_loc_hardware_breakpoint)
10729 return target_insert_hw_breakpoint (bl->gdbarch,
10730 &bl->target_info);
10731 else
10732 return target_insert_breakpoint (bl->gdbarch,
10733 &bl->target_info);
10734}
10735
10736int
10737bkpt_remove_location (struct bp_location *bl)
10738{
10739 if (bl->loc_type == bp_loc_hardware_breakpoint)
10740 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
10741 else
10742 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
10743}
10744
10745int
10746bkpt_breakpoint_hit (const struct bp_location *bl,
10747 struct address_space *aspace, CORE_ADDR bp_addr)
10748{
10749 struct breakpoint *b = bl->owner;
10750
10751 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
10752 aspace, bp_addr))
10753 return 0;
10754
10755 if (overlay_debugging /* unmapped overlay section */
10756 && section_is_overlay (bl->section)
10757 && !section_is_mapped (bl->section))
10758 return 0;
10759
10760 return 1;
10761}
10762
10763void
10764bkpt_check_status (bpstat bs)
10765{
06edf0c0 10766 /* nothing, always stop */
348d480f
PA
10767}
10768
10769int
10770bkpt_resources_needed (const struct bp_location *bl)
10771{
10772 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
10773
10774 return 1;
10775}
10776
10777int
10778bkpt_works_in_software_mode (const struct breakpoint *b)
10779{
10780 gdb_assert_not_reached ("bkpt_works_in_software_mode called");
10781}
10782
10783enum print_stop_action
10784bkpt_print_it (bpstat bs)
10785{
348d480f
PA
10786 struct breakpoint *b;
10787 const struct bp_location *bl;
001c8c33 10788 int bp_temp;
348d480f
PA
10789
10790 gdb_assert (bs->bp_location_at != NULL);
10791
10792 bl = bs->bp_location_at;
10793 b = bs->breakpoint_at;
10794
001c8c33
PA
10795 bp_temp = b->disposition == disp_del;
10796 if (bl->address != bl->requested_address)
10797 breakpoint_adjustment_warning (bl->requested_address,
10798 bl->address,
10799 b->number, 1);
10800 annotate_breakpoint (b->number);
10801 if (bp_temp)
10802 ui_out_text (uiout, "\nTemporary breakpoint ");
10803 else
10804 ui_out_text (uiout, "\nBreakpoint ");
10805 if (ui_out_is_mi_like_p (uiout))
348d480f 10806 {
001c8c33
PA
10807 ui_out_field_string (uiout, "reason",
10808 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10809 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 10810 }
001c8c33
PA
10811 ui_out_field_int (uiout, "bkptno", b->number);
10812 ui_out_text (uiout, ", ");
06edf0c0 10813
001c8c33 10814 return PRINT_SRC_AND_LOC;
06edf0c0
PA
10815}
10816
10817void
10818bkpt_print_mention (struct breakpoint *b)
10819{
10820 if (ui_out_is_mi_like_p (uiout))
10821 return;
10822
10823 switch (b->type)
10824 {
10825 case bp_breakpoint:
10826 case bp_gnu_ifunc_resolver:
10827 if (b->disposition == disp_del)
10828 printf_filtered (_("Temporary breakpoint"));
10829 else
10830 printf_filtered (_("Breakpoint"));
10831 printf_filtered (_(" %d"), b->number);
10832 if (b->type == bp_gnu_ifunc_resolver)
10833 printf_filtered (_(" at gnu-indirect-function resolver"));
10834 break;
10835 case bp_hardware_breakpoint:
10836 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
10837 break;
10838 }
10839
10840 say_where (b);
10841}
10842
10843void
10844bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
10845{
10846 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
10847 fprintf_unfiltered (fp, "tbreak");
10848 else if (tp->type == bp_breakpoint)
10849 fprintf_unfiltered (fp, "break");
10850 else if (tp->type == bp_hardware_breakpoint
10851 && tp->disposition == disp_del)
10852 fprintf_unfiltered (fp, "thbreak");
10853 else if (tp->type == bp_hardware_breakpoint)
10854 fprintf_unfiltered (fp, "hbreak");
10855 else
10856 internal_error (__FILE__, __LINE__,
10857 _("unhandled breakpoint type %d"), (int) tp->type);
10858
10859 if (tp->exp_string)
10860 fprintf_unfiltered (fp, " %s", tp->exp_string);
10861 else if (tp->addr_string)
10862 fprintf_unfiltered (fp, " %s", tp->addr_string);
10863 else
10864 {
10865 char tmp[40];
10866
10867 sprintf_vma (tmp, tp->loc->address);
10868 fprintf_unfiltered (fp, " *0x%s", tmp);
10869 }
10870}
10871
10872/* The breakpoint_ops structure to be used in regular breakpoints. */
10873
10874struct breakpoint_ops bkpt_breakpoint_ops =
10875{
10876 bkpt_dtor,
10877 bkpt_allocate_location,
10878 bkpt_re_set,
10879 bkpt_insert_location,
10880 bkpt_remove_location,
10881 bkpt_breakpoint_hit,
10882 bkpt_check_status,
10883 bkpt_resources_needed,
10884 null_works_in_software_mode,
10885 bkpt_print_it,
10886 NULL, /* print_one */
10887 null_print_one_detail,
10888 bkpt_print_mention,
10889 bkpt_print_recreate
10890};
10891
10892/* Virtual table for internal breakpoints. */
10893
10894static void
10895internal_bkpt_re_set (struct breakpoint *b)
10896{
10897 switch (b->type)
10898 {
10899 /* Delete overlay event and longjmp master breakpoints; they
10900 will be reset later by breakpoint_re_set. */
10901 case bp_overlay_event:
10902 case bp_longjmp_master:
10903 case bp_std_terminate_master:
10904 case bp_exception_master:
10905 delete_breakpoint (b);
10906 break;
10907
10908 /* This breakpoint is special, it's set up when the inferior
10909 starts and we really don't want to touch it. */
10910 case bp_shlib_event:
10911
10912 /* Like bp_shlib_event, this breakpoint type is special. Once
10913 it is set up, we do not want to touch it. */
10914 case bp_thread_event:
10915 break;
10916 }
10917}
10918
10919static void
10920internal_bkpt_check_status (bpstat bs)
10921{
10922 /* We do not stop for these. */
10923 bs->stop = 0;
10924}
10925
10926static enum print_stop_action
10927internal_bkpt_print_it (bpstat bs)
10928{
06edf0c0 10929 struct breakpoint *b;
06edf0c0 10930
06edf0c0
PA
10931 b = bs->breakpoint_at;
10932
06edf0c0
PA
10933 switch (b->type)
10934 {
348d480f
PA
10935 case bp_shlib_event:
10936 /* Did we stop because the user set the stop_on_solib_events
10937 variable? (If so, we report this as a generic, "Stopped due
10938 to shlib event" message.) */
10939 printf_filtered (_("Stopped due to shared library event\n"));
348d480f
PA
10940 break;
10941
10942 case bp_thread_event:
10943 /* Not sure how we will get here.
10944 GDB should not stop for these breakpoints. */
10945 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
10946 break;
10947
10948 case bp_overlay_event:
10949 /* By analogy with the thread event, GDB should not stop for these. */
10950 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
10951 break;
10952
10953 case bp_longjmp_master:
10954 /* These should never be enabled. */
10955 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
10956 break;
10957
10958 case bp_std_terminate_master:
10959 /* These should never be enabled. */
10960 printf_filtered (_("std::terminate Master Breakpoint: "
10961 "gdb should not stop!\n"));
348d480f
PA
10962 break;
10963
10964 case bp_exception_master:
10965 /* These should never be enabled. */
10966 printf_filtered (_("Exception Master Breakpoint: "
10967 "gdb should not stop!\n"));
06edf0c0
PA
10968 break;
10969 }
10970
001c8c33 10971 return PRINT_NOTHING;
06edf0c0
PA
10972}
10973
10974static void
10975internal_bkpt_print_mention (struct breakpoint *b)
10976{
10977 /* Nothing to mention. These breakpoints are internal. */
10978}
10979
10980static void
10981internal_bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
10982{
10983 gdb_assert_not_reached ("internal_bkpt_print_recreate called");
10984}
10985
10986/* The breakpoint_ops structure to be used with internal
10987 breakpoints. */
10988
10989static struct breakpoint_ops internal_breakpoint_ops =
10990{
10991 bkpt_dtor,
10992 bkpt_allocate_location,
10993 internal_bkpt_re_set,
10994 bkpt_insert_location,
10995 bkpt_remove_location,
10996 bkpt_breakpoint_hit,
10997 internal_bkpt_check_status,
10998 bkpt_resources_needed,
10999 null_works_in_software_mode,
11000 internal_bkpt_print_it,
11001 NULL, /* print_one */
11002 null_print_one_detail,
11003 internal_bkpt_print_mention,
11004 internal_bkpt_print_recreate
11005};
11006
11007/* Virtual table for momentary breakpoints */
11008
11009static void
11010momentary_bkpt_re_set (struct breakpoint *b)
11011{
11012 /* Keep temporary breakpoints, which can be encountered when we step
11013 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11014 Otherwise these should have been blown away via the cleanup chain
11015 or by breakpoint_init_inferior when we rerun the executable. */
11016}
11017
11018static void
11019momentary_bkpt_check_status (bpstat bs)
11020{
11021 /* Nothing. The point of these breakpoints is causing a stop. */
11022}
11023
11024static enum print_stop_action
11025momentary_bkpt_print_it (bpstat bs)
11026{
001c8c33 11027 if (ui_out_is_mi_like_p (uiout))
06edf0c0 11028 {
001c8c33 11029 struct breakpoint *b = bs->breakpoint_at;
348d480f 11030
001c8c33
PA
11031 switch (b->type)
11032 {
11033 case bp_finish:
11034 ui_out_field_string
11035 (uiout, "reason",
11036 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11037 break;
348d480f 11038
001c8c33
PA
11039 case bp_until:
11040 ui_out_field_string
11041 (uiout, "reason",
11042 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11043 break;
11044 }
348d480f
PA
11045 }
11046
001c8c33 11047 return PRINT_UNKNOWN;
348d480f
PA
11048}
11049
06edf0c0
PA
11050static void
11051momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 11052{
06edf0c0 11053 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11054}
11055
06edf0c0
PA
11056static void
11057momentary_bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
348d480f 11058{
06edf0c0 11059 gdb_assert_not_reached ("momentary_bkpt_print_recreate called");
348d480f 11060}
876fa593 11061
06edf0c0
PA
11062/* The breakpoint_ops structure to be used with momentary
11063 breakpoints. */
876fa593 11064
06edf0c0 11065static struct breakpoint_ops momentary_breakpoint_ops =
348d480f
PA
11066{
11067 bkpt_dtor,
11068 bkpt_allocate_location,
06edf0c0 11069 momentary_bkpt_re_set,
348d480f
PA
11070 bkpt_insert_location,
11071 bkpt_remove_location,
11072 bkpt_breakpoint_hit,
06edf0c0 11073 momentary_bkpt_check_status,
348d480f
PA
11074 bkpt_resources_needed,
11075 null_works_in_software_mode,
06edf0c0 11076 momentary_bkpt_print_it,
348d480f
PA
11077 NULL, /* print_one */
11078 null_print_one_detail,
06edf0c0
PA
11079 momentary_bkpt_print_mention,
11080 momentary_bkpt_print_recreate
348d480f 11081};
2d134ed3 11082
348d480f 11083/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 11084
348d480f
PA
11085static void
11086tracepoint_re_set (struct breakpoint *b)
11087{
11088 breakpoint_re_set_default (b);
11089}
876fa593 11090
348d480f
PA
11091static int
11092tracepoint_insert_location (struct bp_location *bl)
11093{
11094 gdb_assert_not_reached ("tracepoint_insert_location called");
11095}
74960c60 11096
348d480f
PA
11097static int
11098tracepoint_remove_location (struct bp_location *bl)
11099{
11100 gdb_assert_not_reached ("tracepoint_remove_location called");
11101}
f7545552 11102
348d480f
PA
11103static int
11104tracepoint_breakpoint_hit (const struct bp_location *bl,
11105 struct address_space *aspace, CORE_ADDR bp_addr)
11106{
11107 /* By definition, the inferior does not report stops at
11108 tracepoints. */
11109 return 0;
74960c60
VP
11110}
11111
348d480f
PA
11112static void
11113tracepoint_check_status (bpstat bs)
20874c92 11114{
348d480f
PA
11115 gdb_assert_not_reached ("tracepoint_check_status called");
11116}
20874c92 11117
348d480f
PA
11118static int
11119tracepoint_works_in_software_mode (const struct breakpoint *b)
11120{
11121 gdb_assert_not_reached ("tracepoint_works_in_software_mode called");
11122}
11123
11124static enum print_stop_action
11125tracepoint_print_it (bpstat bs)
11126{
11127 gdb_assert_not_reached ("tracepoint_print_it called");
20874c92
VP
11128}
11129
74960c60 11130static void
348d480f
PA
11131tracepoint_print_one_detail (const struct breakpoint *self,
11132 struct ui_out *uiout)
74960c60 11133{
348d480f
PA
11134 if (self->static_trace_marker_id)
11135 {
11136 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 11137
348d480f
PA
11138 ui_out_text (uiout, "\tmarker id is ");
11139 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
11140 self->static_trace_marker_id);
11141 ui_out_text (uiout, "\n");
11142 }
0d381245
VP
11143}
11144
a474d7c2 11145static void
348d480f 11146tracepoint_print_mention (struct breakpoint *b)
a474d7c2 11147{
348d480f
PA
11148 if (ui_out_is_mi_like_p (uiout))
11149 return;
cc59ec59 11150
348d480f
PA
11151 switch (b->type)
11152 {
11153 case bp_tracepoint:
11154 printf_filtered (_("Tracepoint"));
11155 printf_filtered (_(" %d"), b->number);
11156 break;
11157 case bp_fast_tracepoint:
11158 printf_filtered (_("Fast tracepoint"));
11159 printf_filtered (_(" %d"), b->number);
11160 break;
11161 case bp_static_tracepoint:
11162 printf_filtered (_("Static tracepoint"));
11163 printf_filtered (_(" %d"), b->number);
11164 break;
11165 default:
11166 internal_error (__FILE__, __LINE__,
11167 _("unhandled tracepoint type %d"), (int) b->type);
11168 }
11169
11170 say_where (b);
a474d7c2
PA
11171}
11172
348d480f
PA
11173static void
11174tracepoint_print_recreate (struct breakpoint *tp, struct ui_file *fp)
a474d7c2 11175{
348d480f
PA
11176 if (tp->type == bp_fast_tracepoint)
11177 fprintf_unfiltered (fp, "ftrace");
11178 if (tp->type == bp_static_tracepoint)
11179 fprintf_unfiltered (fp, "strace");
11180 else if (tp->type == bp_tracepoint)
11181 fprintf_unfiltered (fp, "trace");
11182 else
11183 internal_error (__FILE__, __LINE__,
11184 _("unhandled tracepoint type %d"), (int) tp->type);
cc59ec59 11185
348d480f 11186 fprintf_unfiltered (fp, " %s", tp->addr_string);
a474d7c2
PA
11187}
11188
348d480f
PA
11189struct breakpoint_ops tracepoint_breakpoint_ops =
11190{
11191 bkpt_dtor,
11192 bkpt_allocate_location,
11193 tracepoint_re_set,
11194 tracepoint_insert_location,
11195 tracepoint_remove_location,
11196 tracepoint_breakpoint_hit,
11197 tracepoint_check_status,
11198 null_resources_needed,
11199 tracepoint_works_in_software_mode,
11200 tracepoint_print_it,
11201 NULL, /* print_one */
11202 tracepoint_print_one_detail,
11203 tracepoint_print_mention,
11204 tracepoint_print_recreate
11205};
11206
53a5351d 11207/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 11208 structures. */
c906108c
SS
11209
11210void
fba45db2 11211delete_breakpoint (struct breakpoint *bpt)
c906108c 11212{
52f0bd74 11213 struct breakpoint *b;
c906108c 11214
8a3fe4f8 11215 gdb_assert (bpt != NULL);
c906108c 11216
4a64f543
MS
11217 /* Has this bp already been deleted? This can happen because
11218 multiple lists can hold pointers to bp's. bpstat lists are
11219 especial culprits.
11220
11221 One example of this happening is a watchpoint's scope bp. When
11222 the scope bp triggers, we notice that the watchpoint is out of
11223 scope, and delete it. We also delete its scope bp. But the
11224 scope bp is marked "auto-deleting", and is already on a bpstat.
11225 That bpstat is then checked for auto-deleting bp's, which are
11226 deleted.
11227
11228 A real solution to this problem might involve reference counts in
11229 bp's, and/or giving them pointers back to their referencing
11230 bpstat's, and teaching delete_breakpoint to only free a bp's
11231 storage when no more references were extent. A cheaper bandaid
11232 was chosen. */
c906108c
SS
11233 if (bpt->type == bp_none)
11234 return;
11235
4a64f543
MS
11236 /* At least avoid this stale reference until the reference counting
11237 of breakpoints gets resolved. */
d0fb5eae 11238 if (bpt->related_breakpoint != bpt)
e5a0a904 11239 {
d0fb5eae
JK
11240 struct breakpoint *related;
11241
11242 if (bpt->type == bp_watchpoint_scope)
11243 watchpoint_del_at_next_stop (bpt->related_breakpoint);
11244 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
11245 watchpoint_del_at_next_stop (bpt);
11246
11247 /* Unlink bpt from the bpt->related_breakpoint ring. */
11248 for (related = bpt; related->related_breakpoint != bpt;
11249 related = related->related_breakpoint);
11250 related->related_breakpoint = bpt->related_breakpoint;
11251 bpt->related_breakpoint = bpt;
e5a0a904
JK
11252 }
11253
a9634178
TJB
11254 /* watch_command_1 creates a watchpoint but only sets its number if
11255 update_watchpoint succeeds in creating its bp_locations. If there's
11256 a problem in that process, we'll be asked to delete the half-created
11257 watchpoint. In that case, don't announce the deletion. */
11258 if (bpt->number)
11259 observer_notify_breakpoint_deleted (bpt);
c906108c 11260
c906108c
SS
11261 if (breakpoint_chain == bpt)
11262 breakpoint_chain = bpt->next;
11263
c906108c
SS
11264 ALL_BREAKPOINTS (b)
11265 if (b->next == bpt)
c5aa993b
JM
11266 {
11267 b->next = bpt->next;
11268 break;
11269 }
c906108c 11270
f431efe5
PA
11271 /* Be sure no bpstat's are pointing at the breakpoint after it's
11272 been freed. */
11273 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 11274 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
11275 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11276 commands are associated with the bpstat; if we remove it here,
11277 then the later call to bpstat_do_actions (&stop_bpstat); in
11278 event-top.c won't do anything, and temporary breakpoints with
11279 commands won't work. */
11280
11281 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11282
4a64f543
MS
11283 /* Now that breakpoint is removed from breakpoint list, update the
11284 global location list. This will remove locations that used to
11285 belong to this breakpoint. Do this before freeing the breakpoint
11286 itself, since remove_breakpoint looks at location's owner. It
11287 might be better design to have location completely
11288 self-contained, but it's not the case now. */
b60e7edf 11289 update_global_location_list (0);
74960c60 11290
348d480f 11291 bpt->ops->dtor (bpt);
4a64f543
MS
11292 /* On the chance that someone will soon try again to delete this
11293 same bp, we mark it as deleted before freeing its storage. */
c906108c 11294 bpt->type = bp_none;
b8c9b27d 11295 xfree (bpt);
c906108c
SS
11296}
11297
4d6140d9
AC
11298static void
11299do_delete_breakpoint_cleanup (void *b)
11300{
11301 delete_breakpoint (b);
11302}
11303
11304struct cleanup *
11305make_cleanup_delete_breakpoint (struct breakpoint *b)
11306{
11307 return make_cleanup (do_delete_breakpoint_cleanup, b);
11308}
11309
51be5b68
PA
11310/* Iterator function to call a user-provided callback function once
11311 for each of B and its related breakpoints. */
11312
11313static void
11314iterate_over_related_breakpoints (struct breakpoint *b,
11315 void (*function) (struct breakpoint *,
11316 void *),
11317 void *data)
11318{
11319 struct breakpoint *related;
11320
11321 related = b;
11322 do
11323 {
11324 struct breakpoint *next;
11325
11326 /* FUNCTION may delete RELATED. */
11327 next = related->related_breakpoint;
11328
11329 if (next == related)
11330 {
11331 /* RELATED is the last ring entry. */
11332 function (related, data);
11333
11334 /* FUNCTION may have deleted it, so we'd never reach back to
11335 B. There's nothing left to do anyway, so just break
11336 out. */
11337 break;
11338 }
11339 else
11340 function (related, data);
11341
11342 related = next;
11343 }
11344 while (related != b);
11345}
95a42b64
TT
11346
11347static void
11348do_delete_breakpoint (struct breakpoint *b, void *ignore)
11349{
11350 delete_breakpoint (b);
11351}
11352
51be5b68
PA
11353/* A callback for map_breakpoint_numbers that calls
11354 delete_breakpoint. */
11355
11356static void
11357do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11358{
11359 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11360}
11361
c906108c 11362void
fba45db2 11363delete_command (char *arg, int from_tty)
c906108c 11364{
35df4500 11365 struct breakpoint *b, *b_tmp;
c906108c 11366
ea9365bb
TT
11367 dont_repeat ();
11368
c906108c
SS
11369 if (arg == 0)
11370 {
11371 int breaks_to_delete = 0;
11372
46c6471b
PA
11373 /* Delete all breakpoints if no argument. Do not delete
11374 internal breakpoints, these have to be deleted with an
11375 explicit breakpoint number argument. */
c5aa993b 11376 ALL_BREAKPOINTS (b)
46c6471b 11377 if (user_breakpoint_p (b))
973d738b
DJ
11378 {
11379 breaks_to_delete = 1;
11380 break;
11381 }
c906108c
SS
11382
11383 /* Ask user only if there are some breakpoints to delete. */
11384 if (!from_tty
e2e0b3e5 11385 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 11386 {
35df4500 11387 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 11388 if (user_breakpoint_p (b))
c5aa993b 11389 delete_breakpoint (b);
c906108c
SS
11390 }
11391 }
11392 else
51be5b68 11393 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
11394}
11395
0d381245
VP
11396static int
11397all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 11398{
0d381245
VP
11399 for (; loc; loc = loc->next)
11400 if (!loc->shlib_disabled)
11401 return 0;
11402 return 1;
fe3f5fa8
VP
11403}
11404
776592bf
DE
11405/* Subroutine of update_breakpoint_locations to simplify it.
11406 Return non-zero if multiple fns in list LOC have the same name.
11407 Null names are ignored. */
11408
11409static int
11410ambiguous_names_p (struct bp_location *loc)
11411{
11412 struct bp_location *l;
11413 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
11414 (int (*) (const void *,
11415 const void *)) streq,
776592bf
DE
11416 NULL, xcalloc, xfree);
11417
11418 for (l = loc; l != NULL; l = l->next)
11419 {
11420 const char **slot;
11421 const char *name = l->function_name;
11422
11423 /* Allow for some names to be NULL, ignore them. */
11424 if (name == NULL)
11425 continue;
11426
11427 slot = (const char **) htab_find_slot (htab, (const void *) name,
11428 INSERT);
4a64f543
MS
11429 /* NOTE: We can assume slot != NULL here because xcalloc never
11430 returns NULL. */
776592bf
DE
11431 if (*slot != NULL)
11432 {
11433 htab_delete (htab);
11434 return 1;
11435 }
11436 *slot = name;
11437 }
11438
11439 htab_delete (htab);
11440 return 0;
11441}
11442
0fb4aa4b
PA
11443/* When symbols change, it probably means the sources changed as well,
11444 and it might mean the static tracepoint markers are no longer at
11445 the same address or line numbers they used to be at last we
11446 checked. Losing your static tracepoints whenever you rebuild is
11447 undesirable. This function tries to resync/rematch gdb static
11448 tracepoints with the markers on the target, for static tracepoints
11449 that have not been set by marker id. Static tracepoint that have
11450 been set by marker id are reset by marker id in breakpoint_re_set.
11451 The heuristic is:
11452
11453 1) For a tracepoint set at a specific address, look for a marker at
11454 the old PC. If one is found there, assume to be the same marker.
11455 If the name / string id of the marker found is different from the
11456 previous known name, assume that means the user renamed the marker
11457 in the sources, and output a warning.
11458
11459 2) For a tracepoint set at a given line number, look for a marker
11460 at the new address of the old line number. If one is found there,
11461 assume to be the same marker. If the name / string id of the
11462 marker found is different from the previous known name, assume that
11463 means the user renamed the marker in the sources, and output a
11464 warning.
11465
11466 3) If a marker is no longer found at the same address or line, it
11467 may mean the marker no longer exists. But it may also just mean
11468 the code changed a bit. Maybe the user added a few lines of code
11469 that made the marker move up or down (in line number terms). Ask
11470 the target for info about the marker with the string id as we knew
11471 it. If found, update line number and address in the matching
11472 static tracepoint. This will get confused if there's more than one
11473 marker with the same ID (possible in UST, although unadvised
11474 precisely because it confuses tools). */
11475
11476static struct symtab_and_line
11477update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11478{
11479 struct static_tracepoint_marker marker;
11480 CORE_ADDR pc;
11481 int i;
11482
11483 pc = sal.pc;
11484 if (sal.line)
11485 find_line_pc (sal.symtab, sal.line, &pc);
11486
11487 if (target_static_tracepoint_marker_at (pc, &marker))
11488 {
11489 if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
11490 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11491 b->number,
11492 b->static_trace_marker_id, marker.str_id);
11493
11494 xfree (b->static_trace_marker_id);
11495 b->static_trace_marker_id = xstrdup (marker.str_id);
11496 release_static_tracepoint_marker (&marker);
11497
11498 return sal;
11499 }
11500
11501 /* Old marker wasn't found on target at lineno. Try looking it up
11502 by string ID. */
11503 if (!sal.explicit_pc
11504 && sal.line != 0
11505 && sal.symtab != NULL
11506 && b->static_trace_marker_id != NULL)
11507 {
11508 VEC(static_tracepoint_marker_p) *markers;
11509
11510 markers
11511 = target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
11512
11513 if (!VEC_empty(static_tracepoint_marker_p, markers))
11514 {
11515 struct symtab_and_line sal;
11516 struct symbol *sym;
11517 struct static_tracepoint_marker *marker;
11518
11519 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
11520
11521 xfree (b->static_trace_marker_id);
11522 b->static_trace_marker_id = xstrdup (marker->str_id);
11523
11524 warning (_("marker for static tracepoint %d (%s) not "
11525 "found at previous line number"),
11526 b->number, b->static_trace_marker_id);
11527
11528 init_sal (&sal);
11529
11530 sal.pc = marker->address;
11531
11532 sal = find_pc_line (marker->address, 0);
11533 sym = find_pc_sect_function (marker->address, NULL);
11534 ui_out_text (uiout, "Now in ");
11535 if (sym)
11536 {
11537 ui_out_field_string (uiout, "func",
11538 SYMBOL_PRINT_NAME (sym));
11539 ui_out_text (uiout, " at ");
11540 }
11541 ui_out_field_string (uiout, "file", sal.symtab->filename);
11542 ui_out_text (uiout, ":");
11543
11544 if (ui_out_is_mi_like_p (uiout))
11545 {
11546 char *fullname = symtab_to_fullname (sal.symtab);
11547
11548 if (fullname)
11549 ui_out_field_string (uiout, "fullname", fullname);
11550 }
11551
11552 ui_out_field_int (uiout, "line", sal.line);
11553 ui_out_text (uiout, "\n");
11554
11555 b->line_number = sal.line;
11556
11557 xfree (b->source_file);
11558 if (sym)
11559 b->source_file = xstrdup (sal.symtab->filename);
11560 else
11561 b->source_file = NULL;
11562
11563 xfree (b->addr_string);
11564 b->addr_string = xstrprintf ("%s:%d",
11565 sal.symtab->filename, b->line_number);
11566
11567 /* Might be nice to check if function changed, and warn if
11568 so. */
11569
11570 release_static_tracepoint_marker (marker);
11571 }
11572 }
11573 return sal;
11574}
11575
8d3788bd
VP
11576/* Returns 1 iff locations A and B are sufficiently same that
11577 we don't need to report breakpoint as changed. */
11578
11579static int
11580locations_are_equal (struct bp_location *a, struct bp_location *b)
11581{
11582 while (a && b)
11583 {
11584 if (a->address != b->address)
11585 return 0;
11586
11587 if (a->shlib_disabled != b->shlib_disabled)
11588 return 0;
11589
11590 if (a->enabled != b->enabled)
11591 return 0;
11592
11593 a = a->next;
11594 b = b->next;
11595 }
11596
11597 if ((a == NULL) != (b == NULL))
11598 return 0;
11599
11600 return 1;
11601}
11602
f1310107
TJB
11603/* Create new breakpoint locations for B (a hardware or software breakpoint)
11604 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11605 a ranged breakpoint. */
11606
0e30163f 11607void
0d381245 11608update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
11609 struct symtabs_and_lines sals,
11610 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
11611{
11612 int i;
0d381245
VP
11613 struct bp_location *existing_locations = b->loc;
11614
f1310107
TJB
11615 /* Ranged breakpoints have only one start location and one end location. */
11616 gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
11617
4a64f543
MS
11618 /* If there's no new locations, and all existing locations are
11619 pending, don't do anything. This optimizes the common case where
11620 all locations are in the same shared library, that was unloaded.
11621 We'd like to retain the location, so that when the library is
11622 loaded again, we don't loose the enabled/disabled status of the
11623 individual locations. */
0d381245 11624 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
11625 return;
11626
fe3f5fa8
VP
11627 b->loc = NULL;
11628
0d381245 11629 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 11630 {
0d381245 11631 struct bp_location *new_loc =
39d61571 11632 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 11633
0d381245
VP
11634 /* Reparse conditions, they might contain references to the
11635 old symtab. */
11636 if (b->cond_string != NULL)
11637 {
f1310107 11638 char *s;
0d381245 11639 struct gdb_exception e;
fe3f5fa8 11640
0d381245
VP
11641 s = b->cond_string;
11642 TRY_CATCH (e, RETURN_MASK_ERROR)
11643 {
11644 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11645 0);
11646 }
11647 if (e.reason < 0)
11648 {
3e43a32a
MS
11649 warning (_("failed to reevaluate condition "
11650 "for breakpoint %d: %s"),
0d381245
VP
11651 b->number, e.message);
11652 new_loc->enabled = 0;
11653 }
11654 }
fe3f5fa8 11655
0d381245
VP
11656 if (b->source_file != NULL)
11657 xfree (b->source_file);
11658 if (sals.sals[i].symtab == NULL)
11659 b->source_file = NULL;
11660 else
1b36a34b 11661 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 11662
0d381245
VP
11663 if (b->line_number == 0)
11664 b->line_number = sals.sals[i].line;
f1310107
TJB
11665
11666 if (sals_end.nelts)
11667 {
11668 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11669
11670 new_loc->length = end - sals.sals[0].pc + 1;
11671 }
0d381245 11672 }
fe3f5fa8 11673
514f746b
AR
11674 /* Update locations of permanent breakpoints. */
11675 if (b->enable_state == bp_permanent)
11676 make_breakpoint_permanent (b);
11677
4a64f543
MS
11678 /* If possible, carry over 'disable' status from existing
11679 breakpoints. */
0d381245
VP
11680 {
11681 struct bp_location *e = existing_locations;
776592bf
DE
11682 /* If there are multiple breakpoints with the same function name,
11683 e.g. for inline functions, comparing function names won't work.
11684 Instead compare pc addresses; this is just a heuristic as things
11685 may have moved, but in practice it gives the correct answer
11686 often enough until a better solution is found. */
11687 int have_ambiguous_names = ambiguous_names_p (b->loc);
11688
0d381245
VP
11689 for (; e; e = e->next)
11690 {
11691 if (!e->enabled && e->function_name)
11692 {
11693 struct bp_location *l = b->loc;
776592bf
DE
11694 if (have_ambiguous_names)
11695 {
11696 for (; l; l = l->next)
f1310107 11697 if (breakpoint_locations_match (e, l))
776592bf
DE
11698 {
11699 l->enabled = 0;
11700 break;
11701 }
11702 }
11703 else
11704 {
11705 for (; l; l = l->next)
11706 if (l->function_name
11707 && strcmp (e->function_name, l->function_name) == 0)
11708 {
11709 l->enabled = 0;
11710 break;
11711 }
11712 }
0d381245
VP
11713 }
11714 }
11715 }
fe3f5fa8 11716
8d3788bd
VP
11717 if (!locations_are_equal (existing_locations, b->loc))
11718 observer_notify_breakpoint_modified (b);
11719
b60e7edf 11720 update_global_location_list (1);
fe3f5fa8
VP
11721}
11722
ef23e705
TJB
11723/* Find the SaL locations corresponding to the given ADDR_STRING.
11724 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11725
11726static struct symtabs_and_lines
11727addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11728{
11729 char *s;
58438ac1 11730 int marker_spec;
02d20e4a 11731 struct symtabs_and_lines sals = {0};
ef23e705
TJB
11732 struct gdb_exception e;
11733
11734 s = addr_string;
11735 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11736
11737 TRY_CATCH (e, RETURN_MASK_ERROR)
11738 {
11739 if (marker_spec)
11740 {
11741 sals = decode_static_tracepoint_spec (&s);
11742 if (sals.nelts > b->static_trace_marker_id_idx)
11743 {
11744 sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
11745 sals.nelts = 1;
11746 }
11747 else
11748 error (_("marker %s not found"), b->static_trace_marker_id);
11749 }
11750 else
58438ac1 11751 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, NULL);
ef23e705
TJB
11752 }
11753 if (e.reason < 0)
11754 {
11755 int not_found_and_ok = 0;
11756 /* For pending breakpoints, it's expected that parsing will
11757 fail until the right shared library is loaded. User has
11758 already told to create pending breakpoints and don't need
11759 extra messages. If breakpoint is in bp_shlib_disabled
11760 state, then user already saw the message about that
11761 breakpoint being disabled, and don't want to see more
11762 errors. */
58438ac1 11763 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
11764 && (b->condition_not_parsed
11765 || (b->loc && b->loc->shlib_disabled)
11766 || b->enable_state == bp_disabled))
11767 not_found_and_ok = 1;
11768
11769 if (!not_found_and_ok)
11770 {
11771 /* We surely don't want to warn about the same breakpoint
11772 10 times. One solution, implemented here, is disable
11773 the breakpoint on error. Another solution would be to
11774 have separate 'warning emitted' flag. Since this
11775 happens only when a binary has changed, I don't know
11776 which approach is better. */
11777 b->enable_state = bp_disabled;
11778 throw_exception (e);
11779 }
11780 }
11781
58438ac1 11782 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705
TJB
11783 {
11784 gdb_assert (sals.nelts == 1);
11785
11786 resolve_sal_pc (&sals.sals[0]);
11787 if (b->condition_not_parsed && s && s[0])
11788 {
11789 char *cond_string = 0;
11790 int thread = -1;
11791 int task = 0;
11792
11793 find_condition_and_thread (s, sals.sals[0].pc,
11794 &cond_string, &thread, &task);
11795 if (cond_string)
11796 b->cond_string = cond_string;
11797 b->thread = thread;
11798 b->task = task;
11799 b->condition_not_parsed = 0;
11800 }
11801
11802 if (b->type == bp_static_tracepoint && !marker_spec)
11803 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 11804
58438ac1
TT
11805 *found = 1;
11806 }
11807 else
11808 *found = 0;
ef23e705
TJB
11809
11810 return sals;
11811}
11812
348d480f
PA
11813/* The default re_set method, for typical hardware or software
11814 breakpoints. Reevaluate the breakpoint and recreate its
11815 locations. */
11816
11817static void
28010a5d 11818breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
11819{
11820 int found;
f1310107 11821 struct symtabs_and_lines sals, sals_end;
ef23e705 11822 struct symtabs_and_lines expanded = {0};
f1310107 11823 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
11824
11825 sals = addr_string_to_sals (b, b->addr_string, &found);
11826 if (found)
11827 {
11828 make_cleanup (xfree, sals.sals);
11829 expanded = expand_line_sal_maybe (sals.sals[0]);
11830 }
11831
f1310107
TJB
11832 if (b->addr_string_range_end)
11833 {
11834 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11835 if (found)
11836 {
11837 make_cleanup (xfree, sals_end.sals);
11838 expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
11839 }
11840 }
11841
11842 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
11843}
11844
11845/* Prepare the global context for a re-set of breakpoint B. */
11846
11847static struct cleanup *
11848prepare_re_set_context (struct breakpoint *b)
11849{
11850 struct cleanup *cleanups;
11851
11852 input_radix = b->input_radix;
11853 cleanups = save_current_space_and_thread ();
11854 switch_to_program_space_and_thread (b->pspace);
11855 set_language (b->language);
11856
11857 return cleanups;
ef23e705
TJB
11858}
11859
c906108c
SS
11860/* Reset a breakpoint given it's struct breakpoint * BINT.
11861 The value we return ends up being the return value from catch_errors.
11862 Unused in this case. */
11863
11864static int
4efb68b1 11865breakpoint_re_set_one (void *bint)
c906108c 11866{
4a64f543 11867 /* Get past catch_errs. */
53a5351d 11868 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 11869 struct cleanup *cleanups;
c906108c 11870
348d480f
PA
11871 cleanups = prepare_re_set_context (b);
11872 b->ops->re_set (b);
11873 do_cleanups (cleanups);
c906108c
SS
11874 return 0;
11875}
11876
69de3c6a 11877/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 11878void
69de3c6a 11879breakpoint_re_set (void)
c906108c 11880{
35df4500 11881 struct breakpoint *b, *b_tmp;
c906108c
SS
11882 enum language save_language;
11883 int save_input_radix;
6c95b8df 11884 struct cleanup *old_chain;
c5aa993b 11885
c906108c
SS
11886 save_language = current_language->la_language;
11887 save_input_radix = input_radix;
6c95b8df
PA
11888 old_chain = save_current_program_space ();
11889
35df4500 11890 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11891 {
4a64f543 11892 /* Format possible error msg. */
fe3f5fa8 11893 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
11894 b->number);
11895 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 11896 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 11897 do_cleanups (cleanups);
c5aa993b 11898 }
c906108c
SS
11899 set_language (save_language);
11900 input_radix = save_input_radix;
e62c965a 11901
0756c555 11902 jit_breakpoint_re_set ();
4efc6507 11903
6c95b8df
PA
11904 do_cleanups (old_chain);
11905
af02033e
PP
11906 create_overlay_event_breakpoint ();
11907 create_longjmp_master_breakpoint ();
11908 create_std_terminate_master_breakpoint ();
186c406b 11909 create_exception_master_breakpoint ();
c906108c
SS
11910}
11911\f
c906108c
SS
11912/* Reset the thread number of this breakpoint:
11913
11914 - If the breakpoint is for all threads, leave it as-is.
4a64f543 11915 - Else, reset it to the current thread for inferior_ptid. */
c906108c 11916void
fba45db2 11917breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
11918{
11919 if (b->thread != -1)
11920 {
39f77062
KB
11921 if (in_thread_list (inferior_ptid))
11922 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
11923
11924 /* We're being called after following a fork. The new fork is
11925 selected as current, and unless this was a vfork will have a
11926 different program space from the original thread. Reset that
11927 as well. */
11928 b->loc->pspace = current_program_space;
c906108c
SS
11929 }
11930}
11931
03ac34d5
MS
11932/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11933 If from_tty is nonzero, it prints a message to that effect,
11934 which ends with a period (no newline). */
11935
c906108c 11936void
fba45db2 11937set_ignore_count (int bptnum, int count, int from_tty)
c906108c 11938{
52f0bd74 11939 struct breakpoint *b;
c906108c
SS
11940
11941 if (count < 0)
11942 count = 0;
11943
11944 ALL_BREAKPOINTS (b)
11945 if (b->number == bptnum)
c5aa993b 11946 {
d77f58be
SS
11947 if (is_tracepoint (b))
11948 {
11949 if (from_tty && count != 0)
11950 printf_filtered (_("Ignore count ignored for tracepoint %d."),
11951 bptnum);
11952 return;
11953 }
11954
c5aa993b 11955 b->ignore_count = count;
221ea385
KS
11956 if (from_tty)
11957 {
11958 if (count == 0)
3e43a32a
MS
11959 printf_filtered (_("Will stop next time "
11960 "breakpoint %d is reached."),
221ea385
KS
11961 bptnum);
11962 else if (count == 1)
a3f17187 11963 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
11964 bptnum);
11965 else
3e43a32a
MS
11966 printf_filtered (_("Will ignore next %d "
11967 "crossings of breakpoint %d."),
221ea385
KS
11968 count, bptnum);
11969 }
c5aa993b 11970 breakpoints_changed ();
8d3788bd 11971 observer_notify_breakpoint_modified (b);
c5aa993b
JM
11972 return;
11973 }
c906108c 11974
8a3fe4f8 11975 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
11976}
11977
c906108c
SS
11978/* Command to set ignore-count of breakpoint N to COUNT. */
11979
11980static void
fba45db2 11981ignore_command (char *args, int from_tty)
c906108c
SS
11982{
11983 char *p = args;
52f0bd74 11984 int num;
c906108c
SS
11985
11986 if (p == 0)
e2e0b3e5 11987 error_no_arg (_("a breakpoint number"));
c5aa993b 11988
c906108c 11989 num = get_number (&p);
5c44784c 11990 if (num == 0)
8a3fe4f8 11991 error (_("bad breakpoint number: '%s'"), args);
c906108c 11992 if (*p == 0)
8a3fe4f8 11993 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
11994
11995 set_ignore_count (num,
11996 longest_to_int (value_as_long (parse_and_eval (p))),
11997 from_tty);
221ea385
KS
11998 if (from_tty)
11999 printf_filtered ("\n");
c906108c
SS
12000}
12001\f
12002/* Call FUNCTION on each of the breakpoints
12003 whose numbers are given in ARGS. */
12004
12005static void
95a42b64
TT
12006map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
12007 void *),
12008 void *data)
c906108c 12009{
52f0bd74
AC
12010 int num;
12011 struct breakpoint *b, *tmp;
11cf8741 12012 int match;
197f0a60 12013 struct get_number_or_range_state state;
c906108c 12014
197f0a60 12015 if (args == 0)
e2e0b3e5 12016 error_no_arg (_("one or more breakpoint numbers"));
c906108c 12017
197f0a60
TT
12018 init_number_or_range (&state, args);
12019
12020 while (!state.finished)
c906108c 12021 {
197f0a60
TT
12022 char *p = state.string;
12023
11cf8741 12024 match = 0;
c5aa993b 12025
197f0a60 12026 num = get_number_or_range (&state);
5c44784c 12027 if (num == 0)
c5aa993b 12028 {
8a3fe4f8 12029 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
12030 }
12031 else
12032 {
12033 ALL_BREAKPOINTS_SAFE (b, tmp)
12034 if (b->number == num)
12035 {
11cf8741 12036 match = 1;
cdac0397 12037 function (b, data);
11cf8741 12038 break;
5c44784c 12039 }
11cf8741 12040 if (match == 0)
a3f17187 12041 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 12042 }
c906108c
SS
12043 }
12044}
12045
0d381245
VP
12046static struct bp_location *
12047find_location_by_number (char *number)
12048{
12049 char *dot = strchr (number, '.');
12050 char *p1;
12051 int bp_num;
12052 int loc_num;
12053 struct breakpoint *b;
12054 struct bp_location *loc;
12055
12056 *dot = '\0';
12057
12058 p1 = number;
197f0a60 12059 bp_num = get_number (&p1);
0d381245
VP
12060 if (bp_num == 0)
12061 error (_("Bad breakpoint number '%s'"), number);
12062
12063 ALL_BREAKPOINTS (b)
12064 if (b->number == bp_num)
12065 {
12066 break;
12067 }
12068
12069 if (!b || b->number != bp_num)
12070 error (_("Bad breakpoint number '%s'"), number);
12071
12072 p1 = dot+1;
197f0a60 12073 loc_num = get_number (&p1);
0d381245
VP
12074 if (loc_num == 0)
12075 error (_("Bad breakpoint location number '%s'"), number);
12076
12077 --loc_num;
12078 loc = b->loc;
12079 for (;loc_num && loc; --loc_num, loc = loc->next)
12080 ;
12081 if (!loc)
12082 error (_("Bad breakpoint location number '%s'"), dot+1);
12083
12084 return loc;
12085}
12086
12087
1900040c
MS
12088/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12089 If from_tty is nonzero, it prints a message to that effect,
12090 which ends with a period (no newline). */
12091
c906108c 12092void
fba45db2 12093disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
12094{
12095 /* Never disable a watchpoint scope breakpoint; we want to
12096 hit them when we leave scope so we can delete both the
12097 watchpoint and its scope breakpoint at that time. */
12098 if (bpt->type == bp_watchpoint_scope)
12099 return;
12100
c2c6d25f 12101 /* You can't disable permanent breakpoints. */
b5de0fa7 12102 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
12103 return;
12104
b5de0fa7 12105 bpt->enable_state = bp_disabled;
c906108c 12106
d248b706
KY
12107 if (target_supports_enable_disable_tracepoint ()
12108 && current_trace_status ()->running && is_tracepoint (bpt))
12109 {
12110 struct bp_location *location;
12111
12112 for (location = bpt->loc; location; location = location->next)
12113 target_disable_tracepoint (location);
12114 }
12115
b60e7edf 12116 update_global_location_list (0);
c906108c 12117
8d3788bd 12118 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12119}
12120
51be5b68
PA
12121/* A callback for iterate_over_related_breakpoints. */
12122
12123static void
12124do_disable_breakpoint (struct breakpoint *b, void *ignore)
12125{
12126 disable_breakpoint (b);
12127}
12128
95a42b64
TT
12129/* A callback for map_breakpoint_numbers that calls
12130 disable_breakpoint. */
12131
12132static void
12133do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12134{
51be5b68 12135 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
12136}
12137
c906108c 12138static void
fba45db2 12139disable_command (char *args, int from_tty)
c906108c 12140{
c906108c 12141 if (args == 0)
46c6471b
PA
12142 {
12143 struct breakpoint *bpt;
12144
12145 ALL_BREAKPOINTS (bpt)
12146 if (user_breakpoint_p (bpt))
12147 disable_breakpoint (bpt);
12148 }
0d381245
VP
12149 else if (strchr (args, '.'))
12150 {
12151 struct bp_location *loc = find_location_by_number (args);
12152 if (loc)
d248b706
KY
12153 {
12154 loc->enabled = 0;
12155 if (target_supports_enable_disable_tracepoint ()
12156 && current_trace_status ()->running && loc->owner
12157 && is_tracepoint (loc->owner))
12158 target_disable_tracepoint (loc);
12159 }
b60e7edf 12160 update_global_location_list (0);
0d381245 12161 }
c906108c 12162 else
95a42b64 12163 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
12164}
12165
12166static void
51be5b68 12167enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 12168{
afe38095 12169 int target_resources_ok;
c906108c
SS
12170
12171 if (bpt->type == bp_hardware_breakpoint)
12172 {
12173 int i;
c5aa993b 12174 i = hw_breakpoint_used_count ();
53a5351d 12175 target_resources_ok =
d92524f1 12176 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 12177 i + 1, 0);
c906108c 12178 if (target_resources_ok == 0)
8a3fe4f8 12179 error (_("No hardware breakpoint support in the target."));
c906108c 12180 else if (target_resources_ok < 0)
8a3fe4f8 12181 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
12182 }
12183
cc60f2e3 12184 if (is_watchpoint (bpt))
c906108c 12185 {
d07205c2
JK
12186 /* Initialize it just to avoid a GCC false warning. */
12187 enum enable_state orig_enable_state = 0;
dde02812
ES
12188 struct gdb_exception e;
12189
12190 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 12191 {
1e718ff1
TJB
12192 orig_enable_state = bpt->enable_state;
12193 bpt->enable_state = bp_enabled;
dde02812 12194 update_watchpoint (bpt, 1 /* reparse */);
c906108c 12195 }
dde02812 12196 if (e.reason < 0)
c5aa993b 12197 {
1e718ff1 12198 bpt->enable_state = orig_enable_state;
dde02812
ES
12199 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12200 bpt->number);
12201 return;
c5aa993b 12202 }
c906108c 12203 }
0101ce28 12204
b4c291bb
KH
12205 if (bpt->enable_state != bp_permanent)
12206 bpt->enable_state = bp_enabled;
d248b706
KY
12207
12208 if (target_supports_enable_disable_tracepoint ()
12209 && current_trace_status ()->running && is_tracepoint (bpt))
12210 {
12211 struct bp_location *location;
12212
12213 for (location = bpt->loc; location; location = location->next)
12214 target_enable_tracepoint (location);
12215 }
12216
b4c291bb 12217 bpt->disposition = disposition;
b60e7edf 12218 update_global_location_list (1);
b4c291bb
KH
12219 breakpoints_changed ();
12220
8d3788bd 12221 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12222}
12223
fe3f5fa8 12224
c906108c 12225void
fba45db2 12226enable_breakpoint (struct breakpoint *bpt)
c906108c 12227{
51be5b68
PA
12228 enable_breakpoint_disp (bpt, bpt->disposition);
12229}
12230
12231static void
12232do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12233{
12234 enable_breakpoint (bpt);
c906108c
SS
12235}
12236
95a42b64
TT
12237/* A callback for map_breakpoint_numbers that calls
12238 enable_breakpoint. */
12239
12240static void
12241do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12242{
51be5b68 12243 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
12244}
12245
c906108c
SS
12246/* The enable command enables the specified breakpoints (or all defined
12247 breakpoints) so they once again become (or continue to be) effective
1272ad14 12248 in stopping the inferior. */
c906108c 12249
c906108c 12250static void
fba45db2 12251enable_command (char *args, int from_tty)
c906108c 12252{
c906108c 12253 if (args == 0)
46c6471b
PA
12254 {
12255 struct breakpoint *bpt;
12256
12257 ALL_BREAKPOINTS (bpt)
12258 if (user_breakpoint_p (bpt))
12259 enable_breakpoint (bpt);
12260 }
0d381245
VP
12261 else if (strchr (args, '.'))
12262 {
12263 struct bp_location *loc = find_location_by_number (args);
12264 if (loc)
d248b706
KY
12265 {
12266 loc->enabled = 1;
12267 if (target_supports_enable_disable_tracepoint ()
12268 && current_trace_status ()->running && loc->owner
12269 && is_tracepoint (loc->owner))
12270 target_enable_tracepoint (loc);
12271 }
b60e7edf 12272 update_global_location_list (1);
0d381245 12273 }
c906108c 12274 else
95a42b64 12275 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
12276}
12277
12278static void
51be5b68
PA
12279do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12280{
12281 enum bpdisp disp = *(enum bpdisp *) arg;
12282
12283 enable_breakpoint_disp (bpt, disp);
12284}
12285
12286static void
12287do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12288{
51be5b68
PA
12289 enum bpdisp disp = disp_disable;
12290
12291 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12292}
12293
c906108c 12294static void
fba45db2 12295enable_once_command (char *args, int from_tty)
c906108c 12296{
51be5b68 12297 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
12298}
12299
12300static void
51be5b68 12301do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12302{
51be5b68
PA
12303 enum bpdisp disp = disp_del;
12304
12305 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12306}
12307
c906108c 12308static void
fba45db2 12309enable_delete_command (char *args, int from_tty)
c906108c 12310{
51be5b68 12311 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
12312}
12313\f
fa8d40ab
JJ
12314static void
12315set_breakpoint_cmd (char *args, int from_tty)
12316{
12317}
12318
12319static void
12320show_breakpoint_cmd (char *args, int from_tty)
12321{
12322}
12323
1f3b5d1b
PP
12324/* Invalidate last known value of any hardware watchpoint if
12325 the memory which that value represents has been written to by
12326 GDB itself. */
12327
12328static void
12329invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12330 const bfd_byte *data)
12331{
12332 struct breakpoint *bp;
12333
12334 ALL_BREAKPOINTS (bp)
12335 if (bp->enable_state == bp_enabled
12336 && bp->type == bp_hardware_watchpoint
12337 && bp->val_valid && bp->val)
12338 {
12339 struct bp_location *loc;
12340
12341 for (loc = bp->loc; loc != NULL; loc = loc->next)
12342 if (loc->loc_type == bp_loc_hardware_watchpoint
12343 && loc->address + loc->length > addr
12344 && addr + len > loc->address)
12345 {
12346 value_free (bp->val);
12347 bp->val = NULL;
12348 bp->val_valid = 0;
12349 }
12350 }
12351}
12352
c906108c
SS
12353/* Use default_breakpoint_'s, or nothing if they aren't valid. */
12354
12355struct symtabs_and_lines
fba45db2 12356decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
12357{
12358 struct symtabs_and_lines sals;
cc59ec59 12359
c906108c 12360 if (string == 0)
8a3fe4f8 12361 error (_("Empty line specification."));
c906108c
SS
12362 if (default_breakpoint_valid)
12363 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
12364 default_breakpoint_symtab,
12365 default_breakpoint_line,
58438ac1 12366 NULL);
c906108c
SS
12367 else
12368 sals = decode_line_1 (&string, funfirstline,
58438ac1 12369 (struct symtab *) NULL, 0, NULL);
c906108c 12370 if (*string)
8a3fe4f8 12371 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
12372 return sals;
12373}
8181d85f
DJ
12374
12375/* Create and insert a raw software breakpoint at PC. Return an
12376 identifier, which should be used to remove the breakpoint later.
12377 In general, places which call this should be using something on the
12378 breakpoint chain instead; this function should be eliminated
12379 someday. */
12380
12381void *
6c95b8df
PA
12382deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12383 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
12384{
12385 struct bp_target_info *bp_tgt;
12386
6c95b8df 12387 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 12388
6c95b8df 12389 bp_tgt->placed_address_space = aspace;
8181d85f 12390 bp_tgt->placed_address = pc;
6c95b8df 12391
a6d9a66e 12392 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
12393 {
12394 /* Could not insert the breakpoint. */
12395 xfree (bp_tgt);
12396 return NULL;
12397 }
12398
12399 return bp_tgt;
12400}
12401
4a64f543
MS
12402/* Remove a breakpoint BP inserted by
12403 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
12404
12405int
a6d9a66e 12406deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
12407{
12408 struct bp_target_info *bp_tgt = bp;
12409 int ret;
12410
a6d9a66e 12411 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
12412 xfree (bp_tgt);
12413
12414 return ret;
12415}
12416
4a64f543
MS
12417/* One (or perhaps two) breakpoints used for software single
12418 stepping. */
8181d85f
DJ
12419
12420static void *single_step_breakpoints[2];
a6d9a66e 12421static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
12422
12423/* Create and insert a breakpoint for software single step. */
12424
12425void
6c95b8df 12426insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
12427 struct address_space *aspace,
12428 CORE_ADDR next_pc)
8181d85f
DJ
12429{
12430 void **bpt_p;
12431
12432 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
12433 {
12434 bpt_p = &single_step_breakpoints[0];
12435 single_step_gdbarch[0] = gdbarch;
12436 }
8181d85f
DJ
12437 else
12438 {
12439 gdb_assert (single_step_breakpoints[1] == NULL);
12440 bpt_p = &single_step_breakpoints[1];
a6d9a66e 12441 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
12442 }
12443
4a64f543
MS
12444 /* NOTE drow/2006-04-11: A future improvement to this function would
12445 be to only create the breakpoints once, and actually put them on
12446 the breakpoint chain. That would let us use set_raw_breakpoint.
12447 We could adjust the addresses each time they were needed. Doing
12448 this requires corresponding changes elsewhere where single step
12449 breakpoints are handled, however. So, for now, we use this. */
8181d85f 12450
6c95b8df 12451 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 12452 if (*bpt_p == NULL)
5af949e3
UW
12453 error (_("Could not insert single-step breakpoint at %s"),
12454 paddress (gdbarch, next_pc));
8181d85f
DJ
12455}
12456
f02253f1
HZ
12457/* Check if the breakpoints used for software single stepping
12458 were inserted or not. */
12459
12460int
12461single_step_breakpoints_inserted (void)
12462{
12463 return (single_step_breakpoints[0] != NULL
12464 || single_step_breakpoints[1] != NULL);
12465}
12466
8181d85f
DJ
12467/* Remove and delete any breakpoints used for software single step. */
12468
12469void
12470remove_single_step_breakpoints (void)
12471{
12472 gdb_assert (single_step_breakpoints[0] != NULL);
12473
12474 /* See insert_single_step_breakpoint for more about this deprecated
12475 call. */
a6d9a66e
UW
12476 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12477 single_step_breakpoints[0]);
12478 single_step_gdbarch[0] = NULL;
8181d85f
DJ
12479 single_step_breakpoints[0] = NULL;
12480
12481 if (single_step_breakpoints[1] != NULL)
12482 {
a6d9a66e
UW
12483 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12484 single_step_breakpoints[1]);
12485 single_step_gdbarch[1] = NULL;
8181d85f
DJ
12486 single_step_breakpoints[1] = NULL;
12487 }
12488}
12489
d03285ec
UW
12490/* Delete software single step breakpoints without removing them from
12491 the inferior. This is intended to be used if the inferior's address
12492 space where they were inserted is already gone, e.g. after exit or
12493 exec. */
12494
12495void
12496cancel_single_step_breakpoints (void)
12497{
12498 int i;
12499
12500 for (i = 0; i < 2; i++)
12501 if (single_step_breakpoints[i])
12502 {
12503 xfree (single_step_breakpoints[i]);
12504 single_step_breakpoints[i] = NULL;
12505 single_step_gdbarch[i] = NULL;
12506 }
12507}
12508
12509/* Detach software single-step breakpoints from INFERIOR_PTID without
12510 removing them. */
12511
12512static void
12513detach_single_step_breakpoints (void)
12514{
12515 int i;
12516
12517 for (i = 0; i < 2; i++)
12518 if (single_step_breakpoints[i])
12519 target_remove_breakpoint (single_step_gdbarch[i],
12520 single_step_breakpoints[i]);
12521}
12522
4a64f543
MS
12523/* Check whether a software single-step breakpoint is inserted at
12524 PC. */
1aafd4da
UW
12525
12526static int
cc59ec59
MS
12527single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12528 CORE_ADDR pc)
1aafd4da
UW
12529{
12530 int i;
12531
12532 for (i = 0; i < 2; i++)
12533 {
12534 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
12535 if (bp_tgt
12536 && breakpoint_address_match (bp_tgt->placed_address_space,
12537 bp_tgt->placed_address,
12538 aspace, pc))
1aafd4da
UW
12539 return 1;
12540 }
12541
12542 return 0;
12543}
12544
a96d9b2e
SDJ
12545/* Returns 0 if 'bp' is NOT a syscall catchpoint,
12546 non-zero otherwise. */
12547static int
12548is_syscall_catchpoint_enabled (struct breakpoint *bp)
12549{
12550 if (syscall_catchpoint_p (bp)
12551 && bp->enable_state != bp_disabled
12552 && bp->enable_state != bp_call_disabled)
12553 return 1;
12554 else
12555 return 0;
12556}
12557
12558int
12559catch_syscall_enabled (void)
12560{
12561 struct inferior *inf = current_inferior ();
12562
12563 return inf->total_syscalls_count != 0;
12564}
12565
12566int
12567catching_syscall_number (int syscall_number)
12568{
12569 struct breakpoint *bp;
12570
12571 ALL_BREAKPOINTS (bp)
12572 if (is_syscall_catchpoint_enabled (bp))
12573 {
be5c67c1
PA
12574 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12575
12576 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
12577 {
12578 int i, iter;
12579 for (i = 0;
be5c67c1 12580 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
12581 i++)
12582 if (syscall_number == iter)
12583 return 1;
12584 }
12585 else
12586 return 1;
12587 }
12588
12589 return 0;
12590}
12591
12592/* Complete syscall names. Used by "catch syscall". */
12593static char **
12594catch_syscall_completer (struct cmd_list_element *cmd,
12595 char *text, char *word)
12596{
12597 const char **list = get_syscall_names ();
c38eea1a
MS
12598 char **retlist
12599 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 12600
c38eea1a
MS
12601 xfree (list);
12602 return retlist;
a96d9b2e
SDJ
12603}
12604
1042e4c0
SS
12605/* Tracepoint-specific operations. */
12606
12607/* Set tracepoint count to NUM. */
12608static void
12609set_tracepoint_count (int num)
12610{
12611 tracepoint_count = num;
4fa62494 12612 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
12613}
12614
12615void
12616trace_command (char *arg, int from_tty)
12617{
8cdf0e15
VP
12618 if (create_breakpoint (get_current_arch (),
12619 arg,
12620 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12621 0 /* tempflag */,
12622 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
12623 0 /* Ignore count */,
12624 pending_break_support,
348d480f 12625 &tracepoint_breakpoint_ops,
8cdf0e15 12626 from_tty,
84f4c1fe
PM
12627 1 /* enabled */,
12628 0 /* internal */))
fd9b8c24 12629 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
12630}
12631
7a697b8d
SS
12632void
12633ftrace_command (char *arg, int from_tty)
12634{
8cdf0e15
VP
12635 if (create_breakpoint (get_current_arch (),
12636 arg,
12637 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12638 0 /* tempflag */,
12639 bp_fast_tracepoint /* type_wanted */,
12640 0 /* Ignore count */,
12641 pending_break_support,
348d480f 12642 &tracepoint_breakpoint_ops,
0fb4aa4b 12643 from_tty,
84f4c1fe
PM
12644 1 /* enabled */,
12645 0 /* internal */))
0fb4aa4b
PA
12646 set_tracepoint_count (breakpoint_count);
12647}
12648
12649/* strace command implementation. Creates a static tracepoint. */
12650
12651void
12652strace_command (char *arg, int from_tty)
12653{
12654 if (create_breakpoint (get_current_arch (),
12655 arg,
12656 NULL, 0, 1 /* parse arg */,
12657 0 /* tempflag */,
12658 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
12659 0 /* Ignore count */,
12660 pending_break_support,
348d480f 12661 &tracepoint_breakpoint_ops,
8cdf0e15 12662 from_tty,
84f4c1fe
PM
12663 1 /* enabled */,
12664 0 /* internal */))
fd9b8c24 12665 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
12666}
12667
409873ef
SS
12668/* Set up a fake reader function that gets command lines from a linked
12669 list that was acquired during tracepoint uploading. */
12670
12671static struct uploaded_tp *this_utp;
3149d8c1 12672static int next_cmd;
409873ef
SS
12673
12674static char *
12675read_uploaded_action (void)
12676{
12677 char *rslt;
12678
3149d8c1 12679 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 12680
3149d8c1 12681 next_cmd++;
409873ef
SS
12682
12683 return rslt;
12684}
12685
00bf0b85
SS
12686/* Given information about a tracepoint as recorded on a target (which
12687 can be either a live system or a trace file), attempt to create an
12688 equivalent GDB tracepoint. This is not a reliable process, since
12689 the target does not necessarily have all the information used when
12690 the tracepoint was originally defined. */
12691
12692struct breakpoint *
12693create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 12694{
409873ef 12695 char *addr_str, small_buf[100];
d5551862 12696 struct breakpoint *tp;
fd9b8c24 12697
409873ef
SS
12698 if (utp->at_string)
12699 addr_str = utp->at_string;
12700 else
12701 {
12702 /* In the absence of a source location, fall back to raw
12703 address. Since there is no way to confirm that the address
12704 means the same thing as when the trace was started, warn the
12705 user. */
3e43a32a
MS
12706 warning (_("Uploaded tracepoint %d has no "
12707 "source location, using raw address"),
409873ef
SS
12708 utp->number);
12709 sprintf (small_buf, "*%s", hex_string (utp->addr));
12710 addr_str = small_buf;
12711 }
12712
12713 /* There's not much we can do with a sequence of bytecodes. */
12714 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12715 warning (_("Uploaded tracepoint %d condition "
12716 "has no source form, ignoring it"),
409873ef 12717 utp->number);
d5551862 12718
8cdf0e15 12719 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12720 addr_str,
12721 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12722 0 /* tempflag */,
0fb4aa4b 12723 utp->type /* type_wanted */,
8cdf0e15
VP
12724 0 /* Ignore count */,
12725 pending_break_support,
348d480f 12726 &tracepoint_breakpoint_ops,
8cdf0e15 12727 0 /* from_tty */,
84f4c1fe
PM
12728 utp->enabled /* enabled */,
12729 0 /* internal */))
fd9b8c24
PA
12730 return NULL;
12731
00bf0b85
SS
12732 set_tracepoint_count (breakpoint_count);
12733
409873ef 12734 /* Get the tracepoint we just created. */
fd9b8c24
PA
12735 tp = get_tracepoint (tracepoint_count);
12736 gdb_assert (tp != NULL);
d5551862 12737
00bf0b85
SS
12738 if (utp->pass > 0)
12739 {
409873ef 12740 sprintf (small_buf, "%d %d", utp->pass, tp->number);
00bf0b85 12741
409873ef 12742 trace_pass_command (small_buf, 0);
00bf0b85
SS
12743 }
12744
409873ef
SS
12745 /* If we have uploaded versions of the original commands, set up a
12746 special-purpose "reader" function and call the usual command line
12747 reader, then pass the result to the breakpoint command-setting
12748 function. */
3149d8c1 12749 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12750 {
409873ef 12751 struct command_line *cmd_list;
00bf0b85 12752
409873ef 12753 this_utp = utp;
3149d8c1 12754 next_cmd = 0;
d5551862 12755
409873ef
SS
12756 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12757
12758 breakpoint_set_commands (tp, cmd_list);
00bf0b85 12759 }
3149d8c1
SS
12760 else if (!VEC_empty (char_ptr, utp->actions)
12761 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12762 warning (_("Uploaded tracepoint %d actions "
12763 "have no source form, ignoring them"),
409873ef 12764 utp->number);
00bf0b85
SS
12765
12766 return tp;
12767 }
12768
1042e4c0
SS
12769/* Print information on tracepoint number TPNUM_EXP, or all if
12770 omitted. */
12771
12772static void
e5a67952 12773tracepoints_info (char *args, int from_tty)
1042e4c0 12774{
e5a67952 12775 int num_printed;
1042e4c0 12776
e5a67952 12777 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
12778
12779 if (num_printed == 0)
1042e4c0 12780 {
e5a67952 12781 if (args == NULL || *args == '\0')
d77f58be
SS
12782 ui_out_message (uiout, 0, "No tracepoints.\n");
12783 else
e5a67952 12784 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 12785 }
ad443146
SS
12786
12787 default_collect_info ();
1042e4c0
SS
12788}
12789
4a64f543 12790/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
12791 Not supported by all targets. */
12792static void
12793enable_trace_command (char *args, int from_tty)
12794{
12795 enable_command (args, from_tty);
12796}
12797
4a64f543 12798/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
12799 Not supported by all targets. */
12800static void
12801disable_trace_command (char *args, int from_tty)
12802{
12803 disable_command (args, from_tty);
12804}
12805
4a64f543 12806/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
12807static void
12808delete_trace_command (char *arg, int from_tty)
12809{
35df4500 12810 struct breakpoint *b, *b_tmp;
1042e4c0
SS
12811
12812 dont_repeat ();
12813
12814 if (arg == 0)
12815 {
12816 int breaks_to_delete = 0;
12817
12818 /* Delete all breakpoints if no argument.
12819 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
12820 have to be deleted with an explicit breakpoint number
12821 argument. */
1042e4c0 12822 ALL_TRACEPOINTS (b)
46c6471b 12823 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
12824 {
12825 breaks_to_delete = 1;
12826 break;
12827 }
1042e4c0
SS
12828
12829 /* Ask user only if there are some breakpoints to delete. */
12830 if (!from_tty
12831 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12832 {
35df4500 12833 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 12834 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 12835 delete_breakpoint (b);
1042e4c0
SS
12836 }
12837 }
12838 else
51be5b68 12839 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
12840}
12841
197f0a60
TT
12842/* Helper function for trace_pass_command. */
12843
12844static void
12845trace_pass_set_count (struct breakpoint *bp, int count, int from_tty)
12846{
12847 bp->pass_count = count;
12848 observer_notify_tracepoint_modified (bp->number);
12849 if (from_tty)
12850 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
12851 bp->number, count);
12852}
12853
1042e4c0
SS
12854/* Set passcount for tracepoint.
12855
12856 First command argument is passcount, second is tracepoint number.
12857 If tracepoint number omitted, apply to most recently defined.
12858 Also accepts special argument "all". */
12859
12860static void
12861trace_pass_command (char *args, int from_tty)
12862{
197f0a60 12863 struct breakpoint *t1;
1042e4c0 12864 unsigned int count;
1042e4c0
SS
12865
12866 if (args == 0 || *args == 0)
3e43a32a
MS
12867 error (_("passcount command requires an "
12868 "argument (count + optional TP num)"));
1042e4c0 12869
4a64f543 12870 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
12871
12872 while (*args && isspace ((int) *args))
12873 args++;
12874
12875 if (*args && strncasecmp (args, "all", 3) == 0)
12876 {
12877 args += 3; /* Skip special argument "all". */
1042e4c0
SS
12878 if (*args)
12879 error (_("Junk at end of arguments."));
1042e4c0 12880
197f0a60
TT
12881 ALL_TRACEPOINTS (t1)
12882 {
12883 trace_pass_set_count (t1, count, from_tty);
12884 }
12885 }
12886 else if (*args == '\0')
1042e4c0 12887 {
197f0a60 12888 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 12889 if (t1)
197f0a60
TT
12890 trace_pass_set_count (t1, count, from_tty);
12891 }
12892 else
12893 {
12894 struct get_number_or_range_state state;
12895
12896 init_number_or_range (&state, args);
12897 while (!state.finished)
1042e4c0 12898 {
197f0a60
TT
12899 t1 = get_tracepoint_by_number (&args, &state, 1);
12900 if (t1)
12901 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
12902 }
12903 }
1042e4c0
SS
12904}
12905
12906struct breakpoint *
12907get_tracepoint (int num)
12908{
12909 struct breakpoint *t;
12910
12911 ALL_TRACEPOINTS (t)
12912 if (t->number == num)
12913 return t;
12914
12915 return NULL;
12916}
12917
d5551862
SS
12918/* Find the tracepoint with the given target-side number (which may be
12919 different from the tracepoint number after disconnecting and
12920 reconnecting). */
12921
12922struct breakpoint *
12923get_tracepoint_by_number_on_target (int num)
12924{
12925 struct breakpoint *t;
12926
12927 ALL_TRACEPOINTS (t)
12928 if (t->number_on_target == num)
12929 return t;
12930
12931 return NULL;
12932}
12933
1042e4c0 12934/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
12935 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
12936 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0
SS
12937 recent tracepoint (tracepoint_count) is returned. */
12938struct breakpoint *
197f0a60
TT
12939get_tracepoint_by_number (char **arg,
12940 struct get_number_or_range_state *state,
12941 int optional_p)
1042e4c0
SS
12942{
12943 extern int tracepoint_count;
12944 struct breakpoint *t;
12945 int tpnum;
12946 char *instring = arg == NULL ? NULL : *arg;
12947
197f0a60
TT
12948 if (state)
12949 {
12950 gdb_assert (!state->finished);
12951 tpnum = get_number_or_range (state);
12952 }
12953 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
12954 {
12955 if (optional_p)
12956 tpnum = tracepoint_count;
12957 else
12958 error_no_arg (_("tracepoint number"));
12959 }
12960 else
197f0a60 12961 tpnum = get_number (arg);
1042e4c0
SS
12962
12963 if (tpnum <= 0)
12964 {
12965 if (instring && *instring)
12966 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
12967 instring);
12968 else
3e43a32a
MS
12969 printf_filtered (_("Tracepoint argument missing "
12970 "and no previous tracepoint\n"));
1042e4c0
SS
12971 return NULL;
12972 }
12973
12974 ALL_TRACEPOINTS (t)
12975 if (t->number == tpnum)
12976 {
12977 return t;
12978 }
12979
1042e4c0
SS
12980 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
12981 return NULL;
12982}
12983
6149aea9
PA
12984/* Save information on user settable breakpoints (watchpoints, etc) to
12985 a new script file named FILENAME. If FILTER is non-NULL, call it
12986 on each breakpoint and only include the ones for which it returns
12987 non-zero. */
12988
1042e4c0 12989static void
6149aea9
PA
12990save_breakpoints (char *filename, int from_tty,
12991 int (*filter) (const struct breakpoint *))
1042e4c0
SS
12992{
12993 struct breakpoint *tp;
6149aea9 12994 int any = 0;
a7bdde9e 12995 char *pathname;
1042e4c0 12996 struct cleanup *cleanup;
a7bdde9e 12997 struct ui_file *fp;
6149aea9 12998 int extra_trace_bits = 0;
1042e4c0 12999
6149aea9
PA
13000 if (filename == 0 || *filename == 0)
13001 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
13002
13003 /* See if we have anything to save. */
6149aea9 13004 ALL_BREAKPOINTS (tp)
1042e4c0 13005 {
6149aea9 13006 /* Skip internal and momentary breakpoints. */
09d682a4 13007 if (!user_breakpoint_p (tp))
6149aea9
PA
13008 continue;
13009
13010 /* If we have a filter, only save the breakpoints it accepts. */
13011 if (filter && !filter (tp))
13012 continue;
13013
13014 any = 1;
13015
13016 if (is_tracepoint (tp))
13017 {
13018 extra_trace_bits = 1;
13019
13020 /* We can stop searching. */
13021 break;
13022 }
1042e4c0 13023 }
6149aea9
PA
13024
13025 if (!any)
1042e4c0 13026 {
6149aea9 13027 warning (_("Nothing to save."));
1042e4c0
SS
13028 return;
13029 }
13030
6149aea9 13031 pathname = tilde_expand (filename);
1042e4c0 13032 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 13033 fp = gdb_fopen (pathname, "w");
059fb39f 13034 if (!fp)
6149aea9
PA
13035 error (_("Unable to open file '%s' for saving (%s)"),
13036 filename, safe_strerror (errno));
a7bdde9e 13037 make_cleanup_ui_file_delete (fp);
8bf6485c 13038
6149aea9
PA
13039 if (extra_trace_bits)
13040 save_trace_state_variables (fp);
8bf6485c 13041
6149aea9 13042 ALL_BREAKPOINTS (tp)
1042e4c0 13043 {
6149aea9 13044 /* Skip internal and momentary breakpoints. */
09d682a4 13045 if (!user_breakpoint_p (tp))
6149aea9 13046 continue;
8bf6485c 13047
6149aea9
PA
13048 /* If we have a filter, only save the breakpoints it accepts. */
13049 if (filter && !filter (tp))
13050 continue;
13051
348d480f 13052 tp->ops->print_recreate (tp, fp);
1042e4c0 13053
6149aea9
PA
13054 if (tp->thread != -1)
13055 fprintf_unfiltered (fp, " thread %d", tp->thread);
13056
13057 if (tp->task != 0)
13058 fprintf_unfiltered (fp, " task %d", tp->task);
8bf6485c
SS
13059
13060 fprintf_unfiltered (fp, "\n");
13061
6149aea9
PA
13062 /* Note, we can't rely on tp->number for anything, as we can't
13063 assume the recreated breakpoint numbers will match. Use $bpnum
13064 instead. */
13065
13066 if (tp->cond_string)
13067 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
13068
13069 if (tp->ignore_count)
13070 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
13071
1042e4c0 13072 if (tp->pass_count)
a7bdde9e 13073 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
1042e4c0 13074
a7bdde9e 13075 if (tp->commands)
1042e4c0 13076 {
a7bdde9e
VP
13077 volatile struct gdb_exception ex;
13078
6149aea9 13079 fprintf_unfiltered (fp, " commands\n");
a7bdde9e
VP
13080
13081 ui_out_redirect (uiout, fp);
14dba4b4 13082 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 13083 {
9add0f1b 13084 print_command_lines (uiout, tp->commands->commands, 2);
a7bdde9e
VP
13085 }
13086 ui_out_redirect (uiout, NULL);
1042e4c0 13087
a7bdde9e
VP
13088 if (ex.reason < 0)
13089 throw_exception (ex);
1042e4c0 13090
a7bdde9e 13091 fprintf_unfiltered (fp, " end\n");
1042e4c0 13092 }
6149aea9
PA
13093
13094 if (tp->enable_state == bp_disabled)
13095 fprintf_unfiltered (fp, "disable\n");
13096
13097 /* If this is a multi-location breakpoint, check if the locations
13098 should be individually disabled. Watchpoint locations are
13099 special, and not user visible. */
13100 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13101 {
13102 struct bp_location *loc;
13103 int n = 1;
13104
13105 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13106 if (!loc->enabled)
13107 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13108 }
1042e4c0 13109 }
8bf6485c 13110
6149aea9 13111 if (extra_trace_bits && *default_collect)
8bf6485c
SS
13112 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13113
1042e4c0
SS
13114 do_cleanups (cleanup);
13115 if (from_tty)
6149aea9
PA
13116 printf_filtered (_("Saved to file '%s'.\n"), filename);
13117}
13118
13119/* The `save breakpoints' command. */
13120
13121static void
13122save_breakpoints_command (char *args, int from_tty)
13123{
13124 save_breakpoints (args, from_tty, NULL);
13125}
13126
13127/* The `save tracepoints' command. */
13128
13129static void
13130save_tracepoints_command (char *args, int from_tty)
13131{
13132 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
13133}
13134
13135/* Create a vector of all tracepoints. */
13136
13137VEC(breakpoint_p) *
eeae04df 13138all_tracepoints (void)
1042e4c0
SS
13139{
13140 VEC(breakpoint_p) *tp_vec = 0;
13141 struct breakpoint *tp;
13142
13143 ALL_TRACEPOINTS (tp)
13144 {
13145 VEC_safe_push (breakpoint_p, tp_vec, tp);
13146 }
13147
13148 return tp_vec;
13149}
13150
c906108c 13151\f
4a64f543
MS
13152/* This help string is used for the break, hbreak, tbreak and thbreak
13153 commands. It is defined as a macro to prevent duplication.
13154 COMMAND should be a string constant containing the name of the
13155 command. */
31e2b00f
AS
13156#define BREAK_ARGS_HELP(command) \
13157command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13158LOCATION may be a line number, function name, or \"*\" and an address.\n\
13159If a line number is specified, break at start of code for that line.\n\
13160If a function is specified, break at start of code for that function.\n\
13161If an address is specified, break at that exact address.\n\
dc10affe
PA
13162With no LOCATION, uses current execution address of the selected\n\
13163stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
13164\n\
13165THREADNUM is the number from \"info threads\".\n\
13166CONDITION is a boolean expression.\n\
13167\n\
d41c0fc8
PA
13168Multiple breakpoints at one place are permitted, and useful if their\n\
13169conditions are different.\n\
31e2b00f
AS
13170\n\
13171Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13172
44feb3ce
TT
13173/* List of subcommands for "catch". */
13174static struct cmd_list_element *catch_cmdlist;
13175
13176/* List of subcommands for "tcatch". */
13177static struct cmd_list_element *tcatch_cmdlist;
13178
9ac4176b 13179void
44feb3ce
TT
13180add_catch_command (char *name, char *docstring,
13181 void (*sfunc) (char *args, int from_tty,
13182 struct cmd_list_element *command),
a96d9b2e
SDJ
13183 char **(*completer) (struct cmd_list_element *cmd,
13184 char *text, char *word),
44feb3ce
TT
13185 void *user_data_catch,
13186 void *user_data_tcatch)
13187{
13188 struct cmd_list_element *command;
13189
13190 command = add_cmd (name, class_breakpoint, NULL, docstring,
13191 &catch_cmdlist);
13192 set_cmd_sfunc (command, sfunc);
13193 set_cmd_context (command, user_data_catch);
a96d9b2e 13194 set_cmd_completer (command, completer);
44feb3ce
TT
13195
13196 command = add_cmd (name, class_breakpoint, NULL, docstring,
13197 &tcatch_cmdlist);
13198 set_cmd_sfunc (command, sfunc);
13199 set_cmd_context (command, user_data_tcatch);
a96d9b2e 13200 set_cmd_completer (command, completer);
44feb3ce
TT
13201}
13202
6c95b8df 13203static void
a79b8f6e 13204clear_syscall_counts (struct inferior *inf)
6c95b8df 13205{
6c95b8df
PA
13206 inf->total_syscalls_count = 0;
13207 inf->any_syscall_count = 0;
13208 VEC_free (int, inf->syscalls_counts);
13209}
13210
6149aea9
PA
13211static void
13212save_command (char *arg, int from_tty)
13213{
3e43a32a
MS
13214 printf_unfiltered (_("\"save\" must be followed by "
13215 "the name of a save subcommand.\n"));
6149aea9
PA
13216 help_list (save_cmdlist, "save ", -1, gdb_stdout);
13217}
13218
84f4c1fe
PM
13219struct breakpoint *
13220iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13221 void *data)
13222{
35df4500 13223 struct breakpoint *b, *b_tmp;
84f4c1fe 13224
35df4500 13225 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
13226 {
13227 if ((*callback) (b, data))
13228 return b;
13229 }
13230
13231 return NULL;
13232}
13233
c906108c 13234void
fba45db2 13235_initialize_breakpoint (void)
c906108c
SS
13236{
13237 struct cmd_list_element *c;
13238
84acb35a 13239 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 13240 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 13241 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 13242
17450429
PP
13243 breakpoint_objfile_key = register_objfile_data ();
13244
c906108c
SS
13245 breakpoint_chain = 0;
13246 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13247 before a breakpoint is set. */
13248 breakpoint_count = 0;
13249
1042e4c0
SS
13250 tracepoint_count = 0;
13251
1bedd215
AC
13252 add_com ("ignore", class_breakpoint, ignore_command, _("\
13253Set ignore-count of breakpoint number N to COUNT.\n\
13254Usage is `ignore N COUNT'."));
c906108c 13255 if (xdb_commands)
c5aa993b 13256 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 13257
1bedd215
AC
13258 add_com ("commands", class_breakpoint, commands_command, _("\
13259Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
13260Give breakpoint number as argument after \"commands\".\n\
13261With no argument, the targeted breakpoint is the last one set.\n\
13262The commands themselves follow starting on the next line.\n\
13263Type a line containing \"end\" to indicate the end of them.\n\
13264Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 13265then no output is printed when it is hit, except what the commands print."));
c906108c 13266
1bedd215
AC
13267 add_com ("condition", class_breakpoint, condition_command, _("\
13268Specify breakpoint number N to break only if COND is true.\n\
c906108c 13269Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 13270expression to be evaluated whenever breakpoint N is reached."));
c906108c 13271
1bedd215 13272 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 13273Set a temporary breakpoint.\n\
c906108c
SS
13274Like \"break\" except the breakpoint is only temporary,\n\
13275so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
13276by using \"enable delete\" on the breakpoint number.\n\
13277\n"
13278BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 13279 set_cmd_completer (c, location_completer);
c94fdfd0 13280
1bedd215 13281 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 13282Set a hardware assisted breakpoint.\n\
c906108c 13283Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
13284some target hardware may not have this support.\n\
13285\n"
13286BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 13287 set_cmd_completer (c, location_completer);
c906108c 13288
1bedd215 13289 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 13290Set a temporary hardware assisted breakpoint.\n\
c906108c 13291Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
13292so it will be deleted when hit.\n\
13293\n"
13294BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 13295 set_cmd_completer (c, location_completer);
c906108c 13296
1bedd215
AC
13297 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13298Enable some breakpoints.\n\
c906108c
SS
13299Give breakpoint numbers (separated by spaces) as arguments.\n\
13300With no subcommand, breakpoints are enabled until you command otherwise.\n\
13301This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13302With a subcommand you can enable temporarily."),
c906108c
SS
13303 &enablelist, "enable ", 1, &cmdlist);
13304 if (xdb_commands)
1bedd215
AC
13305 add_com ("ab", class_breakpoint, enable_command, _("\
13306Enable some breakpoints.\n\
c906108c
SS
13307Give breakpoint numbers (separated by spaces) as arguments.\n\
13308With no subcommand, breakpoints are enabled until you command otherwise.\n\
13309This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13310With a subcommand you can enable temporarily."));
c906108c
SS
13311
13312 add_com_alias ("en", "enable", class_breakpoint, 1);
13313
84951ab5 13314 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 13315Enable some breakpoints.\n\
c906108c
SS
13316Give breakpoint numbers (separated by spaces) as arguments.\n\
13317This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13318May be abbreviated to simply \"enable\".\n"),
c5aa993b 13319 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 13320
1a966eab
AC
13321 add_cmd ("once", no_class, enable_once_command, _("\
13322Enable breakpoints for one hit. Give breakpoint numbers.\n\
13323If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13324 &enablebreaklist);
13325
1a966eab
AC
13326 add_cmd ("delete", no_class, enable_delete_command, _("\
13327Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13328If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13329 &enablebreaklist);
13330
1a966eab
AC
13331 add_cmd ("delete", no_class, enable_delete_command, _("\
13332Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13333If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13334 &enablelist);
13335
1a966eab
AC
13336 add_cmd ("once", no_class, enable_once_command, _("\
13337Enable breakpoints for one hit. Give breakpoint numbers.\n\
13338If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13339 &enablelist);
13340
1bedd215
AC
13341 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13342Disable some breakpoints.\n\
c906108c
SS
13343Arguments are breakpoint numbers with spaces in between.\n\
13344To disable all breakpoints, give no argument.\n\
64b9b334 13345A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
13346 &disablelist, "disable ", 1, &cmdlist);
13347 add_com_alias ("dis", "disable", class_breakpoint, 1);
13348 add_com_alias ("disa", "disable", class_breakpoint, 1);
13349 if (xdb_commands)
1bedd215
AC
13350 add_com ("sb", class_breakpoint, disable_command, _("\
13351Disable some breakpoints.\n\
c906108c
SS
13352Arguments are breakpoint numbers with spaces in between.\n\
13353To disable all breakpoints, give no argument.\n\
64b9b334 13354A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 13355
1a966eab
AC
13356 add_cmd ("breakpoints", class_alias, disable_command, _("\
13357Disable some breakpoints.\n\
c906108c
SS
13358Arguments are breakpoint numbers with spaces in between.\n\
13359To disable all breakpoints, give no argument.\n\
64b9b334 13360A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 13361This command may be abbreviated \"disable\"."),
c906108c
SS
13362 &disablelist);
13363
1bedd215
AC
13364 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13365Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13366Arguments are breakpoint numbers with spaces in between.\n\
13367To delete all breakpoints, give no argument.\n\
13368\n\
13369Also a prefix command for deletion of other GDB objects.\n\
1bedd215 13370The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
13371 &deletelist, "delete ", 1, &cmdlist);
13372 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 13373 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 13374 if (xdb_commands)
1bedd215
AC
13375 add_com ("db", class_breakpoint, delete_command, _("\
13376Delete some breakpoints.\n\
c906108c 13377Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 13378To delete all breakpoints, give no argument.\n"));
c906108c 13379
1a966eab
AC
13380 add_cmd ("breakpoints", class_alias, delete_command, _("\
13381Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13382Arguments are breakpoint numbers with spaces in between.\n\
13383To delete all breakpoints, give no argument.\n\
1a966eab 13384This command may be abbreviated \"delete\"."),
c906108c
SS
13385 &deletelist);
13386
1bedd215
AC
13387 add_com ("clear", class_breakpoint, clear_command, _("\
13388Clear breakpoint at specified line or function.\n\
c906108c
SS
13389Argument may be line number, function name, or \"*\" and an address.\n\
13390If line number is specified, all breakpoints in that line are cleared.\n\
13391If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
13392If an address is specified, breakpoints at that address are cleared.\n\
13393\n\
13394With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
13395is executing in.\n\
13396\n\
1bedd215 13397See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 13398 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 13399
1bedd215 13400 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
13401Set breakpoint at specified line or function.\n"
13402BREAK_ARGS_HELP ("break")));
5ba2abeb 13403 set_cmd_completer (c, location_completer);
c94fdfd0 13404
c906108c
SS
13405 add_com_alias ("b", "break", class_run, 1);
13406 add_com_alias ("br", "break", class_run, 1);
13407 add_com_alias ("bre", "break", class_run, 1);
13408 add_com_alias ("brea", "break", class_run, 1);
13409
7681d515
PM
13410 if (xdb_commands)
13411 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
13412
13413 if (dbx_commands)
13414 {
1bedd215
AC
13415 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13416Break in function/address or break at a line in the current file."),
c5aa993b
JM
13417 &stoplist, "stop ", 1, &cmdlist);
13418 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 13419 _("Break in function or address."), &stoplist);
c5aa993b 13420 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 13421 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
13422 add_com ("status", class_info, breakpoints_info, _("\
13423Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13424The \"Type\" column indicates one of:\n\
13425\tbreakpoint - normal breakpoint\n\
13426\twatchpoint - watchpoint\n\
13427The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13428the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13429breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13430address and file/line number respectively.\n\
13431\n\
13432Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13433are set to the address of the last breakpoint listed unless the command\n\
13434is prefixed with \"server \".\n\n\
c906108c 13435Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13436breakpoint set."));
c906108c
SS
13437 }
13438
1bedd215 13439 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 13440Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
13441The \"Type\" column indicates one of:\n\
13442\tbreakpoint - normal breakpoint\n\
13443\twatchpoint - watchpoint\n\
13444The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13445the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13446breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13447address and file/line number respectively.\n\
13448\n\
13449Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13450are set to the address of the last breakpoint listed unless the command\n\
13451is prefixed with \"server \".\n\n\
c906108c 13452Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13453breakpoint set."));
c906108c 13454
6b04bdb7
MS
13455 add_info_alias ("b", "breakpoints", 1);
13456
c906108c 13457 if (xdb_commands)
1bedd215
AC
13458 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13459Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13460The \"Type\" column indicates one of:\n\
13461\tbreakpoint - normal breakpoint\n\
13462\twatchpoint - watchpoint\n\
13463The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13464the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13465breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13466address and file/line number respectively.\n\
13467\n\
13468Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13469are set to the address of the last breakpoint listed unless the command\n\
13470is prefixed with \"server \".\n\n\
c906108c 13471Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13472breakpoint set."));
c906108c 13473
1a966eab
AC
13474 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13475Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13476The \"Type\" column indicates one of:\n\
13477\tbreakpoint - normal breakpoint\n\
13478\twatchpoint - watchpoint\n\
13479\tlongjmp - internal breakpoint used to step through longjmp()\n\
13480\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13481\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
13482\tfinish - internal breakpoint used by the \"finish\" command\n\
13483The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
13484the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13485breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
13486address and file/line number respectively.\n\
13487\n\
13488Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13489are set to the address of the last breakpoint listed unless the command\n\
13490is prefixed with \"server \".\n\n\
c906108c 13491Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 13492breakpoint set."),
c906108c
SS
13493 &maintenanceinfolist);
13494
44feb3ce
TT
13495 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13496Set catchpoints to catch events."),
13497 &catch_cmdlist, "catch ",
13498 0/*allow-unknown*/, &cmdlist);
13499
13500 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13501Set temporary catchpoints to catch events."),
13502 &tcatch_cmdlist, "tcatch ",
13503 0/*allow-unknown*/, &cmdlist);
13504
13505 /* Add catch and tcatch sub-commands. */
13506 add_catch_command ("catch", _("\
13507Catch an exception, when caught.\n\
13508With an argument, catch only exceptions with the given name."),
13509 catch_catch_command,
a96d9b2e 13510 NULL,
44feb3ce
TT
13511 CATCH_PERMANENT,
13512 CATCH_TEMPORARY);
13513 add_catch_command ("throw", _("\
13514Catch an exception, when thrown.\n\
13515With an argument, catch only exceptions with the given name."),
13516 catch_throw_command,
a96d9b2e 13517 NULL,
44feb3ce
TT
13518 CATCH_PERMANENT,
13519 CATCH_TEMPORARY);
13520 add_catch_command ("fork", _("Catch calls to fork."),
13521 catch_fork_command_1,
a96d9b2e 13522 NULL,
44feb3ce
TT
13523 (void *) (uintptr_t) catch_fork_permanent,
13524 (void *) (uintptr_t) catch_fork_temporary);
13525 add_catch_command ("vfork", _("Catch calls to vfork."),
13526 catch_fork_command_1,
a96d9b2e 13527 NULL,
44feb3ce
TT
13528 (void *) (uintptr_t) catch_vfork_permanent,
13529 (void *) (uintptr_t) catch_vfork_temporary);
13530 add_catch_command ("exec", _("Catch calls to exec."),
13531 catch_exec_command_1,
a96d9b2e
SDJ
13532 NULL,
13533 CATCH_PERMANENT,
13534 CATCH_TEMPORARY);
13535 add_catch_command ("syscall", _("\
13536Catch system calls by their names and/or numbers.\n\
13537Arguments say which system calls to catch. If no arguments\n\
13538are given, every system call will be caught.\n\
13539Arguments, if given, should be one or more system call names\n\
13540(if your system supports that), or system call numbers."),
13541 catch_syscall_command_1,
13542 catch_syscall_completer,
44feb3ce
TT
13543 CATCH_PERMANENT,
13544 CATCH_TEMPORARY);
c5aa993b 13545
1bedd215
AC
13546 c = add_com ("watch", class_breakpoint, watch_command, _("\
13547Set a watchpoint for an expression.\n\
06a64a0b 13548Usage: watch [-l|-location] EXPRESSION\n\
c906108c 13549A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13550an expression changes.\n\
13551If -l or -location is given, this evaluates EXPRESSION and watches\n\
13552the memory to which it refers."));
65d12d83 13553 set_cmd_completer (c, expression_completer);
c906108c 13554
1bedd215
AC
13555 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13556Set a read watchpoint for an expression.\n\
06a64a0b 13557Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 13558A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13559an expression is read.\n\
13560If -l or -location is given, this evaluates EXPRESSION and watches\n\
13561the memory to which it refers."));
65d12d83 13562 set_cmd_completer (c, expression_completer);
c906108c 13563
1bedd215
AC
13564 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13565Set a watchpoint for an expression.\n\
06a64a0b 13566Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 13567A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13568an expression is either read or written.\n\
13569If -l or -location is given, this evaluates EXPRESSION and watches\n\
13570the memory to which it refers."));
65d12d83 13571 set_cmd_completer (c, expression_completer);
c906108c 13572
d77f58be 13573 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 13574Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 13575
920d2a44
AC
13576 /* XXX: cagney/2005-02-23: This should be a boolean, and should
13577 respond to changes - contrary to the description. */
85c07804
AC
13578 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
13579 &can_use_hw_watchpoints, _("\
13580Set debugger's willingness to use watchpoint hardware."), _("\
13581Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
13582If zero, gdb will not use hardware for new watchpoints, even if\n\
13583such is available. (However, any hardware watchpoints that were\n\
13584created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
13585hardware.)"),
13586 NULL,
920d2a44 13587 show_can_use_hw_watchpoints,
85c07804 13588 &setlist, &showlist);
c906108c
SS
13589
13590 can_use_hw_watchpoints = 1;
fa8d40ab 13591
1042e4c0
SS
13592 /* Tracepoint manipulation commands. */
13593
13594 c = add_com ("trace", class_breakpoint, trace_command, _("\
13595Set a tracepoint at specified line or function.\n\
13596\n"
13597BREAK_ARGS_HELP ("trace") "\n\
13598Do \"help tracepoints\" for info on other tracepoint commands."));
13599 set_cmd_completer (c, location_completer);
13600
13601 add_com_alias ("tp", "trace", class_alias, 0);
13602 add_com_alias ("tr", "trace", class_alias, 1);
13603 add_com_alias ("tra", "trace", class_alias, 1);
13604 add_com_alias ("trac", "trace", class_alias, 1);
13605
7a697b8d
SS
13606 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
13607Set a fast tracepoint at specified line or function.\n\
13608\n"
13609BREAK_ARGS_HELP ("ftrace") "\n\
13610Do \"help tracepoints\" for info on other tracepoint commands."));
13611 set_cmd_completer (c, location_completer);
13612
0fb4aa4b
PA
13613 c = add_com ("strace", class_breakpoint, strace_command, _("\
13614Set a static tracepoint at specified line, function or marker.\n\
13615\n\
13616strace [LOCATION] [if CONDITION]\n\
13617LOCATION may be a line number, function name, \"*\" and an address,\n\
13618or -m MARKER_ID.\n\
13619If a line number is specified, probe the marker at start of code\n\
13620for that line. If a function is specified, probe the marker at start\n\
13621of code for that function. If an address is specified, probe the marker\n\
13622at that exact address. If a marker id is specified, probe the marker\n\
13623with that name. With no LOCATION, uses current execution address of\n\
13624the selected stack frame.\n\
13625Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
13626This collects arbitrary user data passed in the probe point call to the\n\
13627tracing library. You can inspect it when analyzing the trace buffer,\n\
13628by printing the $_sdata variable like any other convenience variable.\n\
13629\n\
13630CONDITION is a boolean expression.\n\
13631\n\
d41c0fc8
PA
13632Multiple tracepoints at one place are permitted, and useful if their\n\
13633conditions are different.\n\
0fb4aa4b
PA
13634\n\
13635Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13636Do \"help tracepoints\" for info on other tracepoint commands."));
13637 set_cmd_completer (c, location_completer);
13638
1042e4c0 13639 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 13640Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
13641Convenience variable \"$tpnum\" contains the number of the\n\
13642last tracepoint set."));
13643
13644 add_info_alias ("tp", "tracepoints", 1);
13645
13646 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13647Delete specified tracepoints.\n\
13648Arguments are tracepoint numbers, separated by spaces.\n\
13649No argument means delete all tracepoints."),
13650 &deletelist);
13651
13652 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13653Disable specified tracepoints.\n\
13654Arguments are tracepoint numbers, separated by spaces.\n\
13655No argument means disable all tracepoints."),
13656 &disablelist);
13657 deprecate_cmd (c, "disable");
13658
13659 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
13660Enable specified tracepoints.\n\
13661Arguments are tracepoint numbers, separated by spaces.\n\
13662No argument means enable all tracepoints."),
13663 &enablelist);
13664 deprecate_cmd (c, "enable");
13665
13666 add_com ("passcount", class_trace, trace_pass_command, _("\
13667Set the passcount for a tracepoint.\n\
13668The trace will end when the tracepoint has been passed 'count' times.\n\
13669Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
13670if TPNUM is omitted, passcount refers to the last tracepoint defined."));
13671
6149aea9
PA
13672 add_prefix_cmd ("save", class_breakpoint, save_command,
13673 _("Save breakpoint definitions as a script."),
13674 &save_cmdlist, "save ",
13675 0/*allow-unknown*/, &cmdlist);
13676
13677 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
13678Save current breakpoint definitions as a script.\n\
cce7e648 13679This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
13680catchpoints, tracepoints). Use the 'source' command in another debug\n\
13681session to restore them."),
13682 &save_cmdlist);
13683 set_cmd_completer (c, filename_completer);
13684
13685 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 13686Save current tracepoint definitions as a script.\n\
6149aea9
PA
13687Use the 'source' command in another debug session to restore them."),
13688 &save_cmdlist);
1042e4c0
SS
13689 set_cmd_completer (c, filename_completer);
13690
6149aea9
PA
13691 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
13692 deprecate_cmd (c, "save tracepoints");
13693
1bedd215 13694 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
13695Breakpoint specific settings\n\
13696Configure various breakpoint-specific variables such as\n\
1bedd215 13697pending breakpoint behavior"),
fa8d40ab
JJ
13698 &breakpoint_set_cmdlist, "set breakpoint ",
13699 0/*allow-unknown*/, &setlist);
1bedd215 13700 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
13701Breakpoint specific settings\n\
13702Configure various breakpoint-specific variables such as\n\
1bedd215 13703pending breakpoint behavior"),
fa8d40ab
JJ
13704 &breakpoint_show_cmdlist, "show breakpoint ",
13705 0/*allow-unknown*/, &showlist);
13706
7915a72c
AC
13707 add_setshow_auto_boolean_cmd ("pending", no_class,
13708 &pending_break_support, _("\
13709Set debugger's behavior regarding pending breakpoints."), _("\
13710Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
13711If on, an unrecognized breakpoint location will cause gdb to create a\n\
13712pending breakpoint. If off, an unrecognized breakpoint location results in\n\
13713an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 13714user-query to see if a pending breakpoint should be created."),
2c5b56ce 13715 NULL,
920d2a44 13716 show_pending_break_support,
6e1d7d6c
AC
13717 &breakpoint_set_cmdlist,
13718 &breakpoint_show_cmdlist);
fa8d40ab
JJ
13719
13720 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
13721
13722 add_setshow_boolean_cmd ("auto-hw", no_class,
13723 &automatic_hardware_breakpoints, _("\
13724Set automatic usage of hardware breakpoints."), _("\
13725Show automatic usage of hardware breakpoints."), _("\
13726If set, the debugger will automatically use hardware breakpoints for\n\
13727breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
13728a warning will be emitted for such breakpoints."),
13729 NULL,
13730 show_automatic_hardware_breakpoints,
13731 &breakpoint_set_cmdlist,
13732 &breakpoint_show_cmdlist);
74960c60 13733
33e5cbd6
PA
13734 add_setshow_enum_cmd ("always-inserted", class_support,
13735 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
13736Set mode for inserting breakpoints."), _("\
13737Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
13738When this mode is off, breakpoints are inserted in inferior when it is\n\
13739resumed, and removed when execution stops. When this mode is on,\n\
13740breakpoints are inserted immediately and removed only when the user\n\
13741deletes the breakpoint. When this mode is auto (which is the default),\n\
13742the behaviour depends on the non-stop setting (see help set non-stop).\n\
13743In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
13744behaves as if always-inserted mode is on; if gdb is controlling the\n\
13745inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
13746 NULL,
13747 &show_always_inserted_mode,
13748 &breakpoint_set_cmdlist,
13749 &breakpoint_show_cmdlist);
f1310107
TJB
13750
13751 add_com ("break-range", class_breakpoint, break_range_command, _("\
13752Set a breakpoint for an address range.\n\
13753break-range START-LOCATION, END-LOCATION\n\
13754where START-LOCATION and END-LOCATION can be one of the following:\n\
13755 LINENUM, for that line in the current file,\n\
13756 FILE:LINENUM, for that line in that file,\n\
13757 +OFFSET, for that number of lines after the current line\n\
13758 or the start of the range\n\
13759 FUNCTION, for the first line in that function,\n\
13760 FILE:FUNCTION, to distinguish among like-named static functions.\n\
13761 *ADDRESS, for the instruction at that address.\n\
13762\n\
13763The breakpoint will stop execution of the inferior whenever it executes\n\
13764an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
13765range (including START-LOCATION and END-LOCATION)."));
13766
765dc015 13767 automatic_hardware_breakpoints = 1;
f3b1572e
PA
13768
13769 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 13770}