]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
2012-01-16 Pedro Alves <palves@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
0b302171 3 Copyright (C) 1986-2012 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
35#include "gdbthread.h"
36#include "target.h"
37#include "language.h"
38#include "gdb_string.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
0225421b 50#include "gdb_assert.h"
fe898f56 51#include "block.h"
a77053c2 52#include "solib.h"
84acb35a
JJ
53#include "solist.h"
54#include "observer.h"
60250e8b 55#include "exceptions.h"
765dc015 56#include "memattr.h"
f7f9143b 57#include "ada-lang.h"
d1aa2f50 58#include "top.h"
79a45b7d 59#include "valprint.h"
4efc6507 60#include "jit.h"
a96d9b2e 61#include "xml-syscall.h"
65d79d4b 62#include "parser-defs.h"
e9cafbcc 63#include "cli/cli-utils.h"
be34f849 64#include "continuations.h"
1bfeeb0f
JL
65#include "stack.h"
66#include "skip.h"
049e32d3 67#include "record.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,
c0a91b2b 111 const struct breakpoint_ops *);
3742cc8b
YQ
112static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
113 const struct symtab_and_line *);
114
4a64f543
MS
115/* This function is used in gdbtk sources and thus can not be made
116 static. */
63c252f8 117struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 118 struct symtab_and_line,
c0a91b2b
TT
119 enum bptype,
120 const struct breakpoint_ops *);
c906108c 121
06edf0c0
PA
122static struct breakpoint *
123 momentary_breakpoint_from_master (struct breakpoint *orig,
124 enum bptype type,
c0a91b2b 125 const struct breakpoint_ops *ops);
06edf0c0 126
76897487
KB
127static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
128
a6d9a66e
UW
129static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
130 CORE_ADDR bpaddr,
88f7da05 131 enum bptype bptype);
76897487 132
6c95b8df
PA
133static void describe_other_breakpoints (struct gdbarch *,
134 struct program_space *, CORE_ADDR,
5af949e3 135 struct obj_section *, int);
c906108c 136
6c95b8df
PA
137static int breakpoint_address_match (struct address_space *aspace1,
138 CORE_ADDR addr1,
139 struct address_space *aspace2,
140 CORE_ADDR addr2);
141
85d721b8
PA
142static int watchpoint_locations_match (struct bp_location *loc1,
143 struct bp_location *loc2);
144
f1310107
TJB
145static int breakpoint_location_address_match (struct bp_location *bl,
146 struct address_space *aspace,
147 CORE_ADDR addr);
148
a14ed312 149static void breakpoints_info (char *, int);
c906108c 150
d77f58be
SS
151static void watchpoints_info (char *, int);
152
e5a67952
MS
153static int breakpoint_1 (char *, int,
154 int (*) (const struct breakpoint *));
c906108c 155
4efb68b1 156static int breakpoint_cond_eval (void *);
c906108c 157
4efb68b1 158static void cleanup_executing_breakpoints (void *);
c906108c 159
a14ed312 160static void commands_command (char *, int);
c906108c 161
a14ed312 162static void condition_command (char *, int);
c906108c 163
c5aa993b
JM
164typedef enum
165 {
166 mark_inserted,
167 mark_uninserted
168 }
169insertion_state_t;
c906108c 170
0bde7532 171static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 172static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 173
e514a9d6 174static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 175
4efb68b1 176static int watchpoint_check (void *);
c906108c 177
a14ed312 178static void maintenance_info_breakpoints (char *, int);
c906108c 179
a14ed312 180static int hw_breakpoint_used_count (void);
c906108c 181
a1398e0c
PA
182static int hw_watchpoint_use_count (struct breakpoint *);
183
184static int hw_watchpoint_used_count_others (struct breakpoint *except,
185 enum bptype type,
186 int *other_type_used);
c906108c 187
a14ed312 188static void hbreak_command (char *, int);
c906108c 189
a14ed312 190static void thbreak_command (char *, int);
c906108c 191
51be5b68 192static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp);
c906108c 193
a14ed312 194static void stop_command (char *arg, int from_tty);
7a292a7a 195
a14ed312 196static void stopin_command (char *arg, int from_tty);
7a292a7a 197
a14ed312 198static void stopat_command (char *arg, int from_tty);
7a292a7a 199
a14ed312 200static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 201
d85310f7
MS
202static void catch_exception_command_1 (enum exception_event_kind ex_event,
203 char *arg, int tempflag, int from_tty);
7a292a7a 204
a14ed312 205static void tcatch_command (char *arg, int from_tty);
7a292a7a 206
d03285ec
UW
207static void detach_single_step_breakpoints (void);
208
6c95b8df
PA
209static int single_step_breakpoint_inserted_here_p (struct address_space *,
210 CORE_ADDR pc);
1aafd4da 211
fe3f5fa8 212static void free_bp_location (struct bp_location *loc);
f431efe5
PA
213static void incref_bp_location (struct bp_location *loc);
214static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 215
39d61571 216static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 217
b60e7edf 218static void update_global_location_list (int);
a5606eee 219
b60e7edf 220static void update_global_location_list_nothrow (int);
74960c60 221
d77f58be 222static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
223
224static void insert_breakpoint_locations (void);
a5606eee 225
a96d9b2e
SDJ
226static int syscall_catchpoint_p (struct breakpoint *b);
227
1042e4c0
SS
228static void tracepoints_info (char *, int);
229
230static void delete_trace_command (char *, int);
231
232static void enable_trace_command (char *, int);
233
234static void disable_trace_command (char *, int);
235
236static void trace_pass_command (char *, int);
237
9c06b0b4
TJB
238static int is_masked_watchpoint (const struct breakpoint *b);
239
0fb4aa4b
PA
240/* Assuming we're creating a static tracepoint, does S look like a
241 static tracepoint marker spec ("-m MARKER_ID")? */
242#define is_marker_spec(s) \
f5a8e22b 243 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 244
2060206e
PA
245/* The abstract base class all breakpoint_ops structures inherit
246 from. */
247static struct breakpoint_ops base_breakpoint_ops;
248
249/* The breakpoint_ops structure to be inherited by all breakpoint_ops
250 that are implemented on top of software or hardware breakpoints
251 (user breakpoints, internal and momentary breakpoints, etc.). */
252static struct breakpoint_ops bkpt_base_breakpoint_ops;
253
254/* Internal breakpoints class type. */
06edf0c0 255static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
256
257/* Momentary breakpoints class type. */
06edf0c0
PA
258static struct breakpoint_ops momentary_breakpoint_ops;
259
2060206e
PA
260/* The breakpoint_ops structure to be used in regular user created
261 breakpoints. */
262struct breakpoint_ops bkpt_breakpoint_ops;
263
5cea2a26
PA
264/* A reference-counted struct command_line. This lets multiple
265 breakpoints share a single command list. */
266struct counted_command_line
267{
268 /* The reference count. */
269 int refc;
270
271 /* The command list. */
272 struct command_line *commands;
273};
274
275struct command_line *
276breakpoint_commands (struct breakpoint *b)
277{
278 return b->commands ? b->commands->commands : NULL;
279}
3daf8fe5 280
f3b1572e
PA
281/* Flag indicating that a command has proceeded the inferior past the
282 current breakpoint. */
283
284static int breakpoint_proceeded;
285
956a9fb9 286const char *
2cec12e5
AR
287bpdisp_text (enum bpdisp disp)
288{
4a64f543
MS
289 /* NOTE: the following values are a part of MI protocol and
290 represent values of 'disp' field returned when inferior stops at
291 a breakpoint. */
bc043ef3 292 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 293
2cec12e5
AR
294 return bpdisps[(int) disp];
295}
c906108c 296
4a64f543 297/* Prototypes for exported functions. */
c906108c 298/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 299 if such is available. */
c906108c
SS
300static int can_use_hw_watchpoints;
301
920d2a44
AC
302static void
303show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
304 struct cmd_list_element *c,
305 const char *value)
306{
3e43a32a
MS
307 fprintf_filtered (file,
308 _("Debugger's willingness to use "
309 "watchpoint hardware is %s.\n"),
920d2a44
AC
310 value);
311}
312
fa8d40ab
JJ
313/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
314 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 315 for unrecognized breakpoint locations.
fa8d40ab
JJ
316 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
317static enum auto_boolean pending_break_support;
920d2a44
AC
318static void
319show_pending_break_support (struct ui_file *file, int from_tty,
320 struct cmd_list_element *c,
321 const char *value)
322{
3e43a32a
MS
323 fprintf_filtered (file,
324 _("Debugger's behavior regarding "
325 "pending breakpoints is %s.\n"),
920d2a44
AC
326 value);
327}
fa8d40ab 328
765dc015 329/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 330 set with "break" but falling in read-only memory.
765dc015
VP
331 If 0, gdb will warn about such breakpoints, but won't automatically
332 use hardware breakpoints. */
333static int automatic_hardware_breakpoints;
334static void
335show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
336 struct cmd_list_element *c,
337 const char *value)
338{
3e43a32a
MS
339 fprintf_filtered (file,
340 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
341 value);
342}
343
33e5cbd6
PA
344/* If on, gdb will keep breakpoints inserted even as inferior is
345 stopped, and immediately insert any new breakpoints. If off, gdb
346 will insert breakpoints into inferior only when resuming it, and
347 will remove breakpoints upon stop. If auto, GDB will behave as ON
348 if in non-stop mode, and as OFF if all-stop mode.*/
349
350static const char always_inserted_auto[] = "auto";
351static const char always_inserted_on[] = "on";
352static const char always_inserted_off[] = "off";
353static const char *always_inserted_enums[] = {
354 always_inserted_auto,
355 always_inserted_off,
356 always_inserted_on,
357 NULL
358};
359static const char *always_inserted_mode = always_inserted_auto;
360static void
74960c60 361show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 362 struct cmd_list_element *c, const char *value)
74960c60 363{
33e5cbd6 364 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
365 fprintf_filtered (file,
366 _("Always inserted breakpoint "
367 "mode is %s (currently %s).\n"),
33e5cbd6
PA
368 value,
369 breakpoints_always_inserted_mode () ? "on" : "off");
370 else
3e43a32a
MS
371 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
372 value);
74960c60
VP
373}
374
33e5cbd6
PA
375int
376breakpoints_always_inserted_mode (void)
377{
049e32d3
PA
378 return ((always_inserted_mode == always_inserted_on
379 || (always_inserted_mode == always_inserted_auto && non_stop))
380 && !RECORD_IS_USED);
33e5cbd6 381}
765dc015 382
a14ed312 383void _initialize_breakpoint (void);
c906108c 384
c906108c
SS
385/* Are we executing breakpoint commands? */
386static int executing_breakpoint_commands;
387
c02f5703
MS
388/* Are overlay event breakpoints enabled? */
389static int overlay_events_enabled;
390
e09342b5
TJB
391/* See description in breakpoint.h. */
392int target_exact_watchpoints = 0;
393
c906108c 394/* Walk the following statement or block through all breakpoints.
e5dd4106 395 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 396 current breakpoint. */
c906108c 397
5c44784c 398#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 399
5c44784c
JM
400#define ALL_BREAKPOINTS_SAFE(B,TMP) \
401 for (B = breakpoint_chain; \
402 B ? (TMP=B->next, 1): 0; \
403 B = TMP)
c906108c 404
4a64f543
MS
405/* Similar iterator for the low-level breakpoints. SAFE variant is
406 not provided so update_global_location_list must not be called
407 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 408
876fa593
JK
409#define ALL_BP_LOCATIONS(B,BP_TMP) \
410 for (BP_TMP = bp_location; \
411 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
412 BP_TMP++)
7cc221ef 413
1042e4c0
SS
414/* Iterator for tracepoints only. */
415
416#define ALL_TRACEPOINTS(B) \
417 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 418 if (is_tracepoint (B))
1042e4c0 419
7cc221ef 420/* Chains of all breakpoints defined. */
c906108c
SS
421
422struct breakpoint *breakpoint_chain;
423
876fa593
JK
424/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
425
426static struct bp_location **bp_location;
427
428/* Number of elements of BP_LOCATION. */
429
430static unsigned bp_location_count;
431
4a64f543
MS
432/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
433 ADDRESS for the current elements of BP_LOCATION which get a valid
434 result from bp_location_has_shadow. You can use it for roughly
435 limiting the subrange of BP_LOCATION to scan for shadow bytes for
436 an address you need to read. */
876fa593
JK
437
438static CORE_ADDR bp_location_placed_address_before_address_max;
439
4a64f543
MS
440/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
441 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
442 BP_LOCATION which get a valid result from bp_location_has_shadow.
443 You can use it for roughly limiting the subrange of BP_LOCATION to
444 scan for shadow bytes for an address you need to read. */
876fa593
JK
445
446static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 447
4a64f543
MS
448/* The locations that no longer correspond to any breakpoint, unlinked
449 from bp_location array, but for which a hit may still be reported
450 by a target. */
20874c92
VP
451VEC(bp_location_p) *moribund_locations = NULL;
452
c906108c
SS
453/* Number of last breakpoint made. */
454
95a42b64
TT
455static int breakpoint_count;
456
86b17b60
PA
457/* The value of `breakpoint_count' before the last command that
458 created breakpoints. If the last (break-like) command created more
459 than one breakpoint, then the difference between BREAKPOINT_COUNT
460 and PREV_BREAKPOINT_COUNT is more than one. */
461static int prev_breakpoint_count;
c906108c 462
1042e4c0
SS
463/* Number of last tracepoint made. */
464
95a42b64 465static int tracepoint_count;
1042e4c0 466
6149aea9
PA
467static struct cmd_list_element *breakpoint_set_cmdlist;
468static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 469struct cmd_list_element *save_cmdlist;
6149aea9 470
468d015d
JJ
471/* Return whether a breakpoint is an active enabled breakpoint. */
472static int
473breakpoint_enabled (struct breakpoint *b)
474{
0d381245 475 return (b->enable_state == bp_enabled);
468d015d
JJ
476}
477
c906108c
SS
478/* Set breakpoint count to NUM. */
479
95a42b64 480static void
fba45db2 481set_breakpoint_count (int num)
c906108c 482{
86b17b60 483 prev_breakpoint_count = breakpoint_count;
c906108c 484 breakpoint_count = num;
4fa62494 485 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
486}
487
86b17b60
PA
488/* Used by `start_rbreak_breakpoints' below, to record the current
489 breakpoint count before "rbreak" creates any breakpoint. */
490static int rbreak_start_breakpoint_count;
491
95a42b64
TT
492/* Called at the start an "rbreak" command to record the first
493 breakpoint made. */
86b17b60 494
95a42b64
TT
495void
496start_rbreak_breakpoints (void)
497{
86b17b60 498 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
499}
500
501/* Called at the end of an "rbreak" command to record the last
502 breakpoint made. */
86b17b60 503
95a42b64
TT
504void
505end_rbreak_breakpoints (void)
506{
86b17b60 507 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
508}
509
4a64f543 510/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
511
512void
fba45db2 513clear_breakpoint_hit_counts (void)
c906108c
SS
514{
515 struct breakpoint *b;
516
517 ALL_BREAKPOINTS (b)
518 b->hit_count = 0;
519}
520
9add0f1b
TT
521/* Allocate a new counted_command_line with reference count of 1.
522 The new structure owns COMMANDS. */
523
524static struct counted_command_line *
525alloc_counted_command_line (struct command_line *commands)
526{
527 struct counted_command_line *result
528 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 529
9add0f1b
TT
530 result->refc = 1;
531 result->commands = commands;
532 return result;
533}
534
535/* Increment reference count. This does nothing if CMD is NULL. */
536
537static void
538incref_counted_command_line (struct counted_command_line *cmd)
539{
540 if (cmd)
541 ++cmd->refc;
542}
543
544/* Decrement reference count. If the reference count reaches 0,
545 destroy the counted_command_line. Sets *CMDP to NULL. This does
546 nothing if *CMDP is NULL. */
547
548static void
549decref_counted_command_line (struct counted_command_line **cmdp)
550{
551 if (*cmdp)
552 {
553 if (--(*cmdp)->refc == 0)
554 {
555 free_command_lines (&(*cmdp)->commands);
556 xfree (*cmdp);
557 }
558 *cmdp = NULL;
559 }
560}
561
562/* A cleanup function that calls decref_counted_command_line. */
563
564static void
565do_cleanup_counted_command_line (void *arg)
566{
567 decref_counted_command_line (arg);
568}
569
570/* Create a cleanup that calls decref_counted_command_line on the
571 argument. */
572
573static struct cleanup *
574make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
575{
576 return make_cleanup (do_cleanup_counted_command_line, cmdp);
577}
578
c906108c 579\f
48cb2d85
VP
580/* Return the breakpoint with the specified number, or NULL
581 if the number does not refer to an existing breakpoint. */
582
583struct breakpoint *
584get_breakpoint (int num)
585{
586 struct breakpoint *b;
587
588 ALL_BREAKPOINTS (b)
589 if (b->number == num)
590 return b;
591
592 return NULL;
593}
5c44784c 594
c906108c 595\f
adc36818
PM
596
597void
598set_breakpoint_condition (struct breakpoint *b, char *exp,
599 int from_tty)
600{
3a5c3e22
PA
601 xfree (b->cond_string);
602 b->cond_string = NULL;
adc36818 603
3a5c3e22 604 if (is_watchpoint (b))
adc36818 605 {
3a5c3e22
PA
606 struct watchpoint *w = (struct watchpoint *) b;
607
608 xfree (w->cond_exp);
609 w->cond_exp = NULL;
610 }
611 else
612 {
613 struct bp_location *loc;
614
615 for (loc = b->loc; loc; loc = loc->next)
616 {
617 xfree (loc->cond);
618 loc->cond = NULL;
619 }
adc36818 620 }
adc36818
PM
621
622 if (*exp == 0)
623 {
624 if (from_tty)
625 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
626 }
627 else
628 {
629 char *arg = exp;
cc59ec59 630
adc36818
PM
631 /* I don't know if it matters whether this is the string the user
632 typed in or the decompiled expression. */
633 b->cond_string = xstrdup (arg);
634 b->condition_not_parsed = 0;
635
636 if (is_watchpoint (b))
637 {
3a5c3e22
PA
638 struct watchpoint *w = (struct watchpoint *) b;
639
adc36818
PM
640 innermost_block = NULL;
641 arg = exp;
3a5c3e22 642 w->cond_exp = parse_exp_1 (&arg, 0, 0);
adc36818
PM
643 if (*arg)
644 error (_("Junk at end of expression"));
3a5c3e22 645 w->cond_exp_valid_block = innermost_block;
adc36818
PM
646 }
647 else
648 {
3a5c3e22
PA
649 struct bp_location *loc;
650
adc36818
PM
651 for (loc = b->loc; loc; loc = loc->next)
652 {
653 arg = exp;
654 loc->cond =
655 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
656 if (*arg)
657 error (_("Junk at end of expression"));
658 }
659 }
660 }
661 breakpoints_changed ();
8d3788bd 662 observer_notify_breakpoint_modified (b);
adc36818
PM
663}
664
c906108c
SS
665/* condition N EXP -- set break condition of breakpoint N to EXP. */
666
667static void
fba45db2 668condition_command (char *arg, int from_tty)
c906108c 669{
52f0bd74 670 struct breakpoint *b;
c906108c 671 char *p;
52f0bd74 672 int bnum;
c906108c
SS
673
674 if (arg == 0)
e2e0b3e5 675 error_no_arg (_("breakpoint number"));
c906108c
SS
676
677 p = arg;
678 bnum = get_number (&p);
5c44784c 679 if (bnum == 0)
8a3fe4f8 680 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
681
682 ALL_BREAKPOINTS (b)
683 if (b->number == bnum)
2f069f6f 684 {
7371cf6d
PM
685 /* Check if this breakpoint has a Python object assigned to
686 it, and if it has a definition of the "stop"
687 method. This method and conditions entered into GDB from
688 the CLI are mutually exclusive. */
689 if (b->py_bp_object
690 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
691 error (_("Cannot set a condition where a Python 'stop' "
692 "method has been defined in the breakpoint."));
2566ad2d 693 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
694 return;
695 }
c906108c 696
8a3fe4f8 697 error (_("No breakpoint number %d."), bnum);
c906108c
SS
698}
699
a7bdde9e
VP
700/* Check that COMMAND do not contain commands that are suitable
701 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
702 Throw if any such commands is found. */
703
a7bdde9e
VP
704static void
705check_no_tracepoint_commands (struct command_line *commands)
706{
707 struct command_line *c;
cc59ec59 708
a7bdde9e
VP
709 for (c = commands; c; c = c->next)
710 {
711 int i;
712
713 if (c->control_type == while_stepping_control)
3e43a32a
MS
714 error (_("The 'while-stepping' command can "
715 "only be used for tracepoints"));
a7bdde9e
VP
716
717 for (i = 0; i < c->body_count; ++i)
718 check_no_tracepoint_commands ((c->body_list)[i]);
719
720 /* Not that command parsing removes leading whitespace and comment
4a64f543 721 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
722 command directly. */
723 if (strstr (c->line, "collect ") == c->line)
724 error (_("The 'collect' command can only be used for tracepoints"));
725
51661e93
VP
726 if (strstr (c->line, "teval ") == c->line)
727 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
728 }
729}
730
d77f58be
SS
731/* Encapsulate tests for different types of tracepoints. */
732
d9b3f62e
PA
733static int
734is_tracepoint_type (enum bptype type)
735{
736 return (type == bp_tracepoint
737 || type == bp_fast_tracepoint
738 || type == bp_static_tracepoint);
739}
740
a7bdde9e 741int
d77f58be 742is_tracepoint (const struct breakpoint *b)
a7bdde9e 743{
d9b3f62e 744 return is_tracepoint_type (b->type);
a7bdde9e 745}
d9b3f62e 746
e5dd4106 747/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
748 breakpoint. This function will throw an exception if a problem is
749 found. */
48cb2d85 750
95a42b64
TT
751static void
752validate_commands_for_breakpoint (struct breakpoint *b,
753 struct command_line *commands)
48cb2d85 754{
d77f58be 755 if (is_tracepoint (b))
a7bdde9e 756 {
4a64f543
MS
757 /* We need to verify that each top-level element of commands is
758 valid for tracepoints, that there's at most one
759 while-stepping element, and that while-stepping's body has
760 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
761 struct command_line *c;
762 struct command_line *while_stepping = 0;
763 for (c = commands; c; c = c->next)
764 {
a7bdde9e
VP
765 if (c->control_type == while_stepping_control)
766 {
767 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
768 error (_("The 'while-stepping' command "
769 "cannot be used for fast tracepoint"));
0fb4aa4b 770 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
771 error (_("The 'while-stepping' command "
772 "cannot be used for static tracepoint"));
a7bdde9e
VP
773
774 if (while_stepping)
3e43a32a
MS
775 error (_("The 'while-stepping' command "
776 "can be used only once"));
a7bdde9e
VP
777 else
778 while_stepping = c;
779 }
780 }
781 if (while_stepping)
782 {
783 struct command_line *c2;
784
785 gdb_assert (while_stepping->body_count == 1);
786 c2 = while_stepping->body_list[0];
787 for (; c2; c2 = c2->next)
788 {
a7bdde9e
VP
789 if (c2->control_type == while_stepping_control)
790 error (_("The 'while-stepping' command cannot be nested"));
791 }
792 }
793 }
794 else
795 {
796 check_no_tracepoint_commands (commands);
797 }
95a42b64
TT
798}
799
0fb4aa4b
PA
800/* Return a vector of all the static tracepoints set at ADDR. The
801 caller is responsible for releasing the vector. */
802
803VEC(breakpoint_p) *
804static_tracepoints_here (CORE_ADDR addr)
805{
806 struct breakpoint *b;
807 VEC(breakpoint_p) *found = 0;
808 struct bp_location *loc;
809
810 ALL_BREAKPOINTS (b)
811 if (b->type == bp_static_tracepoint)
812 {
813 for (loc = b->loc; loc; loc = loc->next)
814 if (loc->address == addr)
815 VEC_safe_push(breakpoint_p, found, b);
816 }
817
818 return found;
819}
820
95a42b64 821/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 822 validate that only allowed commands are included. */
95a42b64
TT
823
824void
4a64f543
MS
825breakpoint_set_commands (struct breakpoint *b,
826 struct command_line *commands)
95a42b64
TT
827{
828 validate_commands_for_breakpoint (b, commands);
a7bdde9e 829
9add0f1b
TT
830 decref_counted_command_line (&b->commands);
831 b->commands = alloc_counted_command_line (commands);
48cb2d85 832 breakpoints_changed ();
8d3788bd 833 observer_notify_breakpoint_modified (b);
48cb2d85
VP
834}
835
45a43567
TT
836/* Set the internal `silent' flag on the breakpoint. Note that this
837 is not the same as the "silent" that may appear in the breakpoint's
838 commands. */
839
840void
841breakpoint_set_silent (struct breakpoint *b, int silent)
842{
843 int old_silent = b->silent;
844
845 b->silent = silent;
846 if (old_silent != silent)
8d3788bd 847 observer_notify_breakpoint_modified (b);
45a43567
TT
848}
849
850/* Set the thread for this breakpoint. If THREAD is -1, make the
851 breakpoint work for any thread. */
852
853void
854breakpoint_set_thread (struct breakpoint *b, int thread)
855{
856 int old_thread = b->thread;
857
858 b->thread = thread;
859 if (old_thread != thread)
8d3788bd 860 observer_notify_breakpoint_modified (b);
45a43567
TT
861}
862
863/* Set the task for this breakpoint. If TASK is 0, make the
864 breakpoint work for any task. */
865
866void
867breakpoint_set_task (struct breakpoint *b, int task)
868{
869 int old_task = b->task;
870
871 b->task = task;
872 if (old_task != task)
8d3788bd 873 observer_notify_breakpoint_modified (b);
45a43567
TT
874}
875
95a42b64
TT
876void
877check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
878{
879 struct breakpoint *b = closure;
cc59ec59 880
a7bdde9e
VP
881 validate_actionline (&line, b);
882}
883
95a42b64
TT
884/* A structure used to pass information through
885 map_breakpoint_numbers. */
886
887struct commands_info
888{
889 /* True if the command was typed at a tty. */
890 int from_tty;
86b17b60
PA
891
892 /* The breakpoint range spec. */
893 char *arg;
894
95a42b64
TT
895 /* Non-NULL if the body of the commands are being read from this
896 already-parsed command. */
897 struct command_line *control;
86b17b60 898
95a42b64
TT
899 /* The command lines read from the user, or NULL if they have not
900 yet been read. */
901 struct counted_command_line *cmd;
902};
903
904/* A callback for map_breakpoint_numbers that sets the commands for
905 commands_command. */
906
c906108c 907static void
95a42b64 908do_map_commands_command (struct breakpoint *b, void *data)
c906108c 909{
95a42b64 910 struct commands_info *info = data;
c906108c 911
95a42b64
TT
912 if (info->cmd == NULL)
913 {
914 struct command_line *l;
5c44784c 915
95a42b64
TT
916 if (info->control != NULL)
917 l = copy_command_lines (info->control->body_list[0]);
918 else
86b17b60
PA
919 {
920 struct cleanup *old_chain;
921 char *str;
c5aa993b 922
3e43a32a
MS
923 str = xstrprintf (_("Type commands for breakpoint(s) "
924 "%s, one per line."),
86b17b60
PA
925 info->arg);
926
927 old_chain = make_cleanup (xfree, str);
928
929 l = read_command_lines (str,
930 info->from_tty, 1,
d77f58be 931 (is_tracepoint (b)
86b17b60
PA
932 ? check_tracepoint_command : 0),
933 b);
934
935 do_cleanups (old_chain);
936 }
a7bdde9e 937
95a42b64
TT
938 info->cmd = alloc_counted_command_line (l);
939 }
940
941 /* If a breakpoint was on the list more than once, we don't need to
942 do anything. */
943 if (b->commands != info->cmd)
944 {
945 validate_commands_for_breakpoint (b, info->cmd->commands);
946 incref_counted_command_line (info->cmd);
947 decref_counted_command_line (&b->commands);
948 b->commands = info->cmd;
949 breakpoints_changed ();
8d3788bd 950 observer_notify_breakpoint_modified (b);
c5aa993b 951 }
95a42b64
TT
952}
953
954static void
4a64f543
MS
955commands_command_1 (char *arg, int from_tty,
956 struct command_line *control)
95a42b64
TT
957{
958 struct cleanup *cleanups;
959 struct commands_info info;
960
961 info.from_tty = from_tty;
962 info.control = control;
963 info.cmd = NULL;
964 /* If we read command lines from the user, then `info' will hold an
965 extra reference to the commands that we must clean up. */
966 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
967
968 if (arg == NULL || !*arg)
969 {
86b17b60 970 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
971 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
972 breakpoint_count);
95a42b64
TT
973 else if (breakpoint_count > 0)
974 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
975 else
976 {
977 /* So that we don't try to free the incoming non-NULL
978 argument in the cleanup below. Mapping breakpoint
979 numbers will fail in this case. */
980 arg = NULL;
981 }
95a42b64 982 }
9766ced4
SS
983 else
984 /* The command loop has some static state, so we need to preserve
985 our argument. */
986 arg = xstrdup (arg);
86b17b60
PA
987
988 if (arg != NULL)
989 make_cleanup (xfree, arg);
990
991 info.arg = arg;
95a42b64
TT
992
993 map_breakpoint_numbers (arg, do_map_commands_command, &info);
994
995 if (info.cmd == NULL)
996 error (_("No breakpoints specified."));
997
998 do_cleanups (cleanups);
999}
1000
1001static void
1002commands_command (char *arg, int from_tty)
1003{
1004 commands_command_1 (arg, from_tty, NULL);
c906108c 1005}
40c03ae8
EZ
1006
1007/* Like commands_command, but instead of reading the commands from
1008 input stream, takes them from an already parsed command structure.
1009
1010 This is used by cli-script.c to DTRT with breakpoint commands
1011 that are part of if and while bodies. */
1012enum command_control_type
1013commands_from_control_command (char *arg, struct command_line *cmd)
1014{
95a42b64
TT
1015 commands_command_1 (arg, 0, cmd);
1016 return simple_control;
40c03ae8 1017}
876fa593
JK
1018
1019/* Return non-zero if BL->TARGET_INFO contains valid information. */
1020
1021static int
1022bp_location_has_shadow (struct bp_location *bl)
1023{
1024 if (bl->loc_type != bp_loc_software_breakpoint)
1025 return 0;
1026 if (!bl->inserted)
1027 return 0;
1028 if (bl->target_info.shadow_len == 0)
e5dd4106 1029 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1030 return 0;
1031 return 1;
1032}
1033
8defab1a 1034/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1035 by replacing any memory breakpoints with their shadowed contents.
1036
1037 The range of shadowed area by each bp_location is:
35df4500
TJB
1038 bl->address - bp_location_placed_address_before_address_max
1039 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1040 The range we were requested to resolve shadows for is:
1041 memaddr ... memaddr + len
1042 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1043 memaddr + len <= (bl->address
1044 - bp_location_placed_address_before_address_max)
876fa593 1045 and:
35df4500 1046 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1047
8defab1a 1048void
f0ba3972
PA
1049breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1050 const gdb_byte *writebuf_org,
1051 ULONGEST memaddr, LONGEST len)
c906108c 1052{
4a64f543
MS
1053 /* Left boundary, right boundary and median element of our binary
1054 search. */
876fa593
JK
1055 unsigned bc_l, bc_r, bc;
1056
4a64f543
MS
1057 /* Find BC_L which is a leftmost element which may affect BUF
1058 content. It is safe to report lower value but a failure to
1059 report higher one. */
876fa593
JK
1060
1061 bc_l = 0;
1062 bc_r = bp_location_count;
1063 while (bc_l + 1 < bc_r)
1064 {
35df4500 1065 struct bp_location *bl;
876fa593
JK
1066
1067 bc = (bc_l + bc_r) / 2;
35df4500 1068 bl = bp_location[bc];
876fa593 1069
4a64f543
MS
1070 /* Check first BL->ADDRESS will not overflow due to the added
1071 constant. Then advance the left boundary only if we are sure
1072 the BC element can in no way affect the BUF content (MEMADDR
1073 to MEMADDR + LEN range).
876fa593 1074
4a64f543
MS
1075 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1076 offset so that we cannot miss a breakpoint with its shadow
1077 range tail still reaching MEMADDR. */
c5aa993b 1078
35df4500
TJB
1079 if ((bl->address + bp_location_shadow_len_after_address_max
1080 >= bl->address)
1081 && (bl->address + bp_location_shadow_len_after_address_max
1082 <= memaddr))
876fa593
JK
1083 bc_l = bc;
1084 else
1085 bc_r = bc;
1086 }
1087
128070bb
PA
1088 /* Due to the binary search above, we need to make sure we pick the
1089 first location that's at BC_L's address. E.g., if there are
1090 multiple locations at the same address, BC_L may end up pointing
1091 at a duplicate location, and miss the "master"/"inserted"
1092 location. Say, given locations L1, L2 and L3 at addresses A and
1093 B:
1094
1095 L1@A, L2@A, L3@B, ...
1096
1097 BC_L could end up pointing at location L2, while the "master"
1098 location could be L1. Since the `loc->inserted' flag is only set
1099 on "master" locations, we'd forget to restore the shadow of L1
1100 and L2. */
1101 while (bc_l > 0
1102 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1103 bc_l--;
1104
876fa593
JK
1105 /* Now do full processing of the found relevant range of elements. */
1106
1107 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1108 {
35df4500 1109 struct bp_location *bl = bp_location[bc];
876fa593
JK
1110 CORE_ADDR bp_addr = 0;
1111 int bp_size = 0;
1112 int bptoffset = 0;
1113
35df4500
TJB
1114 /* bp_location array has BL->OWNER always non-NULL. */
1115 if (bl->owner->type == bp_none)
8a3fe4f8 1116 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1117 bl->owner->number);
ffce0d52 1118
e5dd4106 1119 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1120 content. */
1121
35df4500
TJB
1122 if (bl->address >= bp_location_placed_address_before_address_max
1123 && memaddr + len <= (bl->address
1124 - bp_location_placed_address_before_address_max))
876fa593
JK
1125 break;
1126
35df4500 1127 if (!bp_location_has_shadow (bl))
c5aa993b 1128 continue;
35df4500 1129 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1130 current_program_space->aspace, 0))
1131 continue;
1132
c5aa993b
JM
1133 /* Addresses and length of the part of the breakpoint that
1134 we need to copy. */
35df4500
TJB
1135 bp_addr = bl->target_info.placed_address;
1136 bp_size = bl->target_info.shadow_len;
8defab1a 1137
c5aa993b
JM
1138 if (bp_addr + bp_size <= memaddr)
1139 /* The breakpoint is entirely before the chunk of memory we
1140 are reading. */
1141 continue;
8defab1a 1142
c5aa993b
JM
1143 if (bp_addr >= memaddr + len)
1144 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1145 reading. */
c5aa993b 1146 continue;
c5aa993b 1147
8defab1a
DJ
1148 /* Offset within shadow_contents. */
1149 if (bp_addr < memaddr)
1150 {
1151 /* Only copy the second part of the breakpoint. */
1152 bp_size -= memaddr - bp_addr;
1153 bptoffset = memaddr - bp_addr;
1154 bp_addr = memaddr;
1155 }
c5aa993b 1156
8defab1a
DJ
1157 if (bp_addr + bp_size > memaddr + len)
1158 {
1159 /* Only copy the first part of the breakpoint. */
1160 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1161 }
c5aa993b 1162
f0ba3972
PA
1163 if (readbuf != NULL)
1164 {
1165 /* Update the read buffer with this inserted breakpoint's
1166 shadow. */
1167 memcpy (readbuf + bp_addr - memaddr,
1168 bl->target_info.shadow_contents + bptoffset, bp_size);
1169 }
1170 else
1171 {
1172 struct gdbarch *gdbarch = bl->gdbarch;
1173 const unsigned char *bp;
1174 CORE_ADDR placed_address = bl->target_info.placed_address;
1175 unsigned placed_size = bl->target_info.placed_size;
1176
1177 /* Update the shadow with what we want to write to memory. */
1178 memcpy (bl->target_info.shadow_contents + bptoffset,
1179 writebuf_org + bp_addr - memaddr, bp_size);
1180
1181 /* Determine appropriate breakpoint contents and size for this
1182 address. */
1183 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1184
1185 /* Update the final write buffer with this inserted
1186 breakpoint's INSN. */
1187 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1188 }
c5aa993b 1189 }
c906108c 1190}
c906108c 1191\f
c5aa993b 1192
60e1c644
PA
1193/* Return true if BPT is of any hardware watchpoint kind. */
1194
a5606eee 1195static int
d77f58be 1196is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1197{
1198 return (bpt->type == bp_hardware_watchpoint
1199 || bpt->type == bp_read_watchpoint
1200 || bpt->type == bp_access_watchpoint);
1201}
7270d8f2 1202
60e1c644
PA
1203/* Return true if BPT is of any watchpoint kind, hardware or
1204 software. */
1205
3a5c3e22 1206int
d77f58be 1207is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1208{
1209 return (is_hardware_watchpoint (bpt)
1210 || bpt->type == bp_watchpoint);
1211}
1212
3a5c3e22
PA
1213/* Returns true if the current thread and its running state are safe
1214 to evaluate or update watchpoint B. Watchpoints on local
1215 expressions need to be evaluated in the context of the thread that
1216 was current when the watchpoint was created, and, that thread needs
1217 to be stopped to be able to select the correct frame context.
1218 Watchpoints on global expressions can be evaluated on any thread,
1219 and in any state. It is presently left to the target allowing
1220 memory accesses when threads are running. */
f6bc2008
PA
1221
1222static int
3a5c3e22 1223watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008
PA
1224{
1225 return (ptid_equal (b->watchpoint_thread, null_ptid)
1226 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1227 && !is_executing (inferior_ptid)));
1228}
1229
d0fb5eae
JK
1230/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1231 associated bp_watchpoint_scope breakpoint. */
1232
1233static void
3a5c3e22 1234watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1235{
3a5c3e22 1236 struct breakpoint *b = &w->base;
d0fb5eae
JK
1237
1238 if (b->related_breakpoint != b)
1239 {
1240 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1241 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1242 b->related_breakpoint->disposition = disp_del_at_next_stop;
1243 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1244 b->related_breakpoint = b;
1245 }
1246 b->disposition = disp_del_at_next_stop;
1247}
1248
567e1b4e
JB
1249/* Assuming that B is a watchpoint:
1250 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1251 - Evaluate expression and store the result in B->val
567e1b4e
JB
1252 - Evaluate the condition if there is one, and store the result
1253 in b->loc->cond.
a5606eee
VP
1254 - Update the list of values that must be watched in B->loc.
1255
4a64f543
MS
1256 If the watchpoint disposition is disp_del_at_next_stop, then do
1257 nothing. If this is local watchpoint that is out of scope, delete
1258 it.
1259
1260 Even with `set breakpoint always-inserted on' the watchpoints are
1261 removed + inserted on each stop here. Normal breakpoints must
1262 never be removed because they might be missed by a running thread
1263 when debugging in non-stop mode. On the other hand, hardware
1264 watchpoints (is_hardware_watchpoint; processed here) are specific
1265 to each LWP since they are stored in each LWP's hardware debug
1266 registers. Therefore, such LWP must be stopped first in order to
1267 be able to modify its hardware watchpoints.
1268
1269 Hardware watchpoints must be reset exactly once after being
1270 presented to the user. It cannot be done sooner, because it would
1271 reset the data used to present the watchpoint hit to the user. And
1272 it must not be done later because it could display the same single
1273 watchpoint hit during multiple GDB stops. Note that the latter is
1274 relevant only to the hardware watchpoint types bp_read_watchpoint
1275 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1276 not user-visible - its hit is suppressed if the memory content has
1277 not changed.
1278
1279 The following constraints influence the location where we can reset
1280 hardware watchpoints:
1281
1282 * target_stopped_by_watchpoint and target_stopped_data_address are
1283 called several times when GDB stops.
1284
1285 [linux]
1286 * Multiple hardware watchpoints can be hit at the same time,
1287 causing GDB to stop. GDB only presents one hardware watchpoint
1288 hit at a time as the reason for stopping, and all the other hits
1289 are presented later, one after the other, each time the user
1290 requests the execution to be resumed. Execution is not resumed
1291 for the threads still having pending hit event stored in
1292 LWP_INFO->STATUS. While the watchpoint is already removed from
1293 the inferior on the first stop the thread hit event is kept being
1294 reported from its cached value by linux_nat_stopped_data_address
1295 until the real thread resume happens after the watchpoint gets
1296 presented and thus its LWP_INFO->STATUS gets reset.
1297
1298 Therefore the hardware watchpoint hit can get safely reset on the
1299 watchpoint removal from inferior. */
a79d3c27 1300
b40ce68a 1301static void
3a5c3e22 1302update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1303{
a5606eee 1304 int within_current_scope;
a5606eee 1305 struct frame_id saved_frame_id;
66076460 1306 int frame_saved;
a5606eee 1307
f6bc2008
PA
1308 /* If this is a local watchpoint, we only want to check if the
1309 watchpoint frame is in scope if the current thread is the thread
1310 that was used to create the watchpoint. */
1311 if (!watchpoint_in_thread_scope (b))
1312 return;
1313
3a5c3e22 1314 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1315 return;
1316
66076460 1317 frame_saved = 0;
a5606eee
VP
1318
1319 /* Determine if the watchpoint is within scope. */
1320 if (b->exp_valid_block == NULL)
1321 within_current_scope = 1;
1322 else
1323 {
b5db5dfc
UW
1324 struct frame_info *fi = get_current_frame ();
1325 struct gdbarch *frame_arch = get_frame_arch (fi);
1326 CORE_ADDR frame_pc = get_frame_pc (fi);
1327
1328 /* If we're in a function epilogue, unwinding may not work
1329 properly, so do not attempt to recreate locations at this
1330 point. See similar comments in watchpoint_check. */
1331 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1332 return;
66076460
DJ
1333
1334 /* Save the current frame's ID so we can restore it after
1335 evaluating the watchpoint expression on its own frame. */
1336 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1337 took a frame parameter, so that we didn't have to change the
1338 selected frame. */
1339 frame_saved = 1;
1340 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1341
a5606eee
VP
1342 fi = frame_find_by_id (b->watchpoint_frame);
1343 within_current_scope = (fi != NULL);
1344 if (within_current_scope)
1345 select_frame (fi);
1346 }
1347
b5db5dfc
UW
1348 /* We don't free locations. They are stored in the bp_location array
1349 and update_global_location_list will eventually delete them and
1350 remove breakpoints if needed. */
3a5c3e22 1351 b->base.loc = NULL;
b5db5dfc 1352
a5606eee
VP
1353 if (within_current_scope && reparse)
1354 {
1355 char *s;
d63d0675 1356
a5606eee
VP
1357 if (b->exp)
1358 {
1359 xfree (b->exp);
1360 b->exp = NULL;
1361 }
d63d0675 1362 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1363 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1364 /* If the meaning of expression itself changed, the old value is
1365 no longer relevant. We don't want to report a watchpoint hit
1366 to the user when the old value and the new value may actually
1367 be completely different objects. */
1368 value_free (b->val);
fa4727a6
DJ
1369 b->val = NULL;
1370 b->val_valid = 0;
60e1c644
PA
1371
1372 /* Note that unlike with breakpoints, the watchpoint's condition
1373 expression is stored in the breakpoint object, not in the
1374 locations (re)created below. */
3a5c3e22 1375 if (b->base.cond_string != NULL)
60e1c644
PA
1376 {
1377 if (b->cond_exp != NULL)
1378 {
1379 xfree (b->cond_exp);
1380 b->cond_exp = NULL;
1381 }
1382
3a5c3e22 1383 s = b->base.cond_string;
60e1c644
PA
1384 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1385 }
a5606eee 1386 }
a5606eee
VP
1387
1388 /* If we failed to parse the expression, for example because
1389 it refers to a global variable in a not-yet-loaded shared library,
1390 don't try to insert watchpoint. We don't automatically delete
1391 such watchpoint, though, since failure to parse expression
1392 is different from out-of-scope watchpoint. */
2d134ed3
PA
1393 if ( !target_has_execution)
1394 {
1395 /* Without execution, memory can't change. No use to try and
1396 set watchpoint locations. The watchpoint will be reset when
1397 the target gains execution, through breakpoint_re_set. */
1398 }
1399 else if (within_current_scope && b->exp)
a5606eee 1400 {
0cf6dd15 1401 int pc = 0;
fa4727a6 1402 struct value *val_chain, *v, *result, *next;
2d134ed3 1403 struct program_space *frame_pspace;
a5606eee 1404
0cf6dd15 1405 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1406
a5606eee
VP
1407 /* Avoid setting b->val if it's already set. The meaning of
1408 b->val is 'the last value' user saw, and we should update
1409 it only if we reported that last value to user. As it
9c06b0b4
TJB
1410 happens, the code that reports it updates b->val directly.
1411 We don't keep track of the memory value for masked
1412 watchpoints. */
3a5c3e22 1413 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1414 {
1415 b->val = v;
1416 b->val_valid = 1;
1417 }
a5606eee 1418
2d134ed3
PA
1419 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1420
a5606eee 1421 /* Look at each value on the value chain. */
9fa40276 1422 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1423 {
1424 /* If it's a memory location, and GDB actually needed
1425 its contents to evaluate the expression, then we
fa4727a6
DJ
1426 must watch it. If the first value returned is
1427 still lazy, that means an error occurred reading it;
1428 watch it anyway in case it becomes readable. */
a5606eee 1429 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1430 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1431 {
1432 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1433
a5606eee
VP
1434 /* We only watch structs and arrays if user asked
1435 for it explicitly, never if they just happen to
1436 appear in the middle of some value chain. */
fa4727a6 1437 if (v == result
a5606eee
VP
1438 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1439 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1440 {
1441 CORE_ADDR addr;
1442 int len, type;
1443 struct bp_location *loc, **tmp;
1444
42ae5230 1445 addr = value_address (v);
a5606eee
VP
1446 len = TYPE_LENGTH (value_type (v));
1447 type = hw_write;
3a5c3e22 1448 if (b->base.type == bp_read_watchpoint)
a5606eee 1449 type = hw_read;
3a5c3e22 1450 else if (b->base.type == bp_access_watchpoint)
a5606eee 1451 type = hw_access;
3a5c3e22
PA
1452
1453 loc = allocate_bp_location (&b->base);
1454 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1455 ;
1456 *tmp = loc;
a6d9a66e 1457 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1458
1459 loc->pspace = frame_pspace;
a5606eee
VP
1460 loc->address = addr;
1461 loc->length = len;
1462 loc->watchpoint_type = type;
1463 }
1464 }
9fa40276
TJB
1465 }
1466
1467 /* Change the type of breakpoint between hardware assisted or
1468 an ordinary watchpoint depending on the hardware support
1469 and free hardware slots. REPARSE is set when the inferior
1470 is started. */
a9634178 1471 if (reparse)
9fa40276 1472 {
e09342b5 1473 int reg_cnt;
9fa40276
TJB
1474 enum bp_loc_type loc_type;
1475 struct bp_location *bl;
a5606eee 1476
a9634178 1477 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1478
1479 if (reg_cnt)
9fa40276
TJB
1480 {
1481 int i, target_resources_ok, other_type_used;
a1398e0c 1482 enum bptype type;
9fa40276 1483
a9634178
TJB
1484 /* Use an exact watchpoint when there's only one memory region to be
1485 watched, and only one debug register is needed to watch it. */
1486 b->exact = target_exact_watchpoints && reg_cnt == 1;
1487
9fa40276 1488 /* We need to determine how many resources are already
e09342b5
TJB
1489 used for all other hardware watchpoints plus this one
1490 to see if we still have enough resources to also fit
a1398e0c
PA
1491 this watchpoint in as well. */
1492
1493 /* If this is a software watchpoint, we try to turn it
1494 to a hardware one -- count resources as if B was of
1495 hardware watchpoint type. */
1496 type = b->base.type;
1497 if (type == bp_watchpoint)
1498 type = bp_hardware_watchpoint;
1499
1500 /* This watchpoint may or may not have been placed on
1501 the list yet at this point (it won't be in the list
1502 if we're trying to create it for the first time,
1503 through watch_command), so always account for it
1504 manually. */
1505
1506 /* Count resources used by all watchpoints except B. */
1507 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1508
1509 /* Add in the resources needed for B. */
1510 i += hw_watchpoint_use_count (&b->base);
1511
1512 target_resources_ok
1513 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1514 if (target_resources_ok <= 0)
a9634178 1515 {
3a5c3e22 1516 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1517
1518 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1519 error (_("Target does not support this type of "
1520 "hardware watchpoint."));
9c06b0b4
TJB
1521 else if (target_resources_ok < 0 && !sw_mode)
1522 error (_("There are not enough available hardware "
1523 "resources for this watchpoint."));
a1398e0c
PA
1524
1525 /* Downgrade to software watchpoint. */
1526 b->base.type = bp_watchpoint;
1527 }
1528 else
1529 {
1530 /* If this was a software watchpoint, we've just
1531 found we have enough resources to turn it to a
1532 hardware watchpoint. Otherwise, this is a
1533 nop. */
1534 b->base.type = type;
a9634178 1535 }
9fa40276 1536 }
3a5c3e22 1537 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1538 error (_("Expression cannot be implemented with "
1539 "read/access watchpoint."));
9fa40276 1540 else
3a5c3e22 1541 b->base.type = bp_watchpoint;
9fa40276 1542
3a5c3e22 1543 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1544 : bp_loc_hardware_watchpoint);
3a5c3e22 1545 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1546 bl->loc_type = loc_type;
1547 }
1548
1549 for (v = val_chain; v; v = next)
1550 {
a5606eee
VP
1551 next = value_next (v);
1552 if (v != b->val)
1553 value_free (v);
1554 }
1555
c7437ca6
PA
1556 /* If a software watchpoint is not watching any memory, then the
1557 above left it without any location set up. But,
1558 bpstat_stop_status requires a location to be able to report
1559 stops, so make sure there's at least a dummy one. */
3a5c3e22 1560 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1561 {
3a5c3e22
PA
1562 struct breakpoint *base = &b->base;
1563 base->loc = allocate_bp_location (base);
1564 base->loc->pspace = frame_pspace;
1565 base->loc->address = -1;
1566 base->loc->length = -1;
1567 base->loc->watchpoint_type = -1;
c7437ca6 1568 }
a5606eee
VP
1569 }
1570 else if (!within_current_scope)
7270d8f2 1571 {
ac74f770
MS
1572 printf_filtered (_("\
1573Watchpoint %d deleted because the program has left the block\n\
1574in which its expression is valid.\n"),
3a5c3e22 1575 b->base.number);
d0fb5eae 1576 watchpoint_del_at_next_stop (b);
7270d8f2 1577 }
a5606eee
VP
1578
1579 /* Restore the selected frame. */
66076460
DJ
1580 if (frame_saved)
1581 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1582}
1583
a5606eee 1584
74960c60 1585/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1586 inserted in the inferior. We don't differentiate the type of BL's owner
1587 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1588 breakpoint_ops is not defined, because in insert_bp_location,
1589 tracepoint's insert_location will not be called. */
74960c60 1590static int
35df4500 1591should_be_inserted (struct bp_location *bl)
74960c60 1592{
35df4500 1593 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1594 return 0;
1595
35df4500 1596 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1597 return 0;
1598
35df4500 1599 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1600 return 0;
1601
f8eba3c6
TT
1602 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1603 return 0;
1604
56710373
PA
1605 /* This is set for example, when we're attached to the parent of a
1606 vfork, and have detached from the child. The child is running
1607 free, and we expect it to do an exec or exit, at which point the
1608 OS makes the parent schedulable again (and the target reports
1609 that the vfork is done). Until the child is done with the shared
1610 memory region, do not insert breakpoints in the parent, otherwise
1611 the child could still trip on the parent's breakpoints. Since
1612 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1613 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1614 return 0;
1615
74960c60
VP
1616 return 1;
1617}
1618
934709f0
PW
1619/* Same as should_be_inserted but does the check assuming
1620 that the location is not duplicated. */
1621
1622static int
1623unduplicated_should_be_inserted (struct bp_location *bl)
1624{
1625 int result;
1626 const int save_duplicate = bl->duplicate;
1627
1628 bl->duplicate = 0;
1629 result = should_be_inserted (bl);
1630 bl->duplicate = save_duplicate;
1631 return result;
1632}
1633
35df4500
TJB
1634/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1635 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 1636 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
1637 Returns 0 for success, 1 if the bp_location type is not supported or
1638 -1 for failure.
879bfdc2 1639
4a64f543
MS
1640 NOTE drow/2003-09-09: This routine could be broken down to an
1641 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1642static int
35df4500 1643insert_bp_location (struct bp_location *bl,
26bb91f3 1644 struct ui_file *tmp_error_stream,
3fbb6ffa 1645 int *disabled_breaks,
26bb91f3 1646 int *hw_breakpoint_error)
879bfdc2
DJ
1647{
1648 int val = 0;
1649
35df4500 1650 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1651 return 0;
1652
8181d85f 1653 /* Initialize the target-specific information. */
35df4500
TJB
1654 memset (&bl->target_info, 0, sizeof (bl->target_info));
1655 bl->target_info.placed_address = bl->address;
1656 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1657 bl->target_info.length = bl->length;
8181d85f 1658
35df4500
TJB
1659 if (bl->loc_type == bp_loc_software_breakpoint
1660 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1661 {
35df4500 1662 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1663 {
1664 /* If the explicitly specified breakpoint type
1665 is not hardware breakpoint, check the memory map to see
1666 if the breakpoint address is in read only memory or not.
4a64f543 1667
765dc015
VP
1668 Two important cases are:
1669 - location type is not hardware breakpoint, memory
1670 is readonly. We change the type of the location to
1671 hardware breakpoint.
4a64f543
MS
1672 - location type is hardware breakpoint, memory is
1673 read-write. This means we've previously made the
1674 location hardware one, but then the memory map changed,
1675 so we undo.
765dc015 1676
4a64f543
MS
1677 When breakpoints are removed, remove_breakpoints will use
1678 location types we've just set here, the only possible
1679 problem is that memory map has changed during running
1680 program, but it's not going to work anyway with current
1681 gdb. */
765dc015 1682 struct mem_region *mr
35df4500 1683 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1684
1685 if (mr)
1686 {
1687 if (automatic_hardware_breakpoints)
1688 {
765dc015
VP
1689 enum bp_loc_type new_type;
1690
1691 if (mr->attrib.mode != MEM_RW)
1692 new_type = bp_loc_hardware_breakpoint;
1693 else
1694 new_type = bp_loc_software_breakpoint;
1695
35df4500 1696 if (new_type != bl->loc_type)
765dc015
VP
1697 {
1698 static int said = 0;
cc59ec59 1699
35df4500 1700 bl->loc_type = new_type;
765dc015
VP
1701 if (!said)
1702 {
3e43a32a
MS
1703 fprintf_filtered (gdb_stdout,
1704 _("Note: automatically using "
1705 "hardware breakpoints for "
1706 "read-only addresses.\n"));
765dc015
VP
1707 said = 1;
1708 }
1709 }
1710 }
35df4500 1711 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1712 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1713 warning (_("cannot set software breakpoint "
1714 "at readonly address %s"),
35df4500 1715 paddress (bl->gdbarch, bl->address));
765dc015
VP
1716 }
1717 }
1718
879bfdc2
DJ
1719 /* First check to see if we have to handle an overlay. */
1720 if (overlay_debugging == ovly_off
35df4500
TJB
1721 || bl->section == NULL
1722 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1723 {
1724 /* No overlay handling: just set the breakpoint. */
1725
348d480f 1726 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1727 }
1728 else
1729 {
4a64f543 1730 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1731 Shall we set a breakpoint at the LMA? */
1732 if (!overlay_events_enabled)
1733 {
1734 /* Yes -- overlay event support is not active,
1735 so we must try to set a breakpoint at the LMA.
1736 This will not work for a hardware breakpoint. */
35df4500 1737 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1738 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1739 bl->owner->number);
879bfdc2
DJ
1740 else
1741 {
35df4500
TJB
1742 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1743 bl->section);
879bfdc2 1744 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1745 bl->overlay_target_info = bl->target_info;
1746 bl->overlay_target_info.placed_address = addr;
1747 val = target_insert_breakpoint (bl->gdbarch,
1748 &bl->overlay_target_info);
879bfdc2 1749 if (val != 0)
99361f52 1750 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1751 "Overlay breakpoint %d "
1752 "failed: in ROM?\n",
35df4500 1753 bl->owner->number);
879bfdc2
DJ
1754 }
1755 }
1756 /* Shall we set a breakpoint at the VMA? */
35df4500 1757 if (section_is_mapped (bl->section))
879bfdc2
DJ
1758 {
1759 /* Yes. This overlay section is mapped into memory. */
348d480f 1760 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1761 }
1762 else
1763 {
1764 /* No. This breakpoint will not be inserted.
1765 No error, but do not mark the bp as 'inserted'. */
1766 return 0;
1767 }
1768 }
1769
1770 if (val)
1771 {
1772 /* Can't set the breakpoint. */
35df4500 1773 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1774 {
4a64f543 1775 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1776 val = 0;
35df4500 1777 bl->shlib_disabled = 1;
8d3788bd 1778 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
1779 if (!*disabled_breaks)
1780 {
1781 fprintf_unfiltered (tmp_error_stream,
1782 "Cannot insert breakpoint %d.\n",
1783 bl->owner->number);
1784 fprintf_unfiltered (tmp_error_stream,
1785 "Temporarily disabling shared "
1786 "library breakpoints:\n");
1787 }
1788 *disabled_breaks = 1;
879bfdc2 1789 fprintf_unfiltered (tmp_error_stream,
35df4500 1790 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1791 }
1792 else
879bfdc2 1793 {
35df4500 1794 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1795 {
1796 *hw_breakpoint_error = 1;
3e43a32a
MS
1797 fprintf_unfiltered (tmp_error_stream,
1798 "Cannot insert hardware "
1799 "breakpoint %d.\n",
35df4500 1800 bl->owner->number);
879bfdc2
DJ
1801 }
1802 else
1803 {
1804 fprintf_unfiltered (tmp_error_stream,
1805 "Cannot insert breakpoint %d.\n",
35df4500 1806 bl->owner->number);
879bfdc2
DJ
1807 fprintf_filtered (tmp_error_stream,
1808 "Error accessing memory address ");
35df4500 1809 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1810 tmp_error_stream);
879bfdc2
DJ
1811 fprintf_filtered (tmp_error_stream, ": %s.\n",
1812 safe_strerror (val));
1813 }
1814
1815 }
1816 }
1817 else
35df4500 1818 bl->inserted = 1;
879bfdc2
DJ
1819
1820 return val;
1821 }
1822
35df4500 1823 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1824 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1825 watchpoints. It's not clear that it's necessary... */
35df4500 1826 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1827 {
77b06cd7
TJB
1828 gdb_assert (bl->owner->ops != NULL
1829 && bl->owner->ops->insert_location != NULL);
1830
1831 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1832
1833 /* If trying to set a read-watchpoint, and it turns out it's not
1834 supported, try emulating one with an access watchpoint. */
35df4500 1835 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1836 {
1837 struct bp_location *loc, **loc_temp;
1838
1839 /* But don't try to insert it, if there's already another
1840 hw_access location that would be considered a duplicate
1841 of this one. */
1842 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1843 if (loc != bl
85d721b8 1844 && loc->watchpoint_type == hw_access
35df4500 1845 && watchpoint_locations_match (bl, loc))
85d721b8 1846 {
35df4500
TJB
1847 bl->duplicate = 1;
1848 bl->inserted = 1;
1849 bl->target_info = loc->target_info;
1850 bl->watchpoint_type = hw_access;
85d721b8
PA
1851 val = 0;
1852 break;
1853 }
1854
1855 if (val == 1)
1856 {
77b06cd7
TJB
1857 bl->watchpoint_type = hw_access;
1858 val = bl->owner->ops->insert_location (bl);
1859
1860 if (val)
1861 /* Back to the original value. */
1862 bl->watchpoint_type = hw_read;
85d721b8
PA
1863 }
1864 }
1865
35df4500 1866 bl->inserted = (val == 0);
879bfdc2
DJ
1867 }
1868
35df4500 1869 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1870 {
77b06cd7
TJB
1871 gdb_assert (bl->owner->ops != NULL
1872 && bl->owner->ops->insert_location != NULL);
1873
1874 val = bl->owner->ops->insert_location (bl);
1875 if (val)
1876 {
1877 bl->owner->enable_state = bp_disabled;
1878
1879 if (val == 1)
1880 warning (_("\
1881Error inserting catchpoint %d: Your system does not support this type\n\
1882of catchpoint."), bl->owner->number);
1883 else
1884 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1885 }
1886
1887 bl->inserted = (val == 0);
1640b821
DJ
1888
1889 /* We've already printed an error message if there was a problem
1890 inserting this catchpoint, and we've disabled the catchpoint,
1891 so just return success. */
1892 return 0;
879bfdc2
DJ
1893 }
1894
1895 return 0;
1896}
1897
6c95b8df
PA
1898/* This function is called when program space PSPACE is about to be
1899 deleted. It takes care of updating breakpoints to not reference
1900 PSPACE anymore. */
1901
1902void
1903breakpoint_program_space_exit (struct program_space *pspace)
1904{
1905 struct breakpoint *b, *b_temp;
876fa593 1906 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1907
1908 /* Remove any breakpoint that was set through this program space. */
1909 ALL_BREAKPOINTS_SAFE (b, b_temp)
1910 {
1911 if (b->pspace == pspace)
1912 delete_breakpoint (b);
1913 }
1914
1915 /* Breakpoints set through other program spaces could have locations
1916 bound to PSPACE as well. Remove those. */
876fa593 1917 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1918 {
1919 struct bp_location *tmp;
1920
1921 if (loc->pspace == pspace)
1922 {
2bdf28a0 1923 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1924 if (loc->owner->loc == loc)
1925 loc->owner->loc = loc->next;
1926 else
1927 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1928 if (tmp->next == loc)
1929 {
1930 tmp->next = loc->next;
1931 break;
1932 }
1933 }
1934 }
1935
1936 /* Now update the global location list to permanently delete the
1937 removed locations above. */
1938 update_global_location_list (0);
1939}
1940
74960c60
VP
1941/* Make sure all breakpoints are inserted in inferior.
1942 Throws exception on any error.
1943 A breakpoint that is already inserted won't be inserted
1944 again, so calling this function twice is safe. */
1945void
1946insert_breakpoints (void)
1947{
1948 struct breakpoint *bpt;
1949
1950 ALL_BREAKPOINTS (bpt)
1951 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
1952 {
1953 struct watchpoint *w = (struct watchpoint *) bpt;
1954
1955 update_watchpoint (w, 0 /* don't reparse. */);
1956 }
74960c60 1957
b60e7edf 1958 update_global_location_list (1);
74960c60 1959
c35b1492
PA
1960 /* update_global_location_list does not insert breakpoints when
1961 always_inserted_mode is not enabled. Explicitly insert them
1962 now. */
1963 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1964 insert_breakpoint_locations ();
1965}
1966
c30eee59 1967/* Used when starting or continuing the program. */
c906108c 1968
74960c60
VP
1969static void
1970insert_breakpoint_locations (void)
c906108c 1971{
a5606eee 1972 struct breakpoint *bpt;
35df4500 1973 struct bp_location *bl, **blp_tmp;
eacd795a 1974 int error_flag = 0;
c906108c 1975 int val = 0;
3fbb6ffa 1976 int disabled_breaks = 0;
81d0cc19 1977 int hw_breakpoint_error = 0;
c906108c 1978
81d0cc19 1979 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1980 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1981
81d0cc19
GS
1982 /* Explicitly mark the warning -- this will only be printed if
1983 there was an error. */
1984 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1985
1986 save_current_space_and_thread ();
1987
35df4500 1988 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1989 {
35df4500 1990 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1991 continue;
1992
4a64f543
MS
1993 /* There is no point inserting thread-specific breakpoints if
1994 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1995 has BL->OWNER always non-NULL. */
35df4500
TJB
1996 if (bl->owner->thread != -1
1997 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1998 continue;
1999
35df4500 2000 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2001
2002 /* For targets that support global breakpoints, there's no need
2003 to select an inferior to insert breakpoint to. In fact, even
2004 if we aren't attached to any process yet, we should still
2005 insert breakpoints. */
2006 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2007 && ptid_equal (inferior_ptid, null_ptid))
2008 continue;
2009
3fbb6ffa
TJB
2010 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2011 &hw_breakpoint_error);
879bfdc2 2012 if (val)
eacd795a 2013 error_flag = val;
879bfdc2 2014 }
c906108c 2015
4a64f543
MS
2016 /* If we failed to insert all locations of a watchpoint, remove
2017 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2018 ALL_BREAKPOINTS (bpt)
2019 {
2020 int some_failed = 0;
2021 struct bp_location *loc;
2022
2023 if (!is_hardware_watchpoint (bpt))
2024 continue;
2025
d6b74ac4 2026 if (!breakpoint_enabled (bpt))
a5606eee 2027 continue;
74960c60
VP
2028
2029 if (bpt->disposition == disp_del_at_next_stop)
2030 continue;
a5606eee
VP
2031
2032 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2033 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2034 {
2035 some_failed = 1;
2036 break;
2037 }
2038 if (some_failed)
2039 {
2040 for (loc = bpt->loc; loc; loc = loc->next)
2041 if (loc->inserted)
2042 remove_breakpoint (loc, mark_uninserted);
2043
2044 hw_breakpoint_error = 1;
2045 fprintf_unfiltered (tmp_error_stream,
2046 "Could not insert hardware watchpoint %d.\n",
2047 bpt->number);
eacd795a 2048 error_flag = -1;
a5606eee
VP
2049 }
2050 }
2051
eacd795a 2052 if (error_flag)
81d0cc19
GS
2053 {
2054 /* If a hardware breakpoint or watchpoint was inserted, add a
2055 message about possibly exhausted resources. */
879bfdc2 2056 if (hw_breakpoint_error)
81d0cc19 2057 {
c6510018
MS
2058 fprintf_unfiltered (tmp_error_stream,
2059 "Could not insert hardware breakpoints:\n\
2060You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2061 }
81d0cc19
GS
2062 target_terminal_ours_for_output ();
2063 error_stream (tmp_error_stream);
2064 }
f7545552
TT
2065
2066 do_cleanups (cleanups);
c906108c
SS
2067}
2068
c30eee59
TJB
2069/* Used when the program stops.
2070 Returns zero if successful, or non-zero if there was a problem
2071 removing a breakpoint location. */
2072
c906108c 2073int
fba45db2 2074remove_breakpoints (void)
c906108c 2075{
35df4500 2076 struct bp_location *bl, **blp_tmp;
3a1bae8e 2077 int val = 0;
c906108c 2078
35df4500 2079 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2080 {
1e4d1764 2081 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2082 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2083 }
3a1bae8e 2084 return val;
c906108c
SS
2085}
2086
6c95b8df
PA
2087/* Remove breakpoints of process PID. */
2088
2089int
2090remove_breakpoints_pid (int pid)
2091{
35df4500 2092 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2093 int val;
2094 struct inferior *inf = find_inferior_pid (pid);
2095
35df4500 2096 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2097 {
35df4500 2098 if (bl->pspace != inf->pspace)
6c95b8df
PA
2099 continue;
2100
35df4500 2101 if (bl->inserted)
6c95b8df 2102 {
35df4500 2103 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2104 if (val != 0)
2105 return val;
2106 }
2107 }
2108 return 0;
2109}
2110
c906108c 2111int
fba45db2 2112reattach_breakpoints (int pid)
c906108c 2113{
6c95b8df 2114 struct cleanup *old_chain;
35df4500 2115 struct bp_location *bl, **blp_tmp;
c906108c 2116 int val;
86b887df 2117 struct ui_file *tmp_error_stream;
3fbb6ffa 2118 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2119 struct inferior *inf;
2120 struct thread_info *tp;
2121
2122 tp = any_live_thread_of_process (pid);
2123 if (tp == NULL)
2124 return 1;
2125
2126 inf = find_inferior_pid (pid);
2127 old_chain = save_inferior_ptid ();
2128
2129 inferior_ptid = tp->ptid;
a4954f26 2130
86b887df 2131 tmp_error_stream = mem_fileopen ();
a4954f26 2132 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2133
35df4500 2134 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2135 {
35df4500 2136 if (bl->pspace != inf->pspace)
6c95b8df
PA
2137 continue;
2138
35df4500 2139 if (bl->inserted)
c5aa993b 2140 {
35df4500 2141 bl->inserted = 0;
3fbb6ffa 2142 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2143 if (val != 0)
2144 {
ce696e05 2145 do_cleanups (old_chain);
c5aa993b
JM
2146 return val;
2147 }
2148 }
2149 }
ce696e05 2150 do_cleanups (old_chain);
c906108c
SS
2151 return 0;
2152}
2153
e58b0e63
PA
2154static int internal_breakpoint_number = -1;
2155
84f4c1fe
PM
2156/* Set the breakpoint number of B, depending on the value of INTERNAL.
2157 If INTERNAL is non-zero, the breakpoint number will be populated
2158 from internal_breakpoint_number and that variable decremented.
e5dd4106 2159 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2160 breakpoint_count and that value incremented. Internal breakpoints
2161 do not set the internal var bpnum. */
2162static void
2163set_breakpoint_number (int internal, struct breakpoint *b)
2164{
2165 if (internal)
2166 b->number = internal_breakpoint_number--;
2167 else
2168 {
2169 set_breakpoint_count (breakpoint_count + 1);
2170 b->number = breakpoint_count;
2171 }
2172}
2173
e62c965a 2174static struct breakpoint *
a6d9a66e 2175create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2176 CORE_ADDR address, enum bptype type,
c0a91b2b 2177 const struct breakpoint_ops *ops)
e62c965a 2178{
e62c965a
PP
2179 struct symtab_and_line sal;
2180 struct breakpoint *b;
2181
4a64f543 2182 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2183
2184 sal.pc = address;
2185 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2186 sal.pspace = current_program_space;
e62c965a 2187
06edf0c0 2188 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2189 b->number = internal_breakpoint_number--;
2190 b->disposition = disp_donttouch;
2191
2192 return b;
2193}
2194
17450429
PP
2195static const char *const longjmp_names[] =
2196 {
2197 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2198 };
2199#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2200
2201/* Per-objfile data private to breakpoint.c. */
2202struct breakpoint_objfile_data
2203{
2204 /* Minimal symbol for "_ovly_debug_event" (if any). */
2205 struct minimal_symbol *overlay_msym;
2206
2207 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2208 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2209
2210 /* Minimal symbol for "std::terminate()" (if any). */
2211 struct minimal_symbol *terminate_msym;
2212
2213 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2214 struct minimal_symbol *exception_msym;
2215};
2216
2217static const struct objfile_data *breakpoint_objfile_key;
2218
2219/* Minimal symbol not found sentinel. */
2220static struct minimal_symbol msym_not_found;
2221
2222/* Returns TRUE if MSYM point to the "not found" sentinel. */
2223
2224static int
2225msym_not_found_p (const struct minimal_symbol *msym)
2226{
2227 return msym == &msym_not_found;
2228}
2229
2230/* Return per-objfile data needed by breakpoint.c.
2231 Allocate the data if necessary. */
2232
2233static struct breakpoint_objfile_data *
2234get_breakpoint_objfile_data (struct objfile *objfile)
2235{
2236 struct breakpoint_objfile_data *bp_objfile_data;
2237
2238 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2239 if (bp_objfile_data == NULL)
2240 {
2241 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2242 sizeof (*bp_objfile_data));
2243
2244 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2245 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2246 }
2247 return bp_objfile_data;
2248}
2249
e62c965a 2250static void
af02033e 2251create_overlay_event_breakpoint (void)
e62c965a 2252{
69de3c6a 2253 struct objfile *objfile;
af02033e 2254 const char *const func_name = "_ovly_debug_event";
e62c965a 2255
69de3c6a
PP
2256 ALL_OBJFILES (objfile)
2257 {
2258 struct breakpoint *b;
17450429
PP
2259 struct breakpoint_objfile_data *bp_objfile_data;
2260 CORE_ADDR addr;
69de3c6a 2261
17450429
PP
2262 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2263
2264 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2265 continue;
2266
2267 if (bp_objfile_data->overlay_msym == NULL)
2268 {
2269 struct minimal_symbol *m;
2270
2271 m = lookup_minimal_symbol_text (func_name, objfile);
2272 if (m == NULL)
2273 {
2274 /* Avoid future lookups in this objfile. */
2275 bp_objfile_data->overlay_msym = &msym_not_found;
2276 continue;
2277 }
2278 bp_objfile_data->overlay_msym = m;
2279 }
e62c965a 2280
17450429
PP
2281 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2282 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2283 bp_overlay_event,
2284 &internal_breakpoint_ops);
69de3c6a 2285 b->addr_string = xstrdup (func_name);
e62c965a 2286
69de3c6a
PP
2287 if (overlay_debugging == ovly_auto)
2288 {
2289 b->enable_state = bp_enabled;
2290 overlay_events_enabled = 1;
2291 }
2292 else
2293 {
2294 b->enable_state = bp_disabled;
2295 overlay_events_enabled = 0;
2296 }
e62c965a
PP
2297 }
2298 update_global_location_list (1);
2299}
2300
0fd8e87f 2301static void
af02033e 2302create_longjmp_master_breakpoint (void)
0fd8e87f 2303{
6c95b8df 2304 struct program_space *pspace;
6c95b8df
PA
2305 struct cleanup *old_chain;
2306
2307 old_chain = save_current_program_space ();
0fd8e87f 2308
6c95b8df 2309 ALL_PSPACES (pspace)
af02033e
PP
2310 {
2311 struct objfile *objfile;
2312
2313 set_current_program_space (pspace);
2314
2315 ALL_OBJFILES (objfile)
0fd8e87f 2316 {
af02033e
PP
2317 int i;
2318 struct gdbarch *gdbarch;
17450429 2319 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2320
af02033e
PP
2321 gdbarch = get_objfile_arch (objfile);
2322 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2323 continue;
2324
17450429
PP
2325 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2326
2327 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2328 {
2329 struct breakpoint *b;
af02033e 2330 const char *func_name;
17450429 2331 CORE_ADDR addr;
6c95b8df 2332
17450429 2333 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2334 continue;
0fd8e87f 2335
17450429
PP
2336 func_name = longjmp_names[i];
2337 if (bp_objfile_data->longjmp_msym[i] == NULL)
2338 {
2339 struct minimal_symbol *m;
2340
2341 m = lookup_minimal_symbol_text (func_name, objfile);
2342 if (m == NULL)
2343 {
2344 /* Prevent future lookups in this objfile. */
2345 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2346 continue;
2347 }
2348 bp_objfile_data->longjmp_msym[i] = m;
2349 }
2350
2351 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2352 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2353 &internal_breakpoint_ops);
af02033e
PP
2354 b->addr_string = xstrdup (func_name);
2355 b->enable_state = bp_disabled;
2356 }
0fd8e87f 2357 }
af02033e 2358 }
0fd8e87f 2359 update_global_location_list (1);
6c95b8df
PA
2360
2361 do_cleanups (old_chain);
0fd8e87f
UW
2362}
2363
af02033e 2364/* Create a master std::terminate breakpoint. */
aa7d318d 2365static void
af02033e 2366create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2367{
2368 struct program_space *pspace;
aa7d318d 2369 struct cleanup *old_chain;
af02033e 2370 const char *const func_name = "std::terminate()";
aa7d318d
TT
2371
2372 old_chain = save_current_program_space ();
2373
2374 ALL_PSPACES (pspace)
17450429
PP
2375 {
2376 struct objfile *objfile;
2377 CORE_ADDR addr;
2378
2379 set_current_program_space (pspace);
2380
aa7d318d
TT
2381 ALL_OBJFILES (objfile)
2382 {
2383 struct breakpoint *b;
17450429 2384 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2385
17450429 2386 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2387
17450429
PP
2388 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2389 continue;
2390
2391 if (bp_objfile_data->terminate_msym == NULL)
2392 {
2393 struct minimal_symbol *m;
2394
2395 m = lookup_minimal_symbol (func_name, NULL, objfile);
2396 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2397 && MSYMBOL_TYPE (m) != mst_file_text))
2398 {
2399 /* Prevent future lookups in this objfile. */
2400 bp_objfile_data->terminate_msym = &msym_not_found;
2401 continue;
2402 }
2403 bp_objfile_data->terminate_msym = m;
2404 }
aa7d318d 2405
17450429
PP
2406 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2407 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2408 bp_std_terminate_master,
2409 &internal_breakpoint_ops);
aa7d318d
TT
2410 b->addr_string = xstrdup (func_name);
2411 b->enable_state = bp_disabled;
2412 }
17450429
PP
2413 }
2414
aa7d318d
TT
2415 update_global_location_list (1);
2416
2417 do_cleanups (old_chain);
2418}
2419
186c406b
TT
2420/* Install a master breakpoint on the unwinder's debug hook. */
2421
2422void
2423create_exception_master_breakpoint (void)
2424{
2425 struct objfile *objfile;
17450429 2426 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2427
2428 ALL_OBJFILES (objfile)
2429 {
17450429
PP
2430 struct breakpoint *b;
2431 struct gdbarch *gdbarch;
2432 struct breakpoint_objfile_data *bp_objfile_data;
2433 CORE_ADDR addr;
2434
2435 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2436
2437 if (msym_not_found_p (bp_objfile_data->exception_msym))
2438 continue;
2439
2440 gdbarch = get_objfile_arch (objfile);
186c406b 2441
17450429 2442 if (bp_objfile_data->exception_msym == NULL)
186c406b 2443 {
17450429 2444 struct minimal_symbol *debug_hook;
186c406b 2445
17450429
PP
2446 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2447 if (debug_hook == NULL)
2448 {
2449 bp_objfile_data->exception_msym = &msym_not_found;
2450 continue;
2451 }
2452
2453 bp_objfile_data->exception_msym = debug_hook;
186c406b 2454 }
17450429
PP
2455
2456 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2457 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2458 &current_target);
06edf0c0
PA
2459 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2460 &internal_breakpoint_ops);
17450429
PP
2461 b->addr_string = xstrdup (func_name);
2462 b->enable_state = bp_disabled;
186c406b
TT
2463 }
2464
2465 update_global_location_list (1);
2466}
2467
c906108c 2468void
fba45db2 2469update_breakpoints_after_exec (void)
c906108c 2470{
35df4500 2471 struct breakpoint *b, *b_tmp;
876fa593 2472 struct bp_location *bploc, **bplocp_tmp;
c906108c 2473
25b22b0a
PA
2474 /* We're about to delete breakpoints from GDB's lists. If the
2475 INSERTED flag is true, GDB will try to lift the breakpoints by
2476 writing the breakpoints' "shadow contents" back into memory. The
2477 "shadow contents" are NOT valid after an exec, so GDB should not
2478 do that. Instead, the target is responsible from marking
2479 breakpoints out as soon as it detects an exec. We don't do that
2480 here instead, because there may be other attempts to delete
2481 breakpoints after detecting an exec and before reaching here. */
876fa593 2482 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2483 if (bploc->pspace == current_program_space)
2484 gdb_assert (!bploc->inserted);
c906108c 2485
35df4500 2486 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2487 {
6c95b8df
PA
2488 if (b->pspace != current_program_space)
2489 continue;
2490
4a64f543 2491 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2492 if (b->type == bp_shlib_event)
2493 {
2494 delete_breakpoint (b);
2495 continue;
2496 }
c906108c 2497
4a64f543 2498 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2499 if (b->type == bp_jit_event)
2500 {
2501 delete_breakpoint (b);
2502 continue;
2503 }
2504
1900040c 2505 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2506 as must overlay event and longjmp master breakpoints. */
2507 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2508 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2509 || b->type == bp_exception_master)
c4093a6a
JM
2510 {
2511 delete_breakpoint (b);
2512 continue;
2513 }
2514
4a64f543 2515 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2516 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2517 {
2518 delete_breakpoint (b);
2519 continue;
2520 }
2521
611c83ae
PA
2522 /* Longjmp and longjmp-resume breakpoints are also meaningless
2523 after an exec. */
186c406b
TT
2524 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2525 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2526 {
2527 delete_breakpoint (b);
2528 continue;
2529 }
2530
ce78b96d
JB
2531 if (b->type == bp_catchpoint)
2532 {
2533 /* For now, none of the bp_catchpoint breakpoints need to
2534 do anything at this point. In the future, if some of
2535 the catchpoints need to something, we will need to add
2536 a new method, and call this method from here. */
2537 continue;
2538 }
2539
c5aa993b
JM
2540 /* bp_finish is a special case. The only way we ought to be able
2541 to see one of these when an exec() has happened, is if the user
2542 caught a vfork, and then said "finish". Ordinarily a finish just
2543 carries them to the call-site of the current callee, by setting
2544 a temporary bp there and resuming. But in this case, the finish
2545 will carry them entirely through the vfork & exec.
2546
2547 We don't want to allow a bp_finish to remain inserted now. But
2548 we can't safely delete it, 'cause finish_command has a handle to
2549 the bp on a bpstat, and will later want to delete it. There's a
2550 chance (and I've seen it happen) that if we delete the bp_finish
2551 here, that its storage will get reused by the time finish_command
2552 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2553 We really must allow finish_command to delete a bp_finish.
2554
e5dd4106 2555 In the absence of a general solution for the "how do we know
53a5351d
JM
2556 it's safe to delete something others may have handles to?"
2557 problem, what we'll do here is just uninsert the bp_finish, and
2558 let finish_command delete it.
2559
2560 (We know the bp_finish is "doomed" in the sense that it's
2561 momentary, and will be deleted as soon as finish_command sees
2562 the inferior stopped. So it doesn't matter that the bp's
2563 address is probably bogus in the new a.out, unlike e.g., the
2564 solib breakpoints.) */
c5aa993b 2565
c5aa993b
JM
2566 if (b->type == bp_finish)
2567 {
2568 continue;
2569 }
2570
2571 /* Without a symbolic address, we have little hope of the
2572 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2573 a.out. */
c5aa993b
JM
2574 if (b->addr_string == NULL)
2575 {
2576 delete_breakpoint (b);
2577 continue;
2578 }
c5aa993b 2579 }
1900040c 2580 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2581 create_overlay_event_breakpoint ();
2582 create_longjmp_master_breakpoint ();
2583 create_std_terminate_master_breakpoint ();
186c406b 2584 create_exception_master_breakpoint ();
c906108c
SS
2585}
2586
2587int
fba45db2 2588detach_breakpoints (int pid)
c906108c 2589{
35df4500 2590 struct bp_location *bl, **blp_tmp;
3a1bae8e 2591 int val = 0;
ce696e05 2592 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2593 struct inferior *inf = current_inferior ();
c5aa993b 2594
39f77062 2595 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2596 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2597
6c95b8df 2598 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2599 inferior_ptid = pid_to_ptid (pid);
35df4500 2600 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2601 {
35df4500 2602 if (bl->pspace != inf->pspace)
6c95b8df
PA
2603 continue;
2604
35df4500
TJB
2605 if (bl->inserted)
2606 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2607 }
d03285ec
UW
2608
2609 /* Detach single-step breakpoints as well. */
2610 detach_single_step_breakpoints ();
2611
ce696e05 2612 do_cleanups (old_chain);
3a1bae8e 2613 return val;
c906108c
SS
2614}
2615
35df4500 2616/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2617 Note that this is used to detach breakpoints from a child fork.
2618 When we get here, the child isn't in the inferior list, and neither
2619 do we have objects to represent its address space --- we should
35df4500 2620 *not* look at bl->pspace->aspace here. */
6c95b8df 2621
c906108c 2622static int
35df4500 2623remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2624{
2625 int val;
c5aa993b 2626
35df4500
TJB
2627 /* BL is never in moribund_locations by our callers. */
2628 gdb_assert (bl->owner != NULL);
2bdf28a0 2629
35df4500 2630 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2631 /* Permanent breakpoints cannot be inserted or removed. */
2632 return 0;
2633
74960c60
VP
2634 /* The type of none suggests that owner is actually deleted.
2635 This should not ever happen. */
35df4500 2636 gdb_assert (bl->owner->type != bp_none);
0bde7532 2637
35df4500
TJB
2638 if (bl->loc_type == bp_loc_software_breakpoint
2639 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2640 {
c02f5703
MS
2641 /* "Normal" instruction breakpoint: either the standard
2642 trap-instruction bp (bp_breakpoint), or a
2643 bp_hardware_breakpoint. */
2644
2645 /* First check to see if we have to handle an overlay. */
2646 if (overlay_debugging == ovly_off
35df4500
TJB
2647 || bl->section == NULL
2648 || !(section_is_overlay (bl->section)))
c02f5703
MS
2649 {
2650 /* No overlay handling: just remove the breakpoint. */
348d480f 2651 val = bl->owner->ops->remove_location (bl);
c02f5703 2652 }
c906108c
SS
2653 else
2654 {
4a64f543 2655 /* This breakpoint is in an overlay section.
c02f5703
MS
2656 Did we set a breakpoint at the LMA? */
2657 if (!overlay_events_enabled)
2658 {
2659 /* Yes -- overlay event support is not active, so we
2660 should have set a breakpoint at the LMA. Remove it.
2661 */
c02f5703
MS
2662 /* Ignore any failures: if the LMA is in ROM, we will
2663 have already warned when we failed to insert it. */
35df4500
TJB
2664 if (bl->loc_type == bp_loc_hardware_breakpoint)
2665 target_remove_hw_breakpoint (bl->gdbarch,
2666 &bl->overlay_target_info);
c02f5703 2667 else
35df4500
TJB
2668 target_remove_breakpoint (bl->gdbarch,
2669 &bl->overlay_target_info);
c02f5703
MS
2670 }
2671 /* Did we set a breakpoint at the VMA?
2672 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2673 if (bl->inserted)
c906108c 2674 {
c02f5703
MS
2675 /* Yes -- remove it. Previously we did not bother to
2676 remove the breakpoint if the section had been
2677 unmapped, but let's not rely on that being safe. We
2678 don't know what the overlay manager might do. */
aa67235e
UW
2679
2680 /* However, we should remove *software* breakpoints only
2681 if the section is still mapped, or else we overwrite
2682 wrong code with the saved shadow contents. */
348d480f
PA
2683 if (bl->loc_type == bp_loc_hardware_breakpoint
2684 || section_is_mapped (bl->section))
2685 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
2686 else
2687 val = 0;
c906108c 2688 }
c02f5703
MS
2689 else
2690 {
2691 /* No -- not inserted, so no need to remove. No error. */
2692 val = 0;
2693 }
c906108c 2694 }
879d1e6b
UW
2695
2696 /* In some cases, we might not be able to remove a breakpoint
2697 in a shared library that has already been removed, but we
2698 have not yet processed the shlib unload event. */
35df4500 2699 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2700 val = 0;
2701
c906108c
SS
2702 if (val)
2703 return val;
35df4500 2704 bl->inserted = (is == mark_inserted);
c906108c 2705 }
35df4500 2706 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2707 {
77b06cd7
TJB
2708 gdb_assert (bl->owner->ops != NULL
2709 && bl->owner->ops->remove_location != NULL);
2710
35df4500 2711 bl->inserted = (is == mark_inserted);
77b06cd7 2712 bl->owner->ops->remove_location (bl);
2e70b7b9 2713
c906108c 2714 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2715 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2716 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2717 bl->owner->number);
c906108c 2718 }
35df4500
TJB
2719 else if (bl->owner->type == bp_catchpoint
2720 && breakpoint_enabled (bl->owner)
2721 && !bl->duplicate)
ce78b96d 2722 {
77b06cd7
TJB
2723 gdb_assert (bl->owner->ops != NULL
2724 && bl->owner->ops->remove_location != NULL);
ce78b96d 2725
77b06cd7 2726 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2727 if (val)
2728 return val;
77b06cd7 2729
35df4500 2730 bl->inserted = (is == mark_inserted);
ce78b96d 2731 }
c906108c
SS
2732
2733 return 0;
2734}
2735
6c95b8df 2736static int
35df4500 2737remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2738{
2739 int ret;
2740 struct cleanup *old_chain;
2741
35df4500
TJB
2742 /* BL is never in moribund_locations by our callers. */
2743 gdb_assert (bl->owner != NULL);
2bdf28a0 2744
35df4500 2745 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2746 /* Permanent breakpoints cannot be inserted or removed. */
2747 return 0;
2748
2749 /* The type of none suggests that owner is actually deleted.
2750 This should not ever happen. */
35df4500 2751 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2752
2753 old_chain = save_current_space_and_thread ();
2754
35df4500 2755 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2756
35df4500 2757 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2758
2759 do_cleanups (old_chain);
2760 return ret;
2761}
2762
c906108c
SS
2763/* Clear the "inserted" flag in all breakpoints. */
2764
25b22b0a 2765void
fba45db2 2766mark_breakpoints_out (void)
c906108c 2767{
35df4500 2768 struct bp_location *bl, **blp_tmp;
c906108c 2769
35df4500
TJB
2770 ALL_BP_LOCATIONS (bl, blp_tmp)
2771 if (bl->pspace == current_program_space)
2772 bl->inserted = 0;
c906108c
SS
2773}
2774
53a5351d
JM
2775/* Clear the "inserted" flag in all breakpoints and delete any
2776 breakpoints which should go away between runs of the program.
c906108c
SS
2777
2778 Plus other such housekeeping that has to be done for breakpoints
2779 between runs.
2780
53a5351d
JM
2781 Note: this function gets called at the end of a run (by
2782 generic_mourn_inferior) and when a run begins (by
4a64f543 2783 init_wait_for_inferior). */
c906108c
SS
2784
2785
2786
2787void
fba45db2 2788breakpoint_init_inferior (enum inf_context context)
c906108c 2789{
35df4500
TJB
2790 struct breakpoint *b, *b_tmp;
2791 struct bp_location *bl, **blp_tmp;
1c5cfe86 2792 int ix;
6c95b8df 2793 struct program_space *pspace = current_program_space;
c906108c 2794
50c71eaf
PA
2795 /* If breakpoint locations are shared across processes, then there's
2796 nothing to do. */
2567c7d9 2797 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2798 return;
2799
35df4500 2800 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2801 {
35df4500
TJB
2802 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2803 if (bl->pspace == pspace
2804 && bl->owner->enable_state != bp_permanent)
2805 bl->inserted = 0;
6c95b8df 2806 }
075f6582 2807
35df4500 2808 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2809 {
6c95b8df
PA
2810 if (b->loc && b->loc->pspace != pspace)
2811 continue;
2812
c5aa993b
JM
2813 switch (b->type)
2814 {
2815 case bp_call_dummy:
c906108c 2816
c5aa993b 2817 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2818 cause problems when the inferior is rerun, so we better get
2819 rid of it. */
2820
2821 case bp_watchpoint_scope:
2822
2823 /* Also get rid of scope breakpoints. */
2824
2825 case bp_shlib_event:
2826
2827 /* Also remove solib event breakpoints. Their addresses may
2828 have changed since the last time we ran the program.
2829 Actually we may now be debugging against different target;
2830 and so the solib backend that installed this breakpoint may
2831 not be used in by the target. E.g.,
2832
2833 (gdb) file prog-linux
2834 (gdb) run # native linux target
2835 ...
2836 (gdb) kill
2837 (gdb) file prog-win.exe
2838 (gdb) tar rem :9999 # remote Windows gdbserver.
2839 */
c906108c 2840
c5aa993b
JM
2841 delete_breakpoint (b);
2842 break;
c906108c 2843
c5aa993b
JM
2844 case bp_watchpoint:
2845 case bp_hardware_watchpoint:
2846 case bp_read_watchpoint:
2847 case bp_access_watchpoint:
3a5c3e22
PA
2848 {
2849 struct watchpoint *w = (struct watchpoint *) b;
c906108c 2850
3a5c3e22
PA
2851 /* Likewise for watchpoints on local expressions. */
2852 if (w->exp_valid_block != NULL)
2853 delete_breakpoint (b);
2854 else if (context == inf_starting)
2855 {
2856 /* Reset val field to force reread of starting value in
2857 insert_breakpoints. */
2858 if (w->val)
2859 value_free (w->val);
2860 w->val = NULL;
2861 w->val_valid = 0;
c860120c 2862 }
3a5c3e22 2863 }
c5aa993b
JM
2864 break;
2865 default:
c5aa993b
JM
2866 break;
2867 }
2868 }
1c5cfe86
PA
2869
2870 /* Get rid of the moribund locations. */
35df4500
TJB
2871 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2872 decref_bp_location (&bl);
1c5cfe86 2873 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2874}
2875
6c95b8df
PA
2876/* These functions concern about actual breakpoints inserted in the
2877 target --- to e.g. check if we need to do decr_pc adjustment or if
2878 we need to hop over the bkpt --- so we check for address space
2879 match, not program space. */
2880
c2c6d25f
JM
2881/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2882 exists at PC. It returns ordinary_breakpoint_here if it's an
2883 ordinary breakpoint, or permanent_breakpoint_here if it's a
2884 permanent breakpoint.
2885 - When continuing from a location with an ordinary breakpoint, we
2886 actually single step once before calling insert_breakpoints.
e5dd4106 2887 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
2888 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2889 the target, to advance the PC past the breakpoint. */
c906108c 2890
c2c6d25f 2891enum breakpoint_here
6c95b8df 2892breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2893{
35df4500 2894 struct bp_location *bl, **blp_tmp;
c2c6d25f 2895 int any_breakpoint_here = 0;
c906108c 2896
35df4500 2897 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2898 {
35df4500
TJB
2899 if (bl->loc_type != bp_loc_software_breakpoint
2900 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2901 continue;
2902
f1310107 2903 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2904 if ((breakpoint_enabled (bl->owner)
2905 || bl->owner->enable_state == bp_permanent)
f1310107 2906 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2907 {
2908 if (overlay_debugging
35df4500
TJB
2909 && section_is_overlay (bl->section)
2910 && !section_is_mapped (bl->section))
075f6582 2911 continue; /* unmapped overlay -- can't be a match */
35df4500 2912 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2913 return permanent_breakpoint_here;
2914 else
2915 any_breakpoint_here = 1;
2916 }
2917 }
c906108c 2918
c2c6d25f 2919 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2920}
2921
1c5cfe86
PA
2922/* Return true if there's a moribund breakpoint at PC. */
2923
2924int
6c95b8df 2925moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2926{
2927 struct bp_location *loc;
2928 int ix;
2929
2930 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2931 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2932 return 1;
2933
2934 return 0;
2935}
c2c6d25f 2936
c36b740a 2937/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2938 inserted using regular breakpoint_chain / bp_location array
2939 mechanism. This does not check for single-step breakpoints, which
2940 are inserted and removed using direct target manipulation. */
c906108c
SS
2941
2942int
4a64f543
MS
2943regular_breakpoint_inserted_here_p (struct address_space *aspace,
2944 CORE_ADDR pc)
c906108c 2945{
35df4500 2946 struct bp_location *bl, **blp_tmp;
c906108c 2947
35df4500 2948 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2949 {
35df4500
TJB
2950 if (bl->loc_type != bp_loc_software_breakpoint
2951 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2952 continue;
2953
35df4500 2954 if (bl->inserted
f1310107 2955 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2956 {
2957 if (overlay_debugging
35df4500
TJB
2958 && section_is_overlay (bl->section)
2959 && !section_is_mapped (bl->section))
075f6582
DJ
2960 continue; /* unmapped overlay -- can't be a match */
2961 else
2962 return 1;
2963 }
c5aa993b 2964 }
c36b740a
VP
2965 return 0;
2966}
2967
2968/* Returns non-zero iff there's either regular breakpoint
2969 or a single step breakpoint inserted at PC. */
2970
2971int
6c95b8df 2972breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2973{
6c95b8df 2974 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2975 return 1;
c906108c 2976
6c95b8df 2977 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2978 return 1;
2979
c906108c
SS
2980 return 0;
2981}
2982
4fa8626c
DJ
2983/* This function returns non-zero iff there is a software breakpoint
2984 inserted at PC. */
2985
2986int
3e43a32a
MS
2987software_breakpoint_inserted_here_p (struct address_space *aspace,
2988 CORE_ADDR pc)
4fa8626c 2989{
35df4500 2990 struct bp_location *bl, **blp_tmp;
4fa8626c 2991
35df4500 2992 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2993 {
35df4500 2994 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2995 continue;
2996
35df4500
TJB
2997 if (bl->inserted
2998 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2999 aspace, pc))
4fa8626c
DJ
3000 {
3001 if (overlay_debugging
35df4500
TJB
3002 && section_is_overlay (bl->section)
3003 && !section_is_mapped (bl->section))
4fa8626c
DJ
3004 continue; /* unmapped overlay -- can't be a match */
3005 else
3006 return 1;
3007 }
3008 }
3009
1aafd4da 3010 /* Also check for software single-step breakpoints. */
6c95b8df 3011 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3012 return 1;
3013
4fa8626c
DJ
3014 return 0;
3015}
3016
9093389c
PA
3017int
3018hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3019 CORE_ADDR addr, ULONGEST len)
3020{
3021 struct breakpoint *bpt;
3022
3023 ALL_BREAKPOINTS (bpt)
3024 {
3025 struct bp_location *loc;
3026
3027 if (bpt->type != bp_hardware_watchpoint
3028 && bpt->type != bp_access_watchpoint)
3029 continue;
3030
3031 if (!breakpoint_enabled (bpt))
3032 continue;
3033
3034 for (loc = bpt->loc; loc; loc = loc->next)
3035 if (loc->pspace->aspace == aspace && loc->inserted)
3036 {
3037 CORE_ADDR l, h;
3038
3039 /* Check for intersection. */
3040 l = max (loc->address, addr);
3041 h = min (loc->address + loc->length, addr + len);
3042 if (l < h)
3043 return 1;
3044 }
3045 }
3046 return 0;
3047}
3048
075f6582
DJ
3049/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3050 PC is valid for process/thread PTID. */
c906108c
SS
3051
3052int
6c95b8df
PA
3053breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3054 ptid_t ptid)
c906108c 3055{
35df4500 3056 struct bp_location *bl, **blp_tmp;
4a306c9a 3057 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3058 int thread = -1;
4a306c9a 3059 int task = 0;
a6f1cd96 3060
35df4500 3061 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3062 {
35df4500
TJB
3063 if (bl->loc_type != bp_loc_software_breakpoint
3064 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3065 continue;
3066
35df4500
TJB
3067 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3068 if (!breakpoint_enabled (bl->owner)
3069 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3070 continue;
3071
f1310107 3072 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3073 continue;
3074
35df4500 3075 if (bl->owner->thread != -1)
075f6582 3076 {
a6f1cd96
JB
3077 /* This is a thread-specific breakpoint. Check that ptid
3078 matches that thread. If thread hasn't been computed yet,
3079 it is now time to do so. */
3080 if (thread == -1)
3081 thread = pid_to_thread_id (ptid);
35df4500 3082 if (bl->owner->thread != thread)
a6f1cd96 3083 continue;
075f6582 3084 }
a6f1cd96 3085
35df4500 3086 if (bl->owner->task != 0)
4a306c9a
JB
3087 {
3088 /* This is a task-specific breakpoint. Check that ptid
3089 matches that task. If task hasn't been computed yet,
3090 it is now time to do so. */
3091 if (task == 0)
3092 task = ada_get_task_number (ptid);
35df4500 3093 if (bl->owner->task != task)
4a306c9a
JB
3094 continue;
3095 }
3096
a6f1cd96 3097 if (overlay_debugging
35df4500
TJB
3098 && section_is_overlay (bl->section)
3099 && !section_is_mapped (bl->section))
a6f1cd96
JB
3100 continue; /* unmapped overlay -- can't be a match */
3101
3102 return 1;
c5aa993b 3103 }
c906108c
SS
3104
3105 return 0;
3106}
c906108c 3107\f
c5aa993b 3108
c906108c
SS
3109/* bpstat stuff. External routines' interfaces are documented
3110 in breakpoint.h. */
3111
3112int
fba45db2 3113ep_is_catchpoint (struct breakpoint *ep)
c906108c 3114{
533be4dd 3115 return (ep->type == bp_catchpoint);
c906108c
SS
3116}
3117
f431efe5
PA
3118/* Frees any storage that is part of a bpstat. Does not walk the
3119 'next' chain. */
3120
3121static void
198757a8
VP
3122bpstat_free (bpstat bs)
3123{
3124 if (bs->old_val != NULL)
3125 value_free (bs->old_val);
9add0f1b 3126 decref_counted_command_line (&bs->commands);
f431efe5 3127 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3128 xfree (bs);
3129}
3130
c906108c
SS
3131/* Clear a bpstat so that it says we are not at any breakpoint.
3132 Also free any storage that is part of a bpstat. */
3133
3134void
fba45db2 3135bpstat_clear (bpstat *bsp)
c906108c
SS
3136{
3137 bpstat p;
3138 bpstat q;
3139
3140 if (bsp == 0)
3141 return;
3142 p = *bsp;
3143 while (p != NULL)
3144 {
3145 q = p->next;
198757a8 3146 bpstat_free (p);
c906108c
SS
3147 p = q;
3148 }
3149 *bsp = NULL;
3150}
3151
3152/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3153 is part of the bpstat is copied as well. */
3154
3155bpstat
fba45db2 3156bpstat_copy (bpstat bs)
c906108c
SS
3157{
3158 bpstat p = NULL;
3159 bpstat tmp;
3160 bpstat retval = NULL;
3161
3162 if (bs == NULL)
3163 return bs;
3164
3165 for (; bs != NULL; bs = bs->next)
3166 {
3167 tmp = (bpstat) xmalloc (sizeof (*tmp));
3168 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3169 incref_counted_command_line (tmp->commands);
f431efe5 3170 incref_bp_location (tmp->bp_location_at);
31cc81e9 3171 if (bs->old_val != NULL)
3c3185ac
JK
3172 {
3173 tmp->old_val = value_copy (bs->old_val);
3174 release_value (tmp->old_val);
3175 }
31cc81e9 3176
c906108c
SS
3177 if (p == NULL)
3178 /* This is the first thing in the chain. */
3179 retval = tmp;
3180 else
3181 p->next = tmp;
3182 p = tmp;
3183 }
3184 p->next = NULL;
3185 return retval;
3186}
3187
4a64f543 3188/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3189
3190bpstat
fba45db2 3191bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3192{
c5aa993b
JM
3193 if (bsp == NULL)
3194 return NULL;
c906108c 3195
c5aa993b
JM
3196 for (; bsp != NULL; bsp = bsp->next)
3197 {
f431efe5 3198 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3199 return bsp;
3200 }
c906108c
SS
3201 return NULL;
3202}
3203
4a64f543
MS
3204/* Put in *NUM the breakpoint number of the first breakpoint we are
3205 stopped at. *BSP upon return is a bpstat which points to the
3206 remaining breakpoints stopped at (but which is not guaranteed to be
3207 good for anything but further calls to bpstat_num).
3208
8671a17b
PA
3209 Return 0 if passed a bpstat which does not indicate any breakpoints.
3210 Return -1 if stopped at a breakpoint that has been deleted since
3211 we set it.
3212 Return 1 otherwise. */
c906108c
SS
3213
3214int
8671a17b 3215bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3216{
3217 struct breakpoint *b;
3218
3219 if ((*bsp) == NULL)
3220 return 0; /* No more breakpoint values */
8671a17b 3221
4a64f543
MS
3222 /* We assume we'll never have several bpstats that correspond to a
3223 single breakpoint -- otherwise, this function might return the
3224 same number more than once and this will look ugly. */
f431efe5 3225 b = (*bsp)->breakpoint_at;
8671a17b
PA
3226 *bsp = (*bsp)->next;
3227 if (b == NULL)
3228 return -1; /* breakpoint that's been deleted since */
3229
3230 *num = b->number; /* We have its number */
3231 return 1;
c906108c
SS
3232}
3233
e93ca019 3234/* See breakpoint.h. */
c906108c
SS
3235
3236void
e93ca019 3237bpstat_clear_actions (void)
c906108c 3238{
e93ca019
JK
3239 struct thread_info *tp;
3240 bpstat bs;
3241
3242 if (ptid_equal (inferior_ptid, null_ptid))
3243 return;
3244
3245 tp = find_thread_ptid (inferior_ptid);
3246 if (tp == NULL)
3247 return;
3248
3249 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3250 {
9add0f1b 3251 decref_counted_command_line (&bs->commands);
abf85f46 3252
c906108c
SS
3253 if (bs->old_val != NULL)
3254 {
3255 value_free (bs->old_val);
3256 bs->old_val = NULL;
3257 }
3258 }
3259}
3260
f3b1572e
PA
3261/* Called when a command is about to proceed the inferior. */
3262
3263static void
3264breakpoint_about_to_proceed (void)
3265{
3266 if (!ptid_equal (inferior_ptid, null_ptid))
3267 {
3268 struct thread_info *tp = inferior_thread ();
3269
3270 /* Allow inferior function calls in breakpoint commands to not
3271 interrupt the command list. When the call finishes
3272 successfully, the inferior will be standing at the same
3273 breakpoint as if nothing happened. */
16c381f0 3274 if (tp->control.in_infcall)
f3b1572e
PA
3275 return;
3276 }
3277
3278 breakpoint_proceeded = 1;
3279}
3280
4a64f543
MS
3281/* Stub for cleaning up our state if we error-out of a breakpoint
3282 command. */
c906108c 3283static void
4efb68b1 3284cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3285{
3286 executing_breakpoint_commands = 0;
3287}
3288
abf85f46
JK
3289/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3290 or its equivalent. */
3291
3292static int
3293command_line_is_silent (struct command_line *cmd)
3294{
3295 return cmd && (strcmp ("silent", cmd->line) == 0
3296 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3297}
3298
4a64f543
MS
3299/* Execute all the commands associated with all the breakpoints at
3300 this location. Any of these commands could cause the process to
3301 proceed beyond this point, etc. We look out for such changes by
3302 checking the global "breakpoint_proceeded" after each command.
c906108c 3303
347bddb7
PA
3304 Returns true if a breakpoint command resumed the inferior. In that
3305 case, it is the caller's responsibility to recall it again with the
3306 bpstat of the current thread. */
3307
3308static int
3309bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3310{
3311 bpstat bs;
3312 struct cleanup *old_chain;
347bddb7 3313 int again = 0;
c906108c
SS
3314
3315 /* Avoid endless recursion if a `source' command is contained
3316 in bs->commands. */
3317 if (executing_breakpoint_commands)
347bddb7 3318 return 0;
c906108c
SS
3319
3320 executing_breakpoint_commands = 1;
3321 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3322
cf6c5ffb
TT
3323 prevent_dont_repeat ();
3324
4a64f543 3325 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3326 bs = *bsp;
3327
3328 breakpoint_proceeded = 0;
3329 for (; bs != NULL; bs = bs->next)
3330 {
9add0f1b 3331 struct counted_command_line *ccmd;
6c50ab1c
JB
3332 struct command_line *cmd;
3333 struct cleanup *this_cmd_tree_chain;
3334
3335 /* Take ownership of the BSP's command tree, if it has one.
3336
3337 The command tree could legitimately contain commands like
3338 'step' and 'next', which call clear_proceed_status, which
3339 frees stop_bpstat's command tree. To make sure this doesn't
3340 free the tree we're executing out from under us, we need to
3341 take ownership of the tree ourselves. Since a given bpstat's
3342 commands are only executed once, we don't need to copy it; we
3343 can clear the pointer in the bpstat, and make sure we free
3344 the tree when we're done. */
9add0f1b
TT
3345 ccmd = bs->commands;
3346 bs->commands = NULL;
abf85f46
JK
3347 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
3348 cmd = ccmd ? ccmd->commands : NULL;
3349 if (command_line_is_silent (cmd))
3350 {
3351 /* The action has been already done by bpstat_stop_status. */
3352 cmd = cmd->next;
3353 }
6c50ab1c 3354
c906108c
SS
3355 while (cmd != NULL)
3356 {
3357 execute_control_command (cmd);
3358
3359 if (breakpoint_proceeded)
3360 break;
3361 else
3362 cmd = cmd->next;
3363 }
6c50ab1c
JB
3364
3365 /* We can free this command tree now. */
3366 do_cleanups (this_cmd_tree_chain);
3367
c906108c 3368 if (breakpoint_proceeded)
32c1e744
VP
3369 {
3370 if (target_can_async_p ())
347bddb7
PA
3371 /* If we are in async mode, then the target might be still
3372 running, not stopped at any breakpoint, so nothing for
3373 us to do here -- just return to the event loop. */
3374 ;
32c1e744
VP
3375 else
3376 /* In sync mode, when execute_control_command returns
3377 we're already standing on the next breakpoint.
347bddb7
PA
3378 Breakpoint commands for that stop were not run, since
3379 execute_command does not run breakpoint commands --
3380 only command_line_handler does, but that one is not
3381 involved in execution of breakpoint commands. So, we
3382 can now execute breakpoint commands. It should be
3383 noted that making execute_command do bpstat actions is
3384 not an option -- in this case we'll have recursive
3385 invocation of bpstat for each breakpoint with a
3386 command, and can easily blow up GDB stack. Instead, we
3387 return true, which will trigger the caller to recall us
3388 with the new stop_bpstat. */
3389 again = 1;
3390 break;
32c1e744 3391 }
c906108c 3392 }
c2b8ed2c 3393 do_cleanups (old_chain);
347bddb7
PA
3394 return again;
3395}
3396
3397void
3398bpstat_do_actions (void)
3399{
353d1d73
JK
3400 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
3401
347bddb7
PA
3402 /* Do any commands attached to breakpoint we are stopped at. */
3403 while (!ptid_equal (inferior_ptid, null_ptid)
3404 && target_has_execution
3405 && !is_exited (inferior_ptid)
3406 && !is_executing (inferior_ptid))
3407 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3408 and only return when it is stopped at the next breakpoint, we
3409 keep doing breakpoint actions until it returns false to
3410 indicate the inferior was not resumed. */
16c381f0 3411 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3412 break;
353d1d73
JK
3413
3414 discard_cleanups (cleanup_if_error);
c906108c
SS
3415}
3416
fa4727a6
DJ
3417/* Print out the (old or new) value associated with a watchpoint. */
3418
3419static void
3420watchpoint_value_print (struct value *val, struct ui_file *stream)
3421{
3422 if (val == NULL)
3423 fprintf_unfiltered (stream, _("<unreadable>"));
3424 else
79a45b7d
TT
3425 {
3426 struct value_print_options opts;
3427 get_user_print_options (&opts);
3428 value_print (val, stream, &opts);
3429 }
fa4727a6
DJ
3430}
3431
e514a9d6 3432/* Generic routine for printing messages indicating why we
4a64f543 3433 stopped. The behavior of this function depends on the value
e514a9d6
JM
3434 'print_it' in the bpstat structure. Under some circumstances we
3435 may decide not to print anything here and delegate the task to
4a64f543 3436 normal_stop(). */
e514a9d6
JM
3437
3438static enum print_stop_action
3439print_bp_stop_message (bpstat bs)
3440{
3441 switch (bs->print_it)
3442 {
3443 case print_it_noop:
4a64f543 3444 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3445 return PRINT_UNKNOWN;
3446 break;
3447
3448 case print_it_done:
3449 /* We still want to print the frame, but we already printed the
4a64f543 3450 relevant messages. */
e514a9d6
JM
3451 return PRINT_SRC_AND_LOC;
3452 break;
3453
3454 case print_it_normal:
4f8d1dc6 3455 {
f431efe5
PA
3456 struct breakpoint *b = bs->breakpoint_at;
3457
1a6a67de
TJB
3458 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3459 which has since been deleted. */
3460 if (b == NULL)
3461 return PRINT_UNKNOWN;
3462
348d480f
PA
3463 /* Normal case. Call the breakpoint's print_it method. */
3464 return b->ops->print_it (bs);
4f8d1dc6 3465 }
348d480f 3466 break;
3086aeae 3467
e514a9d6 3468 default:
8e65ff28 3469 internal_error (__FILE__, __LINE__,
e2e0b3e5 3470 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3471 break;
c906108c 3472 }
c906108c
SS
3473}
3474
e514a9d6
JM
3475/* Print a message indicating what happened. This is called from
3476 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
3477 list - a list of the eventpoints that caused this stop. KIND is
3478 the target_waitkind for the stopping event. This
e514a9d6
JM
3479 routine calls the generic print routine for printing a message
3480 about reasons for stopping. This will print (for example) the
3481 "Breakpoint n," part of the output. The return value of this
3482 routine is one of:
c906108c 3483
4a64f543 3484 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3485 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3486 code to print the location. An example is
c5aa993b
JM
3487 "Breakpoint 1, " which should be followed by
3488 the location.
917317f4 3489 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3490 to also print the location part of the message.
3491 An example is the catch/throw messages, which
4a64f543 3492 don't require a location appended to the end.
917317f4 3493 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3494 further info to be printed. */
c906108c 3495
917317f4 3496enum print_stop_action
36dfb11c 3497bpstat_print (bpstat bs, int kind)
c906108c
SS
3498{
3499 int val;
c5aa993b 3500
c906108c 3501 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3502 (Currently all watchpoints go on the bpstat whether hit or not.
3503 That probably could (should) be changed, provided care is taken
c906108c 3504 with respect to bpstat_explains_signal). */
e514a9d6
JM
3505 for (; bs; bs = bs->next)
3506 {
3507 val = print_bp_stop_message (bs);
3508 if (val == PRINT_SRC_ONLY
3509 || val == PRINT_SRC_AND_LOC
3510 || val == PRINT_NOTHING)
3511 return val;
3512 }
c906108c 3513
36dfb11c
TT
3514 /* If we had hit a shared library event breakpoint,
3515 print_bp_stop_message would print out this message. If we hit an
3516 OS-level shared library event, do the same thing. */
3517 if (kind == TARGET_WAITKIND_LOADED)
3518 {
3519 ui_out_text (current_uiout, _("Stopped due to shared library event\n"));
3520 if (ui_out_is_mi_like_p (current_uiout))
3521 ui_out_field_string (current_uiout, "reason",
3522 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
3523 return PRINT_NOTHING;
3524 }
3525
e514a9d6 3526 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3527 with and nothing was printed. */
917317f4 3528 return PRINT_UNKNOWN;
c906108c
SS
3529}
3530
4a64f543
MS
3531/* Evaluate the expression EXP and return 1 if value is zero. This is
3532 used inside a catch_errors to evaluate the breakpoint condition.
3533 The argument is a "struct expression *" that has been cast to a
3534 "char *" to make it pass through catch_errors. */
c906108c
SS
3535
3536static int
4efb68b1 3537breakpoint_cond_eval (void *exp)
c906108c 3538{
278cd55f 3539 struct value *mark = value_mark ();
c5aa993b 3540 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3541
c906108c
SS
3542 value_free_to_mark (mark);
3543 return i;
3544}
3545
5760d0ab 3546/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3547
3548static bpstat
5760d0ab 3549bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3550{
3551 bpstat bs;
3552
3553 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3554 bs->next = NULL;
3555 **bs_link_pointer = bs;
3556 *bs_link_pointer = &bs->next;
f431efe5
PA
3557 bs->breakpoint_at = bl->owner;
3558 bs->bp_location_at = bl;
3559 incref_bp_location (bl);
c906108c
SS
3560 /* If the condition is false, etc., don't do the commands. */
3561 bs->commands = NULL;
3562 bs->old_val = NULL;
3563 bs->print_it = print_it_normal;
3564 return bs;
3565}
3566\f
d983da9c
DJ
3567/* The target has stopped with waitstatus WS. Check if any hardware
3568 watchpoints have triggered, according to the target. */
3569
3570int
3571watchpoints_triggered (struct target_waitstatus *ws)
3572{
d92524f1 3573 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3574 CORE_ADDR addr;
3575 struct breakpoint *b;
3576
3577 if (!stopped_by_watchpoint)
3578 {
3579 /* We were not stopped by a watchpoint. Mark all watchpoints
3580 as not triggered. */
3581 ALL_BREAKPOINTS (b)
cc60f2e3 3582 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3583 {
3584 struct watchpoint *w = (struct watchpoint *) b;
3585
3586 w->watchpoint_triggered = watch_triggered_no;
3587 }
d983da9c
DJ
3588
3589 return 0;
3590 }
3591
3592 if (!target_stopped_data_address (&current_target, &addr))
3593 {
3594 /* We were stopped by a watchpoint, but we don't know where.
3595 Mark all watchpoints as unknown. */
3596 ALL_BREAKPOINTS (b)
cc60f2e3 3597 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3598 {
3599 struct watchpoint *w = (struct watchpoint *) b;
3600
3601 w->watchpoint_triggered = watch_triggered_unknown;
3602 }
d983da9c
DJ
3603
3604 return stopped_by_watchpoint;
3605 }
3606
3607 /* The target could report the data address. Mark watchpoints
3608 affected by this data address as triggered, and all others as not
3609 triggered. */
3610
3611 ALL_BREAKPOINTS (b)
cc60f2e3 3612 if (is_hardware_watchpoint (b))
d983da9c 3613 {
3a5c3e22 3614 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 3615 struct bp_location *loc;
d983da9c 3616
3a5c3e22 3617 w->watchpoint_triggered = watch_triggered_no;
a5606eee 3618 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 3619 {
3a5c3e22 3620 if (is_masked_watchpoint (b))
9c06b0b4 3621 {
3a5c3e22
PA
3622 CORE_ADDR newaddr = addr & w->hw_wp_mask;
3623 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
3624
3625 if (newaddr == start)
3626 {
3a5c3e22 3627 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3628 break;
3629 }
3630 }
3631 /* Exact match not required. Within range is sufficient. */
3632 else if (target_watchpoint_addr_within_range (&current_target,
3633 addr, loc->address,
3634 loc->length))
3635 {
3a5c3e22 3636 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3637 break;
3638 }
3639 }
d983da9c
DJ
3640 }
3641
3642 return 1;
3643}
3644
c906108c
SS
3645/* Possible return values for watchpoint_check (this can't be an enum
3646 because of check_errors). */
3647/* The watchpoint has been deleted. */
3648#define WP_DELETED 1
3649/* The value has changed. */
3650#define WP_VALUE_CHANGED 2
3651/* The value has not changed. */
3652#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3653/* Ignore this watchpoint, no matter if the value changed or not. */
3654#define WP_IGNORE 4
c906108c
SS
3655
3656#define BP_TEMPFLAG 1
3657#define BP_HARDWAREFLAG 2
3658
4a64f543
MS
3659/* Evaluate watchpoint condition expression and check if its value
3660 changed.
553e4c11
JB
3661
3662 P should be a pointer to struct bpstat, but is defined as a void *
3663 in order for this function to be usable with catch_errors. */
c906108c
SS
3664
3665static int
4efb68b1 3666watchpoint_check (void *p)
c906108c
SS
3667{
3668 bpstat bs = (bpstat) p;
3a5c3e22 3669 struct watchpoint *b;
c906108c
SS
3670 struct frame_info *fr;
3671 int within_current_scope;
3672
f431efe5 3673 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3674 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 3675 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 3676
f6bc2008
PA
3677 /* If this is a local watchpoint, we only want to check if the
3678 watchpoint frame is in scope if the current thread is the thread
3679 that was used to create the watchpoint. */
3680 if (!watchpoint_in_thread_scope (b))
60e1c644 3681 return WP_IGNORE;
f6bc2008 3682
c906108c
SS
3683 if (b->exp_valid_block == NULL)
3684 within_current_scope = 1;
3685 else
3686 {
edb3359d
DJ
3687 struct frame_info *frame = get_current_frame ();
3688 struct gdbarch *frame_arch = get_frame_arch (frame);
3689 CORE_ADDR frame_pc = get_frame_pc (frame);
3690
4a64f543
MS
3691 /* in_function_epilogue_p() returns a non-zero value if we're
3692 still in the function but the stack frame has already been
3693 invalidated. Since we can't rely on the values of local
3694 variables after the stack has been destroyed, we are treating
3695 the watchpoint in that state as `not changed' without further
3696 checking. Don't mark watchpoints as changed if the current
3697 frame is in an epilogue - even if they are in some other
3698 frame, our view of the stack is likely to be wrong and
3699 frame_find_by_id could error out. */
a0f49112 3700 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3701 return WP_IGNORE;
a0f49112 3702
101dcfbe 3703 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3704 within_current_scope = (fr != NULL);
69fbadd5
DJ
3705
3706 /* If we've gotten confused in the unwinder, we might have
3707 returned a frame that can't describe this variable. */
edb3359d
DJ
3708 if (within_current_scope)
3709 {
3710 struct symbol *function;
3711
3712 function = get_frame_function (fr);
3713 if (function == NULL
3714 || !contained_in (b->exp_valid_block,
3715 SYMBOL_BLOCK_VALUE (function)))
3716 within_current_scope = 0;
3717 }
69fbadd5 3718
edb3359d 3719 if (within_current_scope)
c906108c
SS
3720 /* If we end up stopping, the current frame will get selected
3721 in normal_stop. So this call to select_frame won't affect
3722 the user. */
0f7d239c 3723 select_frame (fr);
c906108c 3724 }
c5aa993b 3725
c906108c
SS
3726 if (within_current_scope)
3727 {
4a64f543
MS
3728 /* We use value_{,free_to_}mark because it could be a *long*
3729 time before we return to the command level and call
3730 free_all_values. We can't call free_all_values because we
3731 might be in the middle of evaluating a function call. */
c906108c 3732
0cf6dd15 3733 int pc = 0;
9c06b0b4 3734 struct value *mark;
fa4727a6
DJ
3735 struct value *new_val;
3736
3a5c3e22 3737 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
3738 /* Since we don't know the exact trigger address (from
3739 stopped_data_address), just tell the user we've triggered
3740 a mask watchpoint. */
3741 return WP_VALUE_CHANGED;
3742
3743 mark = value_mark ();
0cf6dd15 3744 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3745
4a64f543
MS
3746 /* We use value_equal_contents instead of value_equal because
3747 the latter coerces an array to a pointer, thus comparing just
3748 the address of the array instead of its contents. This is
3749 not what we want. */
fa4727a6 3750 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3751 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3752 {
fa4727a6
DJ
3753 if (new_val != NULL)
3754 {
3755 release_value (new_val);
3756 value_free_to_mark (mark);
3757 }
c906108c
SS
3758 bs->old_val = b->val;
3759 b->val = new_val;
fa4727a6 3760 b->val_valid = 1;
c906108c
SS
3761 return WP_VALUE_CHANGED;
3762 }
3763 else
3764 {
60e1c644 3765 /* Nothing changed. */
c906108c 3766 value_free_to_mark (mark);
c906108c
SS
3767 return WP_VALUE_NOT_CHANGED;
3768 }
3769 }
3770 else
3771 {
79a45e25
PA
3772 struct ui_out *uiout = current_uiout;
3773
c906108c 3774 /* This seems like the only logical thing to do because
c5aa993b
JM
3775 if we temporarily ignored the watchpoint, then when
3776 we reenter the block in which it is valid it contains
3777 garbage (in the case of a function, it may have two
3778 garbage values, one before and one after the prologue).
3779 So we can't even detect the first assignment to it and
3780 watch after that (since the garbage may or may not equal
3781 the first value assigned). */
348d480f
PA
3782 /* We print all the stop information in
3783 breakpoint_ops->print_it, but in this case, by the time we
3784 call breakpoint_ops->print_it this bp will be deleted
3785 already. So we have no choice but print the information
3786 here. */
9dc5e2a9 3787 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3788 ui_out_field_string
3789 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3790 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 3791 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
3792 ui_out_text (uiout,
3793 " deleted because the program has left the block in\n\
8b93c638 3794which its expression is valid.\n");
4ce44c66 3795
cdac0397 3796 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 3797 decref_counted_command_line (&b->base.commands);
d0fb5eae 3798 watchpoint_del_at_next_stop (b);
c906108c
SS
3799
3800 return WP_DELETED;
3801 }
3802}
3803
18a18393 3804/* Return true if it looks like target has stopped due to hitting
348d480f
PA
3805 breakpoint location BL. This function does not check if we should
3806 stop, only if BL explains the stop. */
3807
18a18393 3808static int
6c95b8df
PA
3809bpstat_check_location (const struct bp_location *bl,
3810 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3811{
3812 struct breakpoint *b = bl->owner;
3813
348d480f 3814 /* BL is from an existing breakpoint. */
2bdf28a0
JK
3815 gdb_assert (b != NULL);
3816
348d480f 3817 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
18a18393
VP
3818}
3819
3a5c3e22
PA
3820/* Determine if the watched values have actually changed, and we
3821 should stop. If not, set BS->stop to 0. */
3822
18a18393
VP
3823static void
3824bpstat_check_watchpoint (bpstat bs)
3825{
2bdf28a0 3826 const struct bp_location *bl;
3a5c3e22 3827 struct watchpoint *b;
2bdf28a0
JK
3828
3829 /* BS is built for existing struct breakpoint. */
f431efe5 3830 bl = bs->bp_location_at;
2bdf28a0 3831 gdb_assert (bl != NULL);
3a5c3e22 3832 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 3833 gdb_assert (b != NULL);
18a18393 3834
18a18393 3835 {
18a18393
VP
3836 int must_check_value = 0;
3837
3a5c3e22 3838 if (b->base.type == bp_watchpoint)
18a18393
VP
3839 /* For a software watchpoint, we must always check the
3840 watched value. */
3841 must_check_value = 1;
3842 else if (b->watchpoint_triggered == watch_triggered_yes)
3843 /* We have a hardware watchpoint (read, write, or access)
3844 and the target earlier reported an address watched by
3845 this watchpoint. */
3846 must_check_value = 1;
3847 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 3848 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
3849 /* We were stopped by a hardware watchpoint, but the target could
3850 not report the data address. We must check the watchpoint's
3851 value. Access and read watchpoints are out of luck; without
3852 a data address, we can't figure it out. */
3853 must_check_value = 1;
3a5c3e22 3854
18a18393
VP
3855 if (must_check_value)
3856 {
3e43a32a
MS
3857 char *message
3858 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 3859 b->base.number);
18a18393
VP
3860 struct cleanup *cleanups = make_cleanup (xfree, message);
3861 int e = catch_errors (watchpoint_check, bs, message,
3862 RETURN_MASK_ALL);
3863 do_cleanups (cleanups);
3864 switch (e)
3865 {
3866 case WP_DELETED:
3867 /* We've already printed what needs to be printed. */
3868 bs->print_it = print_it_done;
3869 /* Stop. */
3870 break;
60e1c644
PA
3871 case WP_IGNORE:
3872 bs->print_it = print_it_noop;
3873 bs->stop = 0;
3874 break;
18a18393 3875 case WP_VALUE_CHANGED:
3a5c3e22 3876 if (b->base.type == bp_read_watchpoint)
18a18393 3877 {
85d721b8
PA
3878 /* There are two cases to consider here:
3879
4a64f543 3880 1. We're watching the triggered memory for reads.
85d721b8
PA
3881 In that case, trust the target, and always report
3882 the watchpoint hit to the user. Even though
3883 reads don't cause value changes, the value may
3884 have changed since the last time it was read, and
3885 since we're not trapping writes, we will not see
3886 those, and as such we should ignore our notion of
3887 old value.
3888
4a64f543 3889 2. We're watching the triggered memory for both
85d721b8
PA
3890 reads and writes. There are two ways this may
3891 happen:
3892
4a64f543 3893 2.1. This is a target that can't break on data
85d721b8
PA
3894 reads only, but can break on accesses (reads or
3895 writes), such as e.g., x86. We detect this case
3896 at the time we try to insert read watchpoints.
3897
4a64f543 3898 2.2. Otherwise, the target supports read
85d721b8
PA
3899 watchpoints, but, the user set an access or write
3900 watchpoint watching the same memory as this read
3901 watchpoint.
3902
3903 If we're watching memory writes as well as reads,
3904 ignore watchpoint hits when we find that the
3905 value hasn't changed, as reads don't cause
3906 changes. This still gives false positives when
3907 the program writes the same value to memory as
3908 what there was already in memory (we will confuse
3909 it for a read), but it's much better than
3910 nothing. */
3911
3912 int other_write_watchpoint = 0;
3913
3914 if (bl->watchpoint_type == hw_read)
3915 {
3916 struct breakpoint *other_b;
3917
3918 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
3919 if (other_b->type == bp_hardware_watchpoint
3920 || other_b->type == bp_access_watchpoint)
85d721b8 3921 {
3a5c3e22
PA
3922 struct watchpoint *other_w =
3923 (struct watchpoint *) other_b;
3924
3925 if (other_w->watchpoint_triggered
3926 == watch_triggered_yes)
3927 {
3928 other_write_watchpoint = 1;
3929 break;
3930 }
85d721b8
PA
3931 }
3932 }
3933
3934 if (other_write_watchpoint
3935 || bl->watchpoint_type == hw_access)
3936 {
3937 /* We're watching the same memory for writes,
3938 and the value changed since the last time we
3939 updated it, so this trap must be for a write.
3940 Ignore it. */
3941 bs->print_it = print_it_noop;
3942 bs->stop = 0;
3943 }
18a18393
VP
3944 }
3945 break;
3946 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
3947 if (b->base.type == bp_hardware_watchpoint
3948 || b->base.type == bp_watchpoint)
18a18393
VP
3949 {
3950 /* Don't stop: write watchpoints shouldn't fire if
3951 the value hasn't changed. */
3952 bs->print_it = print_it_noop;
3953 bs->stop = 0;
3954 }
3955 /* Stop. */
3956 break;
3957 default:
3958 /* Can't happen. */
3959 case 0:
3960 /* Error from catch_errors. */
3a5c3e22 3961 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 3962 watchpoint_del_at_next_stop (b);
18a18393
VP
3963 /* We've already printed what needs to be printed. */
3964 bs->print_it = print_it_done;
3965 break;
3966 }
3967 }
3968 else /* must_check_value == 0 */
3969 {
3970 /* This is a case where some watchpoint(s) triggered, but
3971 not at the address of this watchpoint, or else no
3972 watchpoint triggered after all. So don't print
3973 anything for this watchpoint. */
3974 bs->print_it = print_it_noop;
3975 bs->stop = 0;
3976 }
3977 }
3978}
3979
3980
3981/* Check conditions (condition proper, frame, thread and ignore count)
3982 of breakpoint referred to by BS. If we should not stop for this
3983 breakpoint, set BS->stop to 0. */
f431efe5 3984
18a18393
VP
3985static void
3986bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3987{
3988 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
3989 const struct bp_location *bl;
3990 struct breakpoint *b;
3991
3992 /* BS is built for existing struct breakpoint. */
f431efe5 3993 bl = bs->bp_location_at;
2bdf28a0 3994 gdb_assert (bl != NULL);
f431efe5 3995 b = bs->breakpoint_at;
2bdf28a0 3996 gdb_assert (b != NULL);
18a18393
VP
3997
3998 if (frame_id_p (b->frame_id)
edb3359d 3999 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4000 bs->stop = 0;
4001 else if (bs->stop)
4002 {
4003 int value_is_zero = 0;
60e1c644
PA
4004 struct expression *cond;
4005
7371cf6d
PM
4006 /* Evaluate Python breakpoints that have a "stop"
4007 method implemented. */
4008 if (b->py_bp_object)
4009 bs->stop = gdbpy_should_stop (b->py_bp_object);
4010
60e1c644 4011 if (is_watchpoint (b))
3a5c3e22
PA
4012 {
4013 struct watchpoint *w = (struct watchpoint *) b;
4014
4015 cond = w->cond_exp;
4016 }
60e1c644
PA
4017 else
4018 cond = bl->cond;
4019
f431efe5 4020 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4021 {
60e1c644 4022 int within_current_scope = 1;
3a5c3e22 4023 struct watchpoint * w;
60e1c644 4024
c5bc3a77
DJ
4025 /* We use value_mark and value_free_to_mark because it could
4026 be a long time before we return to the command level and
4027 call free_all_values. We can't call free_all_values
4028 because we might be in the middle of evaluating a
4029 function call. */
4030 struct value *mark = value_mark ();
4031
3a5c3e22
PA
4032 if (is_watchpoint (b))
4033 w = (struct watchpoint *) b;
4034 else
4035 w = NULL;
4036
edb3359d
DJ
4037 /* Need to select the frame, with all that implies so that
4038 the conditions will have the right context. Because we
4039 use the frame, we will not see an inlined function's
4040 variables when we arrive at a breakpoint at the start
4041 of the inlined function; the current frame will be the
4042 call site. */
3a5c3e22 4043 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4044 select_frame (get_current_frame ());
4045 else
4046 {
4047 struct frame_info *frame;
4048
4049 /* For local watchpoint expressions, which particular
4050 instance of a local is being watched matters, so we
4051 keep track of the frame to evaluate the expression
4052 in. To evaluate the condition however, it doesn't
4053 really matter which instantiation of the function
4054 where the condition makes sense triggers the
4055 watchpoint. This allows an expression like "watch
4056 global if q > 10" set in `func', catch writes to
4057 global on all threads that call `func', or catch
4058 writes on all recursive calls of `func' by a single
4059 thread. We simply always evaluate the condition in
4060 the innermost frame that's executing where it makes
4061 sense to evaluate the condition. It seems
4062 intuitive. */
3a5c3e22 4063 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4064 if (frame != NULL)
4065 select_frame (frame);
4066 else
4067 within_current_scope = 0;
4068 }
4069 if (within_current_scope)
4070 value_is_zero
4071 = catch_errors (breakpoint_cond_eval, cond,
4072 "Error in testing breakpoint condition:\n",
4073 RETURN_MASK_ALL);
4074 else
4075 {
4076 warning (_("Watchpoint condition cannot be tested "
4077 "in the current scope"));
4078 /* If we failed to set the right context for this
4079 watchpoint, unconditionally report it. */
4080 value_is_zero = 0;
4081 }
4a64f543 4082 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4083 value_free_to_mark (mark);
18a18393 4084 }
60e1c644
PA
4085
4086 if (cond && value_is_zero)
18a18393
VP
4087 {
4088 bs->stop = 0;
4089 }
4090 else if (b->thread != -1 && b->thread != thread_id)
4091 {
4092 bs->stop = 0;
4093 }
4094 else if (b->ignore_count > 0)
4095 {
4096 b->ignore_count--;
4097 annotate_ignore_count_change ();
4098 bs->stop = 0;
4a64f543 4099 /* Increase the hit count even though we don't stop. */
18a18393 4100 ++(b->hit_count);
8d3788bd 4101 observer_notify_breakpoint_modified (b);
18a18393
VP
4102 }
4103 }
4104}
4105
4106
9709f61c 4107/* Get a bpstat associated with having just stopped at address
d983da9c 4108 BP_ADDR in thread PTID.
c906108c 4109
d983da9c 4110 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4111 don't understand this stop. Result is a chain of bpstat's such
4112 that:
c906108c 4113
c5aa993b 4114 if we don't understand the stop, the result is a null pointer.
c906108c 4115
c5aa993b 4116 if we understand why we stopped, the result is not null.
c906108c 4117
c5aa993b
JM
4118 Each element of the chain refers to a particular breakpoint or
4119 watchpoint at which we have stopped. (We may have stopped for
4120 several reasons concurrently.)
c906108c 4121
c5aa993b
JM
4122 Each element of the chain has valid next, breakpoint_at,
4123 commands, FIXME??? fields. */
c906108c
SS
4124
4125bpstat
6c95b8df
PA
4126bpstat_stop_status (struct address_space *aspace,
4127 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4128{
0d381245 4129 struct breakpoint *b = NULL;
afe38095 4130 struct bp_location *bl;
20874c92 4131 struct bp_location *loc;
5760d0ab
JK
4132 /* First item of allocated bpstat's. */
4133 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4134 /* Pointer to the last thing in the chain currently. */
5760d0ab 4135 bpstat bs;
20874c92 4136 int ix;
429374b8 4137 int need_remove_insert;
f431efe5 4138 int removed_any;
c906108c 4139
f431efe5
PA
4140 /* First, build the bpstat chain with locations that explain a
4141 target stop, while being careful to not set the target running,
4142 as that may invalidate locations (in particular watchpoint
4143 locations are recreated). Resuming will happen here with
4144 breakpoint conditions or watchpoint expressions that include
4145 inferior function calls. */
c5aa993b 4146
429374b8
JK
4147 ALL_BREAKPOINTS (b)
4148 {
4149 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4150 continue;
a5606eee 4151
429374b8
JK
4152 for (bl = b->loc; bl != NULL; bl = bl->next)
4153 {
4a64f543
MS
4154 /* For hardware watchpoints, we look only at the first
4155 location. The watchpoint_check function will work on the
4156 entire expression, not the individual locations. For
4157 read watchpoints, the watchpoints_triggered function has
4158 checked all locations already. */
429374b8
JK
4159 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4160 break;
18a18393 4161
429374b8
JK
4162 if (bl->shlib_disabled)
4163 continue;
c5aa993b 4164
429374b8
JK
4165 if (!bpstat_check_location (bl, aspace, bp_addr))
4166 continue;
c5aa993b 4167
4a64f543
MS
4168 /* Come here if it's a watchpoint, or if the break address
4169 matches. */
c5aa993b 4170
4a64f543
MS
4171 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4172 explain stop. */
c5aa993b 4173
f431efe5
PA
4174 /* Assume we stop. Should we find a watchpoint that is not
4175 actually triggered, or if the condition of the breakpoint
4176 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4177 bs->stop = 1;
4178 bs->print = 1;
d983da9c 4179
f431efe5
PA
4180 /* If this is a scope breakpoint, mark the associated
4181 watchpoint as triggered so that we will handle the
4182 out-of-scope event. We'll get to the watchpoint next
4183 iteration. */
d0fb5eae 4184 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4185 {
4186 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4187
4188 w->watchpoint_triggered = watch_triggered_yes;
4189 }
f431efe5
PA
4190 }
4191 }
4192
4193 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4194 {
f1310107 4195 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4196 {
5760d0ab 4197 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4198 /* For hits of moribund locations, we should just proceed. */
4199 bs->stop = 0;
4200 bs->print = 0;
4201 bs->print_it = print_it_noop;
4202 }
4203 }
4204
f431efe5
PA
4205 /* Now go through the locations that caused the target to stop, and
4206 check whether we're interested in reporting this stop to higher
4207 layers, or whether we should resume the target transparently. */
4208
4209 removed_any = 0;
4210
5760d0ab 4211 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4212 {
4213 if (!bs->stop)
4214 continue;
4215
f431efe5 4216 b = bs->breakpoint_at;
348d480f
PA
4217 b->ops->check_status (bs);
4218 if (bs->stop)
28010a5d 4219 {
348d480f 4220 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4221
429374b8
JK
4222 if (bs->stop)
4223 {
4224 ++(b->hit_count);
8d3788bd 4225 observer_notify_breakpoint_modified (b);
c906108c 4226
4a64f543 4227 /* We will stop here. */
429374b8
JK
4228 if (b->disposition == disp_disable)
4229 {
4230 if (b->enable_state != bp_permanent)
4231 b->enable_state = bp_disabled;
f431efe5 4232 removed_any = 1;
429374b8
JK
4233 }
4234 if (b->silent)
4235 bs->print = 0;
4236 bs->commands = b->commands;
9add0f1b 4237 incref_counted_command_line (bs->commands);
abf85f46
JK
4238 if (command_line_is_silent (bs->commands
4239 ? bs->commands->commands : NULL))
4240 bs->print = 0;
429374b8
JK
4241 }
4242
e5dd4106 4243 /* Print nothing for this entry if we don't stop or don't print. */
429374b8
JK
4244 if (bs->stop == 0 || bs->print == 0)
4245 bs->print_it = print_it_noop;
348d480f 4246 }
429374b8 4247 }
876fa593 4248
d983da9c
DJ
4249 /* If we aren't stopping, the value of some hardware watchpoint may
4250 not have changed, but the intermediate memory locations we are
4251 watching may have. Don't bother if we're stopping; this will get
4252 done later. */
d832cb68 4253 need_remove_insert = 0;
5760d0ab
JK
4254 if (! bpstat_causes_stop (bs_head))
4255 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4256 if (!bs->stop
f431efe5
PA
4257 && bs->breakpoint_at
4258 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4259 {
3a5c3e22
PA
4260 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4261
4262 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 4263 need_remove_insert = 1;
d983da9c
DJ
4264 }
4265
d832cb68 4266 if (need_remove_insert)
2d134ed3 4267 update_global_location_list (1);
f431efe5
PA
4268 else if (removed_any)
4269 update_global_location_list (0);
d832cb68 4270
5760d0ab 4271 return bs_head;
c906108c 4272}
628fe4e4
JK
4273
4274static void
4275handle_jit_event (void)
4276{
4277 struct frame_info *frame;
4278 struct gdbarch *gdbarch;
4279
4280 /* Switch terminal for any messages produced by
4281 breakpoint_re_set. */
4282 target_terminal_ours_for_output ();
4283
4284 frame = get_current_frame ();
4285 gdbarch = get_frame_arch (frame);
4286
4287 jit_event_handler (gdbarch);
4288
4289 target_terminal_inferior ();
4290}
4291
4292/* Prepare WHAT final decision for infrun. */
4293
4294/* Decide what infrun needs to do with this bpstat. */
4295
c906108c 4296struct bpstat_what
0e30163f 4297bpstat_what (bpstat bs_head)
c906108c 4298{
c906108c 4299 struct bpstat_what retval;
628fe4e4
JK
4300 /* We need to defer calling `solib_add', as adding new symbols
4301 resets breakpoints, which in turn deletes breakpoint locations,
4302 and hence may clear unprocessed entries in the BS chain. */
4303 int shlib_event = 0;
4304 int jit_event = 0;
0e30163f 4305 bpstat bs;
c906108c 4306
628fe4e4 4307 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4308 retval.call_dummy = STOP_NONE;
186c406b 4309 retval.is_longjmp = 0;
628fe4e4 4310
0e30163f 4311 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4312 {
628fe4e4
JK
4313 /* Extract this BS's action. After processing each BS, we check
4314 if its action overrides all we've seem so far. */
4315 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4316 enum bptype bptype;
4317
c906108c 4318 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4319 {
4320 /* I suspect this can happen if it was a momentary
4321 breakpoint which has since been deleted. */
4322 bptype = bp_none;
4323 }
20874c92 4324 else
f431efe5 4325 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4326
4327 switch (bptype)
c906108c
SS
4328 {
4329 case bp_none:
628fe4e4 4330 break;
c906108c
SS
4331 case bp_breakpoint:
4332 case bp_hardware_breakpoint:
4333 case bp_until:
4334 case bp_finish:
4335 if (bs->stop)
4336 {
4337 if (bs->print)
628fe4e4 4338 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4339 else
628fe4e4 4340 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4341 }
4342 else
628fe4e4 4343 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4344 break;
4345 case bp_watchpoint:
4346 case bp_hardware_watchpoint:
4347 case bp_read_watchpoint:
4348 case bp_access_watchpoint:
4349 if (bs->stop)
4350 {
4351 if (bs->print)
628fe4e4 4352 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4353 else
628fe4e4 4354 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4355 }
4356 else
628fe4e4
JK
4357 {
4358 /* There was a watchpoint, but we're not stopping.
4359 This requires no further action. */
4360 }
c906108c
SS
4361 break;
4362 case bp_longjmp:
186c406b 4363 case bp_exception:
628fe4e4 4364 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4365 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4366 break;
4367 case bp_longjmp_resume:
186c406b 4368 case bp_exception_resume:
628fe4e4 4369 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4370 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4371 break;
4372 case bp_step_resume:
4373 if (bs->stop)
628fe4e4
JK
4374 this_action = BPSTAT_WHAT_STEP_RESUME;
4375 else
c906108c 4376 {
628fe4e4
JK
4377 /* It is for the wrong frame. */
4378 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4379 }
c906108c 4380 break;
2c03e5be
PA
4381 case bp_hp_step_resume:
4382 if (bs->stop)
4383 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4384 else
4385 {
4386 /* It is for the wrong frame. */
4387 this_action = BPSTAT_WHAT_SINGLE;
4388 }
4389 break;
c906108c 4390 case bp_watchpoint_scope:
c4093a6a 4391 case bp_thread_event:
1900040c 4392 case bp_overlay_event:
0fd8e87f 4393 case bp_longjmp_master:
aa7d318d 4394 case bp_std_terminate_master:
186c406b 4395 case bp_exception_master:
628fe4e4 4396 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4397 break;
ce78b96d 4398 case bp_catchpoint:
c5aa993b
JM
4399 if (bs->stop)
4400 {
4401 if (bs->print)
628fe4e4 4402 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4403 else
628fe4e4 4404 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4405 }
4406 else
628fe4e4
JK
4407 {
4408 /* There was a catchpoint, but we're not stopping.
4409 This requires no further action. */
4410 }
4411 break;
4412 case bp_shlib_event:
4413 shlib_event = 1;
4414
4415 /* If requested, stop when the dynamic linker notifies GDB
4416 of events. This allows the user to get control and place
4417 breakpoints in initializer routines for dynamically
4418 loaded objects (among other things). */
4419 if (stop_on_solib_events)
4420 this_action = BPSTAT_WHAT_STOP_NOISY;
4421 else
4422 this_action = BPSTAT_WHAT_SINGLE;
4423 break;
4424 case bp_jit_event:
4425 jit_event = 1;
4426 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4427 break;
c906108c 4428 case bp_call_dummy:
53a5351d
JM
4429 /* Make sure the action is stop (silent or noisy),
4430 so infrun.c pops the dummy frame. */
aa7d318d 4431 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4432 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4433 break;
4434 case bp_std_terminate:
4435 /* Make sure the action is stop (silent or noisy),
4436 so infrun.c pops the dummy frame. */
aa7d318d 4437 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4438 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4439 break;
1042e4c0 4440 case bp_tracepoint:
7a697b8d 4441 case bp_fast_tracepoint:
0fb4aa4b 4442 case bp_static_tracepoint:
1042e4c0
SS
4443 /* Tracepoint hits should not be reported back to GDB, and
4444 if one got through somehow, it should have been filtered
4445 out already. */
4446 internal_error (__FILE__, __LINE__,
7a697b8d 4447 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4448 break;
4449 case bp_gnu_ifunc_resolver:
4450 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4451 this_action = BPSTAT_WHAT_SINGLE;
4452 break;
4453 case bp_gnu_ifunc_resolver_return:
4454 /* The breakpoint will be removed, execution will restart from the
4455 PC of the former breakpoint. */
4456 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4457 break;
628fe4e4
JK
4458 default:
4459 internal_error (__FILE__, __LINE__,
4460 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4461 }
628fe4e4
JK
4462
4463 retval.main_action = max (retval.main_action, this_action);
c906108c 4464 }
628fe4e4 4465
0e30163f
JK
4466 /* These operations may affect the bs->breakpoint_at state so they are
4467 delayed after MAIN_ACTION is decided above. */
4468
628fe4e4
JK
4469 if (shlib_event)
4470 {
4471 if (debug_infrun)
4472 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4473
4474 /* Check for any newly added shared libraries if we're supposed
4475 to be adding them automatically. */
4476
4477 /* Switch terminal for any messages produced by
4478 breakpoint_re_set. */
4479 target_terminal_ours_for_output ();
4480
4481#ifdef SOLIB_ADD
4482 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4483#else
4484 solib_add (NULL, 0, &current_target, auto_solib_add);
4485#endif
4486
4487 target_terminal_inferior ();
4488 }
4489
4490 if (jit_event)
4491 {
4492 if (debug_infrun)
4493 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4494
4495 handle_jit_event ();
4496 }
4497
0e30163f
JK
4498 for (bs = bs_head; bs != NULL; bs = bs->next)
4499 {
4500 struct breakpoint *b = bs->breakpoint_at;
4501
4502 if (b == NULL)
4503 continue;
4504 switch (b->type)
4505 {
4506 case bp_gnu_ifunc_resolver:
4507 gnu_ifunc_resolver_stop (b);
4508 break;
4509 case bp_gnu_ifunc_resolver_return:
4510 gnu_ifunc_resolver_return_stop (b);
4511 break;
4512 }
4513 }
4514
c906108c
SS
4515 return retval;
4516}
4517
4518/* Nonzero if we should step constantly (e.g. watchpoints on machines
4519 without hardware support). This isn't related to a specific bpstat,
4520 just to things like whether watchpoints are set. */
4521
c5aa993b 4522int
fba45db2 4523bpstat_should_step (void)
c906108c
SS
4524{
4525 struct breakpoint *b;
cc59ec59 4526
c906108c 4527 ALL_BREAKPOINTS (b)
717a8278 4528 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4529 return 1;
c906108c
SS
4530 return 0;
4531}
4532
67822962
PA
4533int
4534bpstat_causes_stop (bpstat bs)
4535{
4536 for (; bs != NULL; bs = bs->next)
4537 if (bs->stop)
4538 return 1;
4539
4540 return 0;
4541}
4542
c906108c 4543\f
c5aa993b 4544
170b53b2
UW
4545/* Compute a string of spaces suitable to indent the next line
4546 so it starts at the position corresponding to the table column
4547 named COL_NAME in the currently active table of UIOUT. */
4548
4549static char *
4550wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4551{
4552 static char wrap_indent[80];
4553 int i, total_width, width, align;
4554 char *text;
4555
4556 total_width = 0;
4557 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4558 {
4559 if (strcmp (text, col_name) == 0)
4560 {
4561 gdb_assert (total_width < sizeof wrap_indent);
4562 memset (wrap_indent, ' ', total_width);
4563 wrap_indent[total_width] = 0;
4564
4565 return wrap_indent;
4566 }
4567
4568 total_width += width + 1;
4569 }
4570
4571 return NULL;
4572}
4573
859825b8
JK
4574/* Print the LOC location out of the list of B->LOC locations. */
4575
170b53b2
UW
4576static void
4577print_breakpoint_location (struct breakpoint *b,
4578 struct bp_location *loc)
0d381245 4579{
79a45e25 4580 struct ui_out *uiout = current_uiout;
6c95b8df
PA
4581 struct cleanup *old_chain = save_current_program_space ();
4582
859825b8
JK
4583 if (loc != NULL && loc->shlib_disabled)
4584 loc = NULL;
4585
6c95b8df
PA
4586 if (loc != NULL)
4587 set_current_program_space (loc->pspace);
4588
56435ebe
TT
4589 if (b->display_canonical)
4590 ui_out_field_string (uiout, "what", b->addr_string);
f8eba3c6 4591 else if (loc && loc->source_file)
0d381245
VP
4592 {
4593 struct symbol *sym
4594 = find_pc_sect_function (loc->address, loc->section);
4595 if (sym)
4596 {
4597 ui_out_text (uiout, "in ");
4598 ui_out_field_string (uiout, "func",
4599 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4600 ui_out_text (uiout, " ");
4601 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4602 ui_out_text (uiout, "at ");
0d381245 4603 }
f8eba3c6 4604 ui_out_field_string (uiout, "file", loc->source_file);
0d381245
VP
4605 ui_out_text (uiout, ":");
4606
4607 if (ui_out_is_mi_like_p (uiout))
4608 {
4609 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4610 char *fullname = symtab_to_fullname (sal.symtab);
4611
4612 if (fullname)
4613 ui_out_field_string (uiout, "fullname", fullname);
4614 }
4615
f8eba3c6 4616 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 4617 }
859825b8 4618 else if (loc)
0d381245 4619 {
170b53b2
UW
4620 struct ui_stream *stb = ui_out_stream_new (uiout);
4621 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4622
22e722e1
DJ
4623 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4624 demangle, "");
0d381245 4625 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4626
4627 do_cleanups (stb_chain);
0d381245 4628 }
859825b8
JK
4629 else
4630 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4631
4632 do_cleanups (old_chain);
0d381245
VP
4633}
4634
269b11a2
PA
4635static const char *
4636bptype_string (enum bptype type)
c906108c 4637{
c4093a6a
JM
4638 struct ep_type_description
4639 {
4640 enum bptype type;
4641 char *description;
4642 };
4643 static struct ep_type_description bptypes[] =
c906108c 4644 {
c5aa993b
JM
4645 {bp_none, "?deleted?"},
4646 {bp_breakpoint, "breakpoint"},
c906108c 4647 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4648 {bp_until, "until"},
4649 {bp_finish, "finish"},
4650 {bp_watchpoint, "watchpoint"},
c906108c 4651 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4652 {bp_read_watchpoint, "read watchpoint"},
4653 {bp_access_watchpoint, "acc watchpoint"},
4654 {bp_longjmp, "longjmp"},
4655 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4656 {bp_exception, "exception"},
4657 {bp_exception_resume, "exception resume"},
c5aa993b 4658 {bp_step_resume, "step resume"},
2c03e5be 4659 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4660 {bp_watchpoint_scope, "watchpoint scope"},
4661 {bp_call_dummy, "call dummy"},
aa7d318d 4662 {bp_std_terminate, "std::terminate"},
c5aa993b 4663 {bp_shlib_event, "shlib events"},
c4093a6a 4664 {bp_thread_event, "thread events"},
1900040c 4665 {bp_overlay_event, "overlay events"},
0fd8e87f 4666 {bp_longjmp_master, "longjmp master"},
aa7d318d 4667 {bp_std_terminate_master, "std::terminate master"},
186c406b 4668 {bp_exception_master, "exception master"},
ce78b96d 4669 {bp_catchpoint, "catchpoint"},
1042e4c0 4670 {bp_tracepoint, "tracepoint"},
7a697b8d 4671 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4672 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4673 {bp_jit_event, "jit events"},
0e30163f
JK
4674 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4675 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4676 };
269b11a2
PA
4677
4678 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4679 || ((int) type != bptypes[(int) type].type))
4680 internal_error (__FILE__, __LINE__,
4681 _("bptypes table does not describe type #%d."),
4682 (int) type);
4683
4684 return bptypes[(int) type].description;
4685}
4686
4687/* Print B to gdb_stdout. */
4688
4689static void
4690print_one_breakpoint_location (struct breakpoint *b,
4691 struct bp_location *loc,
4692 int loc_number,
4693 struct bp_location **last_loc,
269b11a2
PA
4694 int allflag)
4695{
4696 struct command_line *l;
c2c6d25f 4697 static char bpenables[] = "nynny";
c906108c 4698
79a45e25 4699 struct ui_out *uiout = current_uiout;
0d381245
VP
4700 int header_of_multiple = 0;
4701 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4702 struct value_print_options opts;
4703
4704 get_user_print_options (&opts);
0d381245
VP
4705
4706 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4707 /* See comment in print_one_breakpoint concerning treatment of
4708 breakpoints with single disabled location. */
0d381245
VP
4709 if (loc == NULL
4710 && (b->loc != NULL
4711 && (b->loc->next != NULL || !b->loc->enabled)))
4712 header_of_multiple = 1;
4713 if (loc == NULL)
4714 loc = b->loc;
4715
c4093a6a
JM
4716 annotate_record ();
4717
4718 /* 1 */
4719 annotate_field (0);
0d381245
VP
4720 if (part_of_multiple)
4721 {
4722 char *formatted;
0c6773c1 4723 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4724 ui_out_field_string (uiout, "number", formatted);
4725 xfree (formatted);
4726 }
4727 else
4728 {
4729 ui_out_field_int (uiout, "number", b->number);
4730 }
c4093a6a
JM
4731
4732 /* 2 */
4733 annotate_field (1);
0d381245
VP
4734 if (part_of_multiple)
4735 ui_out_field_skip (uiout, "type");
269b11a2
PA
4736 else
4737 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4738
4739 /* 3 */
4740 annotate_field (2);
0d381245
VP
4741 if (part_of_multiple)
4742 ui_out_field_skip (uiout, "disp");
4743 else
2cec12e5 4744 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4745
c4093a6a
JM
4746
4747 /* 4 */
4748 annotate_field (3);
0d381245 4749 if (part_of_multiple)
54e52265 4750 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4751 else
4a64f543
MS
4752 ui_out_field_fmt (uiout, "enabled", "%c",
4753 bpenables[(int) b->enable_state]);
54e52265 4754 ui_out_spaces (uiout, 2);
0d381245 4755
c4093a6a
JM
4756
4757 /* 5 and 6 */
3086aeae 4758 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4759 {
4a64f543
MS
4760 /* Although the print_one can possibly print all locations,
4761 calling it here is not likely to get any nice result. So,
4762 make sure there's just one location. */
0d381245 4763 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4764 b->ops->print_one (b, last_loc);
0d381245 4765 }
3086aeae
DJ
4766 else
4767 switch (b->type)
4768 {
4769 case bp_none:
4770 internal_error (__FILE__, __LINE__,
e2e0b3e5 4771 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4772 break;
c906108c 4773
3086aeae
DJ
4774 case bp_watchpoint:
4775 case bp_hardware_watchpoint:
4776 case bp_read_watchpoint:
4777 case bp_access_watchpoint:
3a5c3e22
PA
4778 {
4779 struct watchpoint *w = (struct watchpoint *) b;
4780
4781 /* Field 4, the address, is omitted (which makes the columns
4782 not line up too nicely with the headers, but the effect
4783 is relatively readable). */
4784 if (opts.addressprint)
4785 ui_out_field_skip (uiout, "addr");
4786 annotate_field (5);
4787 ui_out_field_string (uiout, "what", w->exp_string);
4788 }
3086aeae
DJ
4789 break;
4790
3086aeae
DJ
4791 case bp_breakpoint:
4792 case bp_hardware_breakpoint:
4793 case bp_until:
4794 case bp_finish:
4795 case bp_longjmp:
4796 case bp_longjmp_resume:
186c406b
TT
4797 case bp_exception:
4798 case bp_exception_resume:
3086aeae 4799 case bp_step_resume:
2c03e5be 4800 case bp_hp_step_resume:
3086aeae
DJ
4801 case bp_watchpoint_scope:
4802 case bp_call_dummy:
aa7d318d 4803 case bp_std_terminate:
3086aeae
DJ
4804 case bp_shlib_event:
4805 case bp_thread_event:
4806 case bp_overlay_event:
0fd8e87f 4807 case bp_longjmp_master:
aa7d318d 4808 case bp_std_terminate_master:
186c406b 4809 case bp_exception_master:
1042e4c0 4810 case bp_tracepoint:
7a697b8d 4811 case bp_fast_tracepoint:
0fb4aa4b 4812 case bp_static_tracepoint:
4efc6507 4813 case bp_jit_event:
0e30163f
JK
4814 case bp_gnu_ifunc_resolver:
4815 case bp_gnu_ifunc_resolver_return:
79a45b7d 4816 if (opts.addressprint)
3086aeae
DJ
4817 {
4818 annotate_field (4);
54e52265 4819 if (header_of_multiple)
0d381245 4820 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4821 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4822 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4823 else
5af949e3
UW
4824 ui_out_field_core_addr (uiout, "addr",
4825 loc->gdbarch, loc->address);
3086aeae
DJ
4826 }
4827 annotate_field (5);
0d381245 4828 if (!header_of_multiple)
170b53b2 4829 print_breakpoint_location (b, loc);
0d381245 4830 if (b->loc)
a6d9a66e 4831 *last_loc = b->loc;
3086aeae
DJ
4832 break;
4833 }
c906108c 4834
6c95b8df
PA
4835
4836 /* For backward compatibility, don't display inferiors unless there
4837 are several. */
4838 if (loc != NULL
4839 && !header_of_multiple
4840 && (allflag
4841 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4842 && (number_of_program_spaces () > 1
4843 || number_of_inferiors () > 1)
4a64f543
MS
4844 /* LOC is for existing B, it cannot be in
4845 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4846 && loc->owner->type != bp_catchpoint)))
4847 {
4848 struct inferior *inf;
4849 int first = 1;
4850
4851 for (inf = inferior_list; inf != NULL; inf = inf->next)
4852 {
4853 if (inf->pspace == loc->pspace)
4854 {
4855 if (first)
4856 {
4857 first = 0;
4858 ui_out_text (uiout, " inf ");
4859 }
4860 else
4861 ui_out_text (uiout, ", ");
4862 ui_out_text (uiout, plongest (inf->num));
4863 }
4864 }
4865 }
4866
4a306c9a 4867 if (!part_of_multiple)
c4093a6a 4868 {
4a306c9a
JB
4869 if (b->thread != -1)
4870 {
4871 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4872 "stop only in" line a little further down. */
4a306c9a
JB
4873 ui_out_text (uiout, " thread ");
4874 ui_out_field_int (uiout, "thread", b->thread);
4875 }
4876 else if (b->task != 0)
4877 {
4878 ui_out_text (uiout, " task ");
4879 ui_out_field_int (uiout, "task", b->task);
4880 }
c4093a6a 4881 }
f1310107 4882
8b93c638 4883 ui_out_text (uiout, "\n");
f1310107 4884
348d480f 4885 if (!part_of_multiple)
f1310107
TJB
4886 b->ops->print_one_detail (b, uiout);
4887
0d381245 4888 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4889 {
4890 annotate_field (6);
8b93c638 4891 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 4892 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 4893 the frame ID. */
5af949e3
UW
4894 ui_out_field_core_addr (uiout, "frame",
4895 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4896 ui_out_text (uiout, "\n");
c4093a6a
JM
4897 }
4898
28010a5d 4899 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
4900 {
4901 annotate_field (7);
d77f58be 4902 if (is_tracepoint (b))
1042e4c0
SS
4903 ui_out_text (uiout, "\ttrace only if ");
4904 else
4905 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4906 ui_out_field_string (uiout, "cond", b->cond_string);
4907 ui_out_text (uiout, "\n");
4908 }
4909
0d381245 4910 if (!part_of_multiple && b->thread != -1)
c4093a6a 4911 {
4a64f543 4912 /* FIXME should make an annotation for this. */
8b93c638
JM
4913 ui_out_text (uiout, "\tstop only in thread ");
4914 ui_out_field_int (uiout, "thread", b->thread);
4915 ui_out_text (uiout, "\n");
c4093a6a
JM
4916 }
4917
63c715c6 4918 if (!part_of_multiple && b->hit_count)
c4093a6a 4919 {
4a64f543 4920 /* FIXME should make an annotation for this. */
8b93c638
JM
4921 if (ep_is_catchpoint (b))
4922 ui_out_text (uiout, "\tcatchpoint");
f196051f
SS
4923 else if (is_tracepoint (b))
4924 ui_out_text (uiout, "\ttracepoint");
8b93c638
JM
4925 else
4926 ui_out_text (uiout, "\tbreakpoint");
4927 ui_out_text (uiout, " already hit ");
4928 ui_out_field_int (uiout, "times", b->hit_count);
4929 if (b->hit_count == 1)
4930 ui_out_text (uiout, " time\n");
4931 else
4932 ui_out_text (uiout, " times\n");
c4093a6a
JM
4933 }
4934
4a64f543
MS
4935 /* Output the count also if it is zero, but only if this is mi.
4936 FIXME: Should have a better test for this. */
9dc5e2a9 4937 if (ui_out_is_mi_like_p (uiout))
63c715c6 4938 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4939 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4940
0d381245 4941 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4942 {
4943 annotate_field (8);
8b93c638
JM
4944 ui_out_text (uiout, "\tignore next ");
4945 ui_out_field_int (uiout, "ignore", b->ignore_count);
4946 ui_out_text (uiout, " hits\n");
c4093a6a 4947 }
059fb39f 4948
f196051f
SS
4949 if (!part_of_multiple && is_tracepoint (b))
4950 {
4951 struct tracepoint *tp = (struct tracepoint *) b;
4952
4953 if (tp->traceframe_usage)
4954 {
4955 ui_out_text (uiout, "\ttrace buffer usage ");
4956 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
4957 ui_out_text (uiout, " bytes\n");
4958 }
4959 }
4960
9add0f1b 4961 l = b->commands ? b->commands->commands : NULL;
059fb39f 4962 if (!part_of_multiple && l)
c4093a6a 4963 {
3b31d625
EZ
4964 struct cleanup *script_chain;
4965
c4093a6a 4966 annotate_field (9);
3b31d625 4967 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4968 print_command_lines (uiout, l, 4);
3b31d625 4969 do_cleanups (script_chain);
c4093a6a 4970 }
d24317b4 4971
d9b3f62e 4972 if (is_tracepoint (b))
1042e4c0 4973 {
d9b3f62e
PA
4974 struct tracepoint *t = (struct tracepoint *) b;
4975
4976 if (!part_of_multiple && t->pass_count)
4977 {
4978 annotate_field (10);
4979 ui_out_text (uiout, "\tpass count ");
4980 ui_out_field_int (uiout, "pass", t->pass_count);
4981 ui_out_text (uiout, " \n");
4982 }
1042e4c0
SS
4983 }
4984
d24317b4
VP
4985 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4986 {
3a5c3e22
PA
4987 if (is_watchpoint (b))
4988 {
4989 struct watchpoint *w = (struct watchpoint *) b;
4990
4991 ui_out_field_string (uiout, "original-location", w->exp_string);
4992 }
4993 else if (b->addr_string)
d24317b4 4994 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 4995 }
c4093a6a 4996}
c5aa993b 4997
0d381245
VP
4998static void
4999print_one_breakpoint (struct breakpoint *b,
4a64f543 5000 struct bp_location **last_loc,
6c95b8df 5001 int allflag)
0d381245 5002{
8d3788bd 5003 struct cleanup *bkpt_chain;
79a45e25 5004 struct ui_out *uiout = current_uiout;
8d3788bd
VP
5005
5006 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5007
12c5a436 5008 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 5009 do_cleanups (bkpt_chain);
0d381245
VP
5010
5011 /* If this breakpoint has custom print function,
5012 it's already printed. Otherwise, print individual
5013 locations, if any. */
5014 if (b->ops == NULL || b->ops->print_one == NULL)
5015 {
4a64f543
MS
5016 /* If breakpoint has a single location that is disabled, we
5017 print it as if it had several locations, since otherwise it's
5018 hard to represent "breakpoint enabled, location disabled"
5019 situation.
5020
5021 Note that while hardware watchpoints have several locations
a3be7890 5022 internally, that's not a property exposed to user. */
0d381245 5023 if (b->loc
a5606eee 5024 && !is_hardware_watchpoint (b)
8d3788bd 5025 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5026 {
5027 struct bp_location *loc;
5028 int n = 1;
8d3788bd 5029
0d381245 5030 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5031 {
5032 struct cleanup *inner2 =
5033 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5034 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5035 do_cleanups (inner2);
5036 }
0d381245
VP
5037 }
5038 }
5039}
5040
a6d9a66e
UW
5041static int
5042breakpoint_address_bits (struct breakpoint *b)
5043{
5044 int print_address_bits = 0;
5045 struct bp_location *loc;
5046
5047 for (loc = b->loc; loc; loc = loc->next)
5048 {
c7437ca6
PA
5049 int addr_bit;
5050
5051 /* Software watchpoints that aren't watching memory don't have
5052 an address to print. */
5053 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5054 continue;
5055
5056 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5057 if (addr_bit > print_address_bits)
5058 print_address_bits = addr_bit;
5059 }
5060
5061 return print_address_bits;
5062}
0d381245 5063
c4093a6a
JM
5064struct captured_breakpoint_query_args
5065 {
5066 int bnum;
5067 };
c5aa993b 5068
c4093a6a 5069static int
2b65245e 5070do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5071{
5072 struct captured_breakpoint_query_args *args = data;
52f0bd74 5073 struct breakpoint *b;
a6d9a66e 5074 struct bp_location *dummy_loc = NULL;
cc59ec59 5075
c4093a6a
JM
5076 ALL_BREAKPOINTS (b)
5077 {
5078 if (args->bnum == b->number)
c5aa993b 5079 {
12c5a436 5080 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5081 return GDB_RC_OK;
c5aa993b 5082 }
c4093a6a
JM
5083 }
5084 return GDB_RC_NONE;
5085}
c5aa993b 5086
c4093a6a 5087enum gdb_rc
4a64f543
MS
5088gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5089 char **error_message)
c4093a6a
JM
5090{
5091 struct captured_breakpoint_query_args args;
cc59ec59 5092
c4093a6a
JM
5093 args.bnum = bnum;
5094 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5095 an error. */
b0b13bb4
DJ
5096 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5097 error_message, RETURN_MASK_ALL) < 0)
5098 return GDB_RC_FAIL;
5099 else
5100 return GDB_RC_OK;
c4093a6a 5101}
c5aa993b 5102
09d682a4
TT
5103/* Return true if this breakpoint was set by the user, false if it is
5104 internal or momentary. */
5105
5106int
5107user_breakpoint_p (struct breakpoint *b)
5108{
46c6471b 5109 return b->number > 0;
09d682a4
TT
5110}
5111
7f3b0473 5112/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5113 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5114 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5115 FILTER is non-NULL, call it on each breakpoint and only include the
5116 ones for which it returns non-zero. Return the total number of
5117 breakpoints listed. */
c906108c 5118
d77f58be 5119static int
e5a67952 5120breakpoint_1 (char *args, int allflag,
4a64f543 5121 int (*filter) (const struct breakpoint *))
c4093a6a 5122{
52f0bd74 5123 struct breakpoint *b;
a6d9a66e 5124 struct bp_location *last_loc = NULL;
7f3b0473 5125 int nr_printable_breakpoints;
3b31d625 5126 struct cleanup *bkpttbl_chain;
79a45b7d 5127 struct value_print_options opts;
a6d9a66e 5128 int print_address_bits = 0;
269b11a2 5129 int print_type_col_width = 14;
79a45e25 5130 struct ui_out *uiout = current_uiout;
269b11a2 5131
79a45b7d
TT
5132 get_user_print_options (&opts);
5133
4a64f543
MS
5134 /* Compute the number of rows in the table, as well as the size
5135 required for address fields. */
7f3b0473
AC
5136 nr_printable_breakpoints = 0;
5137 ALL_BREAKPOINTS (b)
e5a67952
MS
5138 {
5139 /* If we have a filter, only list the breakpoints it accepts. */
5140 if (filter && !filter (b))
5141 continue;
5142
5143 /* If we have an "args" string, it is a list of breakpoints to
5144 accept. Skip the others. */
5145 if (args != NULL && *args != '\0')
5146 {
5147 if (allflag && parse_and_eval_long (args) != b->number)
5148 continue;
5149 if (!allflag && !number_is_in_list (args, b->number))
5150 continue;
5151 }
269b11a2 5152
e5a67952
MS
5153 if (allflag || user_breakpoint_p (b))
5154 {
5155 int addr_bit, type_len;
a6d9a66e 5156
e5a67952
MS
5157 addr_bit = breakpoint_address_bits (b);
5158 if (addr_bit > print_address_bits)
5159 print_address_bits = addr_bit;
269b11a2 5160
e5a67952
MS
5161 type_len = strlen (bptype_string (b->type));
5162 if (type_len > print_type_col_width)
5163 print_type_col_width = type_len;
5164
5165 nr_printable_breakpoints++;
5166 }
5167 }
7f3b0473 5168
79a45b7d 5169 if (opts.addressprint)
3b31d625 5170 bkpttbl_chain
3e43a32a
MS
5171 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5172 nr_printable_breakpoints,
3b31d625 5173 "BreakpointTable");
8b93c638 5174 else
3b31d625 5175 bkpttbl_chain
3e43a32a
MS
5176 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5177 nr_printable_breakpoints,
3b31d625 5178 "BreakpointTable");
8b93c638 5179
7f3b0473 5180 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5181 annotate_breakpoints_headers ();
5182 if (nr_printable_breakpoints > 0)
5183 annotate_field (0);
4a64f543 5184 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5185 if (nr_printable_breakpoints > 0)
5186 annotate_field (1);
269b11a2 5187 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5188 "type", "Type"); /* 2 */
d7faa9e7
AC
5189 if (nr_printable_breakpoints > 0)
5190 annotate_field (2);
4a64f543 5191 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5192 if (nr_printable_breakpoints > 0)
5193 annotate_field (3);
54e52265 5194 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5195 if (opts.addressprint)
e5a67952
MS
5196 {
5197 if (nr_printable_breakpoints > 0)
5198 annotate_field (4);
5199 if (print_address_bits <= 32)
5200 ui_out_table_header (uiout, 10, ui_left,
5201 "addr", "Address"); /* 5 */
5202 else
5203 ui_out_table_header (uiout, 18, ui_left,
5204 "addr", "Address"); /* 5 */
5205 }
d7faa9e7
AC
5206 if (nr_printable_breakpoints > 0)
5207 annotate_field (5);
5208 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5209 ui_out_table_body (uiout);
5210 if (nr_printable_breakpoints > 0)
5211 annotate_breakpoints_table ();
7f3b0473 5212
c4093a6a 5213 ALL_BREAKPOINTS (b)
e5a67952
MS
5214 {
5215 QUIT;
5216 /* If we have a filter, only list the breakpoints it accepts. */
5217 if (filter && !filter (b))
5218 continue;
5219
5220 /* If we have an "args" string, it is a list of breakpoints to
5221 accept. Skip the others. */
5222
5223 if (args != NULL && *args != '\0')
5224 {
5225 if (allflag) /* maintenance info breakpoint */
5226 {
5227 if (parse_and_eval_long (args) != b->number)
5228 continue;
5229 }
5230 else /* all others */
5231 {
5232 if (!number_is_in_list (args, b->number))
5233 continue;
5234 }
5235 }
5236 /* We only print out user settable breakpoints unless the
5237 allflag is set. */
5238 if (allflag || user_breakpoint_p (b))
12c5a436 5239 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5240 }
5241
3b31d625 5242 do_cleanups (bkpttbl_chain);
698384cd 5243
7f3b0473 5244 if (nr_printable_breakpoints == 0)
c906108c 5245 {
4a64f543
MS
5246 /* If there's a filter, let the caller decide how to report
5247 empty list. */
d77f58be
SS
5248 if (!filter)
5249 {
e5a67952 5250 if (args == NULL || *args == '\0')
d77f58be
SS
5251 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5252 else
4a64f543 5253 ui_out_message (uiout, 0,
e5a67952
MS
5254 "No breakpoint or watchpoint matching '%s'.\n",
5255 args);
d77f58be 5256 }
c906108c
SS
5257 }
5258 else
c4093a6a 5259 {
a6d9a66e
UW
5260 if (last_loc && !server_command)
5261 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5262 }
c906108c 5263
4a64f543 5264 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5265 there have been breakpoints? */
c906108c 5266 annotate_breakpoints_table_end ();
d77f58be
SS
5267
5268 return nr_printable_breakpoints;
c906108c
SS
5269}
5270
ad443146
SS
5271/* Display the value of default-collect in a way that is generally
5272 compatible with the breakpoint list. */
5273
5274static void
5275default_collect_info (void)
5276{
79a45e25
PA
5277 struct ui_out *uiout = current_uiout;
5278
ad443146
SS
5279 /* If it has no value (which is frequently the case), say nothing; a
5280 message like "No default-collect." gets in user's face when it's
5281 not wanted. */
5282 if (!*default_collect)
5283 return;
5284
5285 /* The following phrase lines up nicely with per-tracepoint collect
5286 actions. */
5287 ui_out_text (uiout, "default collect ");
5288 ui_out_field_string (uiout, "default-collect", default_collect);
5289 ui_out_text (uiout, " \n");
5290}
5291
c906108c 5292static void
e5a67952 5293breakpoints_info (char *args, int from_tty)
c906108c 5294{
e5a67952 5295 breakpoint_1 (args, 0, NULL);
ad443146
SS
5296
5297 default_collect_info ();
d77f58be
SS
5298}
5299
5300static void
e5a67952 5301watchpoints_info (char *args, int from_tty)
d77f58be 5302{
e5a67952 5303 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 5304 struct ui_out *uiout = current_uiout;
d77f58be
SS
5305
5306 if (num_printed == 0)
5307 {
e5a67952 5308 if (args == NULL || *args == '\0')
d77f58be
SS
5309 ui_out_message (uiout, 0, "No watchpoints.\n");
5310 else
e5a67952 5311 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5312 }
c906108c
SS
5313}
5314
7a292a7a 5315static void
e5a67952 5316maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5317{
e5a67952 5318 breakpoint_1 (args, 1, NULL);
ad443146
SS
5319
5320 default_collect_info ();
c906108c
SS
5321}
5322
0d381245 5323static int
714835d5 5324breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5325 struct program_space *pspace,
714835d5 5326 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5327{
5328 struct bp_location *bl = b->loc;
cc59ec59 5329
0d381245
VP
5330 for (; bl; bl = bl->next)
5331 {
6c95b8df
PA
5332 if (bl->pspace == pspace
5333 && bl->address == pc
0d381245
VP
5334 && (!overlay_debugging || bl->section == section))
5335 return 1;
5336 }
5337 return 0;
5338}
5339
672f9b60 5340/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
5341 concerns with logical breakpoints, so we match program spaces, not
5342 address spaces. */
c906108c
SS
5343
5344static void
6c95b8df
PA
5345describe_other_breakpoints (struct gdbarch *gdbarch,
5346 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5347 struct obj_section *section, int thread)
c906108c 5348{
52f0bd74
AC
5349 int others = 0;
5350 struct breakpoint *b;
c906108c
SS
5351
5352 ALL_BREAKPOINTS (b)
672f9b60
KP
5353 others += (user_breakpoint_p (b)
5354 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
5355 if (others > 0)
5356 {
a3f17187
AC
5357 if (others == 1)
5358 printf_filtered (_("Note: breakpoint "));
5359 else /* if (others == ???) */
5360 printf_filtered (_("Note: breakpoints "));
c906108c 5361 ALL_BREAKPOINTS (b)
672f9b60 5362 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5363 {
5364 others--;
5365 printf_filtered ("%d", b->number);
5366 if (b->thread == -1 && thread != -1)
5367 printf_filtered (" (all threads)");
5368 else if (b->thread != -1)
5369 printf_filtered (" (thread %d)", b->thread);
5370 printf_filtered ("%s%s ",
059fb39f 5371 ((b->enable_state == bp_disabled
f8eba3c6 5372 || b->enable_state == bp_call_disabled)
0d381245
VP
5373 ? " (disabled)"
5374 : b->enable_state == bp_permanent
5375 ? " (permanent)"
5376 : ""),
5377 (others > 1) ? ","
5378 : ((others == 1) ? " and" : ""));
5379 }
a3f17187 5380 printf_filtered (_("also set at pc "));
5af949e3 5381 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5382 printf_filtered (".\n");
5383 }
5384}
5385\f
c906108c 5386
e4f237da
KB
5387/* Return true iff it is meaningful to use the address member of
5388 BPT. For some breakpoint types, the address member is irrelevant
5389 and it makes no sense to attempt to compare it to other addresses
5390 (or use it for any other purpose either).
5391
4a64f543
MS
5392 More specifically, each of the following breakpoint types will
5393 always have a zero valued address and we don't want to mark
5394 breakpoints of any of these types to be a duplicate of an actual
5395 breakpoint at address zero:
e4f237da
KB
5396
5397 bp_watchpoint
2d134ed3
PA
5398 bp_catchpoint
5399
5400*/
e4f237da
KB
5401
5402static int
5403breakpoint_address_is_meaningful (struct breakpoint *bpt)
5404{
5405 enum bptype type = bpt->type;
5406
2d134ed3
PA
5407 return (type != bp_watchpoint && type != bp_catchpoint);
5408}
5409
5410/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5411 true if LOC1 and LOC2 represent the same watchpoint location. */
5412
5413static int
4a64f543
MS
5414watchpoint_locations_match (struct bp_location *loc1,
5415 struct bp_location *loc2)
2d134ed3 5416{
3a5c3e22
PA
5417 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
5418 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
5419
5420 /* Both of them must exist. */
5421 gdb_assert (w1 != NULL);
5422 gdb_assert (w2 != NULL);
2bdf28a0 5423
4a64f543
MS
5424 /* If the target can evaluate the condition expression in hardware,
5425 then we we need to insert both watchpoints even if they are at
5426 the same place. Otherwise the watchpoint will only trigger when
5427 the condition of whichever watchpoint was inserted evaluates to
5428 true, not giving a chance for GDB to check the condition of the
5429 other watchpoint. */
3a5c3e22 5430 if ((w1->cond_exp
4a64f543
MS
5431 && target_can_accel_watchpoint_condition (loc1->address,
5432 loc1->length,
0cf6dd15 5433 loc1->watchpoint_type,
3a5c3e22
PA
5434 w1->cond_exp))
5435 || (w2->cond_exp
4a64f543
MS
5436 && target_can_accel_watchpoint_condition (loc2->address,
5437 loc2->length,
0cf6dd15 5438 loc2->watchpoint_type,
3a5c3e22 5439 w2->cond_exp)))
0cf6dd15
TJB
5440 return 0;
5441
85d721b8
PA
5442 /* Note that this checks the owner's type, not the location's. In
5443 case the target does not support read watchpoints, but does
5444 support access watchpoints, we'll have bp_read_watchpoint
5445 watchpoints with hw_access locations. Those should be considered
5446 duplicates of hw_read locations. The hw_read locations will
5447 become hw_access locations later. */
2d134ed3
PA
5448 return (loc1->owner->type == loc2->owner->type
5449 && loc1->pspace->aspace == loc2->pspace->aspace
5450 && loc1->address == loc2->address
5451 && loc1->length == loc2->length);
e4f237da
KB
5452}
5453
6c95b8df
PA
5454/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5455 same breakpoint location. In most targets, this can only be true
5456 if ASPACE1 matches ASPACE2. On targets that have global
5457 breakpoints, the address space doesn't really matter. */
5458
5459static int
5460breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5461 struct address_space *aspace2, CORE_ADDR addr2)
5462{
5463 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5464 || aspace1 == aspace2)
5465 && addr1 == addr2);
5466}
5467
f1310107
TJB
5468/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5469 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5470 matches ASPACE2. On targets that have global breakpoints, the address
5471 space doesn't really matter. */
5472
5473static int
5474breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5475 int len1, struct address_space *aspace2,
5476 CORE_ADDR addr2)
5477{
5478 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5479 || aspace1 == aspace2)
5480 && addr2 >= addr1 && addr2 < addr1 + len1);
5481}
5482
5483/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5484 a ranged breakpoint. In most targets, a match happens only if ASPACE
5485 matches the breakpoint's address space. On targets that have global
5486 breakpoints, the address space doesn't really matter. */
5487
5488static int
5489breakpoint_location_address_match (struct bp_location *bl,
5490 struct address_space *aspace,
5491 CORE_ADDR addr)
5492{
5493 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5494 aspace, addr)
5495 || (bl->length
5496 && breakpoint_address_match_range (bl->pspace->aspace,
5497 bl->address, bl->length,
5498 aspace, addr)));
5499}
5500
1e4d1764
YQ
5501/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
5502 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
5503 true, otherwise returns false. */
5504
5505static int
5506tracepoint_locations_match (struct bp_location *loc1,
5507 struct bp_location *loc2)
5508{
5509 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
5510 /* Since tracepoint locations are never duplicated with others', tracepoint
5511 locations at the same address of different tracepoints are regarded as
5512 different locations. */
5513 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
5514 else
5515 return 0;
5516}
5517
2d134ed3
PA
5518/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5519 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5520 represent the same location. */
5521
5522static int
4a64f543
MS
5523breakpoint_locations_match (struct bp_location *loc1,
5524 struct bp_location *loc2)
2d134ed3 5525{
2bdf28a0
JK
5526 int hw_point1, hw_point2;
5527
5528 /* Both of them must not be in moribund_locations. */
5529 gdb_assert (loc1->owner != NULL);
5530 gdb_assert (loc2->owner != NULL);
5531
5532 hw_point1 = is_hardware_watchpoint (loc1->owner);
5533 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5534
5535 if (hw_point1 != hw_point2)
5536 return 0;
5537 else if (hw_point1)
5538 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
5539 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
5540 return tracepoint_locations_match (loc1, loc2);
2d134ed3 5541 else
f1310107
TJB
5542 /* We compare bp_location.length in order to cover ranged breakpoints. */
5543 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5544 loc2->pspace->aspace, loc2->address)
5545 && loc1->length == loc2->length);
2d134ed3
PA
5546}
5547
76897487
KB
5548static void
5549breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5550 int bnum, int have_bnum)
5551{
f63fbe86
MS
5552 /* The longest string possibly returned by hex_string_custom
5553 is 50 chars. These must be at least that big for safety. */
5554 char astr1[64];
5555 char astr2[64];
76897487 5556
bb599908
PH
5557 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5558 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5559 if (have_bnum)
8a3fe4f8 5560 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5561 bnum, astr1, astr2);
5562 else
8a3fe4f8 5563 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5564}
5565
4a64f543
MS
5566/* Adjust a breakpoint's address to account for architectural
5567 constraints on breakpoint placement. Return the adjusted address.
5568 Note: Very few targets require this kind of adjustment. For most
5569 targets, this function is simply the identity function. */
76897487
KB
5570
5571static CORE_ADDR
a6d9a66e
UW
5572adjust_breakpoint_address (struct gdbarch *gdbarch,
5573 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5574{
a6d9a66e 5575 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5576 {
5577 /* Very few targets need any kind of breakpoint adjustment. */
5578 return bpaddr;
5579 }
88f7da05
KB
5580 else if (bptype == bp_watchpoint
5581 || bptype == bp_hardware_watchpoint
5582 || bptype == bp_read_watchpoint
5583 || bptype == bp_access_watchpoint
fe798b75 5584 || bptype == bp_catchpoint)
88f7da05
KB
5585 {
5586 /* Watchpoints and the various bp_catch_* eventpoints should not
5587 have their addresses modified. */
5588 return bpaddr;
5589 }
76897487
KB
5590 else
5591 {
5592 CORE_ADDR adjusted_bpaddr;
5593
5594 /* Some targets have architectural constraints on the placement
5595 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5596 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5597
5598 /* An adjusted breakpoint address can significantly alter
5599 a user's expectations. Print a warning if an adjustment
5600 is required. */
5601 if (adjusted_bpaddr != bpaddr)
5602 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5603
5604 return adjusted_bpaddr;
5605 }
5606}
5607
28010a5d
PA
5608void
5609init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5610 struct breakpoint *owner)
7cc221ef 5611{
7cc221ef
DJ
5612 memset (loc, 0, sizeof (*loc));
5613
348d480f
PA
5614 gdb_assert (ops != NULL);
5615
28010a5d
PA
5616 loc->ops = ops;
5617 loc->owner = owner;
511a6cd4 5618 loc->cond = NULL;
0d381245
VP
5619 loc->shlib_disabled = 0;
5620 loc->enabled = 1;
e049a4b5 5621
28010a5d 5622 switch (owner->type)
e049a4b5
DJ
5623 {
5624 case bp_breakpoint:
5625 case bp_until:
5626 case bp_finish:
5627 case bp_longjmp:
5628 case bp_longjmp_resume:
186c406b
TT
5629 case bp_exception:
5630 case bp_exception_resume:
e049a4b5 5631 case bp_step_resume:
2c03e5be 5632 case bp_hp_step_resume:
e049a4b5
DJ
5633 case bp_watchpoint_scope:
5634 case bp_call_dummy:
aa7d318d 5635 case bp_std_terminate:
e049a4b5
DJ
5636 case bp_shlib_event:
5637 case bp_thread_event:
5638 case bp_overlay_event:
4efc6507 5639 case bp_jit_event:
0fd8e87f 5640 case bp_longjmp_master:
aa7d318d 5641 case bp_std_terminate_master:
186c406b 5642 case bp_exception_master:
0e30163f
JK
5643 case bp_gnu_ifunc_resolver:
5644 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5645 loc->loc_type = bp_loc_software_breakpoint;
5646 break;
5647 case bp_hardware_breakpoint:
5648 loc->loc_type = bp_loc_hardware_breakpoint;
5649 break;
5650 case bp_hardware_watchpoint:
5651 case bp_read_watchpoint:
5652 case bp_access_watchpoint:
5653 loc->loc_type = bp_loc_hardware_watchpoint;
5654 break;
5655 case bp_watchpoint:
ce78b96d 5656 case bp_catchpoint:
15c3d785
PA
5657 case bp_tracepoint:
5658 case bp_fast_tracepoint:
0fb4aa4b 5659 case bp_static_tracepoint:
e049a4b5
DJ
5660 loc->loc_type = bp_loc_other;
5661 break;
5662 default:
e2e0b3e5 5663 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5664 }
5665
f431efe5 5666 loc->refc = 1;
28010a5d
PA
5667}
5668
5669/* Allocate a struct bp_location. */
5670
5671static struct bp_location *
5672allocate_bp_location (struct breakpoint *bpt)
5673{
348d480f
PA
5674 return bpt->ops->allocate_location (bpt);
5675}
7cc221ef 5676
f431efe5
PA
5677static void
5678free_bp_location (struct bp_location *loc)
fe3f5fa8 5679{
348d480f 5680 loc->ops->dtor (loc);
fe3f5fa8
VP
5681 xfree (loc);
5682}
5683
f431efe5
PA
5684/* Increment reference count. */
5685
5686static void
5687incref_bp_location (struct bp_location *bl)
5688{
5689 ++bl->refc;
5690}
5691
5692/* Decrement reference count. If the reference count reaches 0,
5693 destroy the bp_location. Sets *BLP to NULL. */
5694
5695static void
5696decref_bp_location (struct bp_location **blp)
5697{
0807b50c
PA
5698 gdb_assert ((*blp)->refc > 0);
5699
f431efe5
PA
5700 if (--(*blp)->refc == 0)
5701 free_bp_location (*blp);
5702 *blp = NULL;
5703}
5704
346774a9 5705/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5706
346774a9
PA
5707static void
5708add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5709{
346774a9 5710 struct breakpoint *b1;
c906108c 5711
346774a9
PA
5712 /* Add this breakpoint to the end of the chain so that a list of
5713 breakpoints will come out in order of increasing numbers. */
5714
5715 b1 = breakpoint_chain;
5716 if (b1 == 0)
5717 breakpoint_chain = b;
5718 else
5719 {
5720 while (b1->next)
5721 b1 = b1->next;
5722 b1->next = b;
5723 }
5724}
5725
5726/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5727
5728static void
5729init_raw_breakpoint_without_location (struct breakpoint *b,
5730 struct gdbarch *gdbarch,
28010a5d 5731 enum bptype bptype,
c0a91b2b 5732 const struct breakpoint_ops *ops)
346774a9 5733{
c906108c 5734 memset (b, 0, sizeof (*b));
2219d63c 5735
348d480f
PA
5736 gdb_assert (ops != NULL);
5737
28010a5d 5738 b->ops = ops;
4d28f7a8 5739 b->type = bptype;
a6d9a66e 5740 b->gdbarch = gdbarch;
c906108c
SS
5741 b->language = current_language->la_language;
5742 b->input_radix = input_radix;
5743 b->thread = -1;
b5de0fa7 5744 b->enable_state = bp_enabled;
c906108c
SS
5745 b->next = 0;
5746 b->silent = 0;
5747 b->ignore_count = 0;
5748 b->commands = NULL;
818dd999 5749 b->frame_id = null_frame_id;
0d381245 5750 b->condition_not_parsed = 0;
84f4c1fe 5751 b->py_bp_object = NULL;
d0fb5eae 5752 b->related_breakpoint = b;
346774a9
PA
5753}
5754
5755/* Helper to set_raw_breakpoint below. Creates a breakpoint
5756 that has type BPTYPE and has no locations as yet. */
346774a9
PA
5757
5758static struct breakpoint *
5759set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 5760 enum bptype bptype,
c0a91b2b 5761 const struct breakpoint_ops *ops)
346774a9
PA
5762{
5763 struct breakpoint *b = XNEW (struct breakpoint);
5764
348d480f 5765 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 5766 add_to_breakpoint_chain (b);
0d381245
VP
5767 return b;
5768}
5769
0e30163f
JK
5770/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5771 resolutions should be made as the user specified the location explicitly
5772 enough. */
5773
0d381245 5774static void
0e30163f 5775set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5776{
2bdf28a0
JK
5777 gdb_assert (loc->owner != NULL);
5778
0d381245 5779 if (loc->owner->type == bp_breakpoint
1042e4c0 5780 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5781 || is_tracepoint (loc->owner))
0d381245 5782 {
0e30163f
JK
5783 int is_gnu_ifunc;
5784
5785 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5786 NULL, NULL, &is_gnu_ifunc);
5787
5788 if (is_gnu_ifunc && !explicit_loc)
5789 {
5790 struct breakpoint *b = loc->owner;
5791
5792 gdb_assert (loc->pspace == current_program_space);
5793 if (gnu_ifunc_resolve_name (loc->function_name,
5794 &loc->requested_address))
5795 {
5796 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5797 loc->address = adjust_breakpoint_address (loc->gdbarch,
5798 loc->requested_address,
5799 b->type);
5800 }
5801 else if (b->type == bp_breakpoint && b->loc == loc
5802 && loc->next == NULL && b->related_breakpoint == b)
5803 {
5804 /* Create only the whole new breakpoint of this type but do not
5805 mess more complicated breakpoints with multiple locations. */
5806 b->type = bp_gnu_ifunc_resolver;
5807 }
5808 }
5809
0d381245
VP
5810 if (loc->function_name)
5811 loc->function_name = xstrdup (loc->function_name);
5812 }
5813}
5814
a6d9a66e 5815/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 5816struct gdbarch *
a6d9a66e
UW
5817get_sal_arch (struct symtab_and_line sal)
5818{
5819 if (sal.section)
5820 return get_objfile_arch (sal.section->objfile);
5821 if (sal.symtab)
5822 return get_objfile_arch (sal.symtab->objfile);
5823
5824 return NULL;
5825}
5826
346774a9
PA
5827/* Low level routine for partially initializing a breakpoint of type
5828 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5829 file name, and line number are provided by SAL.
0d381245
VP
5830
5831 It is expected that the caller will complete the initialization of
5832 the newly created breakpoint struct as well as output any status
c56053d2 5833 information regarding the creation of a new breakpoint. */
0d381245 5834
346774a9
PA
5835static void
5836init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 5837 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5838 const struct breakpoint_ops *ops)
0d381245 5839{
28010a5d 5840 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5841
3742cc8b 5842 add_location_to_breakpoint (b, &sal);
0d381245 5843
6c95b8df
PA
5844 if (bptype != bp_catchpoint)
5845 gdb_assert (sal.pspace != NULL);
5846
f8eba3c6
TT
5847 /* Store the program space that was used to set the breakpoint,
5848 except for ordinary breakpoints, which are independent of the
5849 program space. */
5850 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
5851 b->pspace = sal.pspace;
0d381245 5852
c906108c 5853 breakpoints_changed ();
346774a9 5854}
c906108c 5855
346774a9
PA
5856/* set_raw_breakpoint is a low level routine for allocating and
5857 partially initializing a breakpoint of type BPTYPE. The newly
5858 created breakpoint's address, section, source file name, and line
5859 number are provided by SAL. The newly created and partially
5860 initialized breakpoint is added to the breakpoint chain and
5861 is also returned as the value of this function.
5862
5863 It is expected that the caller will complete the initialization of
5864 the newly created breakpoint struct as well as output any status
5865 information regarding the creation of a new breakpoint. In
5866 particular, set_raw_breakpoint does NOT set the breakpoint
5867 number! Care should be taken to not allow an error to occur
5868 prior to completing the initialization of the breakpoint. If this
5869 should happen, a bogus breakpoint will be left on the chain. */
5870
5871struct breakpoint *
5872set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 5873 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5874 const struct breakpoint_ops *ops)
346774a9
PA
5875{
5876 struct breakpoint *b = XNEW (struct breakpoint);
5877
348d480f 5878 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 5879 add_to_breakpoint_chain (b);
c906108c
SS
5880 return b;
5881}
5882
c2c6d25f
JM
5883
5884/* Note that the breakpoint object B describes a permanent breakpoint
5885 instruction, hard-wired into the inferior's code. */
5886void
5887make_breakpoint_permanent (struct breakpoint *b)
5888{
0d381245 5889 struct bp_location *bl;
cc59ec59 5890
b5de0fa7 5891 b->enable_state = bp_permanent;
c2c6d25f 5892
4a64f543
MS
5893 /* By definition, permanent breakpoints are already present in the
5894 code. Mark all locations as inserted. For now,
5895 make_breakpoint_permanent is called in just one place, so it's
5896 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 5897 multiple locations or not, but it's easy to implement. */
0d381245
VP
5898 for (bl = b->loc; bl; bl = bl->next)
5899 bl->inserted = 1;
c2c6d25f
JM
5900}
5901
53a5351d 5902/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5903 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5904 initiated the operation. */
c906108c
SS
5905
5906void
186c406b 5907set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5908{
35df4500 5909 struct breakpoint *b, *b_tmp;
186c406b 5910 int thread = tp->num;
0fd8e87f
UW
5911
5912 /* To avoid having to rescan all objfile symbols at every step,
5913 we maintain a list of continually-inserted but always disabled
5914 longjmp "master" breakpoints. Here, we simply create momentary
5915 clones of those and enable them for the requested thread. */
35df4500 5916 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5917 if (b->pspace == current_program_space
186c406b
TT
5918 && (b->type == bp_longjmp_master
5919 || b->type == bp_exception_master))
0fd8e87f 5920 {
06edf0c0
PA
5921 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5922 struct breakpoint *clone;
cc59ec59 5923
06edf0c0
PA
5924 clone = momentary_breakpoint_from_master (b, type,
5925 &momentary_breakpoint_ops);
0fd8e87f
UW
5926 clone->thread = thread;
5927 }
186c406b
TT
5928
5929 tp->initiating_frame = frame;
c906108c
SS
5930}
5931
611c83ae 5932/* Delete all longjmp breakpoints from THREAD. */
c906108c 5933void
611c83ae 5934delete_longjmp_breakpoint (int thread)
c906108c 5935{
35df4500 5936 struct breakpoint *b, *b_tmp;
c906108c 5937
35df4500 5938 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5939 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5940 {
5941 if (b->thread == thread)
5942 delete_breakpoint (b);
5943 }
c906108c
SS
5944}
5945
1900040c
MS
5946void
5947enable_overlay_breakpoints (void)
5948{
52f0bd74 5949 struct breakpoint *b;
1900040c
MS
5950
5951 ALL_BREAKPOINTS (b)
5952 if (b->type == bp_overlay_event)
5953 {
5954 b->enable_state = bp_enabled;
b60e7edf 5955 update_global_location_list (1);
c02f5703 5956 overlay_events_enabled = 1;
1900040c
MS
5957 }
5958}
5959
5960void
5961disable_overlay_breakpoints (void)
5962{
52f0bd74 5963 struct breakpoint *b;
1900040c
MS
5964
5965 ALL_BREAKPOINTS (b)
5966 if (b->type == bp_overlay_event)
5967 {
5968 b->enable_state = bp_disabled;
b60e7edf 5969 update_global_location_list (0);
c02f5703 5970 overlay_events_enabled = 0;
1900040c
MS
5971 }
5972}
5973
aa7d318d
TT
5974/* Set an active std::terminate breakpoint for each std::terminate
5975 master breakpoint. */
5976void
5977set_std_terminate_breakpoint (void)
5978{
35df4500 5979 struct breakpoint *b, *b_tmp;
aa7d318d 5980
35df4500 5981 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5982 if (b->pspace == current_program_space
5983 && b->type == bp_std_terminate_master)
5984 {
06edf0c0
PA
5985 momentary_breakpoint_from_master (b, bp_std_terminate,
5986 &momentary_breakpoint_ops);
aa7d318d
TT
5987 }
5988}
5989
5990/* Delete all the std::terminate breakpoints. */
5991void
5992delete_std_terminate_breakpoint (void)
5993{
35df4500 5994 struct breakpoint *b, *b_tmp;
aa7d318d 5995
35df4500 5996 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5997 if (b->type == bp_std_terminate)
5998 delete_breakpoint (b);
5999}
6000
c4093a6a 6001struct breakpoint *
a6d9a66e 6002create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6003{
6004 struct breakpoint *b;
c4093a6a 6005
06edf0c0
PA
6006 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6007 &internal_breakpoint_ops);
6008
b5de0fa7 6009 b->enable_state = bp_enabled;
c4093a6a 6010 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6011 b->addr_string
6012 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6013
b60e7edf 6014 update_global_location_list_nothrow (1);
74960c60 6015
c4093a6a
JM
6016 return b;
6017}
6018
6019void
6020remove_thread_event_breakpoints (void)
6021{
35df4500 6022 struct breakpoint *b, *b_tmp;
c4093a6a 6023
35df4500 6024 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6025 if (b->type == bp_thread_event
6026 && b->loc->pspace == current_program_space)
c4093a6a
JM
6027 delete_breakpoint (b);
6028}
6029
0101ce28
JJ
6030struct lang_and_radix
6031 {
6032 enum language lang;
6033 int radix;
6034 };
6035
4efc6507
DE
6036/* Create a breakpoint for JIT code registration and unregistration. */
6037
6038struct breakpoint *
6039create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6040{
6041 struct breakpoint *b;
6042
06edf0c0
PA
6043 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
6044 &internal_breakpoint_ops);
4efc6507
DE
6045 update_global_location_list_nothrow (1);
6046 return b;
6047}
0101ce28 6048
03673fc7
PP
6049/* Remove JIT code registration and unregistration breakpoint(s). */
6050
6051void
6052remove_jit_event_breakpoints (void)
6053{
6054 struct breakpoint *b, *b_tmp;
6055
6056 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6057 if (b->type == bp_jit_event
6058 && b->loc->pspace == current_program_space)
6059 delete_breakpoint (b);
6060}
6061
cae688ec
JJ
6062void
6063remove_solib_event_breakpoints (void)
6064{
35df4500 6065 struct breakpoint *b, *b_tmp;
cae688ec 6066
35df4500 6067 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6068 if (b->type == bp_shlib_event
6069 && b->loc->pspace == current_program_space)
cae688ec
JJ
6070 delete_breakpoint (b);
6071}
6072
6073struct breakpoint *
a6d9a66e 6074create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6075{
6076 struct breakpoint *b;
6077
06edf0c0
PA
6078 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6079 &internal_breakpoint_ops);
b60e7edf 6080 update_global_location_list_nothrow (1);
cae688ec
JJ
6081 return b;
6082}
6083
6084/* Disable any breakpoints that are on code in shared libraries. Only
6085 apply to enabled breakpoints, disabled ones can just stay disabled. */
6086
6087void
cb851954 6088disable_breakpoints_in_shlibs (void)
cae688ec 6089{
876fa593 6090 struct bp_location *loc, **locp_tmp;
cae688ec 6091
876fa593 6092 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6093 {
2bdf28a0 6094 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6095 struct breakpoint *b = loc->owner;
2bdf28a0 6096
4a64f543
MS
6097 /* We apply the check to all breakpoints, including disabled for
6098 those with loc->duplicate set. This is so that when breakpoint
6099 becomes enabled, or the duplicate is removed, gdb will try to
6100 insert all breakpoints. If we don't set shlib_disabled here,
6101 we'll try to insert those breakpoints and fail. */
1042e4c0 6102 if (((b->type == bp_breakpoint)
508ccb1f 6103 || (b->type == bp_jit_event)
1042e4c0 6104 || (b->type == bp_hardware_breakpoint)
d77f58be 6105 || (is_tracepoint (b)))
6c95b8df 6106 && loc->pspace == current_program_space
0d381245 6107 && !loc->shlib_disabled
a77053c2 6108#ifdef PC_SOLIB
0d381245 6109 && PC_SOLIB (loc->address)
a77053c2 6110#else
6c95b8df 6111 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6112#endif
6113 )
0d381245
VP
6114 {
6115 loc->shlib_disabled = 1;
6116 }
cae688ec
JJ
6117 }
6118}
6119
1e4d1764
YQ
6120/* Disable any breakpoints and tracepoints that are in an unloaded shared
6121 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 6122 disabled. */
84acb35a 6123
75149521 6124static void
84acb35a
JJ
6125disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6126{
876fa593 6127 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6128 int disabled_shlib_breaks = 0;
6129
c86cf029
VP
6130 /* SunOS a.out shared libraries are always mapped, so do not
6131 disable breakpoints; they will only be reported as unloaded
6132 through clear_solib when GDB discards its shared library
6133 list. See clear_solib for more information. */
6134 if (exec_bfd != NULL
6135 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6136 return;
6137
876fa593 6138 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6139 {
2bdf28a0 6140 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6141 struct breakpoint *b = loc->owner;
cc59ec59 6142
1e4d1764 6143 if (solib->pspace == loc->pspace
e2dd7057 6144 && !loc->shlib_disabled
1e4d1764
YQ
6145 && (((b->type == bp_breakpoint
6146 || b->type == bp_jit_event
6147 || b->type == bp_hardware_breakpoint)
6148 && (loc->loc_type == bp_loc_hardware_breakpoint
6149 || loc->loc_type == bp_loc_software_breakpoint))
6150 || is_tracepoint (b))
e2dd7057 6151 && solib_contains_address_p (solib, loc->address))
84acb35a 6152 {
e2dd7057
PP
6153 loc->shlib_disabled = 1;
6154 /* At this point, we cannot rely on remove_breakpoint
6155 succeeding so we must mark the breakpoint as not inserted
6156 to prevent future errors occurring in remove_breakpoints. */
6157 loc->inserted = 0;
8d3788bd
VP
6158
6159 /* This may cause duplicate notifications for the same breakpoint. */
6160 observer_notify_breakpoint_modified (b);
6161
e2dd7057
PP
6162 if (!disabled_shlib_breaks)
6163 {
6164 target_terminal_ours_for_output ();
3e43a32a
MS
6165 warning (_("Temporarily disabling breakpoints "
6166 "for unloaded shared library \"%s\""),
e2dd7057 6167 solib->so_name);
84acb35a 6168 }
e2dd7057 6169 disabled_shlib_breaks = 1;
84acb35a
JJ
6170 }
6171 }
84acb35a
JJ
6172}
6173
ce78b96d
JB
6174/* FORK & VFORK catchpoints. */
6175
e29a4733
PA
6176/* An instance of this type is used to represent a fork or vfork
6177 catchpoint. It includes a "struct breakpoint" as a kind of base
6178 class; users downcast to "struct breakpoint *" when needed. A
6179 breakpoint is really of this type iff its ops pointer points to
6180 CATCH_FORK_BREAKPOINT_OPS. */
6181
6182struct fork_catchpoint
6183{
6184 /* The base class. */
6185 struct breakpoint base;
6186
6187 /* Process id of a child process whose forking triggered this
6188 catchpoint. This field is only valid immediately after this
6189 catchpoint has triggered. */
6190 ptid_t forked_inferior_pid;
6191};
6192
4a64f543
MS
6193/* Implement the "insert" breakpoint_ops method for fork
6194 catchpoints. */
ce78b96d 6195
77b06cd7
TJB
6196static int
6197insert_catch_fork (struct bp_location *bl)
ce78b96d 6198{
77b06cd7 6199 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6200}
6201
4a64f543
MS
6202/* Implement the "remove" breakpoint_ops method for fork
6203 catchpoints. */
ce78b96d
JB
6204
6205static int
77b06cd7 6206remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6207{
6208 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6209}
6210
6211/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6212 catchpoints. */
6213
6214static int
f1310107
TJB
6215breakpoint_hit_catch_fork (const struct bp_location *bl,
6216 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6217{
e29a4733
PA
6218 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6219
6220 return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6221}
6222
4a64f543
MS
6223/* Implement the "print_it" breakpoint_ops method for fork
6224 catchpoints. */
ce78b96d
JB
6225
6226static enum print_stop_action
348d480f 6227print_it_catch_fork (bpstat bs)
ce78b96d 6228{
36dfb11c 6229 struct ui_out *uiout = current_uiout;
348d480f
PA
6230 struct breakpoint *b = bs->breakpoint_at;
6231 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 6232
ce78b96d 6233 annotate_catchpoint (b->number);
36dfb11c
TT
6234 if (b->disposition == disp_del)
6235 ui_out_text (uiout, "\nTemporary catchpoint ");
6236 else
6237 ui_out_text (uiout, "\nCatchpoint ");
6238 if (ui_out_is_mi_like_p (uiout))
6239 {
6240 ui_out_field_string (uiout, "reason",
6241 async_reason_lookup (EXEC_ASYNC_FORK));
6242 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6243 }
6244 ui_out_field_int (uiout, "bkptno", b->number);
6245 ui_out_text (uiout, " (forked process ");
6246 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6247 ui_out_text (uiout, "), ");
ce78b96d
JB
6248 return PRINT_SRC_AND_LOC;
6249}
6250
4a64f543
MS
6251/* Implement the "print_one" breakpoint_ops method for fork
6252 catchpoints. */
ce78b96d
JB
6253
6254static void
a6d9a66e 6255print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6256{
e29a4733 6257 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6258 struct value_print_options opts;
79a45e25 6259 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6260
6261 get_user_print_options (&opts);
6262
4a64f543
MS
6263 /* Field 4, the address, is omitted (which makes the columns not
6264 line up too nicely with the headers, but the effect is relatively
6265 readable). */
79a45b7d 6266 if (opts.addressprint)
ce78b96d
JB
6267 ui_out_field_skip (uiout, "addr");
6268 annotate_field (5);
6269 ui_out_text (uiout, "fork");
e29a4733 6270 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6271 {
6272 ui_out_text (uiout, ", process ");
6273 ui_out_field_int (uiout, "what",
e29a4733 6274 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6275 ui_out_spaces (uiout, 1);
6276 }
6277}
6278
6279/* Implement the "print_mention" breakpoint_ops method for fork
6280 catchpoints. */
6281
6282static void
6283print_mention_catch_fork (struct breakpoint *b)
6284{
6285 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6286}
6287
6149aea9
PA
6288/* Implement the "print_recreate" breakpoint_ops method for fork
6289 catchpoints. */
6290
6291static void
6292print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6293{
6294 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 6295 print_recreate_thread (b, fp);
6149aea9
PA
6296}
6297
ce78b96d
JB
6298/* The breakpoint_ops structure to be used in fork catchpoints. */
6299
2060206e 6300static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 6301
4a64f543
MS
6302/* Implement the "insert" breakpoint_ops method for vfork
6303 catchpoints. */
ce78b96d 6304
77b06cd7
TJB
6305static int
6306insert_catch_vfork (struct bp_location *bl)
ce78b96d 6307{
77b06cd7 6308 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6309}
6310
4a64f543
MS
6311/* Implement the "remove" breakpoint_ops method for vfork
6312 catchpoints. */
ce78b96d
JB
6313
6314static int
77b06cd7 6315remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6316{
6317 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6318}
6319
6320/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6321 catchpoints. */
6322
6323static int
f1310107
TJB
6324breakpoint_hit_catch_vfork (const struct bp_location *bl,
6325 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6326{
e29a4733
PA
6327 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6328
6329 return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6330}
6331
4a64f543
MS
6332/* Implement the "print_it" breakpoint_ops method for vfork
6333 catchpoints. */
ce78b96d
JB
6334
6335static enum print_stop_action
348d480f 6336print_it_catch_vfork (bpstat bs)
ce78b96d 6337{
36dfb11c 6338 struct ui_out *uiout = current_uiout;
348d480f 6339 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
6340 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6341
ce78b96d 6342 annotate_catchpoint (b->number);
36dfb11c
TT
6343 if (b->disposition == disp_del)
6344 ui_out_text (uiout, "\nTemporary catchpoint ");
6345 else
6346 ui_out_text (uiout, "\nCatchpoint ");
6347 if (ui_out_is_mi_like_p (uiout))
6348 {
6349 ui_out_field_string (uiout, "reason",
6350 async_reason_lookup (EXEC_ASYNC_VFORK));
6351 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6352 }
6353 ui_out_field_int (uiout, "bkptno", b->number);
6354 ui_out_text (uiout, " (vforked process ");
6355 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
6356 ui_out_text (uiout, "), ");
ce78b96d
JB
6357 return PRINT_SRC_AND_LOC;
6358}
6359
4a64f543
MS
6360/* Implement the "print_one" breakpoint_ops method for vfork
6361 catchpoints. */
ce78b96d
JB
6362
6363static void
a6d9a66e 6364print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6365{
e29a4733 6366 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6367 struct value_print_options opts;
79a45e25 6368 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6369
6370 get_user_print_options (&opts);
4a64f543
MS
6371 /* Field 4, the address, is omitted (which makes the columns not
6372 line up too nicely with the headers, but the effect is relatively
6373 readable). */
79a45b7d 6374 if (opts.addressprint)
ce78b96d
JB
6375 ui_out_field_skip (uiout, "addr");
6376 annotate_field (5);
6377 ui_out_text (uiout, "vfork");
e29a4733 6378 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6379 {
6380 ui_out_text (uiout, ", process ");
6381 ui_out_field_int (uiout, "what",
e29a4733 6382 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6383 ui_out_spaces (uiout, 1);
6384 }
6385}
6386
6387/* Implement the "print_mention" breakpoint_ops method for vfork
6388 catchpoints. */
6389
6390static void
6391print_mention_catch_vfork (struct breakpoint *b)
6392{
6393 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6394}
6395
6149aea9
PA
6396/* Implement the "print_recreate" breakpoint_ops method for vfork
6397 catchpoints. */
6398
6399static void
6400print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6401{
6402 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 6403 print_recreate_thread (b, fp);
6149aea9
PA
6404}
6405
ce78b96d
JB
6406/* The breakpoint_ops structure to be used in vfork catchpoints. */
6407
2060206e 6408static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 6409
be5c67c1
PA
6410/* An instance of this type is used to represent a syscall catchpoint.
6411 It includes a "struct breakpoint" as a kind of base class; users
6412 downcast to "struct breakpoint *" when needed. A breakpoint is
6413 really of this type iff its ops pointer points to
6414 CATCH_SYSCALL_BREAKPOINT_OPS. */
6415
6416struct syscall_catchpoint
6417{
6418 /* The base class. */
6419 struct breakpoint base;
6420
6421 /* Syscall numbers used for the 'catch syscall' feature. If no
6422 syscall has been specified for filtering, its value is NULL.
6423 Otherwise, it holds a list of all syscalls to be caught. The
6424 list elements are allocated with xmalloc. */
6425 VEC(int) *syscalls_to_be_caught;
6426};
6427
6428/* Implement the "dtor" breakpoint_ops method for syscall
6429 catchpoints. */
6430
6431static void
6432dtor_catch_syscall (struct breakpoint *b)
6433{
6434 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6435
6436 VEC_free (int, c->syscalls_to_be_caught);
348d480f 6437
2060206e 6438 base_breakpoint_ops.dtor (b);
be5c67c1
PA
6439}
6440
a96d9b2e
SDJ
6441/* Implement the "insert" breakpoint_ops method for syscall
6442 catchpoints. */
6443
77b06cd7
TJB
6444static int
6445insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6446{
be5c67c1 6447 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6448 struct inferior *inf = current_inferior ();
6449
6450 ++inf->total_syscalls_count;
be5c67c1 6451 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6452 ++inf->any_syscall_count;
6453 else
6454 {
6455 int i, iter;
cc59ec59 6456
a96d9b2e 6457 for (i = 0;
be5c67c1 6458 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6459 i++)
6460 {
6461 int elem;
cc59ec59 6462
a96d9b2e
SDJ
6463 if (iter >= VEC_length (int, inf->syscalls_counts))
6464 {
6465 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6466 uintptr_t vec_addr_offset
6467 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6468 uintptr_t vec_addr;
6469 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6470 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6471 vec_addr_offset;
6472 memset ((void *) vec_addr, 0,
6473 (iter + 1 - old_size) * sizeof (int));
6474 }
6475 elem = VEC_index (int, inf->syscalls_counts, iter);
6476 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6477 }
6478 }
6479
77b06cd7
TJB
6480 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6481 inf->total_syscalls_count != 0,
6482 inf->any_syscall_count,
6483 VEC_length (int, inf->syscalls_counts),
6484 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6485}
6486
6487/* Implement the "remove" breakpoint_ops method for syscall
6488 catchpoints. */
6489
6490static int
77b06cd7 6491remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6492{
be5c67c1 6493 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6494 struct inferior *inf = current_inferior ();
6495
6496 --inf->total_syscalls_count;
be5c67c1 6497 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6498 --inf->any_syscall_count;
6499 else
6500 {
6501 int i, iter;
cc59ec59 6502
a96d9b2e 6503 for (i = 0;
be5c67c1 6504 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6505 i++)
6506 {
6507 int elem;
6508 if (iter >= VEC_length (int, inf->syscalls_counts))
6509 /* Shouldn't happen. */
6510 continue;
6511 elem = VEC_index (int, inf->syscalls_counts, iter);
6512 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6513 }
6514 }
6515
6516 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6517 inf->total_syscalls_count != 0,
6518 inf->any_syscall_count,
6519 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6520 VEC_address (int,
6521 inf->syscalls_counts));
a96d9b2e
SDJ
6522}
6523
6524/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6525 catchpoints. */
6526
6527static int
f1310107
TJB
6528breakpoint_hit_catch_syscall (const struct bp_location *bl,
6529 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6530{
4a64f543
MS
6531 /* We must check if we are catching specific syscalls in this
6532 breakpoint. If we are, then we must guarantee that the called
6533 syscall is the same syscall we are catching. */
a96d9b2e 6534 int syscall_number = 0;
be5c67c1
PA
6535 const struct syscall_catchpoint *c
6536 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6537
6538 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6539 return 0;
6540
6541 /* Now, checking if the syscall is the same. */
be5c67c1 6542 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6543 {
6544 int i, iter;
cc59ec59 6545
a96d9b2e 6546 for (i = 0;
be5c67c1 6547 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6548 i++)
6549 if (syscall_number == iter)
6550 break;
6551 /* Not the same. */
6552 if (!iter)
6553 return 0;
6554 }
6555
6556 return 1;
6557}
6558
6559/* Implement the "print_it" breakpoint_ops method for syscall
6560 catchpoints. */
6561
6562static enum print_stop_action
348d480f 6563print_it_catch_syscall (bpstat bs)
a96d9b2e 6564{
36dfb11c 6565 struct ui_out *uiout = current_uiout;
348d480f 6566 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
6567 /* These are needed because we want to know in which state a
6568 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6569 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6570 must print "called syscall" or "returned from syscall". */
6571 ptid_t ptid;
6572 struct target_waitstatus last;
6573 struct syscall s;
a96d9b2e
SDJ
6574 char *syscall_id;
6575
6576 get_last_target_status (&ptid, &last);
6577
6578 get_syscall_by_number (last.value.syscall_number, &s);
6579
6580 annotate_catchpoint (b->number);
6581
36dfb11c
TT
6582 if (b->disposition == disp_del)
6583 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 6584 else
36dfb11c
TT
6585 ui_out_text (uiout, "\nCatchpoint ");
6586 if (ui_out_is_mi_like_p (uiout))
6587 {
6588 ui_out_field_string (uiout, "reason",
6589 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
6590 ? EXEC_ASYNC_SYSCALL_ENTRY
6591 : EXEC_ASYNC_SYSCALL_RETURN));
6592 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6593 }
6594 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
6595
6596 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
6597 ui_out_text (uiout, " (call to syscall ");
6598 else
6599 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 6600
36dfb11c
TT
6601 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
6602 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
6603 if (s.name != NULL)
6604 ui_out_field_string (uiout, "syscall-name", s.name);
6605
6606 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
6607
6608 return PRINT_SRC_AND_LOC;
6609}
6610
6611/* Implement the "print_one" breakpoint_ops method for syscall
6612 catchpoints. */
6613
6614static void
6615print_one_catch_syscall (struct breakpoint *b,
f1310107 6616 struct bp_location **last_loc)
a96d9b2e 6617{
be5c67c1 6618 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 6619 struct value_print_options opts;
79a45e25 6620 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
6621
6622 get_user_print_options (&opts);
4a64f543
MS
6623 /* Field 4, the address, is omitted (which makes the columns not
6624 line up too nicely with the headers, but the effect is relatively
6625 readable). */
a96d9b2e
SDJ
6626 if (opts.addressprint)
6627 ui_out_field_skip (uiout, "addr");
6628 annotate_field (5);
6629
be5c67c1
PA
6630 if (c->syscalls_to_be_caught
6631 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6632 ui_out_text (uiout, "syscalls \"");
6633 else
6634 ui_out_text (uiout, "syscall \"");
6635
be5c67c1 6636 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6637 {
6638 int i, iter;
6639 char *text = xstrprintf ("%s", "");
cc59ec59 6640
a96d9b2e 6641 for (i = 0;
be5c67c1 6642 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6643 i++)
6644 {
6645 char *x = text;
6646 struct syscall s;
6647 get_syscall_by_number (iter, &s);
6648
6649 if (s.name != NULL)
6650 text = xstrprintf ("%s%s, ", text, s.name);
6651 else
6652 text = xstrprintf ("%s%d, ", text, iter);
6653
6654 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 6655 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
6656 on every call. */
6657 xfree (x);
6658 }
6659 /* Remove the last comma. */
6660 text[strlen (text) - 2] = '\0';
6661 ui_out_field_string (uiout, "what", text);
6662 }
6663 else
6664 ui_out_field_string (uiout, "what", "<any syscall>");
6665 ui_out_text (uiout, "\" ");
6666}
6667
6668/* Implement the "print_mention" breakpoint_ops method for syscall
6669 catchpoints. */
6670
6671static void
6672print_mention_catch_syscall (struct breakpoint *b)
6673{
be5c67c1
PA
6674 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6675
6676 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6677 {
6678 int i, iter;
6679
be5c67c1 6680 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6681 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6682 else
6683 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6684
6685 for (i = 0;
be5c67c1 6686 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6687 i++)
6688 {
6689 struct syscall s;
6690 get_syscall_by_number (iter, &s);
6691
6692 if (s.name)
6693 printf_filtered (" '%s' [%d]", s.name, s.number);
6694 else
6695 printf_filtered (" %d", s.number);
6696 }
6697 printf_filtered (")");
6698 }
6699 else
6700 printf_filtered (_("Catchpoint %d (any syscall)"),
6701 b->number);
6702}
6703
6149aea9
PA
6704/* Implement the "print_recreate" breakpoint_ops method for syscall
6705 catchpoints. */
6706
6707static void
6708print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6709{
be5c67c1
PA
6710 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6711
6149aea9
PA
6712 fprintf_unfiltered (fp, "catch syscall");
6713
be5c67c1 6714 if (c->syscalls_to_be_caught)
6149aea9
PA
6715 {
6716 int i, iter;
6717
6718 for (i = 0;
be5c67c1 6719 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
6720 i++)
6721 {
6722 struct syscall s;
6723
6724 get_syscall_by_number (iter, &s);
6725 if (s.name)
6726 fprintf_unfiltered (fp, " %s", s.name);
6727 else
6728 fprintf_unfiltered (fp, " %d", s.number);
6729 }
6730 }
d9b3f62e 6731 print_recreate_thread (b, fp);
6149aea9
PA
6732}
6733
a96d9b2e
SDJ
6734/* The breakpoint_ops structure to be used in syscall catchpoints. */
6735
2060206e 6736static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
6737
6738/* Returns non-zero if 'b' is a syscall catchpoint. */
6739
6740static int
6741syscall_catchpoint_p (struct breakpoint *b)
6742{
6743 return (b->ops == &catch_syscall_breakpoint_ops);
6744}
6745
346774a9
PA
6746/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
6747 is non-zero, then make the breakpoint temporary. If COND_STRING is
6748 not NULL, then store it in the breakpoint. OPS, if not NULL, is
6749 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 6750
346774a9
PA
6751static void
6752init_catchpoint (struct breakpoint *b,
6753 struct gdbarch *gdbarch, int tempflag,
6754 char *cond_string,
c0a91b2b 6755 const struct breakpoint_ops *ops)
c906108c 6756{
c5aa993b 6757 struct symtab_and_line sal;
346774a9 6758
fe39c653 6759 init_sal (&sal);
6c95b8df 6760 sal.pspace = current_program_space;
c5aa993b 6761
28010a5d 6762 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 6763
1b36a34b 6764 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 6765 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
6766}
6767
28010a5d 6768void
3ea46bff 6769install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
6770{
6771 add_to_breakpoint_chain (b);
3a5c3e22
PA
6772 set_breakpoint_number (internal, b);
6773 if (!internal)
6774 mention (b);
c56053d2 6775 observer_notify_breakpoint_created (b);
3ea46bff
YQ
6776
6777 if (update_gll)
6778 update_global_location_list (1);
c56053d2
PA
6779}
6780
9b70b993 6781static void
a6d9a66e
UW
6782create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6783 int tempflag, char *cond_string,
c0a91b2b 6784 const struct breakpoint_ops *ops)
c906108c 6785{
e29a4733 6786 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 6787
e29a4733
PA
6788 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6789
6790 c->forked_inferior_pid = null_ptid;
6791
3ea46bff 6792 install_breakpoint (0, &c->base, 1);
c906108c
SS
6793}
6794
fe798b75
JB
6795/* Exec catchpoints. */
6796
b4d90040
PA
6797/* An instance of this type is used to represent an exec catchpoint.
6798 It includes a "struct breakpoint" as a kind of base class; users
6799 downcast to "struct breakpoint *" when needed. A breakpoint is
6800 really of this type iff its ops pointer points to
6801 CATCH_EXEC_BREAKPOINT_OPS. */
6802
6803struct exec_catchpoint
6804{
6805 /* The base class. */
6806 struct breakpoint base;
6807
6808 /* Filename of a program whose exec triggered this catchpoint.
6809 This field is only valid immediately after this catchpoint has
6810 triggered. */
6811 char *exec_pathname;
6812};
6813
6814/* Implement the "dtor" breakpoint_ops method for exec
6815 catchpoints. */
6816
6817static void
6818dtor_catch_exec (struct breakpoint *b)
6819{
6820 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6821
6822 xfree (c->exec_pathname);
348d480f 6823
2060206e 6824 base_breakpoint_ops.dtor (b);
b4d90040
PA
6825}
6826
77b06cd7
TJB
6827static int
6828insert_catch_exec (struct bp_location *bl)
c906108c 6829{
77b06cd7 6830 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6831}
c906108c 6832
fe798b75 6833static int
77b06cd7 6834remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6835{
6836 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6837}
c906108c 6838
fe798b75 6839static int
f1310107
TJB
6840breakpoint_hit_catch_exec (const struct bp_location *bl,
6841 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6842{
b4d90040
PA
6843 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6844
6845 return inferior_has_execd (inferior_ptid, &c->exec_pathname);
fe798b75 6846}
c906108c 6847
fe798b75 6848static enum print_stop_action
348d480f 6849print_it_catch_exec (bpstat bs)
fe798b75 6850{
36dfb11c 6851 struct ui_out *uiout = current_uiout;
348d480f 6852 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
6853 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6854
fe798b75 6855 annotate_catchpoint (b->number);
36dfb11c
TT
6856 if (b->disposition == disp_del)
6857 ui_out_text (uiout, "\nTemporary catchpoint ");
6858 else
6859 ui_out_text (uiout, "\nCatchpoint ");
6860 if (ui_out_is_mi_like_p (uiout))
6861 {
6862 ui_out_field_string (uiout, "reason",
6863 async_reason_lookup (EXEC_ASYNC_EXEC));
6864 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6865 }
6866 ui_out_field_int (uiout, "bkptno", b->number);
6867 ui_out_text (uiout, " (exec'd ");
6868 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
6869 ui_out_text (uiout, "), ");
6870
fe798b75 6871 return PRINT_SRC_AND_LOC;
c906108c
SS
6872}
6873
fe798b75 6874static void
a6d9a66e 6875print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 6876{
b4d90040 6877 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 6878 struct value_print_options opts;
79a45e25 6879 struct ui_out *uiout = current_uiout;
fe798b75
JB
6880
6881 get_user_print_options (&opts);
6882
6883 /* Field 4, the address, is omitted (which makes the columns
6884 not line up too nicely with the headers, but the effect
6885 is relatively readable). */
6886 if (opts.addressprint)
6887 ui_out_field_skip (uiout, "addr");
6888 annotate_field (5);
6889 ui_out_text (uiout, "exec");
b4d90040 6890 if (c->exec_pathname != NULL)
fe798b75
JB
6891 {
6892 ui_out_text (uiout, ", program \"");
b4d90040 6893 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
6894 ui_out_text (uiout, "\" ");
6895 }
6896}
6897
6898static void
6899print_mention_catch_exec (struct breakpoint *b)
6900{
6901 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6902}
6903
6149aea9
PA
6904/* Implement the "print_recreate" breakpoint_ops method for exec
6905 catchpoints. */
6906
6907static void
6908print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6909{
6910 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 6911 print_recreate_thread (b, fp);
6149aea9
PA
6912}
6913
2060206e 6914static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 6915
a96d9b2e
SDJ
6916static void
6917create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 6918 const struct breakpoint_ops *ops)
a96d9b2e 6919{
be5c67c1 6920 struct syscall_catchpoint *c;
a96d9b2e 6921 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 6922
be5c67c1
PA
6923 c = XNEW (struct syscall_catchpoint);
6924 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
6925 c->syscalls_to_be_caught = filter;
a96d9b2e 6926
3ea46bff 6927 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
6928}
6929
c906108c 6930static int
fba45db2 6931hw_breakpoint_used_count (void)
c906108c 6932{
c906108c 6933 int i = 0;
f1310107
TJB
6934 struct breakpoint *b;
6935 struct bp_location *bl;
c906108c
SS
6936
6937 ALL_BREAKPOINTS (b)
c5aa993b 6938 {
d6b74ac4 6939 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6940 for (bl = b->loc; bl; bl = bl->next)
6941 {
6942 /* Special types of hardware breakpoints may use more than
6943 one register. */
348d480f 6944 i += b->ops->resources_needed (bl);
f1310107 6945 }
c5aa993b 6946 }
c906108c
SS
6947
6948 return i;
6949}
6950
a1398e0c
PA
6951/* Returns the resources B would use if it were a hardware
6952 watchpoint. */
6953
c906108c 6954static int
a1398e0c 6955hw_watchpoint_use_count (struct breakpoint *b)
c906108c 6956{
c906108c 6957 int i = 0;
e09342b5 6958 struct bp_location *bl;
c906108c 6959
a1398e0c
PA
6960 if (!breakpoint_enabled (b))
6961 return 0;
6962
6963 for (bl = b->loc; bl; bl = bl->next)
6964 {
6965 /* Special types of hardware watchpoints may use more than
6966 one register. */
6967 i += b->ops->resources_needed (bl);
6968 }
6969
6970 return i;
6971}
6972
6973/* Returns the sum the used resources of all hardware watchpoints of
6974 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
6975 the sum of the used resources of all hardware watchpoints of other
6976 types _not_ TYPE. */
6977
6978static int
6979hw_watchpoint_used_count_others (struct breakpoint *except,
6980 enum bptype type, int *other_type_used)
6981{
6982 int i = 0;
6983 struct breakpoint *b;
6984
c906108c
SS
6985 *other_type_used = 0;
6986 ALL_BREAKPOINTS (b)
e09342b5 6987 {
a1398e0c
PA
6988 if (b == except)
6989 continue;
e09342b5
TJB
6990 if (!breakpoint_enabled (b))
6991 continue;
6992
a1398e0c
PA
6993 if (b->type == type)
6994 i += hw_watchpoint_use_count (b);
6995 else if (is_hardware_watchpoint (b))
6996 *other_type_used = 1;
e09342b5
TJB
6997 }
6998
c906108c
SS
6999 return i;
7000}
7001
c906108c 7002void
fba45db2 7003disable_watchpoints_before_interactive_call_start (void)
c906108c 7004{
c5aa993b 7005 struct breakpoint *b;
c906108c
SS
7006
7007 ALL_BREAKPOINTS (b)
c5aa993b 7008 {
cc60f2e3 7009 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 7010 {
b5de0fa7 7011 b->enable_state = bp_call_disabled;
b60e7edf 7012 update_global_location_list (0);
c5aa993b
JM
7013 }
7014 }
c906108c
SS
7015}
7016
7017void
fba45db2 7018enable_watchpoints_after_interactive_call_stop (void)
c906108c 7019{
c5aa993b 7020 struct breakpoint *b;
c906108c
SS
7021
7022 ALL_BREAKPOINTS (b)
c5aa993b 7023 {
cc60f2e3 7024 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 7025 {
b5de0fa7 7026 b->enable_state = bp_enabled;
b60e7edf 7027 update_global_location_list (1);
c5aa993b
JM
7028 }
7029 }
c906108c
SS
7030}
7031
8bea4e01
UW
7032void
7033disable_breakpoints_before_startup (void)
7034{
6c95b8df 7035 current_program_space->executing_startup = 1;
f8eba3c6 7036 update_global_location_list (0);
8bea4e01
UW
7037}
7038
7039void
7040enable_breakpoints_after_startup (void)
7041{
6c95b8df 7042 current_program_space->executing_startup = 0;
f8eba3c6 7043 breakpoint_re_set ();
8bea4e01
UW
7044}
7045
c906108c
SS
7046
7047/* Set a breakpoint that will evaporate an end of command
7048 at address specified by SAL.
7049 Restrict it to frame FRAME if FRAME is nonzero. */
7050
7051struct breakpoint *
a6d9a66e
UW
7052set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7053 struct frame_id frame_id, enum bptype type)
c906108c 7054{
52f0bd74 7055 struct breakpoint *b;
edb3359d
DJ
7056
7057 /* If FRAME_ID is valid, it should be a real frame, not an inlined
7058 one. */
7059 gdb_assert (!frame_id_inlined_p (frame_id));
7060
06edf0c0 7061 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
7062 b->enable_state = bp_enabled;
7063 b->disposition = disp_donttouch;
818dd999 7064 b->frame_id = frame_id;
c906108c 7065
4a64f543
MS
7066 /* If we're debugging a multi-threaded program, then we want
7067 momentary breakpoints to be active in only a single thread of
7068 control. */
39f77062
KB
7069 if (in_thread_list (inferior_ptid))
7070 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 7071
b60e7edf 7072 update_global_location_list_nothrow (1);
74960c60 7073
c906108c
SS
7074 return b;
7075}
611c83ae 7076
06edf0c0
PA
7077/* Make a momentary breakpoint based on the master breakpoint ORIG.
7078 The new breakpoint will have type TYPE, and use OPS as it
7079 breakpoint_ops. */
e58b0e63 7080
06edf0c0
PA
7081static struct breakpoint *
7082momentary_breakpoint_from_master (struct breakpoint *orig,
7083 enum bptype type,
c0a91b2b 7084 const struct breakpoint_ops *ops)
e58b0e63
PA
7085{
7086 struct breakpoint *copy;
7087
06edf0c0 7088 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 7089 copy->loc = allocate_bp_location (copy);
0e30163f 7090 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 7091
a6d9a66e 7092 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7093 copy->loc->requested_address = orig->loc->requested_address;
7094 copy->loc->address = orig->loc->address;
7095 copy->loc->section = orig->loc->section;
6c95b8df 7096 copy->loc->pspace = orig->loc->pspace;
e58b0e63 7097
f8eba3c6
TT
7098 if (orig->loc->source_file != NULL)
7099 copy->loc->source_file = xstrdup (orig->loc->source_file);
e58b0e63 7100
f8eba3c6 7101 copy->loc->line_number = orig->loc->line_number;
e58b0e63
PA
7102 copy->frame_id = orig->frame_id;
7103 copy->thread = orig->thread;
6c95b8df 7104 copy->pspace = orig->pspace;
e58b0e63
PA
7105
7106 copy->enable_state = bp_enabled;
7107 copy->disposition = disp_donttouch;
7108 copy->number = internal_breakpoint_number--;
7109
7110 update_global_location_list_nothrow (0);
7111 return copy;
7112}
7113
06edf0c0
PA
7114/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7115 ORIG is NULL. */
7116
7117struct breakpoint *
7118clone_momentary_breakpoint (struct breakpoint *orig)
7119{
7120 /* If there's nothing to clone, then return nothing. */
7121 if (orig == NULL)
7122 return NULL;
7123
7124 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
7125}
7126
611c83ae 7127struct breakpoint *
a6d9a66e
UW
7128set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7129 enum bptype type)
611c83ae
PA
7130{
7131 struct symtab_and_line sal;
7132
7133 sal = find_pc_line (pc, 0);
7134 sal.pc = pc;
7135 sal.section = find_pc_overlay (pc);
7136 sal.explicit_pc = 1;
7137
a6d9a66e 7138 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7139}
c906108c 7140\f
c5aa993b 7141
c906108c
SS
7142/* Tell the user we have just set a breakpoint B. */
7143
7144static void
fba45db2 7145mention (struct breakpoint *b)
c906108c 7146{
348d480f 7147 b->ops->print_mention (b);
79a45e25 7148 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 7149 return;
c906108c
SS
7150 printf_filtered ("\n");
7151}
c906108c 7152\f
c5aa993b 7153
0d381245 7154static struct bp_location *
39d61571 7155add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7156 const struct symtab_and_line *sal)
7157{
7158 struct bp_location *loc, **tmp;
3742cc8b
YQ
7159 CORE_ADDR adjusted_address;
7160 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
7161
7162 if (loc_gdbarch == NULL)
7163 loc_gdbarch = b->gdbarch;
7164
7165 /* Adjust the breakpoint's address prior to allocating a location.
7166 Once we call allocate_bp_location(), that mostly uninitialized
7167 location will be placed on the location chain. Adjustment of the
7168 breakpoint may cause target_read_memory() to be called and we do
7169 not want its scan of the location chain to find a breakpoint and
7170 location that's only been partially initialized. */
7171 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
7172 sal->pc, b->type);
0d381245 7173
39d61571 7174 loc = allocate_bp_location (b);
0d381245
VP
7175 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7176 ;
7177 *tmp = loc;
3742cc8b 7178
0d381245 7179 loc->requested_address = sal->pc;
3742cc8b 7180 loc->address = adjusted_address;
6c95b8df
PA
7181 loc->pspace = sal->pspace;
7182 gdb_assert (loc->pspace != NULL);
0d381245 7183 loc->section = sal->section;
3742cc8b 7184 loc->gdbarch = loc_gdbarch;
f8eba3c6
TT
7185
7186 if (sal->symtab != NULL)
7187 loc->source_file = xstrdup (sal->symtab->filename);
7188 loc->line_number = sal->line;
7189
0e30163f
JK
7190 set_breakpoint_location_function (loc,
7191 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7192 return loc;
7193}
514f746b
AR
7194\f
7195
7196/* Return 1 if LOC is pointing to a permanent breakpoint,
7197 return 0 otherwise. */
7198
7199static int
7200bp_loc_is_permanent (struct bp_location *loc)
7201{
7202 int len;
7203 CORE_ADDR addr;
1afeeb75 7204 const gdb_byte *bpoint;
514f746b 7205 gdb_byte *target_mem;
939c61fa
JK
7206 struct cleanup *cleanup;
7207 int retval = 0;
514f746b
AR
7208
7209 gdb_assert (loc != NULL);
7210
7211 addr = loc->address;
1afeeb75 7212 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7213
939c61fa 7214 /* Software breakpoints unsupported? */
1afeeb75 7215 if (bpoint == NULL)
939c61fa
JK
7216 return 0;
7217
514f746b
AR
7218 target_mem = alloca (len);
7219
939c61fa
JK
7220 /* Enable the automatic memory restoration from breakpoints while
7221 we read the memory. Otherwise we could say about our temporary
7222 breakpoints they are permanent. */
6c95b8df
PA
7223 cleanup = save_current_space_and_thread ();
7224
7225 switch_to_program_space_and_thread (loc->pspace);
7226 make_show_memory_breakpoints_cleanup (0);
939c61fa 7227
514f746b 7228 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 7229 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 7230 retval = 1;
514f746b 7231
939c61fa
JK
7232 do_cleanups (cleanup);
7233
7234 return retval;
514f746b
AR
7235}
7236
7237
c3f6f71d 7238
018d34a4
VP
7239/* Create a breakpoint with SAL as location. Use ADDR_STRING
7240 as textual description of the location, and COND_STRING
db107f19 7241 as condition expression. */
018d34a4
VP
7242
7243static void
d9b3f62e
PA
7244init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
7245 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 7246 char *filter, char *cond_string,
d9b3f62e
PA
7247 enum bptype type, enum bpdisp disposition,
7248 int thread, int task, int ignore_count,
c0a91b2b 7249 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e 7250 int enabled, int internal, int display_canonical)
018d34a4 7251{
0d381245 7252 int i;
018d34a4
VP
7253
7254 if (type == bp_hardware_breakpoint)
7255 {
fbbd034e
AS
7256 int target_resources_ok;
7257
7258 i = hw_breakpoint_used_count ();
7259 target_resources_ok =
7260 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7261 i + 1, 0);
7262 if (target_resources_ok == 0)
7263 error (_("No hardware breakpoint support in the target."));
7264 else if (target_resources_ok < 0)
7265 error (_("Hardware breakpoints used exceeds limit."));
7266 }
7267
6c95b8df
PA
7268 gdb_assert (sals.nelts > 0);
7269
0d381245
VP
7270 for (i = 0; i < sals.nelts; ++i)
7271 {
7272 struct symtab_and_line sal = sals.sals[i];
7273 struct bp_location *loc;
7274
7275 if (from_tty)
5af949e3
UW
7276 {
7277 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7278 if (!loc_gdbarch)
7279 loc_gdbarch = gdbarch;
7280
7281 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7282 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7283 }
0d381245
VP
7284
7285 if (i == 0)
7286 {
d9b3f62e 7287 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 7288 b->thread = thread;
4a306c9a 7289 b->task = task;
018d34a4 7290
0d381245
VP
7291 b->cond_string = cond_string;
7292 b->ignore_count = ignore_count;
41447f92 7293 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7294 b->disposition = disposition;
6c95b8df 7295
0fb4aa4b
PA
7296 if (type == bp_static_tracepoint)
7297 {
d9b3f62e 7298 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
7299 struct static_tracepoint_marker marker;
7300
7301 if (is_marker_spec (addr_string))
7302 {
7303 /* We already know the marker exists, otherwise, we
7304 wouldn't see a sal for it. */
7305 char *p = &addr_string[3];
7306 char *endp;
7307 char *marker_str;
0fb4aa4b 7308
e9cafbcc 7309 p = skip_spaces (p);
0fb4aa4b 7310
e9cafbcc 7311 endp = skip_to_space (p);
0fb4aa4b
PA
7312
7313 marker_str = savestring (p, endp - p);
d9b3f62e 7314 t->static_trace_marker_id = marker_str;
0fb4aa4b 7315
3e43a32a
MS
7316 printf_filtered (_("Probed static tracepoint "
7317 "marker \"%s\"\n"),
d9b3f62e 7318 t->static_trace_marker_id);
0fb4aa4b
PA
7319 }
7320 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7321 {
d9b3f62e 7322 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
7323 release_static_tracepoint_marker (&marker);
7324
3e43a32a
MS
7325 printf_filtered (_("Probed static tracepoint "
7326 "marker \"%s\"\n"),
d9b3f62e 7327 t->static_trace_marker_id);
0fb4aa4b
PA
7328 }
7329 else
3e43a32a
MS
7330 warning (_("Couldn't determine the static "
7331 "tracepoint marker to probe"));
0fb4aa4b
PA
7332 }
7333
0d381245
VP
7334 loc = b->loc;
7335 }
7336 else
018d34a4 7337 {
39d61571 7338 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7339 }
7340
514f746b
AR
7341 if (bp_loc_is_permanent (loc))
7342 make_breakpoint_permanent (b);
7343
0d381245
VP
7344 if (b->cond_string)
7345 {
7346 char *arg = b->cond_string;
d32a6982 7347 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7348 if (*arg)
db107f19 7349 error (_("Garbage %s follows condition"), arg);
018d34a4 7350 }
0d381245 7351 }
018d34a4 7352
56435ebe 7353 b->display_canonical = display_canonical;
018d34a4
VP
7354 if (addr_string)
7355 b->addr_string = addr_string;
7356 else
7357 /* addr_string has to be used or breakpoint_re_set will delete
7358 me. */
5af949e3
UW
7359 b->addr_string
7360 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 7361 b->filter = filter;
d9b3f62e 7362}
018d34a4 7363
d9b3f62e
PA
7364static void
7365create_breakpoint_sal (struct gdbarch *gdbarch,
7366 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 7367 char *filter, char *cond_string,
d9b3f62e
PA
7368 enum bptype type, enum bpdisp disposition,
7369 int thread, int task, int ignore_count,
c0a91b2b 7370 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e
PA
7371 int enabled, int internal, int display_canonical)
7372{
7373 struct breakpoint *b;
7374 struct cleanup *old_chain;
7375
7376 if (is_tracepoint_type (type))
7377 {
7378 struct tracepoint *t;
7379
7380 t = XCNEW (struct tracepoint);
7381 b = &t->base;
7382 }
7383 else
7384 b = XNEW (struct breakpoint);
7385
7386 old_chain = make_cleanup (xfree, b);
7387
7388 init_breakpoint_sal (b, gdbarch,
7389 sals, addr_string,
f8eba3c6 7390 filter, cond_string,
d9b3f62e
PA
7391 type, disposition,
7392 thread, task, ignore_count,
7393 ops, from_tty,
7394 enabled, internal, display_canonical);
7395 discard_cleanups (old_chain);
7396
3ea46bff 7397 install_breakpoint (internal, b, 0);
018d34a4
VP
7398}
7399
7400/* Add SALS.nelts breakpoints to the breakpoint table. For each
7401 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7402 value. COND_STRING, if not NULL, specified the condition to be
7403 used for all breakpoints. Essentially the only case where
7404 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7405 function. In that case, it's still not possible to specify
7406 separate conditions for different overloaded functions, so
7407 we take just a single condition string.
7408
c3f6f71d 7409 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7410 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7411 array contents). If the function fails (error() is called), the
7412 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7413 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7414
7415static void
8cdf0e15 7416create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 7417 struct linespec_result *canonical,
8cdf0e15
VP
7418 char *cond_string,
7419 enum bptype type, enum bpdisp disposition,
7420 int thread, int task, int ignore_count,
c0a91b2b 7421 const struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7422 int enabled, int internal)
c906108c 7423{
018d34a4 7424 int i;
f8eba3c6 7425 struct linespec_sals *lsal;
cc59ec59 7426
f8eba3c6
TT
7427 if (canonical->pre_expanded)
7428 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
7429
7430 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 7431 {
f8eba3c6
TT
7432 /* Note that 'addr_string' can be NULL in the case of a plain
7433 'break', without arguments. */
7434 char *addr_string = (canonical->addr_string
7435 ? xstrdup (canonical->addr_string)
7436 : NULL);
7437 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
7438 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 7439
f8eba3c6
TT
7440 make_cleanup (xfree, filter_string);
7441 create_breakpoint_sal (gdbarch, lsal->sals,
7442 addr_string,
7443 filter_string,
8cdf0e15 7444 cond_string, type, disposition,
84f4c1fe 7445 thread, task, ignore_count, ops,
56435ebe
TT
7446 from_tty, enabled, internal,
7447 canonical->special_display);
f8eba3c6 7448 discard_cleanups (inner);
c3f6f71d 7449 }
c3f6f71d 7450}
c906108c 7451
9998af43 7452/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7453 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7454 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7455 address strings. ADDRESS points to the end of the SAL.
7456
7457 The array and the line spec strings are allocated on the heap, it is
7458 the caller's responsibility to free them. */
c906108c 7459
b9362cc7 7460static void
c3f6f71d 7461parse_breakpoint_sals (char **address,
58438ac1 7462 struct linespec_result *canonical)
c3f6f71d
JM
7463{
7464 char *addr_start = *address;
cc59ec59 7465
c3f6f71d 7466 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7467 breakpoint. */
c3f6f71d
JM
7468 if ((*address) == NULL
7469 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 7470 {
1bfeeb0f
JL
7471 /* The last displayed codepoint, if it's valid, is our default breakpoint
7472 address. */
7473 if (last_displayed_sal_is_valid ())
c906108c 7474 {
f8eba3c6 7475 struct linespec_sals lsal;
c3f6f71d 7476 struct symtab_and_line sal;
cc59ec59 7477
4a64f543 7478 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 7479 lsal.sals.sals = (struct symtab_and_line *)
c906108c 7480 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
7481
7482 /* Set sal's pspace, pc, symtab, and line to the values
7483 corresponding to the last call to print_frame_info. */
7484 get_last_displayed_sal (&sal);
7485 sal.section = find_pc_overlay (sal.pc);
00903456 7486
4a64f543 7487 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
7488 where PC is the last displayed codepoint's address. So
7489 make sure to set sal.explicit_pc to prevent GDB from
7490 trying to expand the list of sals to include all other
7491 instances with the same symtab and line. */
00903456
JK
7492 sal.explicit_pc = 1;
7493
f8eba3c6
TT
7494 lsal.sals.sals[0] = sal;
7495 lsal.sals.nelts = 1;
7496 lsal.canonical = NULL;
7497
7498 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
7499 }
7500 else
8a3fe4f8 7501 error (_("No default breakpoint address now."));
c906108c
SS
7502 }
7503 else
7504 {
c906108c 7505 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7506 current_source_symtab (which is decode_line_1's default).
7507 This should produce the results we want almost all of the
f8eba3c6
TT
7508 time while leaving default_breakpoint_* alone. */
7509 if (last_displayed_sal_is_valid ())
7510 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
7511 get_last_displayed_symtab (),
7512 get_last_displayed_line (),
7513 canonical, NULL, NULL);
c906108c 7514 else
f8eba3c6
TT
7515 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
7516 (struct symtab *) NULL, 0,
7517 canonical, NULL, NULL);
c906108c 7518 }
c3f6f71d 7519}
c906108c 7520
c906108c 7521
c3f6f71d 7522/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7523 inserted as a breakpoint. If it can't throw an error. */
c906108c 7524
b9362cc7 7525static void
23e7acfb 7526breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7527{
7528 int i;
cc59ec59 7529
c3f6f71d 7530 for (i = 0; i < sals->nelts; i++)
ee53e872 7531 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7532}
7533
7a697b8d
SS
7534/* Fast tracepoints may have restrictions on valid locations. For
7535 instance, a fast tracepoint using a jump instead of a trap will
7536 likely have to overwrite more bytes than a trap would, and so can
7537 only be placed where the instruction is longer than the jump, or a
7538 multi-instruction sequence does not have a jump into the middle of
7539 it, etc. */
7540
7541static void
7542check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7543 struct symtabs_and_lines *sals)
7544{
7545 int i, rslt;
7546 struct symtab_and_line *sal;
7547 char *msg;
7548 struct cleanup *old_chain;
7549
7550 for (i = 0; i < sals->nelts; i++)
7551 {
f8eba3c6
TT
7552 struct gdbarch *sarch;
7553
7a697b8d
SS
7554 sal = &sals->sals[i];
7555
f8eba3c6
TT
7556 sarch = get_sal_arch (*sal);
7557 /* We fall back to GDBARCH if there is no architecture
7558 associated with SAL. */
7559 if (sarch == NULL)
7560 sarch = gdbarch;
7561 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
7562 NULL, &msg);
7563 old_chain = make_cleanup (xfree, msg);
7564
7565 if (!rslt)
7566 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 7567 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
7568
7569 do_cleanups (old_chain);
7570 }
7571}
7572
018d34a4
VP
7573/* Given TOK, a string specification of condition and thread, as
7574 accepted by the 'break' command, extract the condition
7575 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7576 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7577 If no condition is found, *COND_STRING is set to NULL.
7578 If no thread is found, *THREAD is set to -1. */
7579static void
7580find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7581 char **cond_string, int *thread, int *task)
018d34a4
VP
7582{
7583 *cond_string = NULL;
7584 *thread = -1;
7585 while (tok && *tok)
7586 {
7587 char *end_tok;
7588 int toklen;
7589 char *cond_start = NULL;
7590 char *cond_end = NULL;
cc59ec59 7591
e9cafbcc 7592 tok = skip_spaces (tok);
018d34a4 7593
e9cafbcc 7594 end_tok = skip_to_space (tok);
018d34a4
VP
7595
7596 toklen = end_tok - tok;
7597
7598 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7599 {
f7545552
TT
7600 struct expression *expr;
7601
018d34a4 7602 tok = cond_start = end_tok + 1;
f7545552
TT
7603 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7604 xfree (expr);
018d34a4
VP
7605 cond_end = tok;
7606 *cond_string = savestring (cond_start,
7607 cond_end - cond_start);
7608 }
7609 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7610 {
7611 char *tmptok;
7612
7613 tok = end_tok + 1;
7614 tmptok = tok;
7615 *thread = strtol (tok, &tok, 0);
7616 if (tok == tmptok)
7617 error (_("Junk after thread keyword."));
7618 if (!valid_thread_id (*thread))
7619 error (_("Unknown thread %d."), *thread);
7620 }
4a306c9a
JB
7621 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7622 {
7623 char *tmptok;
7624
7625 tok = end_tok + 1;
7626 tmptok = tok;
7627 *task = strtol (tok, &tok, 0);
7628 if (tok == tmptok)
7629 error (_("Junk after task keyword."));
7630 if (!valid_task_id (*task))
b6199126 7631 error (_("Unknown task %d."), *task);
4a306c9a 7632 }
018d34a4
VP
7633 else
7634 error (_("Junk at end of arguments."));
7635 }
7636}
7637
0fb4aa4b
PA
7638/* Decode a static tracepoint marker spec. */
7639
7640static struct symtabs_and_lines
7641decode_static_tracepoint_spec (char **arg_p)
7642{
7643 VEC(static_tracepoint_marker_p) *markers = NULL;
7644 struct symtabs_and_lines sals;
7645 struct symtab_and_line sal;
7646 struct symbol *sym;
7647 struct cleanup *old_chain;
7648 char *p = &(*arg_p)[3];
7649 char *endp;
7650 char *marker_str;
7651 int i;
7652
e9cafbcc 7653 p = skip_spaces (p);
0fb4aa4b 7654
e9cafbcc 7655 endp = skip_to_space (p);
0fb4aa4b
PA
7656
7657 marker_str = savestring (p, endp - p);
7658 old_chain = make_cleanup (xfree, marker_str);
7659
7660 markers = target_static_tracepoint_markers_by_strid (marker_str);
7661 if (VEC_empty(static_tracepoint_marker_p, markers))
7662 error (_("No known static tracepoint marker named %s"), marker_str);
7663
7664 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7665 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7666
7667 for (i = 0; i < sals.nelts; i++)
7668 {
7669 struct static_tracepoint_marker *marker;
7670
7671 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7672
7673 init_sal (&sals.sals[i]);
7674
7675 sals.sals[i] = find_pc_line (marker->address, 0);
7676 sals.sals[i].pc = marker->address;
7677
7678 release_static_tracepoint_marker (marker);
7679 }
7680
7681 do_cleanups (old_chain);
7682
7683 *arg_p = endp;
7684 return sals;
7685}
7686
fd9b8c24
PA
7687/* Set a breakpoint. This function is shared between CLI and MI
7688 functions for setting a breakpoint. This function has two major
7689 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7690 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7691 breakpoint location, address and thread. Otherwise, ARG is just
7692 the location of breakpoint, with condition and thread specified by
7693 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7694 the breakpoint number will be allocated from the internal
7695 breakpoint count. Returns true if any breakpoint was created;
7696 false otherwise. */
0101ce28 7697
8cdf0e15
VP
7698int
7699create_breakpoint (struct gdbarch *gdbarch,
7700 char *arg, char *cond_string, int thread,
7701 int parse_condition_and_thread,
0fb4aa4b 7702 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7703 int ignore_count,
7704 enum auto_boolean pending_break_support,
c0a91b2b 7705 const struct breakpoint_ops *ops,
84f4c1fe 7706 int from_tty, int enabled, int internal)
c3f6f71d 7707{
b78a6381 7708 volatile struct gdb_exception e;
f8eba3c6 7709 char *copy_arg = NULL;
c3f6f71d 7710 char *addr_start = arg;
7efd8fc2 7711 struct linespec_result canonical;
c3f6f71d 7712 struct cleanup *old_chain;
80c99de1 7713 struct cleanup *bkpt_chain = NULL;
05ff989b 7714 int i;
0101ce28 7715 int pending = 0;
4a306c9a 7716 int task = 0;
86b17b60 7717 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7718
348d480f
PA
7719 gdb_assert (ops != NULL);
7720
7efd8fc2 7721 init_linespec_result (&canonical);
c3f6f71d 7722
0fb4aa4b
PA
7723 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7724 {
7725 int i;
f8eba3c6 7726 struct linespec_sals lsal;
0fb4aa4b 7727
f8eba3c6 7728 lsal.sals = decode_static_tracepoint_spec (&arg);
0fb4aa4b
PA
7729
7730 copy_arg = savestring (addr_start, arg - addr_start);
f8eba3c6 7731
d14c4eb7 7732 canonical.addr_string = xstrdup (copy_arg);
f8eba3c6
TT
7733 lsal.canonical = xstrdup (copy_arg);
7734 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
7735
0fb4aa4b
PA
7736 goto done;
7737 }
7738
b78a6381
TT
7739 TRY_CATCH (e, RETURN_MASK_ALL)
7740 {
f8eba3c6 7741 parse_breakpoint_sals (&arg, &canonical);
b78a6381 7742 }
0101ce28
JJ
7743
7744 /* If caller is interested in rc value from parse, set value. */
05ff989b 7745 switch (e.reason)
0101ce28 7746 {
05ff989b 7747 case RETURN_QUIT:
98deb0da 7748 throw_exception (e);
05ff989b
AC
7749 case RETURN_ERROR:
7750 switch (e.error)
0101ce28 7751 {
05ff989b 7752 case NOT_FOUND_ERROR:
0101ce28 7753
05ff989b
AC
7754 /* If pending breakpoint support is turned off, throw
7755 error. */
fa8d40ab
JJ
7756
7757 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7758 throw_exception (e);
7759
7760 exception_print (gdb_stderr, e);
fa8d40ab 7761
05ff989b
AC
7762 /* If pending breakpoint support is auto query and the user
7763 selects no, then simply return the error code. */
059fb39f 7764 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
7765 && !nquery (_("Make %s pending on future shared library load? "),
7766 bptype_string (type_wanted)))
fd9b8c24 7767 return 0;
fa8d40ab 7768
05ff989b
AC
7769 /* At this point, either the user was queried about setting
7770 a pending breakpoint and selected yes, or pending
7771 breakpoint behavior is on and thus a pending breakpoint
7772 is defaulted on behalf of the user. */
f8eba3c6
TT
7773 {
7774 struct linespec_sals lsal;
7775
7776 copy_arg = xstrdup (addr_start);
7777 lsal.canonical = xstrdup (copy_arg);
7778 lsal.sals.nelts = 1;
7779 lsal.sals.sals = XNEW (struct symtab_and_line);
7780 init_sal (&lsal.sals.sals[0]);
7781 pending = 1;
7782 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
7783 }
05ff989b
AC
7784 break;
7785 default:
98deb0da 7786 throw_exception (e);
0101ce28 7787 }
2abae994 7788 break;
05ff989b 7789 default:
f8eba3c6 7790 if (VEC_empty (linespec_sals, canonical.sals))
fd9b8c24 7791 return 0;
0101ce28 7792 }
c3f6f71d 7793
0fb4aa4b
PA
7794 done:
7795
4a64f543 7796 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 7797 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 7798
c3f6f71d
JM
7799 /* ----------------------------- SNIP -----------------------------
7800 Anything added to the cleanup chain beyond this point is assumed
7801 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7802 then the memory is not reclaimed. */
7803 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7804
c3f6f71d
JM
7805 /* Resolve all line numbers to PC's and verify that the addresses
7806 are ok for the target. */
0101ce28 7807 if (!pending)
f8eba3c6
TT
7808 {
7809 int ix;
7810 struct linespec_sals *iter;
7811
7812 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
7813 breakpoint_sals_to_pc (&iter->sals);
7814 }
c3f6f71d 7815
7a697b8d 7816 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 7817 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
7818 {
7819 int ix;
7820 struct linespec_sals *iter;
7821
7822 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
7823 check_fast_tracepoint_sals (gdbarch, &iter->sals);
7824 }
7a697b8d 7825
c3f6f71d
JM
7826 /* Verify that condition can be parsed, before setting any
7827 breakpoints. Allocate a separate condition expression for each
4a64f543 7828 breakpoint. */
0101ce28 7829 if (!pending)
c3f6f71d 7830 {
f8eba3c6
TT
7831 struct linespec_sals *lsal;
7832
7833 lsal = VEC_index (linespec_sals, canonical.sals, 0);
7834
2f069f6f 7835 if (parse_condition_and_thread)
72b2ff0e
VP
7836 {
7837 /* Here we only parse 'arg' to separate condition
7838 from thread number, so parsing in context of first
7839 sal is OK. When setting the breakpoint we'll
7840 re-parse it in context of each sal. */
7841 cond_string = NULL;
7842 thread = -1;
f8eba3c6 7843 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
4a306c9a 7844 &thread, &task);
72b2ff0e
VP
7845 if (cond_string)
7846 make_cleanup (xfree, cond_string);
7847 }
2f069f6f 7848 else
72b2ff0e
VP
7849 {
7850 /* Create a private copy of condition string. */
7851 if (cond_string)
7852 {
7853 cond_string = xstrdup (cond_string);
7854 make_cleanup (xfree, cond_string);
7855 }
7856 }
0fb4aa4b
PA
7857
7858 /* If the user is creating a static tracepoint by marker id
7859 (strace -m MARKER_ID), then store the sals index, so that
7860 breakpoint_re_set can try to match up which of the newly
7861 found markers corresponds to this one, and, don't try to
7862 expand multiple locations for each sal, given than SALS
7863 already should contain all sals for MARKER_ID. */
7864 if (type_wanted == bp_static_tracepoint
f8eba3c6 7865 && is_marker_spec (copy_arg))
0fb4aa4b
PA
7866 {
7867 int i;
7868
f8eba3c6 7869 for (i = 0; i < lsal->sals.nelts; ++i)
0fb4aa4b
PA
7870 {
7871 struct symtabs_and_lines expanded;
d9b3f62e 7872 struct tracepoint *tp;
0fb4aa4b 7873 struct cleanup *old_chain;
f8eba3c6 7874 char *addr_string;
0fb4aa4b
PA
7875
7876 expanded.nelts = 1;
f8eba3c6
TT
7877 expanded.sals = &lsal->sals.sals[i];
7878
7879 addr_string = xstrdup (canonical.addr_string);
7880 old_chain = make_cleanup (xfree, addr_string);
0fb4aa4b 7881
d9b3f62e
PA
7882 tp = XCNEW (struct tracepoint);
7883 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f8eba3c6 7884 addr_string, NULL,
d9b3f62e
PA
7885 cond_string, type_wanted,
7886 tempflag ? disp_del : disp_donttouch,
7887 thread, task, ignore_count, ops,
7888 from_tty, enabled, internal,
7889 canonical.special_display);
0fb4aa4b
PA
7890 /* Given that its possible to have multiple markers with
7891 the same string id, if the user is creating a static
7892 tracepoint by marker id ("strace -m MARKER_ID"), then
7893 store the sals index, so that breakpoint_re_set can
7894 try to match up which of the newly found markers
7895 corresponds to this one */
7896 tp->static_trace_marker_id_idx = i;
d9b3f62e 7897
3ea46bff 7898 install_breakpoint (internal, &tp->base, 0);
d9b3f62e 7899
f8eba3c6 7900 discard_cleanups (old_chain);
0fb4aa4b
PA
7901 }
7902 }
7903 else
f8eba3c6 7904 create_breakpoints_sal (gdbarch, &canonical, cond_string,
3e43a32a
MS
7905 type_wanted,
7906 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 7907 thread, task, ignore_count, ops, from_tty,
84f4c1fe 7908 enabled, internal);
c906108c 7909 }
0101ce28
JJ
7910 else
7911 {
0101ce28
JJ
7912 struct breakpoint *b;
7913
0101ce28
JJ
7914 make_cleanup (xfree, copy_arg);
7915
bfccc43c
YQ
7916 if (is_tracepoint_type (type_wanted))
7917 {
7918 struct tracepoint *t;
7919
7920 t = XCNEW (struct tracepoint);
7921 b = &t->base;
7922 }
7923 else
7924 b = XNEW (struct breakpoint);
7925
7926 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
7927
f8eba3c6 7928 b->addr_string = copy_arg;
72b2ff0e 7929 b->cond_string = NULL;
0101ce28 7930 b->ignore_count = ignore_count;
0101ce28 7931 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 7932 b->condition_not_parsed = 1;
41447f92 7933 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
7934 if ((type_wanted != bp_breakpoint
7935 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 7936 b->pspace = current_program_space;
8bea4e01 7937
bfccc43c 7938 install_breakpoint (internal, b, 0);
0101ce28
JJ
7939 }
7940
f8eba3c6 7941 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 7942 {
3e43a32a
MS
7943 warning (_("Multiple breakpoints were set.\nUse the "
7944 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 7945 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
7946 }
7947
80c99de1
PA
7948 /* That's it. Discard the cleanups for data inserted into the
7949 breakpoint. */
7950 discard_cleanups (bkpt_chain);
7951 /* But cleanup everything else. */
c3f6f71d 7952 do_cleanups (old_chain);
217dc9e2 7953
80c99de1 7954 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 7955 update_global_location_list (1);
fd9b8c24
PA
7956
7957 return 1;
c3f6f71d 7958}
c906108c 7959
348d480f 7960/* Set a breakpoint.
72b2ff0e
VP
7961 ARG is a string describing breakpoint address,
7962 condition, and thread.
7963 FLAG specifies if a breakpoint is hardware on,
7964 and if breakpoint is temporary, using BP_HARDWARE_FLAG
7965 and BP_TEMPFLAG. */
348d480f 7966
98deb0da 7967static void
72b2ff0e 7968break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 7969{
72b2ff0e 7970 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
7971 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
7972 ? bp_hardware_breakpoint
7973 : bp_breakpoint);
c3f6f71d 7974
8cdf0e15
VP
7975 create_breakpoint (get_current_arch (),
7976 arg,
7977 NULL, 0, 1 /* parse arg */,
0fb4aa4b 7978 tempflag, type_wanted,
8cdf0e15
VP
7979 0 /* Ignore count */,
7980 pending_break_support,
348d480f 7981 &bkpt_breakpoint_ops,
8cdf0e15 7982 from_tty,
84f4c1fe
PM
7983 1 /* enabled */,
7984 0 /* internal */);
c906108c
SS
7985}
7986
c906108c
SS
7987/* Helper function for break_command_1 and disassemble_command. */
7988
7989void
fba45db2 7990resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
7991{
7992 CORE_ADDR pc;
7993
7994 if (sal->pc == 0 && sal->symtab != NULL)
7995 {
7996 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 7997 error (_("No line %d in file \"%s\"."),
c906108c
SS
7998 sal->line, sal->symtab->filename);
7999 sal->pc = pc;
6a048695 8000
4a64f543
MS
8001 /* If this SAL corresponds to a breakpoint inserted using a line
8002 number, then skip the function prologue if necessary. */
6a048695 8003 if (sal->explicit_line)
059acae7 8004 skip_prologue_sal (sal);
c906108c
SS
8005 }
8006
8007 if (sal->section == 0 && sal->symtab != NULL)
8008 {
8009 struct blockvector *bv;
c5aa993b
JM
8010 struct block *b;
8011 struct symbol *sym;
c906108c 8012
801e3a5b 8013 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8014 if (bv != NULL)
8015 {
7f0df278 8016 sym = block_linkage_function (b);
c906108c
SS
8017 if (sym != NULL)
8018 {
8019 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8020 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8021 }
8022 else
8023 {
4a64f543
MS
8024 /* It really is worthwhile to have the section, so we'll
8025 just have to look harder. This case can be executed
8026 if we have line numbers but no functions (as can
8027 happen in assembly source). */
c906108c 8028
c5aa993b 8029 struct minimal_symbol *msym;
6c95b8df
PA
8030 struct cleanup *old_chain = save_current_space_and_thread ();
8031
8032 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8033
8034 msym = lookup_minimal_symbol_by_pc (sal->pc);
8035 if (msym)
714835d5 8036 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8037
8038 do_cleanups (old_chain);
c906108c
SS
8039 }
8040 }
8041 }
8042}
8043
8044void
fba45db2 8045break_command (char *arg, int from_tty)
c906108c 8046{
db107f19 8047 break_command_1 (arg, 0, from_tty);
c906108c
SS
8048}
8049
c906108c 8050void
fba45db2 8051tbreak_command (char *arg, int from_tty)
c906108c 8052{
db107f19 8053 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8054}
8055
c906108c 8056static void
fba45db2 8057hbreak_command (char *arg, int from_tty)
c906108c 8058{
db107f19 8059 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8060}
8061
8062static void
fba45db2 8063thbreak_command (char *arg, int from_tty)
c906108c 8064{
db107f19 8065 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8066}
8067
8068static void
fba45db2 8069stop_command (char *arg, int from_tty)
c906108c 8070{
a3f17187 8071 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8072Usage: stop in <function | address>\n\
a3f17187 8073 stop at <line>\n"));
c906108c
SS
8074}
8075
8076static void
fba45db2 8077stopin_command (char *arg, int from_tty)
c906108c
SS
8078{
8079 int badInput = 0;
8080
c5aa993b 8081 if (arg == (char *) NULL)
c906108c
SS
8082 badInput = 1;
8083 else if (*arg != '*')
8084 {
8085 char *argptr = arg;
8086 int hasColon = 0;
8087
4a64f543 8088 /* Look for a ':'. If this is a line number specification, then
53a5351d 8089 say it is bad, otherwise, it should be an address or
4a64f543 8090 function/method name. */
c906108c 8091 while (*argptr && !hasColon)
c5aa993b
JM
8092 {
8093 hasColon = (*argptr == ':');
8094 argptr++;
8095 }
c906108c
SS
8096
8097 if (hasColon)
c5aa993b 8098 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8099 else
c5aa993b 8100 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8101 }
8102
8103 if (badInput)
a3f17187 8104 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8105 else
db107f19 8106 break_command_1 (arg, 0, from_tty);
c906108c
SS
8107}
8108
8109static void
fba45db2 8110stopat_command (char *arg, int from_tty)
c906108c
SS
8111{
8112 int badInput = 0;
8113
c5aa993b 8114 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8115 badInput = 1;
8116 else
8117 {
8118 char *argptr = arg;
8119 int hasColon = 0;
8120
4a64f543
MS
8121 /* Look for a ':'. If there is a '::' then get out, otherwise
8122 it is probably a line number. */
c906108c 8123 while (*argptr && !hasColon)
c5aa993b
JM
8124 {
8125 hasColon = (*argptr == ':');
8126 argptr++;
8127 }
c906108c
SS
8128
8129 if (hasColon)
c5aa993b 8130 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8131 else
c5aa993b 8132 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8133 }
8134
8135 if (badInput)
a3f17187 8136 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8137 else
db107f19 8138 break_command_1 (arg, 0, from_tty);
c906108c
SS
8139}
8140
f1310107
TJB
8141/* Implement the "breakpoint_hit" breakpoint_ops method for
8142 ranged breakpoints. */
8143
8144static int
8145breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8146 struct address_space *aspace,
8147 CORE_ADDR bp_addr)
8148{
8149 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8150 bl->length, aspace, bp_addr);
8151}
8152
8153/* Implement the "resources_needed" breakpoint_ops method for
8154 ranged breakpoints. */
8155
8156static int
8157resources_needed_ranged_breakpoint (const struct bp_location *bl)
8158{
8159 return target_ranged_break_num_registers ();
8160}
8161
8162/* Implement the "print_it" breakpoint_ops method for
8163 ranged breakpoints. */
8164
8165static enum print_stop_action
348d480f 8166print_it_ranged_breakpoint (bpstat bs)
f1310107 8167{
348d480f 8168 struct breakpoint *b = bs->breakpoint_at;
f1310107 8169 struct bp_location *bl = b->loc;
79a45e25 8170 struct ui_out *uiout = current_uiout;
f1310107
TJB
8171
8172 gdb_assert (b->type == bp_hardware_breakpoint);
8173
8174 /* Ranged breakpoints have only one location. */
8175 gdb_assert (bl && bl->next == NULL);
8176
8177 annotate_breakpoint (b->number);
8178 if (b->disposition == disp_del)
8179 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8180 else
8181 ui_out_text (uiout, "\nRanged breakpoint ");
8182 if (ui_out_is_mi_like_p (uiout))
8183 {
8184 ui_out_field_string (uiout, "reason",
8185 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8186 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8187 }
8188 ui_out_field_int (uiout, "bkptno", b->number);
8189 ui_out_text (uiout, ", ");
8190
8191 return PRINT_SRC_AND_LOC;
8192}
8193
8194/* Implement the "print_one" breakpoint_ops method for
8195 ranged breakpoints. */
8196
8197static void
8198print_one_ranged_breakpoint (struct breakpoint *b,
8199 struct bp_location **last_loc)
8200{
8201 struct bp_location *bl = b->loc;
8202 struct value_print_options opts;
79a45e25 8203 struct ui_out *uiout = current_uiout;
f1310107
TJB
8204
8205 /* Ranged breakpoints have only one location. */
8206 gdb_assert (bl && bl->next == NULL);
8207
8208 get_user_print_options (&opts);
8209
8210 if (opts.addressprint)
8211 /* We don't print the address range here, it will be printed later
8212 by print_one_detail_ranged_breakpoint. */
8213 ui_out_field_skip (uiout, "addr");
8214 annotate_field (5);
8215 print_breakpoint_location (b, bl);
8216 *last_loc = bl;
8217}
8218
8219/* Implement the "print_one_detail" breakpoint_ops method for
8220 ranged breakpoints. */
8221
8222static void
8223print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8224 struct ui_out *uiout)
8225{
8226 CORE_ADDR address_start, address_end;
8227 struct bp_location *bl = b->loc;
8228 struct ui_stream *stb = ui_out_stream_new (uiout);
8229 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8230
8231 gdb_assert (bl);
8232
8233 address_start = bl->address;
8234 address_end = address_start + bl->length - 1;
8235
8236 ui_out_text (uiout, "\taddress range: ");
8237 fprintf_unfiltered (stb->stream, "[%s, %s]",
8238 print_core_address (bl->gdbarch, address_start),
8239 print_core_address (bl->gdbarch, address_end));
8240 ui_out_field_stream (uiout, "addr", stb);
8241 ui_out_text (uiout, "\n");
8242
8243 do_cleanups (cleanup);
8244}
8245
8246/* Implement the "print_mention" breakpoint_ops method for
8247 ranged breakpoints. */
8248
8249static void
8250print_mention_ranged_breakpoint (struct breakpoint *b)
8251{
8252 struct bp_location *bl = b->loc;
79a45e25 8253 struct ui_out *uiout = current_uiout;
f1310107
TJB
8254
8255 gdb_assert (bl);
8256 gdb_assert (b->type == bp_hardware_breakpoint);
8257
8258 if (ui_out_is_mi_like_p (uiout))
8259 return;
8260
8261 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8262 b->number, paddress (bl->gdbarch, bl->address),
8263 paddress (bl->gdbarch, bl->address + bl->length - 1));
8264}
8265
8266/* Implement the "print_recreate" breakpoint_ops method for
8267 ranged breakpoints. */
8268
8269static void
8270print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8271{
8272 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8273 b->addr_string_range_end);
d9b3f62e 8274 print_recreate_thread (b, fp);
f1310107
TJB
8275}
8276
8277/* The breakpoint_ops structure to be used in ranged breakpoints. */
8278
2060206e 8279static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
8280
8281/* Find the address where the end of the breakpoint range should be
8282 placed, given the SAL of the end of the range. This is so that if
8283 the user provides a line number, the end of the range is set to the
8284 last instruction of the given line. */
8285
8286static CORE_ADDR
8287find_breakpoint_range_end (struct symtab_and_line sal)
8288{
8289 CORE_ADDR end;
8290
8291 /* If the user provided a PC value, use it. Otherwise,
8292 find the address of the end of the given location. */
8293 if (sal.explicit_pc)
8294 end = sal.pc;
8295 else
8296 {
8297 int ret;
8298 CORE_ADDR start;
8299
8300 ret = find_line_pc_range (sal, &start, &end);
8301 if (!ret)
8302 error (_("Could not find location of the end of the range."));
8303
8304 /* find_line_pc_range returns the start of the next line. */
8305 end--;
8306 }
8307
8308 return end;
8309}
8310
8311/* Implement the "break-range" CLI command. */
8312
8313static void
8314break_range_command (char *arg, int from_tty)
8315{
8316 char *arg_start, *addr_string_start, *addr_string_end;
8317 struct linespec_result canonical_start, canonical_end;
8318 int bp_count, can_use_bp, length;
8319 CORE_ADDR end;
8320 struct breakpoint *b;
8321 struct symtab_and_line sal_start, sal_end;
f1310107 8322 struct cleanup *cleanup_bkpt;
f8eba3c6 8323 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
8324
8325 /* We don't support software ranged breakpoints. */
8326 if (target_ranged_break_num_registers () < 0)
8327 error (_("This target does not support hardware ranged breakpoints."));
8328
8329 bp_count = hw_breakpoint_used_count ();
8330 bp_count += target_ranged_break_num_registers ();
8331 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8332 bp_count, 0);
8333 if (can_use_bp < 0)
8334 error (_("Hardware breakpoints used exceeds limit."));
8335
f8eba3c6 8336 arg = skip_spaces (arg);
f1310107
TJB
8337 if (arg == NULL || arg[0] == '\0')
8338 error(_("No address range specified."));
8339
f1310107
TJB
8340 init_linespec_result (&canonical_start);
8341
f8eba3c6
TT
8342 arg_start = arg;
8343 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 8344
f8eba3c6 8345 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
8346
8347 if (arg[0] != ',')
8348 error (_("Too few arguments."));
f8eba3c6 8349 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 8350 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
8351
8352 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
8353
8354 if (VEC_length (linespec_sals, canonical_start.sals) > 1
8355 || lsal_start->sals.nelts != 1)
f1310107
TJB
8356 error (_("Cannot create a ranged breakpoint with multiple locations."));
8357
f8eba3c6
TT
8358 sal_start = lsal_start->sals.sals[0];
8359 addr_string_start = savestring (arg_start, arg - arg_start);
8360 make_cleanup (xfree, addr_string_start);
f1310107
TJB
8361
8362 arg++; /* Skip the comma. */
f8eba3c6 8363 arg = skip_spaces (arg);
f1310107
TJB
8364
8365 /* Parse the end location. */
8366
f1310107
TJB
8367 init_linespec_result (&canonical_end);
8368 arg_start = arg;
8369
f8eba3c6 8370 /* We call decode_line_full directly here instead of using
f1310107
TJB
8371 parse_breakpoint_sals because we need to specify the start location's
8372 symtab and line as the default symtab and line for the end of the
8373 range. This makes it possible to have ranges like "foo.c:27, +14",
8374 where +14 means 14 lines from the start location. */
f8eba3c6
TT
8375 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
8376 sal_start.symtab, sal_start.line,
8377 &canonical_end, NULL, NULL);
8378
8379 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 8380
f8eba3c6 8381 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 8382 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
8383
8384 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
8385 if (VEC_length (linespec_sals, canonical_end.sals) > 1
8386 || lsal_end->sals.nelts != 1)
f1310107
TJB
8387 error (_("Cannot create a ranged breakpoint with multiple locations."));
8388
f8eba3c6
TT
8389 sal_end = lsal_end->sals.sals[0];
8390 addr_string_end = savestring (arg_start, arg - arg_start);
8391 make_cleanup (xfree, addr_string_end);
f1310107
TJB
8392
8393 end = find_breakpoint_range_end (sal_end);
8394 if (sal_start.pc > end)
177b42fe 8395 error (_("Invalid address range, end precedes start."));
f1310107
TJB
8396
8397 length = end - sal_start.pc + 1;
8398 if (length < 0)
8399 /* Length overflowed. */
8400 error (_("Address range too large."));
8401 else if (length == 1)
8402 {
8403 /* This range is simple enough to be handled by
8404 the `hbreak' command. */
8405 hbreak_command (addr_string_start, 1);
8406
8407 do_cleanups (cleanup_bkpt);
8408
8409 return;
8410 }
8411
8412 /* Now set up the breakpoint. */
8413 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 8414 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
8415 set_breakpoint_count (breakpoint_count + 1);
8416 b->number = breakpoint_count;
8417 b->disposition = disp_donttouch;
f8eba3c6
TT
8418 b->addr_string = xstrdup (addr_string_start);
8419 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
8420 b->loc->length = length;
8421
f8eba3c6 8422 do_cleanups (cleanup_bkpt);
f1310107
TJB
8423
8424 mention (b);
8d3788bd 8425 observer_notify_breakpoint_created (b);
f1310107
TJB
8426 update_global_location_list (1);
8427}
8428
4a64f543
MS
8429/* Return non-zero if EXP is verified as constant. Returned zero
8430 means EXP is variable. Also the constant detection may fail for
8431 some constant expressions and in such case still falsely return
8432 zero. */
65d79d4b
SDJ
8433static int
8434watchpoint_exp_is_const (const struct expression *exp)
8435{
8436 int i = exp->nelts;
8437
8438 while (i > 0)
8439 {
8440 int oplenp, argsp;
8441
8442 /* We are only interested in the descriptor of each element. */
8443 operator_length (exp, i, &oplenp, &argsp);
8444 i -= oplenp;
8445
8446 switch (exp->elts[i].opcode)
8447 {
8448 case BINOP_ADD:
8449 case BINOP_SUB:
8450 case BINOP_MUL:
8451 case BINOP_DIV:
8452 case BINOP_REM:
8453 case BINOP_MOD:
8454 case BINOP_LSH:
8455 case BINOP_RSH:
8456 case BINOP_LOGICAL_AND:
8457 case BINOP_LOGICAL_OR:
8458 case BINOP_BITWISE_AND:
8459 case BINOP_BITWISE_IOR:
8460 case BINOP_BITWISE_XOR:
8461 case BINOP_EQUAL:
8462 case BINOP_NOTEQUAL:
8463 case BINOP_LESS:
8464 case BINOP_GTR:
8465 case BINOP_LEQ:
8466 case BINOP_GEQ:
8467 case BINOP_REPEAT:
8468 case BINOP_COMMA:
8469 case BINOP_EXP:
8470 case BINOP_MIN:
8471 case BINOP_MAX:
8472 case BINOP_INTDIV:
8473 case BINOP_CONCAT:
8474 case BINOP_IN:
8475 case BINOP_RANGE:
8476 case TERNOP_COND:
8477 case TERNOP_SLICE:
8478 case TERNOP_SLICE_COUNT:
8479
8480 case OP_LONG:
8481 case OP_DOUBLE:
8482 case OP_DECFLOAT:
8483 case OP_LAST:
8484 case OP_COMPLEX:
8485 case OP_STRING:
8486 case OP_BITSTRING:
8487 case OP_ARRAY:
8488 case OP_TYPE:
8489 case OP_NAME:
8490 case OP_OBJC_NSSTRING:
8491
8492 case UNOP_NEG:
8493 case UNOP_LOGICAL_NOT:
8494 case UNOP_COMPLEMENT:
8495 case UNOP_ADDR:
8496 case UNOP_HIGH:
4a64f543
MS
8497 /* Unary, binary and ternary operators: We have to check
8498 their operands. If they are constant, then so is the
8499 result of that operation. For instance, if A and B are
8500 determined to be constants, then so is "A + B".
8501
8502 UNOP_IND is one exception to the rule above, because the
8503 value of *ADDR is not necessarily a constant, even when
8504 ADDR is. */
65d79d4b
SDJ
8505 break;
8506
8507 case OP_VAR_VALUE:
8508 /* Check whether the associated symbol is a constant.
4a64f543 8509
65d79d4b 8510 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8511 possible that a buggy compiler could mark a variable as
8512 constant even when it is not, and TYPE_CONST would return
8513 true in this case, while SYMBOL_CLASS wouldn't.
8514
8515 We also have to check for function symbols because they
8516 are always constant. */
65d79d4b
SDJ
8517 {
8518 struct symbol *s = exp->elts[i + 2].symbol;
8519
8520 if (SYMBOL_CLASS (s) != LOC_BLOCK
8521 && SYMBOL_CLASS (s) != LOC_CONST
8522 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8523 return 0;
8524 break;
8525 }
8526
8527 /* The default action is to return 0 because we are using
8528 the optimistic approach here: If we don't know something,
8529 then it is not a constant. */
8530 default:
8531 return 0;
8532 }
8533 }
8534
8535 return 1;
8536}
8537
3a5c3e22
PA
8538/* Implement the "dtor" breakpoint_ops method for watchpoints. */
8539
8540static void
8541dtor_watchpoint (struct breakpoint *self)
8542{
8543 struct watchpoint *w = (struct watchpoint *) self;
8544
8545 xfree (w->cond_exp);
8546 xfree (w->exp);
8547 xfree (w->exp_string);
8548 xfree (w->exp_string_reparse);
8549 value_free (w->val);
8550
8551 base_breakpoint_ops.dtor (self);
8552}
8553
348d480f
PA
8554/* Implement the "re_set" breakpoint_ops method for watchpoints. */
8555
8556static void
8557re_set_watchpoint (struct breakpoint *b)
8558{
3a5c3e22
PA
8559 struct watchpoint *w = (struct watchpoint *) b;
8560
348d480f
PA
8561 /* Watchpoint can be either on expression using entirely global
8562 variables, or it can be on local variables.
8563
8564 Watchpoints of the first kind are never auto-deleted, and even
8565 persist across program restarts. Since they can use variables
8566 from shared libraries, we need to reparse expression as libraries
8567 are loaded and unloaded.
8568
8569 Watchpoints on local variables can also change meaning as result
8570 of solib event. For example, if a watchpoint uses both a local
8571 and a global variables in expression, it's a local watchpoint,
8572 but unloading of a shared library will make the expression
8573 invalid. This is not a very common use case, but we still
8574 re-evaluate expression, to avoid surprises to the user.
8575
8576 Note that for local watchpoints, we re-evaluate it only if
8577 watchpoints frame id is still valid. If it's not, it means the
8578 watchpoint is out of scope and will be deleted soon. In fact,
8579 I'm not sure we'll ever be called in this case.
8580
8581 If a local watchpoint's frame id is still valid, then
3a5c3e22 8582 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 8583
3a5c3e22
PA
8584 Don't do anything about disabled watchpoints, since they will be
8585 reevaluated again when enabled. */
8586 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
8587}
8588
77b06cd7
TJB
8589/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8590
8591static int
8592insert_watchpoint (struct bp_location *bl)
8593{
3a5c3e22
PA
8594 struct watchpoint *w = (struct watchpoint *) bl->owner;
8595 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8596
8597 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8598 w->cond_exp);
77b06cd7
TJB
8599}
8600
8601/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8602
8603static int
8604remove_watchpoint (struct bp_location *bl)
8605{
3a5c3e22
PA
8606 struct watchpoint *w = (struct watchpoint *) bl->owner;
8607 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8608
8609 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8610 w->cond_exp);
e09342b5
TJB
8611}
8612
e09342b5 8613static int
348d480f
PA
8614breakpoint_hit_watchpoint (const struct bp_location *bl,
8615 struct address_space *aspace, CORE_ADDR bp_addr)
e09342b5 8616{
348d480f 8617 struct breakpoint *b = bl->owner;
3a5c3e22 8618 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 8619
348d480f
PA
8620 /* Continuable hardware watchpoints are treated as non-existent if the
8621 reason we stopped wasn't a hardware watchpoint (we didn't stop on
8622 some data address). Otherwise gdb won't stop on a break instruction
8623 in the code (not from a breakpoint) when a hardware watchpoint has
8624 been defined. Also skip watchpoints which we know did not trigger
8625 (did not match the data address). */
8626 if (is_hardware_watchpoint (b)
3a5c3e22 8627 && w->watchpoint_triggered == watch_triggered_no)
348d480f 8628 return 0;
9c06b0b4 8629
348d480f 8630 return 1;
9c06b0b4
TJB
8631}
8632
348d480f
PA
8633static void
8634check_status_watchpoint (bpstat bs)
9c06b0b4 8635{
348d480f 8636 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 8637
348d480f 8638 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
8639}
8640
8641/* Implement the "resources_needed" breakpoint_ops method for
348d480f 8642 hardware watchpoints. */
9c06b0b4
TJB
8643
8644static int
348d480f 8645resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 8646{
3a5c3e22
PA
8647 struct watchpoint *w = (struct watchpoint *) bl->owner;
8648 int length = w->exact? 1 : bl->length;
348d480f
PA
8649
8650 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
8651}
8652
8653/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 8654 hardware watchpoints. */
9c06b0b4
TJB
8655
8656static int
348d480f 8657works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 8658{
efa80663
PA
8659 /* Read and access watchpoints only work with hardware support. */
8660 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
8661}
8662
9c06b0b4 8663static enum print_stop_action
348d480f 8664print_it_watchpoint (bpstat bs)
9c06b0b4 8665{
348d480f
PA
8666 struct cleanup *old_chain;
8667 struct breakpoint *b;
8668 const struct bp_location *bl;
8669 struct ui_stream *stb;
8670 enum print_stop_action result;
3a5c3e22 8671 struct watchpoint *w;
79a45e25 8672 struct ui_out *uiout = current_uiout;
348d480f
PA
8673
8674 gdb_assert (bs->bp_location_at != NULL);
8675
8676 bl = bs->bp_location_at;
8677 b = bs->breakpoint_at;
3a5c3e22 8678 w = (struct watchpoint *) b;
348d480f
PA
8679
8680 stb = ui_out_stream_new (uiout);
8681 old_chain = make_cleanup_ui_out_stream_delete (stb);
9c06b0b4
TJB
8682
8683 switch (b->type)
8684 {
348d480f 8685 case bp_watchpoint:
9c06b0b4
TJB
8686 case bp_hardware_watchpoint:
8687 annotate_watchpoint (b->number);
8688 if (ui_out_is_mi_like_p (uiout))
8689 ui_out_field_string
8690 (uiout, "reason",
8691 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
8692 mention (b);
8693 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8694 ui_out_text (uiout, "\nOld value = ");
8695 watchpoint_value_print (bs->old_val, stb->stream);
8696 ui_out_field_stream (uiout, "old", stb);
8697 ui_out_text (uiout, "\nNew value = ");
3a5c3e22 8698 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8699 ui_out_field_stream (uiout, "new", stb);
8700 ui_out_text (uiout, "\n");
8701 /* More than one watchpoint may have been triggered. */
8702 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8703 break;
8704
8705 case bp_read_watchpoint:
8706 if (ui_out_is_mi_like_p (uiout))
8707 ui_out_field_string
8708 (uiout, "reason",
8709 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
8710 mention (b);
8711 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8712 ui_out_text (uiout, "\nValue = ");
3a5c3e22 8713 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8714 ui_out_field_stream (uiout, "value", stb);
8715 ui_out_text (uiout, "\n");
8716 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8717 break;
8718
8719 case bp_access_watchpoint:
348d480f
PA
8720 if (bs->old_val != NULL)
8721 {
8722 annotate_watchpoint (b->number);
8723 if (ui_out_is_mi_like_p (uiout))
8724 ui_out_field_string
8725 (uiout, "reason",
8726 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8727 mention (b);
8728 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8729 ui_out_text (uiout, "\nOld value = ");
8730 watchpoint_value_print (bs->old_val, stb->stream);
8731 ui_out_field_stream (uiout, "old", stb);
8732 ui_out_text (uiout, "\nNew value = ");
8733 }
8734 else
8735 {
8736 mention (b);
8737 if (ui_out_is_mi_like_p (uiout))
8738 ui_out_field_string
8739 (uiout, "reason",
8740 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8741 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8742 ui_out_text (uiout, "\nValue = ");
8743 }
3a5c3e22 8744 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8745 ui_out_field_stream (uiout, "new", stb);
8746 ui_out_text (uiout, "\n");
8747 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8748 break;
8749 default:
348d480f 8750 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8751 }
8752
348d480f
PA
8753 do_cleanups (old_chain);
8754 return result;
8755}
8756
8757/* Implement the "print_mention" breakpoint_ops method for hardware
8758 watchpoints. */
8759
8760static void
8761print_mention_watchpoint (struct breakpoint *b)
8762{
8763 struct cleanup *ui_out_chain;
3a5c3e22 8764 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 8765 struct ui_out *uiout = current_uiout;
348d480f
PA
8766
8767 switch (b->type)
8768 {
8769 case bp_watchpoint:
8770 ui_out_text (uiout, "Watchpoint ");
8771 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8772 break;
8773 case bp_hardware_watchpoint:
8774 ui_out_text (uiout, "Hardware watchpoint ");
8775 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8776 break;
8777 case bp_read_watchpoint:
8778 ui_out_text (uiout, "Hardware read watchpoint ");
8779 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8780 break;
8781 case bp_access_watchpoint:
8782 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
8783 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8784 break;
8785 default:
8786 internal_error (__FILE__, __LINE__,
8787 _("Invalid hardware watchpoint type."));
8788 }
8789
8790 ui_out_field_int (uiout, "number", b->number);
8791 ui_out_text (uiout, ": ");
3a5c3e22 8792 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
8793 do_cleanups (ui_out_chain);
8794}
8795
8796/* Implement the "print_recreate" breakpoint_ops method for
8797 watchpoints. */
8798
8799static void
8800print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
8801{
3a5c3e22
PA
8802 struct watchpoint *w = (struct watchpoint *) b;
8803
348d480f
PA
8804 switch (b->type)
8805 {
8806 case bp_watchpoint:
8807 case bp_hardware_watchpoint:
8808 fprintf_unfiltered (fp, "watch");
8809 break;
8810 case bp_read_watchpoint:
8811 fprintf_unfiltered (fp, "rwatch");
8812 break;
8813 case bp_access_watchpoint:
8814 fprintf_unfiltered (fp, "awatch");
8815 break;
8816 default:
8817 internal_error (__FILE__, __LINE__,
8818 _("Invalid watchpoint type."));
8819 }
8820
3a5c3e22 8821 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 8822 print_recreate_thread (b, fp);
348d480f
PA
8823}
8824
8825/* The breakpoint_ops structure to be used in hardware watchpoints. */
8826
2060206e 8827static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
8828
8829/* Implement the "insert" breakpoint_ops method for
8830 masked hardware watchpoints. */
8831
8832static int
8833insert_masked_watchpoint (struct bp_location *bl)
8834{
3a5c3e22
PA
8835 struct watchpoint *w = (struct watchpoint *) bl->owner;
8836
8837 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8838 bl->watchpoint_type);
8839}
8840
8841/* Implement the "remove" breakpoint_ops method for
8842 masked hardware watchpoints. */
8843
8844static int
8845remove_masked_watchpoint (struct bp_location *bl)
8846{
3a5c3e22
PA
8847 struct watchpoint *w = (struct watchpoint *) bl->owner;
8848
8849 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8850 bl->watchpoint_type);
8851}
8852
8853/* Implement the "resources_needed" breakpoint_ops method for
8854 masked hardware watchpoints. */
8855
8856static int
8857resources_needed_masked_watchpoint (const struct bp_location *bl)
8858{
3a5c3e22
PA
8859 struct watchpoint *w = (struct watchpoint *) bl->owner;
8860
8861 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
8862}
8863
8864/* Implement the "works_in_software_mode" breakpoint_ops method for
8865 masked hardware watchpoints. */
8866
8867static int
8868works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
8869{
8870 return 0;
8871}
8872
8873/* Implement the "print_it" breakpoint_ops method for
8874 masked hardware watchpoints. */
8875
8876static enum print_stop_action
8877print_it_masked_watchpoint (bpstat bs)
8878{
8879 struct breakpoint *b = bs->breakpoint_at;
79a45e25 8880 struct ui_out *uiout = current_uiout;
348d480f
PA
8881
8882 /* Masked watchpoints have only one location. */
8883 gdb_assert (b->loc && b->loc->next == NULL);
8884
8885 switch (b->type)
8886 {
8887 case bp_hardware_watchpoint:
8888 annotate_watchpoint (b->number);
8889 if (ui_out_is_mi_like_p (uiout))
8890 ui_out_field_string
8891 (uiout, "reason",
8892 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
8893 break;
8894
8895 case bp_read_watchpoint:
8896 if (ui_out_is_mi_like_p (uiout))
8897 ui_out_field_string
8898 (uiout, "reason",
8899 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
8900 break;
8901
8902 case bp_access_watchpoint:
8903 if (ui_out_is_mi_like_p (uiout))
8904 ui_out_field_string
8905 (uiout, "reason",
8906 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8907 break;
8908 default:
8909 internal_error (__FILE__, __LINE__,
8910 _("Invalid hardware watchpoint type."));
8911 }
8912
8913 mention (b);
9c06b0b4
TJB
8914 ui_out_text (uiout, _("\n\
8915Check the underlying instruction at PC for the memory\n\
8916address and value which triggered this watchpoint.\n"));
8917 ui_out_text (uiout, "\n");
8918
8919 /* More than one watchpoint may have been triggered. */
8920 return PRINT_UNKNOWN;
8921}
8922
8923/* Implement the "print_one_detail" breakpoint_ops method for
8924 masked hardware watchpoints. */
8925
8926static void
8927print_one_detail_masked_watchpoint (const struct breakpoint *b,
8928 struct ui_out *uiout)
8929{
3a5c3e22
PA
8930 struct watchpoint *w = (struct watchpoint *) b;
8931
9c06b0b4
TJB
8932 /* Masked watchpoints have only one location. */
8933 gdb_assert (b->loc && b->loc->next == NULL);
8934
8935 ui_out_text (uiout, "\tmask ");
3a5c3e22 8936 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
8937 ui_out_text (uiout, "\n");
8938}
8939
8940/* Implement the "print_mention" breakpoint_ops method for
8941 masked hardware watchpoints. */
8942
8943static void
8944print_mention_masked_watchpoint (struct breakpoint *b)
8945{
3a5c3e22 8946 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 8947 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
8948 struct cleanup *ui_out_chain;
8949
8950 switch (b->type)
8951 {
8952 case bp_hardware_watchpoint:
8953 ui_out_text (uiout, "Masked hardware watchpoint ");
8954 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8955 break;
8956 case bp_read_watchpoint:
8957 ui_out_text (uiout, "Masked hardware read watchpoint ");
8958 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8959 break;
8960 case bp_access_watchpoint:
8961 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
8962 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8963 break;
8964 default:
8965 internal_error (__FILE__, __LINE__,
8966 _("Invalid hardware watchpoint type."));
8967 }
8968
8969 ui_out_field_int (uiout, "number", b->number);
8970 ui_out_text (uiout, ": ");
3a5c3e22 8971 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
8972 do_cleanups (ui_out_chain);
8973}
8974
8975/* Implement the "print_recreate" breakpoint_ops method for
8976 masked hardware watchpoints. */
8977
8978static void
8979print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
8980{
3a5c3e22 8981 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
8982 char tmp[40];
8983
8984 switch (b->type)
8985 {
8986 case bp_hardware_watchpoint:
8987 fprintf_unfiltered (fp, "watch");
8988 break;
8989 case bp_read_watchpoint:
8990 fprintf_unfiltered (fp, "rwatch");
8991 break;
8992 case bp_access_watchpoint:
8993 fprintf_unfiltered (fp, "awatch");
8994 break;
8995 default:
8996 internal_error (__FILE__, __LINE__,
8997 _("Invalid hardware watchpoint type."));
8998 }
8999
3a5c3e22
PA
9000 sprintf_vma (tmp, w->hw_wp_mask);
9001 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 9002 print_recreate_thread (b, fp);
9c06b0b4
TJB
9003}
9004
9005/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
9006
2060206e 9007static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
9008
9009/* Tell whether the given watchpoint is a masked hardware watchpoint. */
9010
9011static int
9012is_masked_watchpoint (const struct breakpoint *b)
9013{
9014 return b->ops == &masked_watchpoint_breakpoint_ops;
9015}
9016
53a5351d
JM
9017/* accessflag: hw_write: watch write,
9018 hw_read: watch read,
9019 hw_access: watch access (read or write) */
c906108c 9020static void
84f4c1fe
PM
9021watch_command_1 (char *arg, int accessflag, int from_tty,
9022 int just_location, int internal)
c906108c 9023{
a9634178 9024 volatile struct gdb_exception e;
d983da9c 9025 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 9026 struct expression *exp;
60e1c644 9027 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 9028 struct value *val, *mark, *result;
c906108c 9029 struct frame_info *frame;
c906108c
SS
9030 char *exp_start = NULL;
9031 char *exp_end = NULL;
9c06b0b4
TJB
9032 char *tok, *end_tok;
9033 int toklen = -1;
c906108c
SS
9034 char *cond_start = NULL;
9035 char *cond_end = NULL;
c906108c 9036 enum bptype bp_type;
37e4754d 9037 int thread = -1;
0cf6dd15 9038 int pc = 0;
9c06b0b4
TJB
9039 /* Flag to indicate whether we are going to use masks for
9040 the hardware watchpoint. */
9041 int use_mask = 0;
9042 CORE_ADDR mask = 0;
3a5c3e22 9043 struct watchpoint *w;
c906108c 9044
37e4754d
LM
9045 /* Make sure that we actually have parameters to parse. */
9046 if (arg != NULL && arg[0] != '\0')
9047 {
9c06b0b4 9048 char *value_start;
37e4754d 9049
9c06b0b4
TJB
9050 /* Look for "parameter value" pairs at the end
9051 of the arguments string. */
9052 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
9053 {
9054 /* Skip whitespace at the end of the argument list. */
9055 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9056 tok--;
9057
9058 /* Find the beginning of the last token.
9059 This is the value of the parameter. */
9060 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9061 tok--;
9062 value_start = tok + 1;
9063
9064 /* Skip whitespace. */
9065 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9066 tok--;
9067
9068 end_tok = tok;
9069
9070 /* Find the beginning of the second to last token.
9071 This is the parameter itself. */
9072 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9073 tok--;
9074 tok++;
9075 toklen = end_tok - tok + 1;
9076
9077 if (toklen == 6 && !strncmp (tok, "thread", 6))
9078 {
9079 /* At this point we've found a "thread" token, which means
9080 the user is trying to set a watchpoint that triggers
9081 only in a specific thread. */
9082 char *endp;
37e4754d 9083
9c06b0b4
TJB
9084 if (thread != -1)
9085 error(_("You can specify only one thread."));
37e4754d 9086
9c06b0b4
TJB
9087 /* Extract the thread ID from the next token. */
9088 thread = strtol (value_start, &endp, 0);
37e4754d 9089
9c06b0b4
TJB
9090 /* Check if the user provided a valid numeric value for the
9091 thread ID. */
9092 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9093 error (_("Invalid thread ID specification %s."), value_start);
9094
9095 /* Check if the thread actually exists. */
9096 if (!valid_thread_id (thread))
9097 error (_("Unknown thread %d."), thread);
9098 }
9099 else if (toklen == 4 && !strncmp (tok, "mask", 4))
9100 {
9101 /* We've found a "mask" token, which means the user wants to
9102 create a hardware watchpoint that is going to have the mask
9103 facility. */
9104 struct value *mask_value, *mark;
37e4754d 9105
9c06b0b4
TJB
9106 if (use_mask)
9107 error(_("You can specify only one mask."));
37e4754d 9108
9c06b0b4 9109 use_mask = just_location = 1;
37e4754d 9110
9c06b0b4
TJB
9111 mark = value_mark ();
9112 mask_value = parse_to_comma_and_eval (&value_start);
9113 mask = value_as_address (mask_value);
9114 value_free_to_mark (mark);
9115 }
9116 else
9117 /* We didn't recognize what we found. We should stop here. */
9118 break;
37e4754d 9119
9c06b0b4
TJB
9120 /* Truncate the string and get rid of the "parameter value" pair before
9121 the arguments string is parsed by the parse_exp_1 function. */
9122 *tok = '\0';
9123 }
37e4754d
LM
9124 }
9125
9126 /* Parse the rest of the arguments. */
c906108c
SS
9127 innermost_block = NULL;
9128 exp_start = arg;
9129 exp = parse_exp_1 (&arg, 0, 0);
9130 exp_end = arg;
fa8a61dc
TT
9131 /* Remove trailing whitespace from the expression before saving it.
9132 This makes the eventual display of the expression string a bit
9133 prettier. */
9134 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9135 --exp_end;
9136
65d79d4b
SDJ
9137 /* Checking if the expression is not constant. */
9138 if (watchpoint_exp_is_const (exp))
9139 {
9140 int len;
9141
9142 len = exp_end - exp_start;
9143 while (len > 0 && isspace (exp_start[len - 1]))
9144 len--;
9145 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9146 }
9147
c906108c
SS
9148 exp_valid_block = innermost_block;
9149 mark = value_mark ();
a1442452 9150 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
9151
9152 if (just_location)
9153 {
9c06b0b4
TJB
9154 int ret;
9155
06a64a0b 9156 exp_valid_block = NULL;
a1442452 9157 val = value_addr (result);
06a64a0b
TT
9158 release_value (val);
9159 value_free_to_mark (mark);
9c06b0b4
TJB
9160
9161 if (use_mask)
9162 {
9163 ret = target_masked_watch_num_registers (value_as_address (val),
9164 mask);
9165 if (ret == -1)
9166 error (_("This target does not support masked watchpoints."));
9167 else if (ret == -2)
9168 error (_("Invalid mask or memory region."));
9169 }
06a64a0b
TT
9170 }
9171 else if (val != NULL)
fa4727a6 9172 release_value (val);
c906108c 9173
e9cafbcc
TT
9174 tok = skip_spaces (arg);
9175 end_tok = skip_to_space (tok);
c906108c
SS
9176
9177 toklen = end_tok - tok;
9178 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9179 {
2d134ed3
PA
9180 struct expression *cond;
9181
60e1c644 9182 innermost_block = NULL;
c906108c
SS
9183 tok = cond_start = end_tok + 1;
9184 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
9185
9186 /* The watchpoint expression may not be local, but the condition
9187 may still be. E.g.: `watch global if local > 0'. */
9188 cond_exp_valid_block = innermost_block;
9189
2d134ed3 9190 xfree (cond);
c906108c
SS
9191 cond_end = tok;
9192 }
9193 if (*tok)
8a3fe4f8 9194 error (_("Junk at end of command."));
c906108c 9195
53a5351d 9196 if (accessflag == hw_read)
c5aa993b 9197 bp_type = bp_read_watchpoint;
53a5351d 9198 else if (accessflag == hw_access)
c5aa993b
JM
9199 bp_type = bp_access_watchpoint;
9200 else
9201 bp_type = bp_hardware_watchpoint;
c906108c 9202
d983da9c 9203 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
9204
9205 /* If the expression is "local", then set up a "watchpoint scope"
9206 breakpoint at the point where we've left the scope of the watchpoint
9207 expression. Create the scope breakpoint before the watchpoint, so
9208 that we will encounter it first in bpstat_stop_status. */
60e1c644 9209 if (exp_valid_block && frame)
d983da9c 9210 {
edb3359d
DJ
9211 if (frame_id_p (frame_unwind_caller_id (frame)))
9212 {
9213 scope_breakpoint
a6d9a66e
UW
9214 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9215 frame_unwind_caller_pc (frame),
06edf0c0
PA
9216 bp_watchpoint_scope,
9217 &momentary_breakpoint_ops);
d983da9c 9218
edb3359d 9219 scope_breakpoint->enable_state = bp_enabled;
d983da9c 9220
edb3359d
DJ
9221 /* Automatically delete the breakpoint when it hits. */
9222 scope_breakpoint->disposition = disp_del;
d983da9c 9223
edb3359d
DJ
9224 /* Only break in the proper frame (help with recursion). */
9225 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 9226
edb3359d 9227 /* Set the address at which we will stop. */
a6d9a66e
UW
9228 scope_breakpoint->loc->gdbarch
9229 = frame_unwind_caller_arch (frame);
edb3359d
DJ
9230 scope_breakpoint->loc->requested_address
9231 = frame_unwind_caller_pc (frame);
9232 scope_breakpoint->loc->address
a6d9a66e
UW
9233 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9234 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9235 scope_breakpoint->type);
9236 }
d983da9c
DJ
9237 }
9238
c906108c 9239 /* Now set up the breakpoint. */
3a5c3e22
PA
9240
9241 w = XCNEW (struct watchpoint);
9242 b = &w->base;
348d480f 9243 if (use_mask)
3a5c3e22
PA
9244 init_raw_breakpoint_without_location (b, NULL, bp_type,
9245 &masked_watchpoint_breakpoint_ops);
348d480f 9246 else
3a5c3e22
PA
9247 init_raw_breakpoint_without_location (b, NULL, bp_type,
9248 &watchpoint_breakpoint_ops);
37e4754d 9249 b->thread = thread;
b5de0fa7 9250 b->disposition = disp_donttouch;
348d480f 9251 b->pspace = current_program_space;
3a5c3e22
PA
9252 w->exp = exp;
9253 w->exp_valid_block = exp_valid_block;
9254 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
9255 if (just_location)
9256 {
9257 struct type *t = value_type (val);
9258 CORE_ADDR addr = value_as_address (val);
9259 char *name;
9260
9261 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9262 name = type_to_string (t);
9263
3a5c3e22 9264 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 9265 core_addr_to_string (addr));
06a64a0b
TT
9266 xfree (name);
9267
3a5c3e22 9268 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9269 (int) (exp_end - exp_start), exp_start);
9270
06a64a0b
TT
9271 /* The above expression is in C. */
9272 b->language = language_c;
9273 }
9274 else
3a5c3e22 9275 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
9276
9277 if (use_mask)
9278 {
3a5c3e22 9279 w->hw_wp_mask = mask;
9c06b0b4
TJB
9280 }
9281 else
9282 {
3a5c3e22
PA
9283 w->val = val;
9284 w->val_valid = 1;
9c06b0b4 9285 }
77b06cd7 9286
c906108c
SS
9287 if (cond_start)
9288 b->cond_string = savestring (cond_start, cond_end - cond_start);
9289 else
9290 b->cond_string = 0;
c5aa993b 9291
c906108c 9292 if (frame)
f6bc2008 9293 {
3a5c3e22
PA
9294 w->watchpoint_frame = get_frame_id (frame);
9295 w->watchpoint_thread = inferior_ptid;
f6bc2008 9296 }
c906108c 9297 else
f6bc2008 9298 {
3a5c3e22
PA
9299 w->watchpoint_frame = null_frame_id;
9300 w->watchpoint_thread = null_ptid;
f6bc2008 9301 }
c906108c 9302
d983da9c 9303 if (scope_breakpoint != NULL)
c906108c 9304 {
d983da9c
DJ
9305 /* The scope breakpoint is related to the watchpoint. We will
9306 need to act on them together. */
9307 b->related_breakpoint = scope_breakpoint;
9308 scope_breakpoint->related_breakpoint = b;
c906108c 9309 }
d983da9c 9310
06a64a0b
TT
9311 if (!just_location)
9312 value_free_to_mark (mark);
2d134ed3 9313
a9634178
TJB
9314 TRY_CATCH (e, RETURN_MASK_ALL)
9315 {
9316 /* Finally update the new watchpoint. This creates the locations
9317 that should be inserted. */
3a5c3e22 9318 update_watchpoint (w, 1);
a9634178
TJB
9319 }
9320 if (e.reason < 0)
9321 {
9322 delete_breakpoint (b);
9323 throw_exception (e);
9324 }
9325
3ea46bff 9326 install_breakpoint (internal, b, 1);
c906108c
SS
9327}
9328
e09342b5 9329/* Return count of debug registers needed to watch the given expression.
e09342b5 9330 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9331
c906108c 9332static int
a9634178 9333can_use_hardware_watchpoint (struct value *v)
c906108c
SS
9334{
9335 int found_memory_cnt = 0;
2e70b7b9 9336 struct value *head = v;
c906108c
SS
9337
9338 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9339 if (!can_use_hw_watchpoints)
c906108c 9340 return 0;
c5aa993b 9341
5c44784c
JM
9342 /* Make sure that the value of the expression depends only upon
9343 memory contents, and values computed from them within GDB. If we
9344 find any register references or function calls, we can't use a
9345 hardware watchpoint.
9346
9347 The idea here is that evaluating an expression generates a series
9348 of values, one holding the value of every subexpression. (The
9349 expression a*b+c has five subexpressions: a, b, a*b, c, and
9350 a*b+c.) GDB's values hold almost enough information to establish
9351 the criteria given above --- they identify memory lvalues,
9352 register lvalues, computed values, etcetera. So we can evaluate
9353 the expression, and then scan the chain of values that leaves
9354 behind to decide whether we can detect any possible change to the
9355 expression's final value using only hardware watchpoints.
9356
9357 However, I don't think that the values returned by inferior
9358 function calls are special in any way. So this function may not
9359 notice that an expression involving an inferior function call
9360 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9361 for (; v; v = value_next (v))
c906108c 9362 {
5c44784c 9363 if (VALUE_LVAL (v) == lval_memory)
c906108c 9364 {
8464be76
DJ
9365 if (v != head && value_lazy (v))
9366 /* A lazy memory lvalue in the chain is one that GDB never
9367 needed to fetch; we either just used its address (e.g.,
9368 `a' in `a.b') or we never needed it at all (e.g., `a'
9369 in `a,b'). This doesn't apply to HEAD; if that is
9370 lazy then it was not readable, but watch it anyway. */
5c44784c 9371 ;
53a5351d 9372 else
5c44784c
JM
9373 {
9374 /* Ahh, memory we actually used! Check if we can cover
9375 it with hardware watchpoints. */
df407dfe 9376 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9377
9378 /* We only watch structs and arrays if user asked for it
9379 explicitly, never if they just happen to appear in a
9380 middle of some value chain. */
9381 if (v == head
9382 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9383 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9384 {
42ae5230 9385 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9386 int len;
9387 int num_regs;
9388
a9634178 9389 len = (target_exact_watchpoints
e09342b5
TJB
9390 && is_scalar_type_recursive (vtype))?
9391 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9392
e09342b5
TJB
9393 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9394 if (!num_regs)
2e70b7b9
MS
9395 return 0;
9396 else
e09342b5 9397 found_memory_cnt += num_regs;
2e70b7b9 9398 }
5c44784c 9399 }
c5aa993b 9400 }
5086187c
AC
9401 else if (VALUE_LVAL (v) != not_lval
9402 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9403 return 0; /* These are values from the history (e.g., $1). */
5086187c 9404 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9405 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9406 }
9407
9408 /* The expression itself looks suitable for using a hardware
9409 watchpoint, but give the target machine a chance to reject it. */
9410 return found_memory_cnt;
9411}
9412
8b93c638 9413void
84f4c1fe 9414watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9415{
84f4c1fe 9416 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9417}
9418
9419/* A helper function that looks for an argument at the start of a
9420 string. The argument must also either be at the end of the string,
9421 or be followed by whitespace. Returns 1 if it finds the argument,
9422 0 otherwise. If the argument is found, it updates *STR. */
9423
9424static int
9425check_for_argument (char **str, char *arg, int arg_len)
9426{
9427 if (strncmp (*str, arg, arg_len) == 0
9428 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9429 {
9430 *str += arg_len;
9431 return 1;
9432 }
9433 return 0;
9434}
9435
9436/* A helper function that looks for the "-location" argument and then
9437 calls watch_command_1. */
9438
9439static void
9440watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9441{
9442 int just_location = 0;
9443
9444 if (arg
9445 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9446 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9447 {
e9cafbcc 9448 arg = skip_spaces (arg);
06a64a0b
TT
9449 just_location = 1;
9450 }
9451
84f4c1fe 9452 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9453}
8926118c 9454
c5aa993b 9455static void
fba45db2 9456watch_command (char *arg, int from_tty)
c906108c 9457{
06a64a0b 9458 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9459}
9460
8b93c638 9461void
84f4c1fe 9462rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9463{
84f4c1fe 9464 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9465}
8926118c 9466
c5aa993b 9467static void
fba45db2 9468rwatch_command (char *arg, int from_tty)
c906108c 9469{
06a64a0b 9470 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9471}
9472
8b93c638 9473void
84f4c1fe 9474awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9475{
84f4c1fe 9476 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9477}
8926118c 9478
c5aa993b 9479static void
fba45db2 9480awatch_command (char *arg, int from_tty)
c906108c 9481{
06a64a0b 9482 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9483}
c906108c 9484\f
c5aa993b 9485
43ff13b4 9486/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9487 because it uses the mechanisms of breakpoints. */
9488
bfec99b2
PA
9489struct until_break_command_continuation_args
9490{
9491 struct breakpoint *breakpoint;
9492 struct breakpoint *breakpoint2;
186c406b 9493 int thread_num;
bfec99b2
PA
9494};
9495
43ff13b4 9496/* This function is called by fetch_inferior_event via the
4a64f543 9497 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9498 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9499 command. */
c2c6d25f 9500static void
fa4cd53f 9501until_break_command_continuation (void *arg, int err)
43ff13b4 9502{
bfec99b2
PA
9503 struct until_break_command_continuation_args *a = arg;
9504
9505 delete_breakpoint (a->breakpoint);
9506 if (a->breakpoint2)
9507 delete_breakpoint (a->breakpoint2);
186c406b 9508 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9509}
9510
c906108c 9511void
ae66c1fc 9512until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9513{
9514 struct symtabs_and_lines sals;
9515 struct symtab_and_line sal;
206415a3 9516 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9517 struct breakpoint *breakpoint;
f107f563 9518 struct breakpoint *breakpoint2 = NULL;
c906108c 9519 struct cleanup *old_chain;
186c406b
TT
9520 int thread;
9521 struct thread_info *tp;
c906108c
SS
9522
9523 clear_proceed_status ();
9524
9525 /* Set a breakpoint where the user wants it and at return from
4a64f543 9526 this function. */
c5aa993b 9527
1bfeeb0f 9528 if (last_displayed_sal_is_valid ())
f8eba3c6 9529 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 9530 get_last_displayed_symtab (),
f8eba3c6 9531 get_last_displayed_line ());
c906108c 9532 else
f8eba3c6
TT
9533 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
9534 (struct symtab *) NULL, 0);
c5aa993b 9535
c906108c 9536 if (sals.nelts != 1)
8a3fe4f8 9537 error (_("Couldn't get information on specified line."));
c5aa993b 9538
c906108c 9539 sal = sals.sals[0];
4a64f543 9540 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9541
c906108c 9542 if (*arg)
8a3fe4f8 9543 error (_("Junk at end of arguments."));
c5aa993b 9544
c906108c 9545 resolve_sal_pc (&sal);
c5aa993b 9546
ae66c1fc
EZ
9547 if (anywhere)
9548 /* If the user told us to continue until a specified location,
9549 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9550 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9551 null_frame_id, bp_until);
ae66c1fc 9552 else
4a64f543
MS
9553 /* Otherwise, specify the selected frame, because we want to stop
9554 only at the very same frame. */
a6d9a66e
UW
9555 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9556 get_stack_frame_id (frame),
ae66c1fc 9557 bp_until);
c5aa993b 9558
f107f563 9559 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9560
186c406b
TT
9561 tp = inferior_thread ();
9562 thread = tp->num;
9563
ae66c1fc
EZ
9564 /* Keep within the current frame, or in frames called by the current
9565 one. */
edb3359d
DJ
9566
9567 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9568 {
edb3359d
DJ
9569 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9570 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9571 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9572 sal,
edb3359d 9573 frame_unwind_caller_id (frame),
f107f563
VP
9574 bp_until);
9575 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9576
9577 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9578 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9579 }
c5aa993b 9580
c906108c 9581 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9582
4a64f543
MS
9583 /* If we are running asynchronously, and proceed call above has
9584 actually managed to start the target, arrange for breakpoints to
9585 be deleted when the target stops. Otherwise, we're already
9586 stopped and delete breakpoints via cleanup chain. */
f107f563 9587
8ea051c5 9588 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9589 {
bfec99b2
PA
9590 struct until_break_command_continuation_args *args;
9591 args = xmalloc (sizeof (*args));
f107f563 9592
bfec99b2
PA
9593 args->breakpoint = breakpoint;
9594 args->breakpoint2 = breakpoint2;
186c406b 9595 args->thread_num = thread;
f107f563
VP
9596
9597 discard_cleanups (old_chain);
95e54da7
PA
9598 add_continuation (inferior_thread (),
9599 until_break_command_continuation, args,
604ead4a 9600 xfree);
f107f563
VP
9601 }
9602 else
c5aa993b 9603 do_cleanups (old_chain);
c906108c 9604}
ae66c1fc 9605
c906108c
SS
9606/* This function attempts to parse an optional "if <cond>" clause
9607 from the arg string. If one is not found, it returns NULL.
c5aa993b 9608
c906108c
SS
9609 Else, it returns a pointer to the condition string. (It does not
9610 attempt to evaluate the string against a particular block.) And,
9611 it updates arg to point to the first character following the parsed
4a64f543 9612 if clause in the arg string. */
53a5351d 9613
c906108c 9614static char *
fba45db2 9615ep_parse_optional_if_clause (char **arg)
c906108c 9616{
c5aa993b
JM
9617 char *cond_string;
9618
9619 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9620 return NULL;
c5aa993b 9621
4a64f543 9622 /* Skip the "if" keyword. */
c906108c 9623 (*arg) += 2;
c5aa993b 9624
c906108c 9625 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9626 condition string. */
e9cafbcc 9627 *arg = skip_spaces (*arg);
c906108c 9628 cond_string = *arg;
c5aa993b 9629
4a64f543
MS
9630 /* Assume that the condition occupies the remainder of the arg
9631 string. */
c906108c 9632 (*arg) += strlen (cond_string);
c5aa993b 9633
c906108c
SS
9634 return cond_string;
9635}
c5aa993b 9636
c906108c
SS
9637/* Commands to deal with catching events, such as signals, exceptions,
9638 process start/exit, etc. */
c5aa993b
JM
9639
9640typedef enum
9641{
44feb3ce
TT
9642 catch_fork_temporary, catch_vfork_temporary,
9643 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9644}
9645catch_fork_kind;
9646
c906108c 9647static void
cc59ec59
MS
9648catch_fork_command_1 (char *arg, int from_tty,
9649 struct cmd_list_element *command)
c906108c 9650{
a6d9a66e 9651 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9652 char *cond_string = NULL;
44feb3ce
TT
9653 catch_fork_kind fork_kind;
9654 int tempflag;
9655
9656 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9657 tempflag = (fork_kind == catch_fork_temporary
9658 || fork_kind == catch_vfork_temporary);
c5aa993b 9659
44feb3ce
TT
9660 if (!arg)
9661 arg = "";
e9cafbcc 9662 arg = skip_spaces (arg);
c5aa993b 9663
c906108c 9664 /* The allowed syntax is:
c5aa993b
JM
9665 catch [v]fork
9666 catch [v]fork if <cond>
9667
4a64f543 9668 First, check if there's an if clause. */
c906108c 9669 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9670
c906108c 9671 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9672 error (_("Junk at end of arguments."));
c5aa993b 9673
c906108c 9674 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9675 and enable reporting of such events. */
c5aa993b
JM
9676 switch (fork_kind)
9677 {
44feb3ce
TT
9678 case catch_fork_temporary:
9679 case catch_fork_permanent:
a6d9a66e 9680 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9681 &catch_fork_breakpoint_ops);
c906108c 9682 break;
44feb3ce
TT
9683 case catch_vfork_temporary:
9684 case catch_vfork_permanent:
a6d9a66e 9685 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9686 &catch_vfork_breakpoint_ops);
c906108c 9687 break;
c5aa993b 9688 default:
8a3fe4f8 9689 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9690 break;
c5aa993b 9691 }
c906108c
SS
9692}
9693
9694static void
cc59ec59
MS
9695catch_exec_command_1 (char *arg, int from_tty,
9696 struct cmd_list_element *command)
c906108c 9697{
b4d90040 9698 struct exec_catchpoint *c;
a6d9a66e 9699 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9700 int tempflag;
c5aa993b 9701 char *cond_string = NULL;
c906108c 9702
44feb3ce
TT
9703 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9704
9705 if (!arg)
9706 arg = "";
e9cafbcc 9707 arg = skip_spaces (arg);
c906108c
SS
9708
9709 /* The allowed syntax is:
c5aa993b
JM
9710 catch exec
9711 catch exec if <cond>
c906108c 9712
4a64f543 9713 First, check if there's an if clause. */
c906108c
SS
9714 cond_string = ep_parse_optional_if_clause (&arg);
9715
9716 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9717 error (_("Junk at end of arguments."));
c906108c 9718
b4d90040
PA
9719 c = XNEW (struct exec_catchpoint);
9720 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9721 &catch_exec_breakpoint_ops);
9722 c->exec_pathname = NULL;
9723
3ea46bff 9724 install_breakpoint (0, &c->base, 1);
c906108c 9725}
c5aa993b 9726
3086aeae 9727static enum print_stop_action
348d480f 9728print_it_exception_catchpoint (bpstat bs)
3086aeae 9729{
79a45e25 9730 struct ui_out *uiout = current_uiout;
348d480f 9731 struct breakpoint *b = bs->breakpoint_at;
ade92717 9732 int bp_temp, bp_throw;
3086aeae 9733
ade92717 9734 annotate_catchpoint (b->number);
3086aeae 9735
ade92717
AR
9736 bp_throw = strstr (b->addr_string, "throw") != NULL;
9737 if (b->loc->address != b->loc->requested_address)
9738 breakpoint_adjustment_warning (b->loc->requested_address,
9739 b->loc->address,
9740 b->number, 1);
df2b6d2d 9741 bp_temp = b->disposition == disp_del;
ade92717
AR
9742 ui_out_text (uiout,
9743 bp_temp ? "Temporary catchpoint "
9744 : "Catchpoint ");
9745 if (!ui_out_is_mi_like_p (uiout))
9746 ui_out_field_int (uiout, "bkptno", b->number);
9747 ui_out_text (uiout,
c0b37c48
AR
9748 bp_throw ? " (exception thrown), "
9749 : " (exception caught), ");
ade92717
AR
9750 if (ui_out_is_mi_like_p (uiout))
9751 {
9752 ui_out_field_string (uiout, "reason",
9753 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9754 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9755 ui_out_field_int (uiout, "bkptno", b->number);
9756 }
3086aeae
DJ
9757 return PRINT_SRC_AND_LOC;
9758}
9759
9760static void
cc59ec59
MS
9761print_one_exception_catchpoint (struct breakpoint *b,
9762 struct bp_location **last_loc)
3086aeae 9763{
79a45b7d 9764 struct value_print_options opts;
79a45e25 9765 struct ui_out *uiout = current_uiout;
cc59ec59 9766
79a45b7d
TT
9767 get_user_print_options (&opts);
9768 if (opts.addressprint)
3086aeae
DJ
9769 {
9770 annotate_field (4);
604133b5
AR
9771 if (b->loc == NULL || b->loc->shlib_disabled)
9772 ui_out_field_string (uiout, "addr", "<PENDING>");
9773 else
5af949e3
UW
9774 ui_out_field_core_addr (uiout, "addr",
9775 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9776 }
9777 annotate_field (5);
604133b5 9778 if (b->loc)
a6d9a66e 9779 *last_loc = b->loc;
3086aeae
DJ
9780 if (strstr (b->addr_string, "throw") != NULL)
9781 ui_out_field_string (uiout, "what", "exception throw");
9782 else
9783 ui_out_field_string (uiout, "what", "exception catch");
9784}
9785
9786static void
9787print_mention_exception_catchpoint (struct breakpoint *b)
9788{
79a45e25 9789 struct ui_out *uiout = current_uiout;
ade92717
AR
9790 int bp_temp;
9791 int bp_throw;
9792
df2b6d2d 9793 bp_temp = b->disposition == disp_del;
ade92717
AR
9794 bp_throw = strstr (b->addr_string, "throw") != NULL;
9795 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9796 : _("Catchpoint "));
9797 ui_out_field_int (uiout, "bkptno", b->number);
9798 ui_out_text (uiout, bp_throw ? _(" (throw)")
9799 : _(" (catch)"));
3086aeae
DJ
9800}
9801
6149aea9
PA
9802/* Implement the "print_recreate" breakpoint_ops method for throw and
9803 catch catchpoints. */
9804
9805static void
4a64f543
MS
9806print_recreate_exception_catchpoint (struct breakpoint *b,
9807 struct ui_file *fp)
6149aea9
PA
9808{
9809 int bp_temp;
9810 int bp_throw;
9811
9812 bp_temp = b->disposition == disp_del;
9813 bp_throw = strstr (b->addr_string, "throw") != NULL;
9814 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9815 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 9816 print_recreate_thread (b, fp);
6149aea9
PA
9817}
9818
2060206e 9819static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
9820
9821static int
9822handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9823 enum exception_event_kind ex_event, int from_tty)
9824{
604133b5
AR
9825 char *trigger_func_name;
9826
3086aeae 9827 if (ex_event == EX_EVENT_CATCH)
604133b5 9828 trigger_func_name = "__cxa_begin_catch";
3086aeae 9829 else
604133b5 9830 trigger_func_name = "__cxa_throw";
3086aeae 9831
8cdf0e15
VP
9832 create_breakpoint (get_current_arch (),
9833 trigger_func_name, cond_string, -1,
9834 0 /* condition and thread are valid. */,
0fb4aa4b 9835 tempflag, bp_breakpoint,
8cdf0e15
VP
9836 0,
9837 AUTO_BOOLEAN_TRUE /* pending */,
9838 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9839 1 /* enabled */,
9840 0 /* internal */);
3086aeae 9841
3086aeae
DJ
9842 return 1;
9843}
9844
4a64f543 9845/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9846
9847static void
fba45db2
KB
9848catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9849 int tempflag, int from_tty)
c906108c 9850{
c5aa993b 9851 char *cond_string = NULL;
c5aa993b 9852
44feb3ce
TT
9853 if (!arg)
9854 arg = "";
e9cafbcc 9855 arg = skip_spaces (arg);
c5aa993b 9856
c906108c
SS
9857 cond_string = ep_parse_optional_if_clause (&arg);
9858
9859 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9860 error (_("Junk at end of arguments."));
c906108c 9861
059fb39f
PM
9862 if (ex_event != EX_EVENT_THROW
9863 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9864 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9865
3086aeae
DJ
9866 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9867 return;
9868
8a3fe4f8 9869 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9870}
9871
44feb3ce
TT
9872/* Implementation of "catch catch" command. */
9873
9874static void
9875catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9876{
9877 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9878
44feb3ce
TT
9879 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9880}
9881
9882/* Implementation of "catch throw" command. */
9883
9884static void
9885catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9886{
9887 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9888
44feb3ce
TT
9889 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9890}
9891
9ac4176b 9892void
28010a5d
PA
9893init_ada_exception_breakpoint (struct breakpoint *b,
9894 struct gdbarch *gdbarch,
9895 struct symtab_and_line sal,
9896 char *addr_string,
c0a91b2b 9897 const struct breakpoint_ops *ops,
28010a5d
PA
9898 int tempflag,
9899 int from_tty)
f7f9143b 9900{
f7f9143b
JB
9901 if (from_tty)
9902 {
5af949e3
UW
9903 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9904 if (!loc_gdbarch)
9905 loc_gdbarch = gdbarch;
9906
6c95b8df
PA
9907 describe_other_breakpoints (loc_gdbarch,
9908 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9909 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9910 version for exception catchpoints, because two catchpoints
9911 used for different exception names will use the same address.
9912 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9913 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 9914 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
9915 the user what type of catchpoint it is. The above is good
9916 enough for now, though. */
9917 }
9918
28010a5d 9919 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
9920
9921 b->enable_state = bp_enabled;
9922 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
9923 b->addr_string = addr_string;
9924 b->language = language_ada;
f7f9143b
JB
9925}
9926
a96d9b2e
SDJ
9927/* Splits the argument using space as delimiter. Returns an xmalloc'd
9928 filter list, or NULL if no filtering is required. */
9929static VEC(int) *
9930catch_syscall_split_args (char *arg)
9931{
9932 VEC(int) *result = NULL;
29d0bb3d 9933 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
9934
9935 while (*arg != '\0')
9936 {
9937 int i, syscall_number;
9938 char *endptr;
9939 char cur_name[128];
9940 struct syscall s;
9941
9942 /* Skip whitespace. */
9943 while (isspace (*arg))
9944 arg++;
9945
9946 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9947 cur_name[i] = arg[i];
9948 cur_name[i] = '\0';
9949 arg += i;
9950
9951 /* Check if the user provided a syscall name or a number. */
9952 syscall_number = (int) strtol (cur_name, &endptr, 0);
9953 if (*endptr == '\0')
bccd0dd2 9954 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9955 else
9956 {
9957 /* We have a name. Let's check if it's valid and convert it
9958 to a number. */
9959 get_syscall_by_name (cur_name, &s);
9960
9961 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9962 /* Here we have to issue an error instead of a warning,
9963 because GDB cannot do anything useful if there's no
9964 syscall number to be caught. */
a96d9b2e
SDJ
9965 error (_("Unknown syscall name '%s'."), cur_name);
9966 }
9967
9968 /* Ok, it's valid. */
9969 VEC_safe_push (int, result, s.number);
9970 }
9971
9972 discard_cleanups (cleanup);
9973 return result;
9974}
9975
9976/* Implement the "catch syscall" command. */
9977
9978static void
cc59ec59
MS
9979catch_syscall_command_1 (char *arg, int from_tty,
9980 struct cmd_list_element *command)
a96d9b2e
SDJ
9981{
9982 int tempflag;
9983 VEC(int) *filter;
9984 struct syscall s;
9985 struct gdbarch *gdbarch = get_current_arch ();
9986
9987 /* Checking if the feature if supported. */
9988 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9989 error (_("The feature 'catch syscall' is not supported on \
ea666128 9990this architecture yet."));
a96d9b2e
SDJ
9991
9992 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9993
e9cafbcc 9994 arg = skip_spaces (arg);
a96d9b2e
SDJ
9995
9996 /* We need to do this first "dummy" translation in order
9997 to get the syscall XML file loaded or, most important,
9998 to display a warning to the user if there's no XML file
9999 for his/her architecture. */
10000 get_syscall_by_number (0, &s);
10001
10002 /* The allowed syntax is:
10003 catch syscall
10004 catch syscall <name | number> [<name | number> ... <name | number>]
10005
10006 Let's check if there's a syscall name. */
10007
10008 if (arg != NULL)
10009 filter = catch_syscall_split_args (arg);
10010 else
10011 filter = NULL;
10012
10013 create_syscall_event_catchpoint (tempflag, filter,
10014 &catch_syscall_breakpoint_ops);
10015}
10016
c906108c 10017static void
fba45db2 10018catch_command (char *arg, int from_tty)
c906108c 10019{
44feb3ce 10020 error (_("Catch requires an event name."));
c906108c
SS
10021}
10022\f
10023
10024static void
fba45db2 10025tcatch_command (char *arg, int from_tty)
c906108c 10026{
44feb3ce 10027 error (_("Catch requires an event name."));
c906108c
SS
10028}
10029
8a2c437b
TT
10030/* A qsort comparison function that sorts breakpoints in order. */
10031
10032static int
10033compare_breakpoints (const void *a, const void *b)
10034{
10035 const breakpoint_p *ba = a;
10036 uintptr_t ua = (uintptr_t) *ba;
10037 const breakpoint_p *bb = b;
10038 uintptr_t ub = (uintptr_t) *bb;
10039
10040 if ((*ba)->number < (*bb)->number)
10041 return -1;
10042 else if ((*ba)->number > (*bb)->number)
10043 return 1;
10044
10045 /* Now sort by address, in case we see, e..g, two breakpoints with
10046 the number 0. */
10047 if (ua < ub)
10048 return -1;
10049 return ub > ub ? 1 : 0;
10050}
10051
80f8a6eb 10052/* Delete breakpoints by address or line. */
c906108c
SS
10053
10054static void
fba45db2 10055clear_command (char *arg, int from_tty)
c906108c 10056{
8a2c437b 10057 struct breakpoint *b, *prev;
d6e956e5
VP
10058 VEC(breakpoint_p) *found = 0;
10059 int ix;
c906108c
SS
10060 int default_match;
10061 struct symtabs_and_lines sals;
10062 struct symtab_and_line sal;
c906108c 10063 int i;
8a2c437b 10064 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
10065
10066 if (arg)
10067 {
f8eba3c6
TT
10068 sals = decode_line_spec (arg, (DECODE_LINE_FUNFIRSTLINE
10069 | DECODE_LINE_LIST_MODE));
c906108c
SS
10070 default_match = 0;
10071 }
10072 else
10073 {
c5aa993b 10074 sals.sals = (struct symtab_and_line *)
c906108c 10075 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 10076 make_cleanup (xfree, sals.sals);
4a64f543 10077 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
10078
10079 /* Set sal's line, symtab, pc, and pspace to the values
10080 corresponding to the last call to print_frame_info. If the
10081 codepoint is not valid, this will set all the fields to 0. */
10082 get_last_displayed_sal (&sal);
c906108c 10083 if (sal.symtab == 0)
8a3fe4f8 10084 error (_("No source file specified."));
c906108c
SS
10085
10086 sals.sals[0] = sal;
10087 sals.nelts = 1;
10088
10089 default_match = 1;
10090 }
10091
4a64f543
MS
10092 /* We don't call resolve_sal_pc here. That's not as bad as it
10093 seems, because all existing breakpoints typically have both
10094 file/line and pc set. So, if clear is given file/line, we can
10095 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10096
10097 We only support clearing given the address explicitly
10098 present in breakpoint table. Say, we've set breakpoint
4a64f543 10099 at file:line. There were several PC values for that file:line,
ed0616c6 10100 due to optimization, all in one block.
4a64f543
MS
10101
10102 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10103 PC corresponding to the same file:line, the breakpoint won't
10104 be cleared. We probably can still clear the breakpoint, but
10105 since the other PC value is never presented to user, user
10106 can only find it by guessing, and it does not seem important
10107 to support that. */
10108
4a64f543
MS
10109 /* For each line spec given, delete bps which correspond to it. Do
10110 it in two passes, solely to preserve the current behavior that
10111 from_tty is forced true if we delete more than one
10112 breakpoint. */
c906108c 10113
80f8a6eb 10114 found = NULL;
8a2c437b 10115 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
10116 for (i = 0; i < sals.nelts; i++)
10117 {
10118 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
10119 If line given (pc == 0), clear all bpts on specified line.
10120 If defaulting, clear all bpts on default line
c906108c 10121 or at default pc.
c5aa993b
JM
10122
10123 defaulting sal.pc != 0 tests to do
10124
10125 0 1 pc
10126 1 1 pc _and_ line
10127 0 0 line
10128 1 0 <can't happen> */
c906108c
SS
10129
10130 sal = sals.sals[i];
c906108c 10131
4a64f543 10132 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 10133 ALL_BREAKPOINTS (b)
c5aa993b 10134 {
0d381245 10135 int match = 0;
4a64f543 10136 /* Are we going to delete b? */
cc60f2e3 10137 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
10138 {
10139 struct bp_location *loc = b->loc;
10140 for (; loc; loc = loc->next)
10141 {
f8eba3c6
TT
10142 /* If the user specified file:line, don't allow a PC
10143 match. This matches historical gdb behavior. */
10144 int pc_match = (!sal.explicit_line
10145 && sal.pc
10146 && (loc->pspace == sal.pspace)
10147 && (loc->address == sal.pc)
10148 && (!section_is_overlay (loc->section)
10149 || loc->section == sal.section));
10150 int line_match = ((default_match || sal.explicit_line)
10151 && loc->source_file != NULL
0d381245 10152 && sal.symtab != NULL
6c95b8df 10153 && sal.pspace == loc->pspace
f8eba3c6 10154 && filename_cmp (loc->source_file,
0ba1096a 10155 sal.symtab->filename) == 0
f8eba3c6 10156 && loc->line_number == sal.line);
0d381245
VP
10157 if (pc_match || line_match)
10158 {
10159 match = 1;
10160 break;
10161 }
10162 }
10163 }
10164
10165 if (match)
d6e956e5 10166 VEC_safe_push(breakpoint_p, found, b);
c906108c 10167 }
80f8a6eb 10168 }
8a2c437b 10169
80f8a6eb 10170 /* Now go thru the 'found' chain and delete them. */
d6e956e5 10171 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
10172 {
10173 if (arg)
8a3fe4f8 10174 error (_("No breakpoint at %s."), arg);
80f8a6eb 10175 else
8a3fe4f8 10176 error (_("No breakpoint at this line."));
80f8a6eb 10177 }
c906108c 10178
8a2c437b
TT
10179 /* Remove duplicates from the vec. */
10180 qsort (VEC_address (breakpoint_p, found),
10181 VEC_length (breakpoint_p, found),
10182 sizeof (breakpoint_p),
10183 compare_breakpoints);
10184 prev = VEC_index (breakpoint_p, found, 0);
10185 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
10186 {
10187 if (b == prev)
10188 {
10189 VEC_ordered_remove (breakpoint_p, found, ix);
10190 --ix;
10191 }
10192 }
10193
d6e956e5 10194 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 10195 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10196 if (from_tty)
a3f17187 10197 {
d6e956e5 10198 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10199 printf_unfiltered (_("Deleted breakpoint "));
10200 else
10201 printf_unfiltered (_("Deleted breakpoints "));
10202 }
80f8a6eb 10203 breakpoints_changed ();
d6e956e5
VP
10204
10205 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10206 {
c5aa993b 10207 if (from_tty)
d6e956e5
VP
10208 printf_unfiltered ("%d ", b->number);
10209 delete_breakpoint (b);
c906108c 10210 }
80f8a6eb
MS
10211 if (from_tty)
10212 putchar_unfiltered ('\n');
8a2c437b
TT
10213
10214 do_cleanups (cleanups);
c906108c
SS
10215}
10216\f
10217/* Delete breakpoint in BS if they are `delete' breakpoints and
10218 all breakpoints that are marked for deletion, whether hit or not.
10219 This is called after any breakpoint is hit, or after errors. */
10220
10221void
fba45db2 10222breakpoint_auto_delete (bpstat bs)
c906108c 10223{
35df4500 10224 struct breakpoint *b, *b_tmp;
c906108c
SS
10225
10226 for (; bs; bs = bs->next)
f431efe5
PA
10227 if (bs->breakpoint_at
10228 && bs->breakpoint_at->disposition == disp_del
c906108c 10229 && bs->stop)
f431efe5 10230 delete_breakpoint (bs->breakpoint_at);
c906108c 10231
35df4500 10232 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10233 {
b5de0fa7 10234 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10235 delete_breakpoint (b);
10236 }
c906108c
SS
10237}
10238
4a64f543
MS
10239/* A comparison function for bp_location AP and BP being interfaced to
10240 qsort. Sort elements primarily by their ADDRESS (no matter what
10241 does breakpoint_address_is_meaningful say for its OWNER),
10242 secondarily by ordering first bp_permanent OWNERed elements and
10243 terciarily just ensuring the array is sorted stable way despite
e5dd4106 10244 qsort being an unstable algorithm. */
876fa593
JK
10245
10246static int
494cfb0f 10247bp_location_compare (const void *ap, const void *bp)
876fa593 10248{
494cfb0f
JK
10249 struct bp_location *a = *(void **) ap;
10250 struct bp_location *b = *(void **) bp;
2bdf28a0 10251 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10252 int a_perm = a->owner->enable_state == bp_permanent;
10253 int b_perm = b->owner->enable_state == bp_permanent;
10254
10255 if (a->address != b->address)
10256 return (a->address > b->address) - (a->address < b->address);
10257
10258 /* Sort permanent breakpoints first. */
10259 if (a_perm != b_perm)
10260 return (a_perm < b_perm) - (a_perm > b_perm);
10261
4a64f543
MS
10262 /* Make the user-visible order stable across GDB runs. Locations of
10263 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10264
10265 if (a->owner->number != b->owner->number)
10266 return (a->owner->number > b->owner->number)
10267 - (a->owner->number < b->owner->number);
10268
10269 return (a > b) - (a < b);
10270}
10271
876fa593 10272/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10273 bp_location_shadow_len_after_address_max according to the current
10274 content of the bp_location array. */
f7545552
TT
10275
10276static void
876fa593 10277bp_location_target_extensions_update (void)
f7545552 10278{
876fa593
JK
10279 struct bp_location *bl, **blp_tmp;
10280
10281 bp_location_placed_address_before_address_max = 0;
10282 bp_location_shadow_len_after_address_max = 0;
10283
10284 ALL_BP_LOCATIONS (bl, blp_tmp)
10285 {
10286 CORE_ADDR start, end, addr;
10287
10288 if (!bp_location_has_shadow (bl))
10289 continue;
10290
10291 start = bl->target_info.placed_address;
10292 end = start + bl->target_info.shadow_len;
10293
10294 gdb_assert (bl->address >= start);
10295 addr = bl->address - start;
10296 if (addr > bp_location_placed_address_before_address_max)
10297 bp_location_placed_address_before_address_max = addr;
10298
10299 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10300
10301 gdb_assert (bl->address < end);
10302 addr = end - bl->address;
10303 if (addr > bp_location_shadow_len_after_address_max)
10304 bp_location_shadow_len_after_address_max = addr;
10305 }
f7545552
TT
10306}
10307
1e4d1764
YQ
10308/* Download tracepoint locations if they haven't been. */
10309
10310static void
10311download_tracepoint_locations (void)
10312{
10313 struct bp_location *bl, **blp_tmp;
10314 struct cleanup *old_chain;
10315
10316 if (!target_can_download_tracepoint ())
10317 return;
10318
10319 old_chain = save_current_space_and_thread ();
10320
10321 ALL_BP_LOCATIONS (bl, blp_tmp)
10322 {
10323 struct tracepoint *t;
10324
10325 if (!is_tracepoint (bl->owner))
10326 continue;
10327
10328 if ((bl->owner->type == bp_fast_tracepoint
10329 ? !may_insert_fast_tracepoints
10330 : !may_insert_tracepoints))
10331 continue;
10332
10333 /* In tracepoint, locations are _never_ duplicated, so
10334 should_be_inserted is equivalent to
10335 unduplicated_should_be_inserted. */
10336 if (!should_be_inserted (bl) || bl->inserted)
10337 continue;
10338
10339 switch_to_program_space_and_thread (bl->pspace);
10340
10341 target_download_tracepoint (bl);
10342
10343 bl->inserted = 1;
10344 t = (struct tracepoint *) bl->owner;
10345 t->number_on_target = bl->owner->number;
10346 }
10347
10348 do_cleanups (old_chain);
10349}
10350
934709f0
PW
10351/* Swap the insertion/duplication state between two locations. */
10352
10353static void
10354swap_insertion (struct bp_location *left, struct bp_location *right)
10355{
10356 const int left_inserted = left->inserted;
10357 const int left_duplicate = left->duplicate;
10358 const struct bp_target_info left_target_info = left->target_info;
10359
1e4d1764
YQ
10360 /* Locations of tracepoints can never be duplicated. */
10361 if (is_tracepoint (left->owner))
10362 gdb_assert (!left->duplicate);
10363 if (is_tracepoint (right->owner))
10364 gdb_assert (!right->duplicate);
10365
934709f0
PW
10366 left->inserted = right->inserted;
10367 left->duplicate = right->duplicate;
10368 left->target_info = right->target_info;
10369 right->inserted = left_inserted;
10370 right->duplicate = left_duplicate;
10371 right->target_info = left_target_info;
10372}
10373
4cd9bd08 10374/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10375 into the inferior, only remove already-inserted locations that no
10376 longer should be inserted. Functions that delete a breakpoint or
10377 breakpoints should pass false, so that deleting a breakpoint
10378 doesn't have the side effect of inserting the locations of other
10379 breakpoints that are marked not-inserted, but should_be_inserted
10380 returns true on them.
10381
10382 This behaviour is useful is situations close to tear-down -- e.g.,
10383 after an exec, while the target still has execution, but breakpoint
10384 shadows of the previous executable image should *NOT* be restored
10385 to the new image; or before detaching, where the target still has
10386 execution and wants to delete breakpoints from GDB's lists, and all
10387 breakpoints had already been removed from the inferior. */
10388
0d381245 10389static void
b60e7edf 10390update_global_location_list (int should_insert)
0d381245 10391{
74960c60 10392 struct breakpoint *b;
876fa593 10393 struct bp_location **locp, *loc;
f7545552
TT
10394 struct cleanup *cleanups;
10395
2d134ed3
PA
10396 /* Used in the duplicates detection below. When iterating over all
10397 bp_locations, points to the first bp_location of a given address.
10398 Breakpoints and watchpoints of different types are never
10399 duplicates of each other. Keep one pointer for each type of
10400 breakpoint/watchpoint, so we only need to loop over all locations
10401 once. */
10402 struct bp_location *bp_loc_first; /* breakpoint */
10403 struct bp_location *wp_loc_first; /* hardware watchpoint */
10404 struct bp_location *awp_loc_first; /* access watchpoint */
10405 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10406
4a64f543
MS
10407 /* Saved former bp_location array which we compare against the newly
10408 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10409 struct bp_location **old_location, **old_locp;
10410 unsigned old_location_count;
10411
10412 old_location = bp_location;
10413 old_location_count = bp_location_count;
10414 bp_location = NULL;
10415 bp_location_count = 0;
10416 cleanups = make_cleanup (xfree, old_location);
0d381245 10417
74960c60 10418 ALL_BREAKPOINTS (b)
876fa593
JK
10419 for (loc = b->loc; loc; loc = loc->next)
10420 bp_location_count++;
10421
10422 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10423 locp = bp_location;
10424 ALL_BREAKPOINTS (b)
10425 for (loc = b->loc; loc; loc = loc->next)
10426 *locp++ = loc;
10427 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10428 bp_location_compare);
876fa593
JK
10429
10430 bp_location_target_extensions_update ();
74960c60 10431
4a64f543
MS
10432 /* Identify bp_location instances that are no longer present in the
10433 new list, and therefore should be freed. Note that it's not
10434 necessary that those locations should be removed from inferior --
10435 if there's another location at the same address (previously
10436 marked as duplicate), we don't need to remove/insert the
10437 location.
876fa593 10438
4a64f543
MS
10439 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10440 and former bp_location array state respectively. */
876fa593
JK
10441
10442 locp = bp_location;
10443 for (old_locp = old_location; old_locp < old_location + old_location_count;
10444 old_locp++)
74960c60 10445 {
876fa593 10446 struct bp_location *old_loc = *old_locp;
c7d46a38 10447 struct bp_location **loc2p;
876fa593 10448
e5dd4106 10449 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10450 not, we have to free it. */
c7d46a38 10451 int found_object = 0;
20874c92
VP
10452 /* Tells if the location should remain inserted in the target. */
10453 int keep_in_target = 0;
10454 int removed = 0;
876fa593 10455
4a64f543
MS
10456 /* Skip LOCP entries which will definitely never be needed.
10457 Stop either at or being the one matching OLD_LOC. */
876fa593 10458 while (locp < bp_location + bp_location_count
c7d46a38 10459 && (*locp)->address < old_loc->address)
876fa593 10460 locp++;
c7d46a38
PA
10461
10462 for (loc2p = locp;
10463 (loc2p < bp_location + bp_location_count
10464 && (*loc2p)->address == old_loc->address);
10465 loc2p++)
10466 {
10467 if (*loc2p == old_loc)
10468 {
10469 found_object = 1;
10470 break;
10471 }
10472 }
74960c60 10473
4a64f543
MS
10474 /* If this location is no longer present, and inserted, look if
10475 there's maybe a new location at the same address. If so,
10476 mark that one inserted, and don't remove this one. This is
10477 needed so that we don't have a time window where a breakpoint
10478 at certain location is not inserted. */
74960c60 10479
876fa593 10480 if (old_loc->inserted)
0d381245 10481 {
4a64f543
MS
10482 /* If the location is inserted now, we might have to remove
10483 it. */
74960c60 10484
876fa593 10485 if (found_object && should_be_inserted (old_loc))
74960c60 10486 {
4a64f543
MS
10487 /* The location is still present in the location list,
10488 and still should be inserted. Don't do anything. */
20874c92 10489 keep_in_target = 1;
74960c60
VP
10490 }
10491 else
10492 {
4a64f543
MS
10493 /* The location is either no longer present, or got
10494 disabled. See if there's another location at the
10495 same address, in which case we don't need to remove
10496 this one from the target. */
876fa593 10497
2bdf28a0 10498 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10499 if (breakpoint_address_is_meaningful (old_loc->owner))
10500 {
876fa593 10501 for (loc2p = locp;
c7d46a38
PA
10502 (loc2p < bp_location + bp_location_count
10503 && (*loc2p)->address == old_loc->address);
876fa593
JK
10504 loc2p++)
10505 {
10506 struct bp_location *loc2 = *loc2p;
10507
2d134ed3 10508 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 10509 {
85d721b8
PA
10510 /* Read watchpoint locations are switched to
10511 access watchpoints, if the former are not
10512 supported, but the latter are. */
10513 if (is_hardware_watchpoint (old_loc->owner))
10514 {
10515 gdb_assert (is_hardware_watchpoint (loc2->owner));
10516 loc2->watchpoint_type = old_loc->watchpoint_type;
10517 }
10518
934709f0
PW
10519 /* loc2 is a duplicated location. We need to check
10520 if it should be inserted in case it will be
10521 unduplicated. */
10522 if (loc2 != old_loc
10523 && unduplicated_should_be_inserted (loc2))
c7d46a38 10524 {
934709f0 10525 swap_insertion (old_loc, loc2);
c7d46a38
PA
10526 keep_in_target = 1;
10527 break;
10528 }
876fa593
JK
10529 }
10530 }
10531 }
74960c60
VP
10532 }
10533
20874c92
VP
10534 if (!keep_in_target)
10535 {
876fa593 10536 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10537 {
4a64f543
MS
10538 /* This is just about all we can do. We could keep
10539 this location on the global list, and try to
10540 remove it next time, but there's no particular
10541 reason why we will succeed next time.
20874c92 10542
4a64f543
MS
10543 Note that at this point, old_loc->owner is still
10544 valid, as delete_breakpoint frees the breakpoint
10545 only after calling us. */
3e43a32a
MS
10546 printf_filtered (_("warning: Error removing "
10547 "breakpoint %d\n"),
876fa593 10548 old_loc->owner->number);
20874c92
VP
10549 }
10550 removed = 1;
10551 }
0d381245 10552 }
74960c60
VP
10553
10554 if (!found_object)
1c5cfe86 10555 {
db82e815
PA
10556 if (removed && non_stop
10557 && breakpoint_address_is_meaningful (old_loc->owner)
10558 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10559 {
db82e815
PA
10560 /* This location was removed from the target. In
10561 non-stop mode, a race condition is possible where
10562 we've removed a breakpoint, but stop events for that
10563 breakpoint are already queued and will arrive later.
10564 We apply an heuristic to be able to distinguish such
10565 SIGTRAPs from other random SIGTRAPs: we keep this
10566 breakpoint location for a bit, and will retire it
10567 after we see some number of events. The theory here
10568 is that reporting of events should, "on the average",
10569 be fair, so after a while we'll see events from all
10570 threads that have anything of interest, and no longer
10571 need to keep this breakpoint location around. We
10572 don't hold locations forever so to reduce chances of
10573 mistaking a non-breakpoint SIGTRAP for a breakpoint
10574 SIGTRAP.
10575
10576 The heuristic failing can be disastrous on
10577 decr_pc_after_break targets.
10578
10579 On decr_pc_after_break targets, like e.g., x86-linux,
10580 if we fail to recognize a late breakpoint SIGTRAP,
10581 because events_till_retirement has reached 0 too
10582 soon, we'll fail to do the PC adjustment, and report
10583 a random SIGTRAP to the user. When the user resumes
10584 the inferior, it will most likely immediately crash
2dec564e 10585 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10586 corrupted, because of being resumed e.g., in the
10587 middle of a multi-byte instruction, or skipped a
10588 one-byte instruction. This was actually seen happen
10589 on native x86-linux, and should be less rare on
10590 targets that do not support new thread events, like
10591 remote, due to the heuristic depending on
10592 thread_count.
10593
10594 Mistaking a random SIGTRAP for a breakpoint trap
10595 causes similar symptoms (PC adjustment applied when
10596 it shouldn't), but then again, playing with SIGTRAPs
10597 behind the debugger's back is asking for trouble.
10598
10599 Since hardware watchpoint traps are always
10600 distinguishable from other traps, so we don't need to
10601 apply keep hardware watchpoint moribund locations
10602 around. We simply always ignore hardware watchpoint
10603 traps we can no longer explain. */
10604
876fa593
JK
10605 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10606 old_loc->owner = NULL;
20874c92 10607
876fa593 10608 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10609 }
10610 else
f431efe5
PA
10611 {
10612 old_loc->owner = NULL;
10613 decref_bp_location (&old_loc);
10614 }
20874c92 10615 }
74960c60 10616 }
1c5cfe86 10617
348d480f
PA
10618 /* Rescan breakpoints at the same address and section, marking the
10619 first one as "first" and any others as "duplicates". This is so
10620 that the bpt instruction is only inserted once. If we have a
10621 permanent breakpoint at the same place as BPT, make that one the
10622 official one, and the rest as duplicates. Permanent breakpoints
10623 are sorted first for the same address.
10624
10625 Do the same for hardware watchpoints, but also considering the
10626 watchpoint's type (regular/access/read) and length. */
10627
10628 bp_loc_first = NULL;
10629 wp_loc_first = NULL;
10630 awp_loc_first = NULL;
10631 rwp_loc_first = NULL;
10632 ALL_BP_LOCATIONS (loc, locp)
10633 {
10634 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10635 non-NULL. */
348d480f 10636 struct bp_location **loc_first_p;
d3fbdd86 10637 b = loc->owner;
348d480f 10638
f8eba3c6 10639 if (!should_be_inserted (loc)
348d480f 10640 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
10641 /* Don't detect duplicate for tracepoint locations because they are
10642 never duplicated. See the comments in field `duplicate' of
10643 `struct bp_location'. */
348d480f
PA
10644 || is_tracepoint (b))
10645 continue;
10646
10647 /* Permanent breakpoint should always be inserted. */
10648 if (b->enable_state == bp_permanent && ! loc->inserted)
10649 internal_error (__FILE__, __LINE__,
10650 _("allegedly permanent breakpoint is not "
10651 "actually inserted"));
10652
10653 if (b->type == bp_hardware_watchpoint)
10654 loc_first_p = &wp_loc_first;
10655 else if (b->type == bp_read_watchpoint)
10656 loc_first_p = &rwp_loc_first;
10657 else if (b->type == bp_access_watchpoint)
10658 loc_first_p = &awp_loc_first;
10659 else
10660 loc_first_p = &bp_loc_first;
10661
10662 if (*loc_first_p == NULL
10663 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10664 || !breakpoint_locations_match (loc, *loc_first_p))
10665 {
10666 *loc_first_p = loc;
10667 loc->duplicate = 0;
10668 continue;
10669 }
10670
934709f0
PW
10671
10672 /* This and the above ensure the invariant that the first location
10673 is not duplicated, and is the inserted one.
10674 All following are marked as duplicated, and are not inserted. */
10675 if (loc->inserted)
10676 swap_insertion (loc, *loc_first_p);
348d480f
PA
10677 loc->duplicate = 1;
10678
10679 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10680 && b->enable_state != bp_permanent)
10681 internal_error (__FILE__, __LINE__,
10682 _("another breakpoint was inserted on top of "
10683 "a permanent breakpoint"));
10684 }
10685
10686 if (breakpoints_always_inserted_mode () && should_insert
10687 && (have_live_inferiors ()
10688 || (gdbarch_has_global_breakpoints (target_gdbarch))))
10689 insert_breakpoint_locations ();
10690
1e4d1764
YQ
10691 if (should_insert)
10692 download_tracepoint_locations ();
10693
348d480f
PA
10694 do_cleanups (cleanups);
10695}
10696
10697void
10698breakpoint_retire_moribund (void)
10699{
10700 struct bp_location *loc;
10701 int ix;
10702
10703 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10704 if (--(loc->events_till_retirement) == 0)
10705 {
10706 decref_bp_location (&loc);
10707 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10708 --ix;
10709 }
10710}
10711
10712static void
10713update_global_location_list_nothrow (int inserting)
10714{
bfd189b1 10715 volatile struct gdb_exception e;
348d480f
PA
10716
10717 TRY_CATCH (e, RETURN_MASK_ERROR)
10718 update_global_location_list (inserting);
10719}
10720
10721/* Clear BKP from a BPS. */
10722
10723static void
10724bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10725{
10726 bpstat bs;
10727
10728 for (bs = bps; bs; bs = bs->next)
10729 if (bs->breakpoint_at == bpt)
10730 {
10731 bs->breakpoint_at = NULL;
10732 bs->old_val = NULL;
10733 /* bs->commands will be freed later. */
10734 }
10735}
10736
10737/* Callback for iterate_over_threads. */
10738static int
10739bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10740{
10741 struct breakpoint *bpt = data;
10742
10743 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10744 return 0;
10745}
10746
10747/* Helper for breakpoint and tracepoint breakpoint_ops->mention
10748 callbacks. */
10749
10750static void
10751say_where (struct breakpoint *b)
10752{
79a45e25 10753 struct ui_out *uiout = current_uiout;
348d480f
PA
10754 struct value_print_options opts;
10755
10756 get_user_print_options (&opts);
10757
10758 /* i18n: cagney/2005-02-11: Below needs to be merged into a
10759 single string. */
10760 if (b->loc == NULL)
10761 {
10762 printf_filtered (_(" (%s) pending."), b->addr_string);
10763 }
10764 else
10765 {
f8eba3c6 10766 if (opts.addressprint || b->loc->source_file == NULL)
348d480f
PA
10767 {
10768 printf_filtered (" at ");
10769 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10770 gdb_stdout);
10771 }
f8eba3c6
TT
10772 if (b->loc->source_file)
10773 {
10774 /* If there is a single location, we can print the location
10775 more nicely. */
10776 if (b->loc->next == NULL)
10777 printf_filtered (": file %s, line %d.",
10778 b->loc->source_file, b->loc->line_number);
10779 else
10780 /* This is not ideal, but each location may have a
10781 different file name, and this at least reflects the
10782 real situation somewhat. */
10783 printf_filtered (": %s.", b->addr_string);
10784 }
348d480f
PA
10785
10786 if (b->loc->next)
10787 {
10788 struct bp_location *loc = b->loc;
10789 int n = 0;
10790 for (; loc; loc = loc->next)
10791 ++n;
10792 printf_filtered (" (%d locations)", n);
10793 }
10794 }
10795}
10796
348d480f
PA
10797/* Default bp_location_ops methods. */
10798
10799static void
10800bp_location_dtor (struct bp_location *self)
10801{
10802 xfree (self->cond);
10803 xfree (self->function_name);
f8eba3c6 10804 xfree (self->source_file);
348d480f
PA
10805}
10806
10807static const struct bp_location_ops bp_location_ops =
10808{
10809 bp_location_dtor
10810};
10811
2060206e
PA
10812/* Default breakpoint_ops methods all breakpoint_ops ultimately
10813 inherit from. */
348d480f 10814
2060206e
PA
10815static void
10816base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
10817{
10818 decref_counted_command_line (&self->commands);
10819 xfree (self->cond_string);
348d480f 10820 xfree (self->addr_string);
f8eba3c6 10821 xfree (self->filter);
348d480f 10822 xfree (self->addr_string_range_end);
348d480f
PA
10823}
10824
2060206e
PA
10825static struct bp_location *
10826base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
10827{
10828 struct bp_location *loc;
10829
10830 loc = XNEW (struct bp_location);
10831 init_bp_location (loc, &bp_location_ops, self);
10832 return loc;
10833}
10834
2060206e
PA
10835static void
10836base_breakpoint_re_set (struct breakpoint *b)
10837{
10838 /* Nothing to re-set. */
10839}
10840
10841#define internal_error_pure_virtual_called() \
10842 gdb_assert_not_reached ("pure virtual function called")
10843
10844static int
10845base_breakpoint_insert_location (struct bp_location *bl)
10846{
10847 internal_error_pure_virtual_called ();
10848}
10849
10850static int
10851base_breakpoint_remove_location (struct bp_location *bl)
10852{
10853 internal_error_pure_virtual_called ();
10854}
10855
10856static int
10857base_breakpoint_breakpoint_hit (const struct bp_location *bl,
10858 struct address_space *aspace,
10859 CORE_ADDR bp_addr)
10860{
10861 internal_error_pure_virtual_called ();
10862}
10863
10864static void
10865base_breakpoint_check_status (bpstat bs)
10866{
10867 /* Always stop. */
10868}
10869
10870/* A "works_in_software_mode" breakpoint_ops method that just internal
10871 errors. */
10872
10873static int
10874base_breakpoint_works_in_software_mode (const struct breakpoint *b)
10875{
10876 internal_error_pure_virtual_called ();
10877}
10878
10879/* A "resources_needed" breakpoint_ops method that just internal
10880 errors. */
10881
10882static int
10883base_breakpoint_resources_needed (const struct bp_location *bl)
10884{
10885 internal_error_pure_virtual_called ();
10886}
10887
10888static enum print_stop_action
10889base_breakpoint_print_it (bpstat bs)
10890{
10891 internal_error_pure_virtual_called ();
10892}
10893
10894static void
10895base_breakpoint_print_one_detail (const struct breakpoint *self,
10896 struct ui_out *uiout)
10897{
10898 /* nothing */
10899}
10900
10901static void
10902base_breakpoint_print_mention (struct breakpoint *b)
10903{
10904 internal_error_pure_virtual_called ();
10905}
10906
10907static void
10908base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
10909{
10910 internal_error_pure_virtual_called ();
10911}
10912
10913static struct breakpoint_ops base_breakpoint_ops =
10914{
10915 base_breakpoint_dtor,
10916 base_breakpoint_allocate_location,
10917 base_breakpoint_re_set,
10918 base_breakpoint_insert_location,
10919 base_breakpoint_remove_location,
10920 base_breakpoint_breakpoint_hit,
10921 base_breakpoint_check_status,
10922 base_breakpoint_resources_needed,
10923 base_breakpoint_works_in_software_mode,
10924 base_breakpoint_print_it,
10925 NULL,
10926 base_breakpoint_print_one_detail,
10927 base_breakpoint_print_mention,
10928 base_breakpoint_print_recreate
10929};
10930
10931/* Default breakpoint_ops methods. */
10932
10933static void
348d480f
PA
10934bkpt_re_set (struct breakpoint *b)
10935{
06edf0c0
PA
10936 /* FIXME: is this still reachable? */
10937 if (b->addr_string == NULL)
10938 {
10939 /* Anything without a string can't be re-set. */
348d480f 10940 delete_breakpoint (b);
06edf0c0 10941 return;
348d480f 10942 }
06edf0c0
PA
10943
10944 breakpoint_re_set_default (b);
348d480f
PA
10945}
10946
2060206e 10947static int
348d480f
PA
10948bkpt_insert_location (struct bp_location *bl)
10949{
10950 if (bl->loc_type == bp_loc_hardware_breakpoint)
10951 return target_insert_hw_breakpoint (bl->gdbarch,
10952 &bl->target_info);
10953 else
10954 return target_insert_breakpoint (bl->gdbarch,
10955 &bl->target_info);
10956}
10957
2060206e 10958static int
348d480f
PA
10959bkpt_remove_location (struct bp_location *bl)
10960{
10961 if (bl->loc_type == bp_loc_hardware_breakpoint)
10962 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
10963 else
10964 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
10965}
10966
2060206e 10967static int
348d480f
PA
10968bkpt_breakpoint_hit (const struct bp_location *bl,
10969 struct address_space *aspace, CORE_ADDR bp_addr)
10970{
10971 struct breakpoint *b = bl->owner;
10972
10973 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
10974 aspace, bp_addr))
10975 return 0;
10976
10977 if (overlay_debugging /* unmapped overlay section */
10978 && section_is_overlay (bl->section)
10979 && !section_is_mapped (bl->section))
10980 return 0;
10981
10982 return 1;
10983}
10984
2060206e 10985static int
348d480f
PA
10986bkpt_resources_needed (const struct bp_location *bl)
10987{
10988 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
10989
10990 return 1;
10991}
10992
2060206e 10993static enum print_stop_action
348d480f
PA
10994bkpt_print_it (bpstat bs)
10995{
348d480f
PA
10996 struct breakpoint *b;
10997 const struct bp_location *bl;
001c8c33 10998 int bp_temp;
79a45e25 10999 struct ui_out *uiout = current_uiout;
348d480f
PA
11000
11001 gdb_assert (bs->bp_location_at != NULL);
11002
11003 bl = bs->bp_location_at;
11004 b = bs->breakpoint_at;
11005
001c8c33
PA
11006 bp_temp = b->disposition == disp_del;
11007 if (bl->address != bl->requested_address)
11008 breakpoint_adjustment_warning (bl->requested_address,
11009 bl->address,
11010 b->number, 1);
11011 annotate_breakpoint (b->number);
11012 if (bp_temp)
11013 ui_out_text (uiout, "\nTemporary breakpoint ");
11014 else
11015 ui_out_text (uiout, "\nBreakpoint ");
11016 if (ui_out_is_mi_like_p (uiout))
348d480f 11017 {
001c8c33
PA
11018 ui_out_field_string (uiout, "reason",
11019 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11020 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 11021 }
001c8c33
PA
11022 ui_out_field_int (uiout, "bkptno", b->number);
11023 ui_out_text (uiout, ", ");
06edf0c0 11024
001c8c33 11025 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11026}
11027
2060206e 11028static void
06edf0c0
PA
11029bkpt_print_mention (struct breakpoint *b)
11030{
79a45e25 11031 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
11032 return;
11033
11034 switch (b->type)
11035 {
11036 case bp_breakpoint:
11037 case bp_gnu_ifunc_resolver:
11038 if (b->disposition == disp_del)
11039 printf_filtered (_("Temporary breakpoint"));
11040 else
11041 printf_filtered (_("Breakpoint"));
11042 printf_filtered (_(" %d"), b->number);
11043 if (b->type == bp_gnu_ifunc_resolver)
11044 printf_filtered (_(" at gnu-indirect-function resolver"));
11045 break;
11046 case bp_hardware_breakpoint:
11047 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
11048 break;
11049 }
11050
11051 say_where (b);
11052}
11053
2060206e 11054static void
06edf0c0
PA
11055bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
11056{
11057 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11058 fprintf_unfiltered (fp, "tbreak");
11059 else if (tp->type == bp_breakpoint)
11060 fprintf_unfiltered (fp, "break");
11061 else if (tp->type == bp_hardware_breakpoint
11062 && tp->disposition == disp_del)
11063 fprintf_unfiltered (fp, "thbreak");
11064 else if (tp->type == bp_hardware_breakpoint)
11065 fprintf_unfiltered (fp, "hbreak");
11066 else
11067 internal_error (__FILE__, __LINE__,
11068 _("unhandled breakpoint type %d"), (int) tp->type);
11069
2060206e 11070 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 11071 print_recreate_thread (tp, fp);
06edf0c0
PA
11072}
11073
06edf0c0
PA
11074/* Virtual table for internal breakpoints. */
11075
11076static void
11077internal_bkpt_re_set (struct breakpoint *b)
11078{
11079 switch (b->type)
11080 {
11081 /* Delete overlay event and longjmp master breakpoints; they
11082 will be reset later by breakpoint_re_set. */
11083 case bp_overlay_event:
11084 case bp_longjmp_master:
11085 case bp_std_terminate_master:
11086 case bp_exception_master:
11087 delete_breakpoint (b);
11088 break;
11089
11090 /* This breakpoint is special, it's set up when the inferior
11091 starts and we really don't want to touch it. */
11092 case bp_shlib_event:
11093
11094 /* Like bp_shlib_event, this breakpoint type is special. Once
11095 it is set up, we do not want to touch it. */
11096 case bp_thread_event:
11097 break;
11098 }
11099}
11100
11101static void
11102internal_bkpt_check_status (bpstat bs)
11103{
11104 /* We do not stop for these. */
11105 bs->stop = 0;
11106}
11107
11108static enum print_stop_action
11109internal_bkpt_print_it (bpstat bs)
11110{
36dfb11c 11111 struct ui_out *uiout = current_uiout;
06edf0c0 11112 struct breakpoint *b;
06edf0c0 11113
06edf0c0
PA
11114 b = bs->breakpoint_at;
11115
06edf0c0
PA
11116 switch (b->type)
11117 {
348d480f
PA
11118 case bp_shlib_event:
11119 /* Did we stop because the user set the stop_on_solib_events
11120 variable? (If so, we report this as a generic, "Stopped due
11121 to shlib event" message.) */
36dfb11c
TT
11122 ui_out_text (uiout, _("Stopped due to shared library event\n"));
11123 if (ui_out_is_mi_like_p (uiout))
11124 ui_out_field_string (uiout, "reason",
11125 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
348d480f
PA
11126 break;
11127
11128 case bp_thread_event:
11129 /* Not sure how we will get here.
11130 GDB should not stop for these breakpoints. */
11131 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11132 break;
11133
11134 case bp_overlay_event:
11135 /* By analogy with the thread event, GDB should not stop for these. */
11136 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11137 break;
11138
11139 case bp_longjmp_master:
11140 /* These should never be enabled. */
11141 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11142 break;
11143
11144 case bp_std_terminate_master:
11145 /* These should never be enabled. */
11146 printf_filtered (_("std::terminate Master Breakpoint: "
11147 "gdb should not stop!\n"));
348d480f
PA
11148 break;
11149
11150 case bp_exception_master:
11151 /* These should never be enabled. */
11152 printf_filtered (_("Exception Master Breakpoint: "
11153 "gdb should not stop!\n"));
06edf0c0
PA
11154 break;
11155 }
11156
001c8c33 11157 return PRINT_NOTHING;
06edf0c0
PA
11158}
11159
11160static void
11161internal_bkpt_print_mention (struct breakpoint *b)
11162{
11163 /* Nothing to mention. These breakpoints are internal. */
11164}
11165
06edf0c0
PA
11166/* Virtual table for momentary breakpoints */
11167
11168static void
11169momentary_bkpt_re_set (struct breakpoint *b)
11170{
11171 /* Keep temporary breakpoints, which can be encountered when we step
11172 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11173 Otherwise these should have been blown away via the cleanup chain
11174 or by breakpoint_init_inferior when we rerun the executable. */
11175}
11176
11177static void
11178momentary_bkpt_check_status (bpstat bs)
11179{
11180 /* Nothing. The point of these breakpoints is causing a stop. */
11181}
11182
11183static enum print_stop_action
11184momentary_bkpt_print_it (bpstat bs)
11185{
79a45e25
PA
11186 struct ui_out *uiout = current_uiout;
11187
001c8c33 11188 if (ui_out_is_mi_like_p (uiout))
06edf0c0 11189 {
001c8c33 11190 struct breakpoint *b = bs->breakpoint_at;
348d480f 11191
001c8c33
PA
11192 switch (b->type)
11193 {
11194 case bp_finish:
11195 ui_out_field_string
11196 (uiout, "reason",
11197 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11198 break;
348d480f 11199
001c8c33
PA
11200 case bp_until:
11201 ui_out_field_string
11202 (uiout, "reason",
11203 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11204 break;
11205 }
348d480f
PA
11206 }
11207
001c8c33 11208 return PRINT_UNKNOWN;
348d480f
PA
11209}
11210
06edf0c0
PA
11211static void
11212momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 11213{
06edf0c0 11214 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11215}
11216
348d480f 11217/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 11218
348d480f
PA
11219static void
11220tracepoint_re_set (struct breakpoint *b)
11221{
11222 breakpoint_re_set_default (b);
11223}
876fa593 11224
348d480f
PA
11225static int
11226tracepoint_breakpoint_hit (const struct bp_location *bl,
11227 struct address_space *aspace, CORE_ADDR bp_addr)
11228{
11229 /* By definition, the inferior does not report stops at
11230 tracepoints. */
11231 return 0;
74960c60
VP
11232}
11233
11234static void
348d480f
PA
11235tracepoint_print_one_detail (const struct breakpoint *self,
11236 struct ui_out *uiout)
74960c60 11237{
d9b3f62e
PA
11238 struct tracepoint *tp = (struct tracepoint *) self;
11239 if (tp->static_trace_marker_id)
348d480f
PA
11240 {
11241 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 11242
348d480f
PA
11243 ui_out_text (uiout, "\tmarker id is ");
11244 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 11245 tp->static_trace_marker_id);
348d480f
PA
11246 ui_out_text (uiout, "\n");
11247 }
0d381245
VP
11248}
11249
a474d7c2 11250static void
348d480f 11251tracepoint_print_mention (struct breakpoint *b)
a474d7c2 11252{
79a45e25 11253 if (ui_out_is_mi_like_p (current_uiout))
348d480f 11254 return;
cc59ec59 11255
348d480f
PA
11256 switch (b->type)
11257 {
11258 case bp_tracepoint:
11259 printf_filtered (_("Tracepoint"));
11260 printf_filtered (_(" %d"), b->number);
11261 break;
11262 case bp_fast_tracepoint:
11263 printf_filtered (_("Fast tracepoint"));
11264 printf_filtered (_(" %d"), b->number);
11265 break;
11266 case bp_static_tracepoint:
11267 printf_filtered (_("Static tracepoint"));
11268 printf_filtered (_(" %d"), b->number);
11269 break;
11270 default:
11271 internal_error (__FILE__, __LINE__,
11272 _("unhandled tracepoint type %d"), (int) b->type);
11273 }
11274
11275 say_where (b);
a474d7c2
PA
11276}
11277
348d480f 11278static void
d9b3f62e 11279tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 11280{
d9b3f62e
PA
11281 struct tracepoint *tp = (struct tracepoint *) self;
11282
11283 if (self->type == bp_fast_tracepoint)
348d480f 11284 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 11285 if (self->type == bp_static_tracepoint)
348d480f 11286 fprintf_unfiltered (fp, "strace");
d9b3f62e 11287 else if (self->type == bp_tracepoint)
348d480f
PA
11288 fprintf_unfiltered (fp, "trace");
11289 else
11290 internal_error (__FILE__, __LINE__,
d9b3f62e 11291 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 11292
d9b3f62e
PA
11293 fprintf_unfiltered (fp, " %s", self->addr_string);
11294 print_recreate_thread (self, fp);
11295
11296 if (tp->pass_count)
11297 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
11298}
11299
2060206e 11300struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 11301
53a5351d 11302/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 11303 structures. */
c906108c
SS
11304
11305void
fba45db2 11306delete_breakpoint (struct breakpoint *bpt)
c906108c 11307{
52f0bd74 11308 struct breakpoint *b;
c906108c 11309
8a3fe4f8 11310 gdb_assert (bpt != NULL);
c906108c 11311
4a64f543
MS
11312 /* Has this bp already been deleted? This can happen because
11313 multiple lists can hold pointers to bp's. bpstat lists are
11314 especial culprits.
11315
11316 One example of this happening is a watchpoint's scope bp. When
11317 the scope bp triggers, we notice that the watchpoint is out of
11318 scope, and delete it. We also delete its scope bp. But the
11319 scope bp is marked "auto-deleting", and is already on a bpstat.
11320 That bpstat is then checked for auto-deleting bp's, which are
11321 deleted.
11322
11323 A real solution to this problem might involve reference counts in
11324 bp's, and/or giving them pointers back to their referencing
11325 bpstat's, and teaching delete_breakpoint to only free a bp's
11326 storage when no more references were extent. A cheaper bandaid
11327 was chosen. */
c906108c
SS
11328 if (bpt->type == bp_none)
11329 return;
11330
4a64f543
MS
11331 /* At least avoid this stale reference until the reference counting
11332 of breakpoints gets resolved. */
d0fb5eae 11333 if (bpt->related_breakpoint != bpt)
e5a0a904 11334 {
d0fb5eae 11335 struct breakpoint *related;
3a5c3e22 11336 struct watchpoint *w;
d0fb5eae
JK
11337
11338 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 11339 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 11340 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
11341 w = (struct watchpoint *) bpt;
11342 else
11343 w = NULL;
11344 if (w != NULL)
11345 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
11346
11347 /* Unlink bpt from the bpt->related_breakpoint ring. */
11348 for (related = bpt; related->related_breakpoint != bpt;
11349 related = related->related_breakpoint);
11350 related->related_breakpoint = bpt->related_breakpoint;
11351 bpt->related_breakpoint = bpt;
e5a0a904
JK
11352 }
11353
a9634178
TJB
11354 /* watch_command_1 creates a watchpoint but only sets its number if
11355 update_watchpoint succeeds in creating its bp_locations. If there's
11356 a problem in that process, we'll be asked to delete the half-created
11357 watchpoint. In that case, don't announce the deletion. */
11358 if (bpt->number)
11359 observer_notify_breakpoint_deleted (bpt);
c906108c 11360
c906108c
SS
11361 if (breakpoint_chain == bpt)
11362 breakpoint_chain = bpt->next;
11363
c906108c
SS
11364 ALL_BREAKPOINTS (b)
11365 if (b->next == bpt)
c5aa993b
JM
11366 {
11367 b->next = bpt->next;
11368 break;
11369 }
c906108c 11370
f431efe5
PA
11371 /* Be sure no bpstat's are pointing at the breakpoint after it's
11372 been freed. */
11373 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 11374 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
11375 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11376 commands are associated with the bpstat; if we remove it here,
11377 then the later call to bpstat_do_actions (&stop_bpstat); in
11378 event-top.c won't do anything, and temporary breakpoints with
11379 commands won't work. */
11380
11381 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11382
4a64f543
MS
11383 /* Now that breakpoint is removed from breakpoint list, update the
11384 global location list. This will remove locations that used to
11385 belong to this breakpoint. Do this before freeing the breakpoint
11386 itself, since remove_breakpoint looks at location's owner. It
11387 might be better design to have location completely
11388 self-contained, but it's not the case now. */
b60e7edf 11389 update_global_location_list (0);
74960c60 11390
348d480f 11391 bpt->ops->dtor (bpt);
4a64f543
MS
11392 /* On the chance that someone will soon try again to delete this
11393 same bp, we mark it as deleted before freeing its storage. */
c906108c 11394 bpt->type = bp_none;
b8c9b27d 11395 xfree (bpt);
c906108c
SS
11396}
11397
4d6140d9
AC
11398static void
11399do_delete_breakpoint_cleanup (void *b)
11400{
11401 delete_breakpoint (b);
11402}
11403
11404struct cleanup *
11405make_cleanup_delete_breakpoint (struct breakpoint *b)
11406{
11407 return make_cleanup (do_delete_breakpoint_cleanup, b);
11408}
11409
51be5b68
PA
11410/* Iterator function to call a user-provided callback function once
11411 for each of B and its related breakpoints. */
11412
11413static void
11414iterate_over_related_breakpoints (struct breakpoint *b,
11415 void (*function) (struct breakpoint *,
11416 void *),
11417 void *data)
11418{
11419 struct breakpoint *related;
11420
11421 related = b;
11422 do
11423 {
11424 struct breakpoint *next;
11425
11426 /* FUNCTION may delete RELATED. */
11427 next = related->related_breakpoint;
11428
11429 if (next == related)
11430 {
11431 /* RELATED is the last ring entry. */
11432 function (related, data);
11433
11434 /* FUNCTION may have deleted it, so we'd never reach back to
11435 B. There's nothing left to do anyway, so just break
11436 out. */
11437 break;
11438 }
11439 else
11440 function (related, data);
11441
11442 related = next;
11443 }
11444 while (related != b);
11445}
95a42b64
TT
11446
11447static void
11448do_delete_breakpoint (struct breakpoint *b, void *ignore)
11449{
11450 delete_breakpoint (b);
11451}
11452
51be5b68
PA
11453/* A callback for map_breakpoint_numbers that calls
11454 delete_breakpoint. */
11455
11456static void
11457do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11458{
11459 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11460}
11461
c906108c 11462void
fba45db2 11463delete_command (char *arg, int from_tty)
c906108c 11464{
35df4500 11465 struct breakpoint *b, *b_tmp;
c906108c 11466
ea9365bb
TT
11467 dont_repeat ();
11468
c906108c
SS
11469 if (arg == 0)
11470 {
11471 int breaks_to_delete = 0;
11472
46c6471b
PA
11473 /* Delete all breakpoints if no argument. Do not delete
11474 internal breakpoints, these have to be deleted with an
11475 explicit breakpoint number argument. */
c5aa993b 11476 ALL_BREAKPOINTS (b)
46c6471b 11477 if (user_breakpoint_p (b))
973d738b
DJ
11478 {
11479 breaks_to_delete = 1;
11480 break;
11481 }
c906108c
SS
11482
11483 /* Ask user only if there are some breakpoints to delete. */
11484 if (!from_tty
e2e0b3e5 11485 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 11486 {
35df4500 11487 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 11488 if (user_breakpoint_p (b))
c5aa993b 11489 delete_breakpoint (b);
c906108c
SS
11490 }
11491 }
11492 else
51be5b68 11493 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
11494}
11495
0d381245
VP
11496static int
11497all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 11498{
0d381245 11499 for (; loc; loc = loc->next)
8645ff69
UW
11500 if (!loc->shlib_disabled
11501 && !loc->pspace->executing_startup)
0d381245
VP
11502 return 0;
11503 return 1;
fe3f5fa8
VP
11504}
11505
776592bf
DE
11506/* Subroutine of update_breakpoint_locations to simplify it.
11507 Return non-zero if multiple fns in list LOC have the same name.
11508 Null names are ignored. */
11509
11510static int
11511ambiguous_names_p (struct bp_location *loc)
11512{
11513 struct bp_location *l;
11514 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
11515 (int (*) (const void *,
11516 const void *)) streq,
776592bf
DE
11517 NULL, xcalloc, xfree);
11518
11519 for (l = loc; l != NULL; l = l->next)
11520 {
11521 const char **slot;
11522 const char *name = l->function_name;
11523
11524 /* Allow for some names to be NULL, ignore them. */
11525 if (name == NULL)
11526 continue;
11527
11528 slot = (const char **) htab_find_slot (htab, (const void *) name,
11529 INSERT);
4a64f543
MS
11530 /* NOTE: We can assume slot != NULL here because xcalloc never
11531 returns NULL. */
776592bf
DE
11532 if (*slot != NULL)
11533 {
11534 htab_delete (htab);
11535 return 1;
11536 }
11537 *slot = name;
11538 }
11539
11540 htab_delete (htab);
11541 return 0;
11542}
11543
0fb4aa4b
PA
11544/* When symbols change, it probably means the sources changed as well,
11545 and it might mean the static tracepoint markers are no longer at
11546 the same address or line numbers they used to be at last we
11547 checked. Losing your static tracepoints whenever you rebuild is
11548 undesirable. This function tries to resync/rematch gdb static
11549 tracepoints with the markers on the target, for static tracepoints
11550 that have not been set by marker id. Static tracepoint that have
11551 been set by marker id are reset by marker id in breakpoint_re_set.
11552 The heuristic is:
11553
11554 1) For a tracepoint set at a specific address, look for a marker at
11555 the old PC. If one is found there, assume to be the same marker.
11556 If the name / string id of the marker found is different from the
11557 previous known name, assume that means the user renamed the marker
11558 in the sources, and output a warning.
11559
11560 2) For a tracepoint set at a given line number, look for a marker
11561 at the new address of the old line number. If one is found there,
11562 assume to be the same marker. If the name / string id of the
11563 marker found is different from the previous known name, assume that
11564 means the user renamed the marker in the sources, and output a
11565 warning.
11566
11567 3) If a marker is no longer found at the same address or line, it
11568 may mean the marker no longer exists. But it may also just mean
11569 the code changed a bit. Maybe the user added a few lines of code
11570 that made the marker move up or down (in line number terms). Ask
11571 the target for info about the marker with the string id as we knew
11572 it. If found, update line number and address in the matching
11573 static tracepoint. This will get confused if there's more than one
11574 marker with the same ID (possible in UST, although unadvised
11575 precisely because it confuses tools). */
11576
11577static struct symtab_and_line
11578update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11579{
d9b3f62e 11580 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
11581 struct static_tracepoint_marker marker;
11582 CORE_ADDR pc;
11583 int i;
11584
11585 pc = sal.pc;
11586 if (sal.line)
11587 find_line_pc (sal.symtab, sal.line, &pc);
11588
11589 if (target_static_tracepoint_marker_at (pc, &marker))
11590 {
d9b3f62e 11591 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
11592 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11593 b->number,
d9b3f62e 11594 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 11595
d9b3f62e
PA
11596 xfree (tp->static_trace_marker_id);
11597 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
11598 release_static_tracepoint_marker (&marker);
11599
11600 return sal;
11601 }
11602
11603 /* Old marker wasn't found on target at lineno. Try looking it up
11604 by string ID. */
11605 if (!sal.explicit_pc
11606 && sal.line != 0
11607 && sal.symtab != NULL
d9b3f62e 11608 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
11609 {
11610 VEC(static_tracepoint_marker_p) *markers;
11611
11612 markers
d9b3f62e 11613 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
11614
11615 if (!VEC_empty(static_tracepoint_marker_p, markers))
11616 {
80e1d417 11617 struct symtab_and_line sal2;
0fb4aa4b 11618 struct symbol *sym;
80e1d417 11619 struct static_tracepoint_marker *tpmarker;
79a45e25 11620 struct ui_out *uiout = current_uiout;
0fb4aa4b 11621
80e1d417 11622 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 11623
d9b3f62e 11624 xfree (tp->static_trace_marker_id);
80e1d417 11625 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
11626
11627 warning (_("marker for static tracepoint %d (%s) not "
11628 "found at previous line number"),
d9b3f62e 11629 b->number, tp->static_trace_marker_id);
0fb4aa4b 11630
80e1d417 11631 init_sal (&sal2);
0fb4aa4b 11632
80e1d417 11633 sal2.pc = tpmarker->address;
0fb4aa4b 11634
80e1d417
AS
11635 sal2 = find_pc_line (tpmarker->address, 0);
11636 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
11637 ui_out_text (uiout, "Now in ");
11638 if (sym)
11639 {
11640 ui_out_field_string (uiout, "func",
11641 SYMBOL_PRINT_NAME (sym));
11642 ui_out_text (uiout, " at ");
11643 }
80e1d417 11644 ui_out_field_string (uiout, "file", sal2.symtab->filename);
0fb4aa4b
PA
11645 ui_out_text (uiout, ":");
11646
11647 if (ui_out_is_mi_like_p (uiout))
11648 {
80e1d417 11649 char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b
PA
11650
11651 if (fullname)
11652 ui_out_field_string (uiout, "fullname", fullname);
11653 }
11654
80e1d417 11655 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
11656 ui_out_text (uiout, "\n");
11657
80e1d417 11658 b->loc->line_number = sal2.line;
0fb4aa4b 11659
f8eba3c6 11660 xfree (b->loc->source_file);
0fb4aa4b 11661 if (sym)
80e1d417 11662 b->loc->source_file = xstrdup (sal2.symtab->filename);
0fb4aa4b 11663 else
f8eba3c6 11664 b->loc->source_file = NULL;
0fb4aa4b
PA
11665
11666 xfree (b->addr_string);
11667 b->addr_string = xstrprintf ("%s:%d",
80e1d417 11668 sal2.symtab->filename,
f8eba3c6 11669 b->loc->line_number);
0fb4aa4b
PA
11670
11671 /* Might be nice to check if function changed, and warn if
11672 so. */
11673
80e1d417 11674 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
11675 }
11676 }
11677 return sal;
11678}
11679
8d3788bd
VP
11680/* Returns 1 iff locations A and B are sufficiently same that
11681 we don't need to report breakpoint as changed. */
11682
11683static int
11684locations_are_equal (struct bp_location *a, struct bp_location *b)
11685{
11686 while (a && b)
11687 {
11688 if (a->address != b->address)
11689 return 0;
11690
11691 if (a->shlib_disabled != b->shlib_disabled)
11692 return 0;
11693
11694 if (a->enabled != b->enabled)
11695 return 0;
11696
11697 a = a->next;
11698 b = b->next;
11699 }
11700
11701 if ((a == NULL) != (b == NULL))
11702 return 0;
11703
11704 return 1;
11705}
11706
f1310107
TJB
11707/* Create new breakpoint locations for B (a hardware or software breakpoint)
11708 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11709 a ranged breakpoint. */
11710
0e30163f 11711void
0d381245 11712update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
11713 struct symtabs_and_lines sals,
11714 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
11715{
11716 int i;
0d381245
VP
11717 struct bp_location *existing_locations = b->loc;
11718
f8eba3c6
TT
11719 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
11720 {
11721 /* Ranged breakpoints have only one start location and one end
11722 location. */
11723 b->enable_state = bp_disabled;
11724 update_global_location_list (1);
11725 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
11726 "multiple locations found\n"),
11727 b->number);
11728 return;
11729 }
f1310107 11730
4a64f543
MS
11731 /* If there's no new locations, and all existing locations are
11732 pending, don't do anything. This optimizes the common case where
11733 all locations are in the same shared library, that was unloaded.
11734 We'd like to retain the location, so that when the library is
11735 loaded again, we don't loose the enabled/disabled status of the
11736 individual locations. */
0d381245 11737 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
11738 return;
11739
fe3f5fa8
VP
11740 b->loc = NULL;
11741
0d381245 11742 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 11743 {
f8eba3c6
TT
11744 struct bp_location *new_loc;
11745
11746 switch_to_program_space_and_thread (sals.sals[i].pspace);
11747
11748 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 11749
0d381245
VP
11750 /* Reparse conditions, they might contain references to the
11751 old symtab. */
11752 if (b->cond_string != NULL)
11753 {
f1310107 11754 char *s;
bfd189b1 11755 volatile struct gdb_exception e;
fe3f5fa8 11756
0d381245
VP
11757 s = b->cond_string;
11758 TRY_CATCH (e, RETURN_MASK_ERROR)
11759 {
11760 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11761 0);
11762 }
11763 if (e.reason < 0)
11764 {
3e43a32a
MS
11765 warning (_("failed to reevaluate condition "
11766 "for breakpoint %d: %s"),
0d381245
VP
11767 b->number, e.message);
11768 new_loc->enabled = 0;
11769 }
11770 }
fe3f5fa8 11771
f1310107
TJB
11772 if (sals_end.nelts)
11773 {
11774 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11775
11776 new_loc->length = end - sals.sals[0].pc + 1;
11777 }
0d381245 11778 }
fe3f5fa8 11779
514f746b
AR
11780 /* Update locations of permanent breakpoints. */
11781 if (b->enable_state == bp_permanent)
11782 make_breakpoint_permanent (b);
11783
4a64f543
MS
11784 /* If possible, carry over 'disable' status from existing
11785 breakpoints. */
0d381245
VP
11786 {
11787 struct bp_location *e = existing_locations;
776592bf
DE
11788 /* If there are multiple breakpoints with the same function name,
11789 e.g. for inline functions, comparing function names won't work.
11790 Instead compare pc addresses; this is just a heuristic as things
11791 may have moved, but in practice it gives the correct answer
11792 often enough until a better solution is found. */
11793 int have_ambiguous_names = ambiguous_names_p (b->loc);
11794
0d381245
VP
11795 for (; e; e = e->next)
11796 {
11797 if (!e->enabled && e->function_name)
11798 {
11799 struct bp_location *l = b->loc;
776592bf
DE
11800 if (have_ambiguous_names)
11801 {
11802 for (; l; l = l->next)
f1310107 11803 if (breakpoint_locations_match (e, l))
776592bf
DE
11804 {
11805 l->enabled = 0;
11806 break;
11807 }
11808 }
11809 else
11810 {
11811 for (; l; l = l->next)
11812 if (l->function_name
11813 && strcmp (e->function_name, l->function_name) == 0)
11814 {
11815 l->enabled = 0;
11816 break;
11817 }
11818 }
0d381245
VP
11819 }
11820 }
11821 }
fe3f5fa8 11822
8d3788bd
VP
11823 if (!locations_are_equal (existing_locations, b->loc))
11824 observer_notify_breakpoint_modified (b);
11825
b60e7edf 11826 update_global_location_list (1);
fe3f5fa8
VP
11827}
11828
ef23e705
TJB
11829/* Find the SaL locations corresponding to the given ADDR_STRING.
11830 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11831
11832static struct symtabs_and_lines
11833addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11834{
11835 char *s;
58438ac1 11836 int marker_spec;
02d20e4a 11837 struct symtabs_and_lines sals = {0};
f8eba3c6 11838 volatile struct gdb_exception e;
ef23e705
TJB
11839
11840 s = addr_string;
11841 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11842
11843 TRY_CATCH (e, RETURN_MASK_ERROR)
11844 {
11845 if (marker_spec)
11846 {
d9b3f62e
PA
11847 struct tracepoint *tp = (struct tracepoint *) b;
11848
ef23e705 11849 sals = decode_static_tracepoint_spec (&s);
d9b3f62e 11850 if (sals.nelts > tp->static_trace_marker_id_idx)
ef23e705 11851 {
d9b3f62e 11852 sals.sals[0] = sals.sals[tp->static_trace_marker_id_idx];
ef23e705
TJB
11853 sals.nelts = 1;
11854 }
11855 else
d9b3f62e 11856 error (_("marker %s not found"), tp->static_trace_marker_id);
ef23e705
TJB
11857 }
11858 else
f8eba3c6
TT
11859 {
11860 struct linespec_result canonical;
11861
11862 init_linespec_result (&canonical);
11863 decode_line_full (&s, DECODE_LINE_FUNFIRSTLINE,
11864 (struct symtab *) NULL, 0,
11865 &canonical, multiple_symbols_all,
11866 b->filter);
11867
11868 /* We should get 0 or 1 resulting SALs. */
11869 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
11870
11871 if (VEC_length (linespec_sals, canonical.sals) > 0)
11872 {
11873 struct linespec_sals *lsal;
11874
11875 lsal = VEC_index (linespec_sals, canonical.sals, 0);
11876 sals = lsal->sals;
11877 /* Arrange it so the destructor does not free the
11878 contents. */
11879 lsal->sals.sals = NULL;
11880 }
11881
11882 destroy_linespec_result (&canonical);
11883 }
ef23e705
TJB
11884 }
11885 if (e.reason < 0)
11886 {
11887 int not_found_and_ok = 0;
11888 /* For pending breakpoints, it's expected that parsing will
11889 fail until the right shared library is loaded. User has
11890 already told to create pending breakpoints and don't need
11891 extra messages. If breakpoint is in bp_shlib_disabled
11892 state, then user already saw the message about that
11893 breakpoint being disabled, and don't want to see more
11894 errors. */
58438ac1 11895 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
11896 && (b->condition_not_parsed
11897 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 11898 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
11899 || b->enable_state == bp_disabled))
11900 not_found_and_ok = 1;
11901
11902 if (!not_found_and_ok)
11903 {
11904 /* We surely don't want to warn about the same breakpoint
11905 10 times. One solution, implemented here, is disable
11906 the breakpoint on error. Another solution would be to
11907 have separate 'warning emitted' flag. Since this
11908 happens only when a binary has changed, I don't know
11909 which approach is better. */
11910 b->enable_state = bp_disabled;
11911 throw_exception (e);
11912 }
11913 }
11914
58438ac1 11915 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 11916 {
f8eba3c6 11917 int i;
ef23e705 11918
f8eba3c6
TT
11919 for (i = 0; i < sals.nelts; ++i)
11920 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
11921 if (b->condition_not_parsed && s && s[0])
11922 {
11923 char *cond_string = 0;
11924 int thread = -1;
11925 int task = 0;
11926
11927 find_condition_and_thread (s, sals.sals[0].pc,
11928 &cond_string, &thread, &task);
11929 if (cond_string)
11930 b->cond_string = cond_string;
11931 b->thread = thread;
11932 b->task = task;
11933 b->condition_not_parsed = 0;
11934 }
11935
11936 if (b->type == bp_static_tracepoint && !marker_spec)
11937 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 11938
58438ac1
TT
11939 *found = 1;
11940 }
11941 else
11942 *found = 0;
ef23e705
TJB
11943
11944 return sals;
11945}
11946
348d480f
PA
11947/* The default re_set method, for typical hardware or software
11948 breakpoints. Reevaluate the breakpoint and recreate its
11949 locations. */
11950
11951static void
28010a5d 11952breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
11953{
11954 int found;
f1310107 11955 struct symtabs_and_lines sals, sals_end;
ef23e705 11956 struct symtabs_and_lines expanded = {0};
f1310107 11957 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
11958
11959 sals = addr_string_to_sals (b, b->addr_string, &found);
11960 if (found)
11961 {
11962 make_cleanup (xfree, sals.sals);
f8eba3c6 11963 expanded = sals;
ef23e705
TJB
11964 }
11965
f1310107
TJB
11966 if (b->addr_string_range_end)
11967 {
11968 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11969 if (found)
11970 {
11971 make_cleanup (xfree, sals_end.sals);
f8eba3c6 11972 expanded_end = sals_end;
f1310107
TJB
11973 }
11974 }
11975
11976 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
11977}
11978
11979/* Prepare the global context for a re-set of breakpoint B. */
11980
11981static struct cleanup *
11982prepare_re_set_context (struct breakpoint *b)
11983{
11984 struct cleanup *cleanups;
11985
11986 input_radix = b->input_radix;
11987 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
11988 if (b->pspace != NULL)
11989 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
11990 set_language (b->language);
11991
11992 return cleanups;
ef23e705
TJB
11993}
11994
c906108c
SS
11995/* Reset a breakpoint given it's struct breakpoint * BINT.
11996 The value we return ends up being the return value from catch_errors.
11997 Unused in this case. */
11998
11999static int
4efb68b1 12000breakpoint_re_set_one (void *bint)
c906108c 12001{
4a64f543 12002 /* Get past catch_errs. */
53a5351d 12003 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 12004 struct cleanup *cleanups;
c906108c 12005
348d480f
PA
12006 cleanups = prepare_re_set_context (b);
12007 b->ops->re_set (b);
12008 do_cleanups (cleanups);
c906108c
SS
12009 return 0;
12010}
12011
69de3c6a 12012/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 12013void
69de3c6a 12014breakpoint_re_set (void)
c906108c 12015{
35df4500 12016 struct breakpoint *b, *b_tmp;
c906108c
SS
12017 enum language save_language;
12018 int save_input_radix;
6c95b8df 12019 struct cleanup *old_chain;
c5aa993b 12020
c906108c
SS
12021 save_language = current_language->la_language;
12022 save_input_radix = input_radix;
6c95b8df
PA
12023 old_chain = save_current_program_space ();
12024
35df4500 12025 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12026 {
4a64f543 12027 /* Format possible error msg. */
fe3f5fa8 12028 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
12029 b->number);
12030 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 12031 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 12032 do_cleanups (cleanups);
c5aa993b 12033 }
c906108c
SS
12034 set_language (save_language);
12035 input_radix = save_input_radix;
e62c965a 12036
0756c555 12037 jit_breakpoint_re_set ();
4efc6507 12038
6c95b8df
PA
12039 do_cleanups (old_chain);
12040
af02033e
PP
12041 create_overlay_event_breakpoint ();
12042 create_longjmp_master_breakpoint ();
12043 create_std_terminate_master_breakpoint ();
186c406b 12044 create_exception_master_breakpoint ();
1bfeeb0f
JL
12045
12046 /* While we're at it, reset the skip list too. */
12047 skip_re_set ();
c906108c
SS
12048}
12049\f
c906108c
SS
12050/* Reset the thread number of this breakpoint:
12051
12052 - If the breakpoint is for all threads, leave it as-is.
4a64f543 12053 - Else, reset it to the current thread for inferior_ptid. */
c906108c 12054void
fba45db2 12055breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
12056{
12057 if (b->thread != -1)
12058 {
39f77062
KB
12059 if (in_thread_list (inferior_ptid))
12060 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
12061
12062 /* We're being called after following a fork. The new fork is
12063 selected as current, and unless this was a vfork will have a
12064 different program space from the original thread. Reset that
12065 as well. */
12066 b->loc->pspace = current_program_space;
c906108c
SS
12067 }
12068}
12069
03ac34d5
MS
12070/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12071 If from_tty is nonzero, it prints a message to that effect,
12072 which ends with a period (no newline). */
12073
c906108c 12074void
fba45db2 12075set_ignore_count (int bptnum, int count, int from_tty)
c906108c 12076{
52f0bd74 12077 struct breakpoint *b;
c906108c
SS
12078
12079 if (count < 0)
12080 count = 0;
12081
12082 ALL_BREAKPOINTS (b)
12083 if (b->number == bptnum)
c5aa993b 12084 {
d77f58be
SS
12085 if (is_tracepoint (b))
12086 {
12087 if (from_tty && count != 0)
12088 printf_filtered (_("Ignore count ignored for tracepoint %d."),
12089 bptnum);
12090 return;
12091 }
12092
c5aa993b 12093 b->ignore_count = count;
221ea385
KS
12094 if (from_tty)
12095 {
12096 if (count == 0)
3e43a32a
MS
12097 printf_filtered (_("Will stop next time "
12098 "breakpoint %d is reached."),
221ea385
KS
12099 bptnum);
12100 else if (count == 1)
a3f17187 12101 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
12102 bptnum);
12103 else
3e43a32a
MS
12104 printf_filtered (_("Will ignore next %d "
12105 "crossings of breakpoint %d."),
221ea385
KS
12106 count, bptnum);
12107 }
c5aa993b 12108 breakpoints_changed ();
8d3788bd 12109 observer_notify_breakpoint_modified (b);
c5aa993b
JM
12110 return;
12111 }
c906108c 12112
8a3fe4f8 12113 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
12114}
12115
c906108c
SS
12116/* Command to set ignore-count of breakpoint N to COUNT. */
12117
12118static void
fba45db2 12119ignore_command (char *args, int from_tty)
c906108c
SS
12120{
12121 char *p = args;
52f0bd74 12122 int num;
c906108c
SS
12123
12124 if (p == 0)
e2e0b3e5 12125 error_no_arg (_("a breakpoint number"));
c5aa993b 12126
c906108c 12127 num = get_number (&p);
5c44784c 12128 if (num == 0)
8a3fe4f8 12129 error (_("bad breakpoint number: '%s'"), args);
c906108c 12130 if (*p == 0)
8a3fe4f8 12131 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
12132
12133 set_ignore_count (num,
12134 longest_to_int (value_as_long (parse_and_eval (p))),
12135 from_tty);
221ea385
KS
12136 if (from_tty)
12137 printf_filtered ("\n");
c906108c
SS
12138}
12139\f
12140/* Call FUNCTION on each of the breakpoints
12141 whose numbers are given in ARGS. */
12142
12143static void
95a42b64
TT
12144map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
12145 void *),
12146 void *data)
c906108c 12147{
52f0bd74
AC
12148 int num;
12149 struct breakpoint *b, *tmp;
11cf8741 12150 int match;
197f0a60 12151 struct get_number_or_range_state state;
c906108c 12152
197f0a60 12153 if (args == 0)
e2e0b3e5 12154 error_no_arg (_("one or more breakpoint numbers"));
c906108c 12155
197f0a60
TT
12156 init_number_or_range (&state, args);
12157
12158 while (!state.finished)
c906108c 12159 {
197f0a60
TT
12160 char *p = state.string;
12161
11cf8741 12162 match = 0;
c5aa993b 12163
197f0a60 12164 num = get_number_or_range (&state);
5c44784c 12165 if (num == 0)
c5aa993b 12166 {
8a3fe4f8 12167 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
12168 }
12169 else
12170 {
12171 ALL_BREAKPOINTS_SAFE (b, tmp)
12172 if (b->number == num)
12173 {
11cf8741 12174 match = 1;
cdac0397 12175 function (b, data);
11cf8741 12176 break;
5c44784c 12177 }
11cf8741 12178 if (match == 0)
a3f17187 12179 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 12180 }
c906108c
SS
12181 }
12182}
12183
0d381245
VP
12184static struct bp_location *
12185find_location_by_number (char *number)
12186{
12187 char *dot = strchr (number, '.');
12188 char *p1;
12189 int bp_num;
12190 int loc_num;
12191 struct breakpoint *b;
12192 struct bp_location *loc;
12193
12194 *dot = '\0';
12195
12196 p1 = number;
197f0a60 12197 bp_num = get_number (&p1);
0d381245
VP
12198 if (bp_num == 0)
12199 error (_("Bad breakpoint number '%s'"), number);
12200
12201 ALL_BREAKPOINTS (b)
12202 if (b->number == bp_num)
12203 {
12204 break;
12205 }
12206
12207 if (!b || b->number != bp_num)
12208 error (_("Bad breakpoint number '%s'"), number);
12209
12210 p1 = dot+1;
197f0a60 12211 loc_num = get_number (&p1);
0d381245
VP
12212 if (loc_num == 0)
12213 error (_("Bad breakpoint location number '%s'"), number);
12214
12215 --loc_num;
12216 loc = b->loc;
12217 for (;loc_num && loc; --loc_num, loc = loc->next)
12218 ;
12219 if (!loc)
12220 error (_("Bad breakpoint location number '%s'"), dot+1);
12221
12222 return loc;
12223}
12224
12225
1900040c
MS
12226/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12227 If from_tty is nonzero, it prints a message to that effect,
12228 which ends with a period (no newline). */
12229
c906108c 12230void
fba45db2 12231disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
12232{
12233 /* Never disable a watchpoint scope breakpoint; we want to
12234 hit them when we leave scope so we can delete both the
12235 watchpoint and its scope breakpoint at that time. */
12236 if (bpt->type == bp_watchpoint_scope)
12237 return;
12238
c2c6d25f 12239 /* You can't disable permanent breakpoints. */
b5de0fa7 12240 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
12241 return;
12242
b5de0fa7 12243 bpt->enable_state = bp_disabled;
c906108c 12244
d248b706
KY
12245 if (target_supports_enable_disable_tracepoint ()
12246 && current_trace_status ()->running && is_tracepoint (bpt))
12247 {
12248 struct bp_location *location;
12249
12250 for (location = bpt->loc; location; location = location->next)
12251 target_disable_tracepoint (location);
12252 }
12253
b60e7edf 12254 update_global_location_list (0);
c906108c 12255
8d3788bd 12256 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12257}
12258
51be5b68
PA
12259/* A callback for iterate_over_related_breakpoints. */
12260
12261static void
12262do_disable_breakpoint (struct breakpoint *b, void *ignore)
12263{
12264 disable_breakpoint (b);
12265}
12266
95a42b64
TT
12267/* A callback for map_breakpoint_numbers that calls
12268 disable_breakpoint. */
12269
12270static void
12271do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12272{
51be5b68 12273 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
12274}
12275
c906108c 12276static void
fba45db2 12277disable_command (char *args, int from_tty)
c906108c 12278{
c906108c 12279 if (args == 0)
46c6471b
PA
12280 {
12281 struct breakpoint *bpt;
12282
12283 ALL_BREAKPOINTS (bpt)
12284 if (user_breakpoint_p (bpt))
12285 disable_breakpoint (bpt);
12286 }
0d381245
VP
12287 else if (strchr (args, '.'))
12288 {
12289 struct bp_location *loc = find_location_by_number (args);
12290 if (loc)
d248b706
KY
12291 {
12292 loc->enabled = 0;
12293 if (target_supports_enable_disable_tracepoint ()
12294 && current_trace_status ()->running && loc->owner
12295 && is_tracepoint (loc->owner))
12296 target_disable_tracepoint (loc);
12297 }
b60e7edf 12298 update_global_location_list (0);
0d381245 12299 }
c906108c 12300 else
95a42b64 12301 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
12302}
12303
12304static void
51be5b68 12305enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 12306{
afe38095 12307 int target_resources_ok;
c906108c
SS
12308
12309 if (bpt->type == bp_hardware_breakpoint)
12310 {
12311 int i;
c5aa993b 12312 i = hw_breakpoint_used_count ();
53a5351d 12313 target_resources_ok =
d92524f1 12314 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 12315 i + 1, 0);
c906108c 12316 if (target_resources_ok == 0)
8a3fe4f8 12317 error (_("No hardware breakpoint support in the target."));
c906108c 12318 else if (target_resources_ok < 0)
8a3fe4f8 12319 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
12320 }
12321
cc60f2e3 12322 if (is_watchpoint (bpt))
c906108c 12323 {
d07205c2
JK
12324 /* Initialize it just to avoid a GCC false warning. */
12325 enum enable_state orig_enable_state = 0;
bfd189b1 12326 volatile struct gdb_exception e;
dde02812
ES
12327
12328 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 12329 {
3a5c3e22
PA
12330 struct watchpoint *w = (struct watchpoint *) bpt;
12331
1e718ff1
TJB
12332 orig_enable_state = bpt->enable_state;
12333 bpt->enable_state = bp_enabled;
3a5c3e22 12334 update_watchpoint (w, 1 /* reparse */);
c906108c 12335 }
dde02812 12336 if (e.reason < 0)
c5aa993b 12337 {
1e718ff1 12338 bpt->enable_state = orig_enable_state;
dde02812
ES
12339 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12340 bpt->number);
12341 return;
c5aa993b 12342 }
c906108c 12343 }
0101ce28 12344
b4c291bb
KH
12345 if (bpt->enable_state != bp_permanent)
12346 bpt->enable_state = bp_enabled;
d248b706
KY
12347
12348 if (target_supports_enable_disable_tracepoint ()
12349 && current_trace_status ()->running && is_tracepoint (bpt))
12350 {
12351 struct bp_location *location;
12352
12353 for (location = bpt->loc; location; location = location->next)
12354 target_enable_tracepoint (location);
12355 }
12356
b4c291bb 12357 bpt->disposition = disposition;
b60e7edf 12358 update_global_location_list (1);
b4c291bb
KH
12359 breakpoints_changed ();
12360
8d3788bd 12361 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12362}
12363
fe3f5fa8 12364
c906108c 12365void
fba45db2 12366enable_breakpoint (struct breakpoint *bpt)
c906108c 12367{
51be5b68
PA
12368 enable_breakpoint_disp (bpt, bpt->disposition);
12369}
12370
12371static void
12372do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12373{
12374 enable_breakpoint (bpt);
c906108c
SS
12375}
12376
95a42b64
TT
12377/* A callback for map_breakpoint_numbers that calls
12378 enable_breakpoint. */
12379
12380static void
12381do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12382{
51be5b68 12383 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
12384}
12385
c906108c
SS
12386/* The enable command enables the specified breakpoints (or all defined
12387 breakpoints) so they once again become (or continue to be) effective
1272ad14 12388 in stopping the inferior. */
c906108c 12389
c906108c 12390static void
fba45db2 12391enable_command (char *args, int from_tty)
c906108c 12392{
c906108c 12393 if (args == 0)
46c6471b
PA
12394 {
12395 struct breakpoint *bpt;
12396
12397 ALL_BREAKPOINTS (bpt)
12398 if (user_breakpoint_p (bpt))
12399 enable_breakpoint (bpt);
12400 }
0d381245
VP
12401 else if (strchr (args, '.'))
12402 {
12403 struct bp_location *loc = find_location_by_number (args);
12404 if (loc)
d248b706
KY
12405 {
12406 loc->enabled = 1;
12407 if (target_supports_enable_disable_tracepoint ()
12408 && current_trace_status ()->running && loc->owner
12409 && is_tracepoint (loc->owner))
12410 target_enable_tracepoint (loc);
12411 }
b60e7edf 12412 update_global_location_list (1);
0d381245 12413 }
c906108c 12414 else
95a42b64 12415 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
12416}
12417
12418static void
51be5b68
PA
12419do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12420{
12421 enum bpdisp disp = *(enum bpdisp *) arg;
12422
12423 enable_breakpoint_disp (bpt, disp);
12424}
12425
12426static void
12427do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12428{
51be5b68
PA
12429 enum bpdisp disp = disp_disable;
12430
12431 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12432}
12433
c906108c 12434static void
fba45db2 12435enable_once_command (char *args, int from_tty)
c906108c 12436{
51be5b68 12437 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
12438}
12439
12440static void
51be5b68 12441do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12442{
51be5b68
PA
12443 enum bpdisp disp = disp_del;
12444
12445 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12446}
12447
c906108c 12448static void
fba45db2 12449enable_delete_command (char *args, int from_tty)
c906108c 12450{
51be5b68 12451 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
12452}
12453\f
fa8d40ab
JJ
12454static void
12455set_breakpoint_cmd (char *args, int from_tty)
12456{
12457}
12458
12459static void
12460show_breakpoint_cmd (char *args, int from_tty)
12461{
12462}
12463
1f3b5d1b
PP
12464/* Invalidate last known value of any hardware watchpoint if
12465 the memory which that value represents has been written to by
12466 GDB itself. */
12467
12468static void
12469invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12470 const bfd_byte *data)
12471{
12472 struct breakpoint *bp;
12473
12474 ALL_BREAKPOINTS (bp)
12475 if (bp->enable_state == bp_enabled
3a5c3e22 12476 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 12477 {
3a5c3e22 12478 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 12479
3a5c3e22
PA
12480 if (wp->val_valid && wp->val)
12481 {
12482 struct bp_location *loc;
12483
12484 for (loc = bp->loc; loc != NULL; loc = loc->next)
12485 if (loc->loc_type == bp_loc_hardware_watchpoint
12486 && loc->address + loc->length > addr
12487 && addr + len > loc->address)
12488 {
12489 value_free (wp->val);
12490 wp->val = NULL;
12491 wp->val_valid = 0;
12492 }
12493 }
1f3b5d1b
PP
12494 }
12495}
12496
1bfeeb0f
JL
12497/* Use the last displayed codepoint's values, or nothing
12498 if they aren't valid. */
c906108c
SS
12499
12500struct symtabs_and_lines
f8eba3c6 12501decode_line_spec_1 (char *string, int flags)
c906108c
SS
12502{
12503 struct symtabs_and_lines sals;
cc59ec59 12504
c906108c 12505 if (string == 0)
8a3fe4f8 12506 error (_("Empty line specification."));
1bfeeb0f 12507 if (last_displayed_sal_is_valid ())
f8eba3c6 12508 sals = decode_line_1 (&string, flags,
1bfeeb0f 12509 get_last_displayed_symtab (),
f8eba3c6 12510 get_last_displayed_line ());
c906108c 12511 else
f8eba3c6 12512 sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
c906108c 12513 if (*string)
8a3fe4f8 12514 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
12515 return sals;
12516}
8181d85f
DJ
12517
12518/* Create and insert a raw software breakpoint at PC. Return an
12519 identifier, which should be used to remove the breakpoint later.
12520 In general, places which call this should be using something on the
12521 breakpoint chain instead; this function should be eliminated
12522 someday. */
12523
12524void *
6c95b8df
PA
12525deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12526 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
12527{
12528 struct bp_target_info *bp_tgt;
12529
6c95b8df 12530 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 12531
6c95b8df 12532 bp_tgt->placed_address_space = aspace;
8181d85f 12533 bp_tgt->placed_address = pc;
6c95b8df 12534
a6d9a66e 12535 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
12536 {
12537 /* Could not insert the breakpoint. */
12538 xfree (bp_tgt);
12539 return NULL;
12540 }
12541
12542 return bp_tgt;
12543}
12544
4a64f543
MS
12545/* Remove a breakpoint BP inserted by
12546 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
12547
12548int
a6d9a66e 12549deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
12550{
12551 struct bp_target_info *bp_tgt = bp;
12552 int ret;
12553
a6d9a66e 12554 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
12555 xfree (bp_tgt);
12556
12557 return ret;
12558}
12559
4a64f543
MS
12560/* One (or perhaps two) breakpoints used for software single
12561 stepping. */
8181d85f
DJ
12562
12563static void *single_step_breakpoints[2];
a6d9a66e 12564static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
12565
12566/* Create and insert a breakpoint for software single step. */
12567
12568void
6c95b8df 12569insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
12570 struct address_space *aspace,
12571 CORE_ADDR next_pc)
8181d85f
DJ
12572{
12573 void **bpt_p;
12574
12575 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
12576 {
12577 bpt_p = &single_step_breakpoints[0];
12578 single_step_gdbarch[0] = gdbarch;
12579 }
8181d85f
DJ
12580 else
12581 {
12582 gdb_assert (single_step_breakpoints[1] == NULL);
12583 bpt_p = &single_step_breakpoints[1];
a6d9a66e 12584 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
12585 }
12586
4a64f543
MS
12587 /* NOTE drow/2006-04-11: A future improvement to this function would
12588 be to only create the breakpoints once, and actually put them on
12589 the breakpoint chain. That would let us use set_raw_breakpoint.
12590 We could adjust the addresses each time they were needed. Doing
12591 this requires corresponding changes elsewhere where single step
12592 breakpoints are handled, however. So, for now, we use this. */
8181d85f 12593
6c95b8df 12594 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 12595 if (*bpt_p == NULL)
5af949e3
UW
12596 error (_("Could not insert single-step breakpoint at %s"),
12597 paddress (gdbarch, next_pc));
8181d85f
DJ
12598}
12599
f02253f1
HZ
12600/* Check if the breakpoints used for software single stepping
12601 were inserted or not. */
12602
12603int
12604single_step_breakpoints_inserted (void)
12605{
12606 return (single_step_breakpoints[0] != NULL
12607 || single_step_breakpoints[1] != NULL);
12608}
12609
8181d85f
DJ
12610/* Remove and delete any breakpoints used for software single step. */
12611
12612void
12613remove_single_step_breakpoints (void)
12614{
12615 gdb_assert (single_step_breakpoints[0] != NULL);
12616
12617 /* See insert_single_step_breakpoint for more about this deprecated
12618 call. */
a6d9a66e
UW
12619 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12620 single_step_breakpoints[0]);
12621 single_step_gdbarch[0] = NULL;
8181d85f
DJ
12622 single_step_breakpoints[0] = NULL;
12623
12624 if (single_step_breakpoints[1] != NULL)
12625 {
a6d9a66e
UW
12626 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12627 single_step_breakpoints[1]);
12628 single_step_gdbarch[1] = NULL;
8181d85f
DJ
12629 single_step_breakpoints[1] = NULL;
12630 }
12631}
12632
d03285ec
UW
12633/* Delete software single step breakpoints without removing them from
12634 the inferior. This is intended to be used if the inferior's address
12635 space where they were inserted is already gone, e.g. after exit or
12636 exec. */
12637
12638void
12639cancel_single_step_breakpoints (void)
12640{
12641 int i;
12642
12643 for (i = 0; i < 2; i++)
12644 if (single_step_breakpoints[i])
12645 {
12646 xfree (single_step_breakpoints[i]);
12647 single_step_breakpoints[i] = NULL;
12648 single_step_gdbarch[i] = NULL;
12649 }
12650}
12651
12652/* Detach software single-step breakpoints from INFERIOR_PTID without
12653 removing them. */
12654
12655static void
12656detach_single_step_breakpoints (void)
12657{
12658 int i;
12659
12660 for (i = 0; i < 2; i++)
12661 if (single_step_breakpoints[i])
12662 target_remove_breakpoint (single_step_gdbarch[i],
12663 single_step_breakpoints[i]);
12664}
12665
4a64f543
MS
12666/* Check whether a software single-step breakpoint is inserted at
12667 PC. */
1aafd4da
UW
12668
12669static int
cc59ec59
MS
12670single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12671 CORE_ADDR pc)
1aafd4da
UW
12672{
12673 int i;
12674
12675 for (i = 0; i < 2; i++)
12676 {
12677 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
12678 if (bp_tgt
12679 && breakpoint_address_match (bp_tgt->placed_address_space,
12680 bp_tgt->placed_address,
12681 aspace, pc))
1aafd4da
UW
12682 return 1;
12683 }
12684
12685 return 0;
12686}
12687
a96d9b2e
SDJ
12688/* Returns 0 if 'bp' is NOT a syscall catchpoint,
12689 non-zero otherwise. */
12690static int
12691is_syscall_catchpoint_enabled (struct breakpoint *bp)
12692{
12693 if (syscall_catchpoint_p (bp)
12694 && bp->enable_state != bp_disabled
12695 && bp->enable_state != bp_call_disabled)
12696 return 1;
12697 else
12698 return 0;
12699}
12700
12701int
12702catch_syscall_enabled (void)
12703{
12704 struct inferior *inf = current_inferior ();
12705
12706 return inf->total_syscalls_count != 0;
12707}
12708
12709int
12710catching_syscall_number (int syscall_number)
12711{
12712 struct breakpoint *bp;
12713
12714 ALL_BREAKPOINTS (bp)
12715 if (is_syscall_catchpoint_enabled (bp))
12716 {
be5c67c1
PA
12717 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12718
12719 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
12720 {
12721 int i, iter;
12722 for (i = 0;
be5c67c1 12723 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
12724 i++)
12725 if (syscall_number == iter)
12726 return 1;
12727 }
12728 else
12729 return 1;
12730 }
12731
12732 return 0;
12733}
12734
12735/* Complete syscall names. Used by "catch syscall". */
12736static char **
12737catch_syscall_completer (struct cmd_list_element *cmd,
12738 char *text, char *word)
12739{
12740 const char **list = get_syscall_names ();
c38eea1a
MS
12741 char **retlist
12742 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 12743
c38eea1a
MS
12744 xfree (list);
12745 return retlist;
a96d9b2e
SDJ
12746}
12747
1042e4c0
SS
12748/* Tracepoint-specific operations. */
12749
12750/* Set tracepoint count to NUM. */
12751static void
12752set_tracepoint_count (int num)
12753{
12754 tracepoint_count = num;
4fa62494 12755 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
12756}
12757
12758void
12759trace_command (char *arg, int from_tty)
12760{
8cdf0e15
VP
12761 if (create_breakpoint (get_current_arch (),
12762 arg,
12763 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12764 0 /* tempflag */,
12765 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
12766 0 /* Ignore count */,
12767 pending_break_support,
348d480f 12768 &tracepoint_breakpoint_ops,
8cdf0e15 12769 from_tty,
84f4c1fe
PM
12770 1 /* enabled */,
12771 0 /* internal */))
fd9b8c24 12772 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
12773}
12774
7a697b8d
SS
12775void
12776ftrace_command (char *arg, int from_tty)
12777{
8cdf0e15
VP
12778 if (create_breakpoint (get_current_arch (),
12779 arg,
12780 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12781 0 /* tempflag */,
12782 bp_fast_tracepoint /* type_wanted */,
12783 0 /* Ignore count */,
12784 pending_break_support,
348d480f 12785 &tracepoint_breakpoint_ops,
0fb4aa4b 12786 from_tty,
84f4c1fe
PM
12787 1 /* enabled */,
12788 0 /* internal */))
0fb4aa4b
PA
12789 set_tracepoint_count (breakpoint_count);
12790}
12791
12792/* strace command implementation. Creates a static tracepoint. */
12793
12794void
12795strace_command (char *arg, int from_tty)
12796{
12797 if (create_breakpoint (get_current_arch (),
12798 arg,
12799 NULL, 0, 1 /* parse arg */,
12800 0 /* tempflag */,
12801 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
12802 0 /* Ignore count */,
12803 pending_break_support,
348d480f 12804 &tracepoint_breakpoint_ops,
8cdf0e15 12805 from_tty,
84f4c1fe
PM
12806 1 /* enabled */,
12807 0 /* internal */))
fd9b8c24 12808 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
12809}
12810
409873ef
SS
12811/* Set up a fake reader function that gets command lines from a linked
12812 list that was acquired during tracepoint uploading. */
12813
12814static struct uploaded_tp *this_utp;
3149d8c1 12815static int next_cmd;
409873ef
SS
12816
12817static char *
12818read_uploaded_action (void)
12819{
12820 char *rslt;
12821
3149d8c1 12822 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 12823
3149d8c1 12824 next_cmd++;
409873ef
SS
12825
12826 return rslt;
12827}
12828
00bf0b85
SS
12829/* Given information about a tracepoint as recorded on a target (which
12830 can be either a live system or a trace file), attempt to create an
12831 equivalent GDB tracepoint. This is not a reliable process, since
12832 the target does not necessarily have all the information used when
12833 the tracepoint was originally defined. */
12834
d9b3f62e 12835struct tracepoint *
00bf0b85 12836create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 12837{
409873ef 12838 char *addr_str, small_buf[100];
d9b3f62e 12839 struct tracepoint *tp;
fd9b8c24 12840
409873ef
SS
12841 if (utp->at_string)
12842 addr_str = utp->at_string;
12843 else
12844 {
12845 /* In the absence of a source location, fall back to raw
12846 address. Since there is no way to confirm that the address
12847 means the same thing as when the trace was started, warn the
12848 user. */
3e43a32a
MS
12849 warning (_("Uploaded tracepoint %d has no "
12850 "source location, using raw address"),
409873ef
SS
12851 utp->number);
12852 sprintf (small_buf, "*%s", hex_string (utp->addr));
12853 addr_str = small_buf;
12854 }
12855
12856 /* There's not much we can do with a sequence of bytecodes. */
12857 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12858 warning (_("Uploaded tracepoint %d condition "
12859 "has no source form, ignoring it"),
409873ef 12860 utp->number);
d5551862 12861
8cdf0e15 12862 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12863 addr_str,
12864 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12865 0 /* tempflag */,
0fb4aa4b 12866 utp->type /* type_wanted */,
8cdf0e15
VP
12867 0 /* Ignore count */,
12868 pending_break_support,
348d480f 12869 &tracepoint_breakpoint_ops,
8cdf0e15 12870 0 /* from_tty */,
84f4c1fe
PM
12871 utp->enabled /* enabled */,
12872 0 /* internal */))
fd9b8c24
PA
12873 return NULL;
12874
00bf0b85
SS
12875 set_tracepoint_count (breakpoint_count);
12876
409873ef 12877 /* Get the tracepoint we just created. */
fd9b8c24
PA
12878 tp = get_tracepoint (tracepoint_count);
12879 gdb_assert (tp != NULL);
d5551862 12880
00bf0b85
SS
12881 if (utp->pass > 0)
12882 {
d9b3f62e 12883 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 12884
409873ef 12885 trace_pass_command (small_buf, 0);
00bf0b85
SS
12886 }
12887
409873ef
SS
12888 /* If we have uploaded versions of the original commands, set up a
12889 special-purpose "reader" function and call the usual command line
12890 reader, then pass the result to the breakpoint command-setting
12891 function. */
3149d8c1 12892 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12893 {
409873ef 12894 struct command_line *cmd_list;
00bf0b85 12895
409873ef 12896 this_utp = utp;
3149d8c1 12897 next_cmd = 0;
d5551862 12898
409873ef
SS
12899 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12900
d9b3f62e 12901 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 12902 }
3149d8c1
SS
12903 else if (!VEC_empty (char_ptr, utp->actions)
12904 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12905 warning (_("Uploaded tracepoint %d actions "
12906 "have no source form, ignoring them"),
409873ef 12907 utp->number);
00bf0b85 12908
f196051f
SS
12909 /* Copy any status information that might be available. */
12910 tp->base.hit_count = utp->hit_count;
12911 tp->traceframe_usage = utp->traceframe_usage;
12912
00bf0b85 12913 return tp;
d9b3f62e 12914}
00bf0b85 12915
1042e4c0
SS
12916/* Print information on tracepoint number TPNUM_EXP, or all if
12917 omitted. */
12918
12919static void
e5a67952 12920tracepoints_info (char *args, int from_tty)
1042e4c0 12921{
79a45e25 12922 struct ui_out *uiout = current_uiout;
e5a67952 12923 int num_printed;
1042e4c0 12924
e5a67952 12925 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
12926
12927 if (num_printed == 0)
1042e4c0 12928 {
e5a67952 12929 if (args == NULL || *args == '\0')
d77f58be
SS
12930 ui_out_message (uiout, 0, "No tracepoints.\n");
12931 else
e5a67952 12932 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 12933 }
ad443146
SS
12934
12935 default_collect_info ();
1042e4c0
SS
12936}
12937
4a64f543 12938/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
12939 Not supported by all targets. */
12940static void
12941enable_trace_command (char *args, int from_tty)
12942{
12943 enable_command (args, from_tty);
12944}
12945
4a64f543 12946/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
12947 Not supported by all targets. */
12948static void
12949disable_trace_command (char *args, int from_tty)
12950{
12951 disable_command (args, from_tty);
12952}
12953
4a64f543 12954/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
12955static void
12956delete_trace_command (char *arg, int from_tty)
12957{
35df4500 12958 struct breakpoint *b, *b_tmp;
1042e4c0
SS
12959
12960 dont_repeat ();
12961
12962 if (arg == 0)
12963 {
12964 int breaks_to_delete = 0;
12965
12966 /* Delete all breakpoints if no argument.
12967 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
12968 have to be deleted with an explicit breakpoint number
12969 argument. */
1042e4c0 12970 ALL_TRACEPOINTS (b)
46c6471b 12971 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
12972 {
12973 breaks_to_delete = 1;
12974 break;
12975 }
1042e4c0
SS
12976
12977 /* Ask user only if there are some breakpoints to delete. */
12978 if (!from_tty
12979 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12980 {
35df4500 12981 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 12982 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 12983 delete_breakpoint (b);
1042e4c0
SS
12984 }
12985 }
12986 else
51be5b68 12987 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
12988}
12989
197f0a60
TT
12990/* Helper function for trace_pass_command. */
12991
12992static void
d9b3f62e 12993trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 12994{
d9b3f62e
PA
12995 tp->pass_count = count;
12996 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
12997 if (from_tty)
12998 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 12999 tp->base.number, count);
197f0a60
TT
13000}
13001
1042e4c0
SS
13002/* Set passcount for tracepoint.
13003
13004 First command argument is passcount, second is tracepoint number.
13005 If tracepoint number omitted, apply to most recently defined.
13006 Also accepts special argument "all". */
13007
13008static void
13009trace_pass_command (char *args, int from_tty)
13010{
d9b3f62e 13011 struct tracepoint *t1;
1042e4c0 13012 unsigned int count;
1042e4c0
SS
13013
13014 if (args == 0 || *args == 0)
3e43a32a
MS
13015 error (_("passcount command requires an "
13016 "argument (count + optional TP num)"));
1042e4c0 13017
4a64f543 13018 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
13019
13020 while (*args && isspace ((int) *args))
13021 args++;
13022
13023 if (*args && strncasecmp (args, "all", 3) == 0)
13024 {
d9b3f62e
PA
13025 struct breakpoint *b;
13026
1042e4c0 13027 args += 3; /* Skip special argument "all". */
1042e4c0
SS
13028 if (*args)
13029 error (_("Junk at end of arguments."));
1042e4c0 13030
d9b3f62e 13031 ALL_TRACEPOINTS (b)
197f0a60 13032 {
d9b3f62e 13033 t1 = (struct tracepoint *) b;
197f0a60
TT
13034 trace_pass_set_count (t1, count, from_tty);
13035 }
13036 }
13037 else if (*args == '\0')
1042e4c0 13038 {
197f0a60 13039 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 13040 if (t1)
197f0a60
TT
13041 trace_pass_set_count (t1, count, from_tty);
13042 }
13043 else
13044 {
13045 struct get_number_or_range_state state;
13046
13047 init_number_or_range (&state, args);
13048 while (!state.finished)
1042e4c0 13049 {
197f0a60
TT
13050 t1 = get_tracepoint_by_number (&args, &state, 1);
13051 if (t1)
13052 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
13053 }
13054 }
1042e4c0
SS
13055}
13056
d9b3f62e 13057struct tracepoint *
1042e4c0
SS
13058get_tracepoint (int num)
13059{
13060 struct breakpoint *t;
13061
13062 ALL_TRACEPOINTS (t)
13063 if (t->number == num)
d9b3f62e 13064 return (struct tracepoint *) t;
1042e4c0
SS
13065
13066 return NULL;
13067}
13068
d5551862
SS
13069/* Find the tracepoint with the given target-side number (which may be
13070 different from the tracepoint number after disconnecting and
13071 reconnecting). */
13072
d9b3f62e 13073struct tracepoint *
d5551862
SS
13074get_tracepoint_by_number_on_target (int num)
13075{
d9b3f62e 13076 struct breakpoint *b;
d5551862 13077
d9b3f62e
PA
13078 ALL_TRACEPOINTS (b)
13079 {
13080 struct tracepoint *t = (struct tracepoint *) b;
13081
13082 if (t->number_on_target == num)
13083 return t;
13084 }
d5551862
SS
13085
13086 return NULL;
13087}
13088
1042e4c0 13089/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
13090 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13091 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 13092 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 13093struct tracepoint *
197f0a60
TT
13094get_tracepoint_by_number (char **arg,
13095 struct get_number_or_range_state *state,
13096 int optional_p)
1042e4c0
SS
13097{
13098 extern int tracepoint_count;
13099 struct breakpoint *t;
13100 int tpnum;
13101 char *instring = arg == NULL ? NULL : *arg;
13102
197f0a60
TT
13103 if (state)
13104 {
13105 gdb_assert (!state->finished);
13106 tpnum = get_number_or_range (state);
13107 }
13108 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
13109 {
13110 if (optional_p)
13111 tpnum = tracepoint_count;
13112 else
13113 error_no_arg (_("tracepoint number"));
13114 }
13115 else
197f0a60 13116 tpnum = get_number (arg);
1042e4c0
SS
13117
13118 if (tpnum <= 0)
13119 {
13120 if (instring && *instring)
13121 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
13122 instring);
13123 else
3e43a32a
MS
13124 printf_filtered (_("Tracepoint argument missing "
13125 "and no previous tracepoint\n"));
1042e4c0
SS
13126 return NULL;
13127 }
13128
13129 ALL_TRACEPOINTS (t)
13130 if (t->number == tpnum)
13131 {
d9b3f62e 13132 return (struct tracepoint *) t;
1042e4c0
SS
13133 }
13134
1042e4c0
SS
13135 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
13136 return NULL;
13137}
13138
d9b3f62e
PA
13139void
13140print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
13141{
13142 if (b->thread != -1)
13143 fprintf_unfiltered (fp, " thread %d", b->thread);
13144
13145 if (b->task != 0)
13146 fprintf_unfiltered (fp, " task %d", b->task);
13147
13148 fprintf_unfiltered (fp, "\n");
13149}
13150
6149aea9
PA
13151/* Save information on user settable breakpoints (watchpoints, etc) to
13152 a new script file named FILENAME. If FILTER is non-NULL, call it
13153 on each breakpoint and only include the ones for which it returns
13154 non-zero. */
13155
1042e4c0 13156static void
6149aea9
PA
13157save_breakpoints (char *filename, int from_tty,
13158 int (*filter) (const struct breakpoint *))
1042e4c0
SS
13159{
13160 struct breakpoint *tp;
6149aea9 13161 int any = 0;
a7bdde9e 13162 char *pathname;
1042e4c0 13163 struct cleanup *cleanup;
a7bdde9e 13164 struct ui_file *fp;
6149aea9 13165 int extra_trace_bits = 0;
1042e4c0 13166
6149aea9
PA
13167 if (filename == 0 || *filename == 0)
13168 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
13169
13170 /* See if we have anything to save. */
6149aea9 13171 ALL_BREAKPOINTS (tp)
1042e4c0 13172 {
6149aea9 13173 /* Skip internal and momentary breakpoints. */
09d682a4 13174 if (!user_breakpoint_p (tp))
6149aea9
PA
13175 continue;
13176
13177 /* If we have a filter, only save the breakpoints it accepts. */
13178 if (filter && !filter (tp))
13179 continue;
13180
13181 any = 1;
13182
13183 if (is_tracepoint (tp))
13184 {
13185 extra_trace_bits = 1;
13186
13187 /* We can stop searching. */
13188 break;
13189 }
1042e4c0 13190 }
6149aea9
PA
13191
13192 if (!any)
1042e4c0 13193 {
6149aea9 13194 warning (_("Nothing to save."));
1042e4c0
SS
13195 return;
13196 }
13197
6149aea9 13198 pathname = tilde_expand (filename);
1042e4c0 13199 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 13200 fp = gdb_fopen (pathname, "w");
059fb39f 13201 if (!fp)
6149aea9
PA
13202 error (_("Unable to open file '%s' for saving (%s)"),
13203 filename, safe_strerror (errno));
a7bdde9e 13204 make_cleanup_ui_file_delete (fp);
8bf6485c 13205
6149aea9
PA
13206 if (extra_trace_bits)
13207 save_trace_state_variables (fp);
8bf6485c 13208
6149aea9 13209 ALL_BREAKPOINTS (tp)
1042e4c0 13210 {
6149aea9 13211 /* Skip internal and momentary breakpoints. */
09d682a4 13212 if (!user_breakpoint_p (tp))
6149aea9 13213 continue;
8bf6485c 13214
6149aea9
PA
13215 /* If we have a filter, only save the breakpoints it accepts. */
13216 if (filter && !filter (tp))
13217 continue;
13218
348d480f 13219 tp->ops->print_recreate (tp, fp);
1042e4c0 13220
6149aea9
PA
13221 /* Note, we can't rely on tp->number for anything, as we can't
13222 assume the recreated breakpoint numbers will match. Use $bpnum
13223 instead. */
13224
13225 if (tp->cond_string)
13226 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
13227
13228 if (tp->ignore_count)
13229 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
13230
a7bdde9e 13231 if (tp->commands)
1042e4c0 13232 {
a7bdde9e
VP
13233 volatile struct gdb_exception ex;
13234
6149aea9 13235 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 13236
79a45e25 13237 ui_out_redirect (current_uiout, fp);
14dba4b4 13238 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 13239 {
79a45e25 13240 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 13241 }
79a45e25 13242 ui_out_redirect (current_uiout, NULL);
1042e4c0 13243
a7bdde9e
VP
13244 if (ex.reason < 0)
13245 throw_exception (ex);
1042e4c0 13246
a7bdde9e 13247 fprintf_unfiltered (fp, " end\n");
1042e4c0 13248 }
6149aea9
PA
13249
13250 if (tp->enable_state == bp_disabled)
13251 fprintf_unfiltered (fp, "disable\n");
13252
13253 /* If this is a multi-location breakpoint, check if the locations
13254 should be individually disabled. Watchpoint locations are
13255 special, and not user visible. */
13256 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13257 {
13258 struct bp_location *loc;
13259 int n = 1;
13260
13261 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13262 if (!loc->enabled)
13263 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13264 }
1042e4c0 13265 }
8bf6485c 13266
6149aea9 13267 if (extra_trace_bits && *default_collect)
8bf6485c
SS
13268 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13269
1042e4c0
SS
13270 do_cleanups (cleanup);
13271 if (from_tty)
6149aea9
PA
13272 printf_filtered (_("Saved to file '%s'.\n"), filename);
13273}
13274
13275/* The `save breakpoints' command. */
13276
13277static void
13278save_breakpoints_command (char *args, int from_tty)
13279{
13280 save_breakpoints (args, from_tty, NULL);
13281}
13282
13283/* The `save tracepoints' command. */
13284
13285static void
13286save_tracepoints_command (char *args, int from_tty)
13287{
13288 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
13289}
13290
13291/* Create a vector of all tracepoints. */
13292
13293VEC(breakpoint_p) *
eeae04df 13294all_tracepoints (void)
1042e4c0
SS
13295{
13296 VEC(breakpoint_p) *tp_vec = 0;
13297 struct breakpoint *tp;
13298
13299 ALL_TRACEPOINTS (tp)
13300 {
13301 VEC_safe_push (breakpoint_p, tp_vec, tp);
13302 }
13303
13304 return tp_vec;
13305}
13306
c906108c 13307\f
4a64f543
MS
13308/* This help string is used for the break, hbreak, tbreak and thbreak
13309 commands. It is defined as a macro to prevent duplication.
13310 COMMAND should be a string constant containing the name of the
13311 command. */
31e2b00f
AS
13312#define BREAK_ARGS_HELP(command) \
13313command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13314LOCATION may be a line number, function name, or \"*\" and an address.\n\
13315If a line number is specified, break at start of code for that line.\n\
13316If a function is specified, break at start of code for that function.\n\
13317If an address is specified, break at that exact address.\n\
dc10affe
PA
13318With no LOCATION, uses current execution address of the selected\n\
13319stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
13320\n\
13321THREADNUM is the number from \"info threads\".\n\
13322CONDITION is a boolean expression.\n\
13323\n\
d41c0fc8
PA
13324Multiple breakpoints at one place are permitted, and useful if their\n\
13325conditions are different.\n\
31e2b00f
AS
13326\n\
13327Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13328
44feb3ce
TT
13329/* List of subcommands for "catch". */
13330static struct cmd_list_element *catch_cmdlist;
13331
13332/* List of subcommands for "tcatch". */
13333static struct cmd_list_element *tcatch_cmdlist;
13334
9ac4176b 13335void
44feb3ce
TT
13336add_catch_command (char *name, char *docstring,
13337 void (*sfunc) (char *args, int from_tty,
13338 struct cmd_list_element *command),
a96d9b2e
SDJ
13339 char **(*completer) (struct cmd_list_element *cmd,
13340 char *text, char *word),
44feb3ce
TT
13341 void *user_data_catch,
13342 void *user_data_tcatch)
13343{
13344 struct cmd_list_element *command;
13345
13346 command = add_cmd (name, class_breakpoint, NULL, docstring,
13347 &catch_cmdlist);
13348 set_cmd_sfunc (command, sfunc);
13349 set_cmd_context (command, user_data_catch);
a96d9b2e 13350 set_cmd_completer (command, completer);
44feb3ce
TT
13351
13352 command = add_cmd (name, class_breakpoint, NULL, docstring,
13353 &tcatch_cmdlist);
13354 set_cmd_sfunc (command, sfunc);
13355 set_cmd_context (command, user_data_tcatch);
a96d9b2e 13356 set_cmd_completer (command, completer);
44feb3ce
TT
13357}
13358
6c95b8df 13359static void
a79b8f6e 13360clear_syscall_counts (struct inferior *inf)
6c95b8df 13361{
6c95b8df
PA
13362 inf->total_syscalls_count = 0;
13363 inf->any_syscall_count = 0;
13364 VEC_free (int, inf->syscalls_counts);
13365}
13366
6149aea9
PA
13367static void
13368save_command (char *arg, int from_tty)
13369{
3e43a32a
MS
13370 printf_unfiltered (_("\"save\" must be followed by "
13371 "the name of a save subcommand.\n"));
6149aea9
PA
13372 help_list (save_cmdlist, "save ", -1, gdb_stdout);
13373}
13374
84f4c1fe
PM
13375struct breakpoint *
13376iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13377 void *data)
13378{
35df4500 13379 struct breakpoint *b, *b_tmp;
84f4c1fe 13380
35df4500 13381 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
13382 {
13383 if ((*callback) (b, data))
13384 return b;
13385 }
13386
13387 return NULL;
13388}
13389
0574c78f
GB
13390/* Zero if any of the breakpoint's locations could be a location where
13391 functions have been inlined, nonzero otherwise. */
13392
13393static int
13394is_non_inline_function (struct breakpoint *b)
13395{
13396 /* The shared library event breakpoint is set on the address of a
13397 non-inline function. */
13398 if (b->type == bp_shlib_event)
13399 return 1;
13400
13401 return 0;
13402}
13403
13404/* Nonzero if the specified PC cannot be a location where functions
13405 have been inlined. */
13406
13407int
13408pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc)
13409{
13410 struct breakpoint *b;
13411 struct bp_location *bl;
13412
13413 ALL_BREAKPOINTS (b)
13414 {
13415 if (!is_non_inline_function (b))
13416 continue;
13417
13418 for (bl = b->loc; bl != NULL; bl = bl->next)
13419 {
13420 if (!bl->shlib_disabled
13421 && bpstat_check_location (bl, aspace, pc))
13422 return 1;
13423 }
13424 }
13425
13426 return 0;
13427}
13428
2060206e
PA
13429void
13430initialize_breakpoint_ops (void)
13431{
13432 static int initialized = 0;
13433
13434 struct breakpoint_ops *ops;
13435
13436 if (initialized)
13437 return;
13438 initialized = 1;
13439
13440 /* The breakpoint_ops structure to be inherit by all kinds of
13441 breakpoints (real breakpoints, i.e., user "break" breakpoints,
13442 internal and momentary breakpoints, etc.). */
13443 ops = &bkpt_base_breakpoint_ops;
13444 *ops = base_breakpoint_ops;
13445 ops->re_set = bkpt_re_set;
13446 ops->insert_location = bkpt_insert_location;
13447 ops->remove_location = bkpt_remove_location;
13448 ops->breakpoint_hit = bkpt_breakpoint_hit;
13449
13450 /* The breakpoint_ops structure to be used in regular breakpoints. */
13451 ops = &bkpt_breakpoint_ops;
13452 *ops = bkpt_base_breakpoint_ops;
13453 ops->re_set = bkpt_re_set;
13454 ops->resources_needed = bkpt_resources_needed;
13455 ops->print_it = bkpt_print_it;
13456 ops->print_mention = bkpt_print_mention;
13457 ops->print_recreate = bkpt_print_recreate;
13458
13459 /* Ranged breakpoints. */
13460 ops = &ranged_breakpoint_ops;
13461 *ops = bkpt_breakpoint_ops;
13462 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
13463 ops->resources_needed = resources_needed_ranged_breakpoint;
13464 ops->print_it = print_it_ranged_breakpoint;
13465 ops->print_one = print_one_ranged_breakpoint;
13466 ops->print_one_detail = print_one_detail_ranged_breakpoint;
13467 ops->print_mention = print_mention_ranged_breakpoint;
13468 ops->print_recreate = print_recreate_ranged_breakpoint;
13469
13470 /* Internal breakpoints. */
13471 ops = &internal_breakpoint_ops;
13472 *ops = bkpt_base_breakpoint_ops;
13473 ops->re_set = internal_bkpt_re_set;
13474 ops->check_status = internal_bkpt_check_status;
13475 ops->print_it = internal_bkpt_print_it;
13476 ops->print_mention = internal_bkpt_print_mention;
13477
13478 /* Momentary breakpoints. */
13479 ops = &momentary_breakpoint_ops;
13480 *ops = bkpt_base_breakpoint_ops;
13481 ops->re_set = momentary_bkpt_re_set;
13482 ops->check_status = momentary_bkpt_check_status;
13483 ops->print_it = momentary_bkpt_print_it;
13484 ops->print_mention = momentary_bkpt_print_mention;
13485
13486 /* GNU v3 exception catchpoints. */
13487 ops = &gnu_v3_exception_catchpoint_ops;
13488 *ops = bkpt_breakpoint_ops;
13489 ops->print_it = print_it_exception_catchpoint;
13490 ops->print_one = print_one_exception_catchpoint;
13491 ops->print_mention = print_mention_exception_catchpoint;
13492 ops->print_recreate = print_recreate_exception_catchpoint;
13493
13494 /* Watchpoints. */
13495 ops = &watchpoint_breakpoint_ops;
13496 *ops = base_breakpoint_ops;
3a5c3e22 13497 ops->dtor = dtor_watchpoint;
2060206e
PA
13498 ops->re_set = re_set_watchpoint;
13499 ops->insert_location = insert_watchpoint;
13500 ops->remove_location = remove_watchpoint;
13501 ops->breakpoint_hit = breakpoint_hit_watchpoint;
13502 ops->check_status = check_status_watchpoint;
13503 ops->resources_needed = resources_needed_watchpoint;
13504 ops->works_in_software_mode = works_in_software_mode_watchpoint;
13505 ops->print_it = print_it_watchpoint;
13506 ops->print_mention = print_mention_watchpoint;
13507 ops->print_recreate = print_recreate_watchpoint;
13508
13509 /* Masked watchpoints. */
13510 ops = &masked_watchpoint_breakpoint_ops;
13511 *ops = watchpoint_breakpoint_ops;
13512 ops->insert_location = insert_masked_watchpoint;
13513 ops->remove_location = remove_masked_watchpoint;
13514 ops->resources_needed = resources_needed_masked_watchpoint;
13515 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
13516 ops->print_it = print_it_masked_watchpoint;
13517 ops->print_one_detail = print_one_detail_masked_watchpoint;
13518 ops->print_mention = print_mention_masked_watchpoint;
13519 ops->print_recreate = print_recreate_masked_watchpoint;
13520
13521 /* Tracepoints. */
13522 ops = &tracepoint_breakpoint_ops;
13523 *ops = base_breakpoint_ops;
13524 ops->re_set = tracepoint_re_set;
13525 ops->breakpoint_hit = tracepoint_breakpoint_hit;
13526 ops->print_one_detail = tracepoint_print_one_detail;
13527 ops->print_mention = tracepoint_print_mention;
13528 ops->print_recreate = tracepoint_print_recreate;
13529
13530 /* Fork catchpoints. */
13531 ops = &catch_fork_breakpoint_ops;
13532 *ops = base_breakpoint_ops;
13533 ops->insert_location = insert_catch_fork;
13534 ops->remove_location = remove_catch_fork;
13535 ops->breakpoint_hit = breakpoint_hit_catch_fork;
13536 ops->print_it = print_it_catch_fork;
13537 ops->print_one = print_one_catch_fork;
13538 ops->print_mention = print_mention_catch_fork;
13539 ops->print_recreate = print_recreate_catch_fork;
13540
13541 /* Vfork catchpoints. */
13542 ops = &catch_vfork_breakpoint_ops;
13543 *ops = base_breakpoint_ops;
13544 ops->insert_location = insert_catch_vfork;
13545 ops->remove_location = remove_catch_vfork;
13546 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
13547 ops->print_it = print_it_catch_vfork;
13548 ops->print_one = print_one_catch_vfork;
13549 ops->print_mention = print_mention_catch_vfork;
13550 ops->print_recreate = print_recreate_catch_vfork;
13551
13552 /* Exec catchpoints. */
13553 ops = &catch_exec_breakpoint_ops;
13554 *ops = base_breakpoint_ops;
13555 ops->dtor = dtor_catch_exec;
13556 ops->insert_location = insert_catch_exec;
13557 ops->remove_location = remove_catch_exec;
13558 ops->breakpoint_hit = breakpoint_hit_catch_exec;
13559 ops->print_it = print_it_catch_exec;
13560 ops->print_one = print_one_catch_exec;
13561 ops->print_mention = print_mention_catch_exec;
13562 ops->print_recreate = print_recreate_catch_exec;
13563
13564 /* Syscall catchpoints. */
13565 ops = &catch_syscall_breakpoint_ops;
13566 *ops = base_breakpoint_ops;
13567 ops->dtor = dtor_catch_syscall;
13568 ops->insert_location = insert_catch_syscall;
13569 ops->remove_location = remove_catch_syscall;
13570 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
13571 ops->print_it = print_it_catch_syscall;
13572 ops->print_one = print_one_catch_syscall;
13573 ops->print_mention = print_mention_catch_syscall;
13574 ops->print_recreate = print_recreate_catch_syscall;
13575}
13576
c906108c 13577void
fba45db2 13578_initialize_breakpoint (void)
c906108c
SS
13579{
13580 struct cmd_list_element *c;
13581
2060206e
PA
13582 initialize_breakpoint_ops ();
13583
84acb35a 13584 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 13585 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 13586 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 13587
17450429
PP
13588 breakpoint_objfile_key = register_objfile_data ();
13589
c906108c
SS
13590 breakpoint_chain = 0;
13591 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13592 before a breakpoint is set. */
13593 breakpoint_count = 0;
13594
1042e4c0
SS
13595 tracepoint_count = 0;
13596
1bedd215
AC
13597 add_com ("ignore", class_breakpoint, ignore_command, _("\
13598Set ignore-count of breakpoint number N to COUNT.\n\
13599Usage is `ignore N COUNT'."));
c906108c 13600 if (xdb_commands)
c5aa993b 13601 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 13602
1bedd215
AC
13603 add_com ("commands", class_breakpoint, commands_command, _("\
13604Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
13605Give breakpoint number as argument after \"commands\".\n\
13606With no argument, the targeted breakpoint is the last one set.\n\
13607The commands themselves follow starting on the next line.\n\
13608Type a line containing \"end\" to indicate the end of them.\n\
13609Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 13610then no output is printed when it is hit, except what the commands print."));
c906108c 13611
1bedd215
AC
13612 add_com ("condition", class_breakpoint, condition_command, _("\
13613Specify breakpoint number N to break only if COND is true.\n\
c906108c 13614Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 13615expression to be evaluated whenever breakpoint N is reached."));
c906108c 13616
1bedd215 13617 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 13618Set a temporary breakpoint.\n\
c906108c
SS
13619Like \"break\" except the breakpoint is only temporary,\n\
13620so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
13621by using \"enable delete\" on the breakpoint number.\n\
13622\n"
13623BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 13624 set_cmd_completer (c, location_completer);
c94fdfd0 13625
1bedd215 13626 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 13627Set a hardware assisted breakpoint.\n\
c906108c 13628Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
13629some target hardware may not have this support.\n\
13630\n"
13631BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 13632 set_cmd_completer (c, location_completer);
c906108c 13633
1bedd215 13634 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 13635Set a temporary hardware assisted breakpoint.\n\
c906108c 13636Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
13637so it will be deleted when hit.\n\
13638\n"
13639BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 13640 set_cmd_completer (c, location_completer);
c906108c 13641
1bedd215
AC
13642 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13643Enable some breakpoints.\n\
c906108c
SS
13644Give breakpoint numbers (separated by spaces) as arguments.\n\
13645With no subcommand, breakpoints are enabled until you command otherwise.\n\
13646This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13647With a subcommand you can enable temporarily."),
c906108c
SS
13648 &enablelist, "enable ", 1, &cmdlist);
13649 if (xdb_commands)
1bedd215
AC
13650 add_com ("ab", class_breakpoint, enable_command, _("\
13651Enable some breakpoints.\n\
c906108c
SS
13652Give breakpoint numbers (separated by spaces) as arguments.\n\
13653With no subcommand, breakpoints are enabled until you command otherwise.\n\
13654This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13655With a subcommand you can enable temporarily."));
c906108c
SS
13656
13657 add_com_alias ("en", "enable", class_breakpoint, 1);
13658
84951ab5 13659 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 13660Enable some breakpoints.\n\
c906108c
SS
13661Give breakpoint numbers (separated by spaces) as arguments.\n\
13662This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13663May be abbreviated to simply \"enable\".\n"),
c5aa993b 13664 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 13665
1a966eab
AC
13666 add_cmd ("once", no_class, enable_once_command, _("\
13667Enable breakpoints for one hit. Give breakpoint numbers.\n\
13668If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13669 &enablebreaklist);
13670
1a966eab
AC
13671 add_cmd ("delete", no_class, enable_delete_command, _("\
13672Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13673If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13674 &enablebreaklist);
13675
1a966eab
AC
13676 add_cmd ("delete", no_class, enable_delete_command, _("\
13677Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13678If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13679 &enablelist);
13680
1a966eab
AC
13681 add_cmd ("once", no_class, enable_once_command, _("\
13682Enable breakpoints for one hit. Give breakpoint numbers.\n\
13683If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13684 &enablelist);
13685
1bedd215
AC
13686 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13687Disable some breakpoints.\n\
c906108c
SS
13688Arguments are breakpoint numbers with spaces in between.\n\
13689To disable all breakpoints, give no argument.\n\
64b9b334 13690A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
13691 &disablelist, "disable ", 1, &cmdlist);
13692 add_com_alias ("dis", "disable", class_breakpoint, 1);
13693 add_com_alias ("disa", "disable", class_breakpoint, 1);
13694 if (xdb_commands)
1bedd215
AC
13695 add_com ("sb", class_breakpoint, disable_command, _("\
13696Disable some breakpoints.\n\
c906108c
SS
13697Arguments are breakpoint numbers with spaces in between.\n\
13698To disable all breakpoints, give no argument.\n\
64b9b334 13699A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 13700
1a966eab
AC
13701 add_cmd ("breakpoints", class_alias, disable_command, _("\
13702Disable some breakpoints.\n\
c906108c
SS
13703Arguments are breakpoint numbers with spaces in between.\n\
13704To disable all breakpoints, give no argument.\n\
64b9b334 13705A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 13706This command may be abbreviated \"disable\"."),
c906108c
SS
13707 &disablelist);
13708
1bedd215
AC
13709 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13710Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13711Arguments are breakpoint numbers with spaces in between.\n\
13712To delete all breakpoints, give no argument.\n\
13713\n\
13714Also a prefix command for deletion of other GDB objects.\n\
1bedd215 13715The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
13716 &deletelist, "delete ", 1, &cmdlist);
13717 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 13718 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 13719 if (xdb_commands)
1bedd215
AC
13720 add_com ("db", class_breakpoint, delete_command, _("\
13721Delete some breakpoints.\n\
c906108c 13722Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 13723To delete all breakpoints, give no argument.\n"));
c906108c 13724
1a966eab
AC
13725 add_cmd ("breakpoints", class_alias, delete_command, _("\
13726Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13727Arguments are breakpoint numbers with spaces in between.\n\
13728To delete all breakpoints, give no argument.\n\
1a966eab 13729This command may be abbreviated \"delete\"."),
c906108c
SS
13730 &deletelist);
13731
1bedd215
AC
13732 add_com ("clear", class_breakpoint, clear_command, _("\
13733Clear breakpoint at specified line or function.\n\
c906108c
SS
13734Argument may be line number, function name, or \"*\" and an address.\n\
13735If line number is specified, all breakpoints in that line are cleared.\n\
13736If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
13737If an address is specified, breakpoints at that address are cleared.\n\
13738\n\
13739With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
13740is executing in.\n\
13741\n\
1bedd215 13742See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 13743 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 13744
1bedd215 13745 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
13746Set breakpoint at specified line or function.\n"
13747BREAK_ARGS_HELP ("break")));
5ba2abeb 13748 set_cmd_completer (c, location_completer);
c94fdfd0 13749
c906108c
SS
13750 add_com_alias ("b", "break", class_run, 1);
13751 add_com_alias ("br", "break", class_run, 1);
13752 add_com_alias ("bre", "break", class_run, 1);
13753 add_com_alias ("brea", "break", class_run, 1);
13754
7681d515
PM
13755 if (xdb_commands)
13756 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
13757
13758 if (dbx_commands)
13759 {
1bedd215
AC
13760 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13761Break in function/address or break at a line in the current file."),
c5aa993b
JM
13762 &stoplist, "stop ", 1, &cmdlist);
13763 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 13764 _("Break in function or address."), &stoplist);
c5aa993b 13765 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 13766 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
13767 add_com ("status", class_info, breakpoints_info, _("\
13768Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13769The \"Type\" column indicates one of:\n\
13770\tbreakpoint - normal breakpoint\n\
13771\twatchpoint - watchpoint\n\
13772The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13773the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13774breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13775address and file/line number respectively.\n\
13776\n\
13777Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13778are set to the address of the last breakpoint listed unless the command\n\
13779is prefixed with \"server \".\n\n\
c906108c 13780Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13781breakpoint set."));
c906108c
SS
13782 }
13783
1bedd215 13784 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 13785Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
13786The \"Type\" column indicates one of:\n\
13787\tbreakpoint - normal breakpoint\n\
13788\twatchpoint - watchpoint\n\
13789The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13790the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13791breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13792address and file/line number respectively.\n\
13793\n\
13794Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13795are set to the address of the last breakpoint listed unless the command\n\
13796is prefixed with \"server \".\n\n\
c906108c 13797Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13798breakpoint set."));
c906108c 13799
6b04bdb7
MS
13800 add_info_alias ("b", "breakpoints", 1);
13801
c906108c 13802 if (xdb_commands)
1bedd215
AC
13803 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13804Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13805The \"Type\" column indicates one of:\n\
13806\tbreakpoint - normal breakpoint\n\
13807\twatchpoint - watchpoint\n\
13808The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13809the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13810breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13811address and file/line number respectively.\n\
13812\n\
13813Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13814are set to the address of the last breakpoint listed unless the command\n\
13815is prefixed with \"server \".\n\n\
c906108c 13816Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13817breakpoint set."));
c906108c 13818
1a966eab
AC
13819 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13820Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13821The \"Type\" column indicates one of:\n\
13822\tbreakpoint - normal breakpoint\n\
13823\twatchpoint - watchpoint\n\
13824\tlongjmp - internal breakpoint used to step through longjmp()\n\
13825\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13826\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
13827\tfinish - internal breakpoint used by the \"finish\" command\n\
13828The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
13829the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13830breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
13831address and file/line number respectively.\n\
13832\n\
13833Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13834are set to the address of the last breakpoint listed unless the command\n\
13835is prefixed with \"server \".\n\n\
c906108c 13836Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 13837breakpoint set."),
c906108c
SS
13838 &maintenanceinfolist);
13839
44feb3ce
TT
13840 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13841Set catchpoints to catch events."),
13842 &catch_cmdlist, "catch ",
13843 0/*allow-unknown*/, &cmdlist);
13844
13845 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13846Set temporary catchpoints to catch events."),
13847 &tcatch_cmdlist, "tcatch ",
13848 0/*allow-unknown*/, &cmdlist);
13849
13850 /* Add catch and tcatch sub-commands. */
13851 add_catch_command ("catch", _("\
88e7d25d 13852Catch an exception, when caught."),
44feb3ce 13853 catch_catch_command,
a96d9b2e 13854 NULL,
44feb3ce
TT
13855 CATCH_PERMANENT,
13856 CATCH_TEMPORARY);
13857 add_catch_command ("throw", _("\
88e7d25d 13858Catch an exception, when thrown."),
44feb3ce 13859 catch_throw_command,
a96d9b2e 13860 NULL,
44feb3ce
TT
13861 CATCH_PERMANENT,
13862 CATCH_TEMPORARY);
13863 add_catch_command ("fork", _("Catch calls to fork."),
13864 catch_fork_command_1,
a96d9b2e 13865 NULL,
44feb3ce
TT
13866 (void *) (uintptr_t) catch_fork_permanent,
13867 (void *) (uintptr_t) catch_fork_temporary);
13868 add_catch_command ("vfork", _("Catch calls to vfork."),
13869 catch_fork_command_1,
a96d9b2e 13870 NULL,
44feb3ce
TT
13871 (void *) (uintptr_t) catch_vfork_permanent,
13872 (void *) (uintptr_t) catch_vfork_temporary);
13873 add_catch_command ("exec", _("Catch calls to exec."),
13874 catch_exec_command_1,
a96d9b2e
SDJ
13875 NULL,
13876 CATCH_PERMANENT,
13877 CATCH_TEMPORARY);
13878 add_catch_command ("syscall", _("\
13879Catch system calls by their names and/or numbers.\n\
13880Arguments say which system calls to catch. If no arguments\n\
13881are given, every system call will be caught.\n\
13882Arguments, if given, should be one or more system call names\n\
13883(if your system supports that), or system call numbers."),
13884 catch_syscall_command_1,
13885 catch_syscall_completer,
44feb3ce
TT
13886 CATCH_PERMANENT,
13887 CATCH_TEMPORARY);
c5aa993b 13888
1bedd215
AC
13889 c = add_com ("watch", class_breakpoint, watch_command, _("\
13890Set a watchpoint for an expression.\n\
06a64a0b 13891Usage: watch [-l|-location] EXPRESSION\n\
c906108c 13892A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13893an expression changes.\n\
13894If -l or -location is given, this evaluates EXPRESSION and watches\n\
13895the memory to which it refers."));
65d12d83 13896 set_cmd_completer (c, expression_completer);
c906108c 13897
1bedd215
AC
13898 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13899Set a read watchpoint for an expression.\n\
06a64a0b 13900Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 13901A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13902an expression is read.\n\
13903If -l or -location is given, this evaluates EXPRESSION and watches\n\
13904the memory to which it refers."));
65d12d83 13905 set_cmd_completer (c, expression_completer);
c906108c 13906
1bedd215
AC
13907 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13908Set a watchpoint for an expression.\n\
06a64a0b 13909Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 13910A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13911an expression is either read or written.\n\
13912If -l or -location is given, this evaluates EXPRESSION and watches\n\
13913the memory to which it refers."));
65d12d83 13914 set_cmd_completer (c, expression_completer);
c906108c 13915
d77f58be 13916 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 13917Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 13918
920d2a44
AC
13919 /* XXX: cagney/2005-02-23: This should be a boolean, and should
13920 respond to changes - contrary to the description. */
85c07804
AC
13921 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
13922 &can_use_hw_watchpoints, _("\
13923Set debugger's willingness to use watchpoint hardware."), _("\
13924Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
13925If zero, gdb will not use hardware for new watchpoints, even if\n\
13926such is available. (However, any hardware watchpoints that were\n\
13927created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
13928hardware.)"),
13929 NULL,
920d2a44 13930 show_can_use_hw_watchpoints,
85c07804 13931 &setlist, &showlist);
c906108c
SS
13932
13933 can_use_hw_watchpoints = 1;
fa8d40ab 13934
1042e4c0
SS
13935 /* Tracepoint manipulation commands. */
13936
13937 c = add_com ("trace", class_breakpoint, trace_command, _("\
13938Set a tracepoint at specified line or function.\n\
13939\n"
13940BREAK_ARGS_HELP ("trace") "\n\
13941Do \"help tracepoints\" for info on other tracepoint commands."));
13942 set_cmd_completer (c, location_completer);
13943
13944 add_com_alias ("tp", "trace", class_alias, 0);
13945 add_com_alias ("tr", "trace", class_alias, 1);
13946 add_com_alias ("tra", "trace", class_alias, 1);
13947 add_com_alias ("trac", "trace", class_alias, 1);
13948
7a697b8d
SS
13949 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
13950Set a fast tracepoint at specified line or function.\n\
13951\n"
13952BREAK_ARGS_HELP ("ftrace") "\n\
13953Do \"help tracepoints\" for info on other tracepoint commands."));
13954 set_cmd_completer (c, location_completer);
13955
0fb4aa4b
PA
13956 c = add_com ("strace", class_breakpoint, strace_command, _("\
13957Set a static tracepoint at specified line, function or marker.\n\
13958\n\
13959strace [LOCATION] [if CONDITION]\n\
13960LOCATION may be a line number, function name, \"*\" and an address,\n\
13961or -m MARKER_ID.\n\
13962If a line number is specified, probe the marker at start of code\n\
13963for that line. If a function is specified, probe the marker at start\n\
13964of code for that function. If an address is specified, probe the marker\n\
13965at that exact address. If a marker id is specified, probe the marker\n\
13966with that name. With no LOCATION, uses current execution address of\n\
13967the selected stack frame.\n\
13968Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
13969This collects arbitrary user data passed in the probe point call to the\n\
13970tracing library. You can inspect it when analyzing the trace buffer,\n\
13971by printing the $_sdata variable like any other convenience variable.\n\
13972\n\
13973CONDITION is a boolean expression.\n\
13974\n\
d41c0fc8
PA
13975Multiple tracepoints at one place are permitted, and useful if their\n\
13976conditions are different.\n\
0fb4aa4b
PA
13977\n\
13978Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13979Do \"help tracepoints\" for info on other tracepoint commands."));
13980 set_cmd_completer (c, location_completer);
13981
1042e4c0 13982 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 13983Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
13984Convenience variable \"$tpnum\" contains the number of the\n\
13985last tracepoint set."));
13986
13987 add_info_alias ("tp", "tracepoints", 1);
13988
13989 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13990Delete specified tracepoints.\n\
13991Arguments are tracepoint numbers, separated by spaces.\n\
13992No argument means delete all tracepoints."),
13993 &deletelist);
13994
13995 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13996Disable specified tracepoints.\n\
13997Arguments are tracepoint numbers, separated by spaces.\n\
13998No argument means disable all tracepoints."),
13999 &disablelist);
14000 deprecate_cmd (c, "disable");
14001
14002 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14003Enable specified tracepoints.\n\
14004Arguments are tracepoint numbers, separated by spaces.\n\
14005No argument means enable all tracepoints."),
14006 &enablelist);
14007 deprecate_cmd (c, "enable");
14008
14009 add_com ("passcount", class_trace, trace_pass_command, _("\
14010Set the passcount for a tracepoint.\n\
14011The trace will end when the tracepoint has been passed 'count' times.\n\
14012Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14013if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14014
6149aea9
PA
14015 add_prefix_cmd ("save", class_breakpoint, save_command,
14016 _("Save breakpoint definitions as a script."),
14017 &save_cmdlist, "save ",
14018 0/*allow-unknown*/, &cmdlist);
14019
14020 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14021Save current breakpoint definitions as a script.\n\
cce7e648 14022This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14023catchpoints, tracepoints). Use the 'source' command in another debug\n\
14024session to restore them."),
14025 &save_cmdlist);
14026 set_cmd_completer (c, filename_completer);
14027
14028 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14029Save current tracepoint definitions as a script.\n\
6149aea9
PA
14030Use the 'source' command in another debug session to restore them."),
14031 &save_cmdlist);
1042e4c0
SS
14032 set_cmd_completer (c, filename_completer);
14033
6149aea9
PA
14034 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
14035 deprecate_cmd (c, "save tracepoints");
14036
1bedd215 14037 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
14038Breakpoint specific settings\n\
14039Configure various breakpoint-specific variables such as\n\
1bedd215 14040pending breakpoint behavior"),
fa8d40ab
JJ
14041 &breakpoint_set_cmdlist, "set breakpoint ",
14042 0/*allow-unknown*/, &setlist);
1bedd215 14043 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
14044Breakpoint specific settings\n\
14045Configure various breakpoint-specific variables such as\n\
1bedd215 14046pending breakpoint behavior"),
fa8d40ab
JJ
14047 &breakpoint_show_cmdlist, "show breakpoint ",
14048 0/*allow-unknown*/, &showlist);
14049
7915a72c
AC
14050 add_setshow_auto_boolean_cmd ("pending", no_class,
14051 &pending_break_support, _("\
14052Set debugger's behavior regarding pending breakpoints."), _("\
14053Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14054If on, an unrecognized breakpoint location will cause gdb to create a\n\
14055pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14056an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14057user-query to see if a pending breakpoint should be created."),
2c5b56ce 14058 NULL,
920d2a44 14059 show_pending_break_support,
6e1d7d6c
AC
14060 &breakpoint_set_cmdlist,
14061 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14062
14063 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14064
14065 add_setshow_boolean_cmd ("auto-hw", no_class,
14066 &automatic_hardware_breakpoints, _("\
14067Set automatic usage of hardware breakpoints."), _("\
14068Show automatic usage of hardware breakpoints."), _("\
14069If set, the debugger will automatically use hardware breakpoints for\n\
14070breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14071a warning will be emitted for such breakpoints."),
14072 NULL,
14073 show_automatic_hardware_breakpoints,
14074 &breakpoint_set_cmdlist,
14075 &breakpoint_show_cmdlist);
74960c60 14076
33e5cbd6
PA
14077 add_setshow_enum_cmd ("always-inserted", class_support,
14078 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
14079Set mode for inserting breakpoints."), _("\
14080Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
14081When this mode is off, breakpoints are inserted in inferior when it is\n\
14082resumed, and removed when execution stops. When this mode is on,\n\
14083breakpoints are inserted immediately and removed only when the user\n\
14084deletes the breakpoint. When this mode is auto (which is the default),\n\
14085the behaviour depends on the non-stop setting (see help set non-stop).\n\
14086In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
14087behaves as if always-inserted mode is on; if gdb is controlling the\n\
14088inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
14089 NULL,
14090 &show_always_inserted_mode,
14091 &breakpoint_set_cmdlist,
14092 &breakpoint_show_cmdlist);
f1310107
TJB
14093
14094 add_com ("break-range", class_breakpoint, break_range_command, _("\
14095Set a breakpoint for an address range.\n\
14096break-range START-LOCATION, END-LOCATION\n\
14097where START-LOCATION and END-LOCATION can be one of the following:\n\
14098 LINENUM, for that line in the current file,\n\
14099 FILE:LINENUM, for that line in that file,\n\
14100 +OFFSET, for that number of lines after the current line\n\
14101 or the start of the range\n\
14102 FUNCTION, for the first line in that function,\n\
14103 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14104 *ADDRESS, for the instruction at that address.\n\
14105\n\
14106The breakpoint will stop execution of the inferior whenever it executes\n\
14107an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14108range (including START-LOCATION and END-LOCATION)."));
14109
765dc015 14110 automatic_hardware_breakpoints = 1;
f3b1572e
PA
14111
14112 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 14113}