PR 10450
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1 4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
0fb0cc75 5 2008, 2009 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
a6d9a66e 23#include "arch-utils.h"
c906108c 24#include <ctype.h>
776592bf 25#include "hashtab.h"
c906108c
SS
26#include "symtab.h"
27#include "frame.h"
28#include "breakpoint.h"
1042e4c0 29#include "tracepoint.h"
c906108c
SS
30#include "gdbtypes.h"
31#include "expression.h"
32#include "gdbcore.h"
33#include "gdbcmd.h"
34#include "value.h"
35#include "command.h"
36#include "inferior.h"
37#include "gdbthread.h"
38#include "target.h"
39#include "language.h"
40#include "gdb_string.h"
41#include "demangle.h"
42#include "annotate.h"
43#include "symfile.h"
44#include "objfiles.h"
0378c332 45#include "source.h"
c5f0f3d0 46#include "linespec.h"
c94fdfd0 47#include "completer.h"
5b7f31a4 48#include "gdb.h"
8b93c638 49#include "ui-out.h"
e1507482 50#include "cli/cli-script.h"
0225421b 51#include "gdb_assert.h"
fe898f56 52#include "block.h"
a77053c2 53#include "solib.h"
84acb35a
JJ
54#include "solist.h"
55#include "observer.h"
60250e8b 56#include "exceptions.h"
765dc015 57#include "memattr.h"
f7f9143b 58#include "ada-lang.h"
d1aa2f50 59#include "top.h"
fa4727a6 60#include "wrapper.h"
79a45b7d 61#include "valprint.h"
4efc6507 62#include "jit.h"
a96d9b2e 63#include "xml-syscall.h"
c906108c 64
1042e4c0
SS
65/* readline include files */
66#include "readline/readline.h"
67#include "readline/history.h"
68
69/* readline defines this. */
70#undef savestring
71
034dad6f 72#include "mi/mi-common.h"
104c1213 73
44feb3ce
TT
74/* Arguments to pass as context to some catch command handlers. */
75#define CATCH_PERMANENT ((void *) (uintptr_t) 0)
76#define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
c906108c 77
44feb3ce 78/* Prototypes for local functions. */
c906108c 79
a14ed312 80static void enable_delete_command (char *, int);
c906108c 81
a14ed312 82static void enable_delete_breakpoint (struct breakpoint *);
c906108c 83
a14ed312 84static void enable_once_command (char *, int);
c906108c 85
a14ed312 86static void enable_once_breakpoint (struct breakpoint *);
c906108c 87
a14ed312 88static void disable_command (char *, int);
c906108c 89
a14ed312 90static void enable_command (char *, int);
c906108c 91
a14ed312 92static void map_breakpoint_numbers (char *, void (*)(struct breakpoint *));
c906108c 93
a14ed312 94static void ignore_command (char *, int);
c906108c 95
4efb68b1 96static int breakpoint_re_set_one (void *);
c906108c 97
a14ed312 98static void clear_command (char *, int);
c906108c 99
a14ed312 100static void catch_command (char *, int);
c906108c 101
a14ed312 102static void watch_command (char *, int);
c906108c 103
a14ed312 104static int can_use_hardware_watchpoint (struct value *);
c906108c 105
98deb0da 106static void break_command_1 (char *, int, int);
c906108c 107
a14ed312 108static void mention (struct breakpoint *);
c906108c 109
63c252f8
PM
110/* This function is used in gdbtk sources and thus can not be made static. */
111struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
a6d9a66e
UW
112 struct symtab_and_line,
113 enum bptype);
c906108c 114
76897487
KB
115static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
116
a6d9a66e
UW
117static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
118 CORE_ADDR bpaddr,
88f7da05 119 enum bptype bptype);
76897487 120
6c95b8df
PA
121static void describe_other_breakpoints (struct gdbarch *,
122 struct program_space *, CORE_ADDR,
5af949e3 123 struct obj_section *, int);
c906108c 124
6c95b8df
PA
125static int breakpoint_address_match (struct address_space *aspace1,
126 CORE_ADDR addr1,
127 struct address_space *aspace2,
128 CORE_ADDR addr2);
129
a14ed312 130static void breakpoints_info (char *, int);
c906108c 131
a14ed312 132static void breakpoint_1 (int, int);
c906108c 133
89f9893c 134static bpstat bpstat_alloc (const struct bp_location *, bpstat);
c906108c 135
4efb68b1 136static int breakpoint_cond_eval (void *);
c906108c 137
4efb68b1 138static void cleanup_executing_breakpoints (void *);
c906108c 139
a14ed312 140static void commands_command (char *, int);
c906108c 141
a14ed312 142static void condition_command (char *, int);
c906108c 143
a14ed312 144static int get_number_trailer (char **, int);
c906108c 145
a14ed312 146void set_breakpoint_count (int);
c906108c 147
c5aa993b
JM
148typedef enum
149 {
150 mark_inserted,
151 mark_uninserted
152 }
153insertion_state_t;
c906108c 154
0bde7532 155static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 156static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 157
a14ed312 158static enum print_stop_action print_it_typical (bpstat);
e514a9d6
JM
159
160static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 161
4efb68b1 162static int watchpoint_check (void *);
c906108c 163
a14ed312 164static void maintenance_info_breakpoints (char *, int);
c906108c 165
a14ed312 166static int hw_breakpoint_used_count (void);
c906108c 167
a14ed312 168static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 169
a14ed312 170static void hbreak_command (char *, int);
c906108c 171
a14ed312 172static void thbreak_command (char *, int);
c906108c 173
a14ed312 174static void watch_command_1 (char *, int, int);
c906108c 175
a14ed312 176static void rwatch_command (char *, int);
c906108c 177
a14ed312 178static void awatch_command (char *, int);
c906108c 179
a14ed312 180static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
c906108c 181
a14ed312 182static void stop_command (char *arg, int from_tty);
7a292a7a 183
a14ed312 184static void stopin_command (char *arg, int from_tty);
7a292a7a 185
a14ed312 186static void stopat_command (char *arg, int from_tty);
7a292a7a 187
a14ed312 188static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 189
a14ed312 190static char *ep_parse_optional_filename (char **arg);
7a292a7a 191
d85310f7
MS
192static void catch_exception_command_1 (enum exception_event_kind ex_event,
193 char *arg, int tempflag, int from_tty);
7a292a7a 194
a14ed312 195static void tcatch_command (char *arg, int from_tty);
7a292a7a 196
a14ed312 197static void ep_skip_leading_whitespace (char **s);
7a292a7a 198
6c95b8df
PA
199static int single_step_breakpoint_inserted_here_p (struct address_space *,
200 CORE_ADDR pc);
1aafd4da 201
fe3f5fa8
VP
202static void free_bp_location (struct bp_location *loc);
203
39d61571 204static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 205
b60e7edf 206static void update_global_location_list (int);
a5606eee 207
b60e7edf 208static void update_global_location_list_nothrow (int);
74960c60
VP
209
210static int is_hardware_watchpoint (struct breakpoint *bpt);
211
212static void insert_breakpoint_locations (void);
a5606eee 213
a96d9b2e
SDJ
214static int syscall_catchpoint_p (struct breakpoint *b);
215
1042e4c0
SS
216static void tracepoints_info (char *, int);
217
218static void delete_trace_command (char *, int);
219
220static void enable_trace_command (char *, int);
221
222static void disable_trace_command (char *, int);
223
224static void trace_pass_command (char *, int);
225
3daf8fe5
JG
226static void skip_prologue_sal (struct symtab_and_line *sal);
227
228
f3b1572e
PA
229/* Flag indicating that a command has proceeded the inferior past the
230 current breakpoint. */
231
232static int breakpoint_proceeded;
233
2cec12e5
AR
234static const char *
235bpdisp_text (enum bpdisp disp)
236{
237 /* NOTE: the following values are a part of MI protocol and represent
238 values of 'disp' field returned when inferior stops at a breakpoint. */
239 static char *bpdisps[] = {"del", "dstp", "dis", "keep"};
240 return bpdisps[(int) disp];
241}
c906108c 242
2cec12e5 243/* Prototypes for exported functions. */
c906108c
SS
244/* If FALSE, gdb will not use hardware support for watchpoints, even
245 if such is available. */
246static int can_use_hw_watchpoints;
247
920d2a44
AC
248static void
249show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
250 struct cmd_list_element *c,
251 const char *value)
252{
253 fprintf_filtered (file, _("\
254Debugger's willingness to use watchpoint hardware is %s.\n"),
255 value);
256}
257
fa8d40ab
JJ
258/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
259 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
260 for unrecognized breakpoint locations.
261 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
262static enum auto_boolean pending_break_support;
920d2a44
AC
263static void
264show_pending_break_support (struct ui_file *file, int from_tty,
265 struct cmd_list_element *c,
266 const char *value)
267{
268 fprintf_filtered (file, _("\
269Debugger's behavior regarding pending breakpoints is %s.\n"),
270 value);
271}
fa8d40ab 272
765dc015
VP
273/* If 1, gdb will automatically use hardware breakpoints for breakpoints
274 set with "break" but falling in read-only memory.
275 If 0, gdb will warn about such breakpoints, but won't automatically
276 use hardware breakpoints. */
277static int automatic_hardware_breakpoints;
278static void
279show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
280 struct cmd_list_element *c,
281 const char *value)
282{
283 fprintf_filtered (file, _("\
284Automatic usage of hardware breakpoints is %s.\n"),
285 value);
286}
287
33e5cbd6
PA
288/* If on, gdb will keep breakpoints inserted even as inferior is
289 stopped, and immediately insert any new breakpoints. If off, gdb
290 will insert breakpoints into inferior only when resuming it, and
291 will remove breakpoints upon stop. If auto, GDB will behave as ON
292 if in non-stop mode, and as OFF if all-stop mode.*/
293
294static const char always_inserted_auto[] = "auto";
295static const char always_inserted_on[] = "on";
296static const char always_inserted_off[] = "off";
297static const char *always_inserted_enums[] = {
298 always_inserted_auto,
299 always_inserted_off,
300 always_inserted_on,
301 NULL
302};
303static const char *always_inserted_mode = always_inserted_auto;
304static void
74960c60 305show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 306 struct cmd_list_element *c, const char *value)
74960c60 307{
33e5cbd6
PA
308 if (always_inserted_mode == always_inserted_auto)
309 fprintf_filtered (file, _("\
310Always inserted breakpoint mode is %s (currently %s).\n"),
311 value,
312 breakpoints_always_inserted_mode () ? "on" : "off");
313 else
314 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"), value);
74960c60
VP
315}
316
33e5cbd6
PA
317int
318breakpoints_always_inserted_mode (void)
319{
320 return (always_inserted_mode == always_inserted_on
321 || (always_inserted_mode == always_inserted_auto && non_stop));
322}
765dc015 323
a14ed312 324void _initialize_breakpoint (void);
c906108c 325
c906108c
SS
326/* Are we executing breakpoint commands? */
327static int executing_breakpoint_commands;
328
c02f5703
MS
329/* Are overlay event breakpoints enabled? */
330static int overlay_events_enabled;
331
c906108c
SS
332/* Walk the following statement or block through all breakpoints.
333 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the current
334 breakpoint. */
335
5c44784c 336#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 337
5c44784c
JM
338#define ALL_BREAKPOINTS_SAFE(B,TMP) \
339 for (B = breakpoint_chain; \
340 B ? (TMP=B->next, 1): 0; \
341 B = TMP)
c906108c 342
876fa593
JK
343/* Similar iterator for the low-level breakpoints. SAFE variant is not
344 provided so update_global_location_list must not be called while executing
345 the block of ALL_BP_LOCATIONS. */
7cc221ef 346
876fa593
JK
347#define ALL_BP_LOCATIONS(B,BP_TMP) \
348 for (BP_TMP = bp_location; \
349 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
350 BP_TMP++)
7cc221ef 351
1042e4c0
SS
352/* Iterator for tracepoints only. */
353
354#define ALL_TRACEPOINTS(B) \
355 for (B = breakpoint_chain; B; B = B->next) \
356 if ((B)->type == bp_tracepoint)
357
7cc221ef 358/* Chains of all breakpoints defined. */
c906108c
SS
359
360struct breakpoint *breakpoint_chain;
361
876fa593
JK
362/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
363
364static struct bp_location **bp_location;
365
366/* Number of elements of BP_LOCATION. */
367
368static unsigned bp_location_count;
369
370/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and ADDRESS
371 for the current elements of BP_LOCATION which get a valid result from
372 bp_location_has_shadow. You can use it for roughly limiting the subrange of
373 BP_LOCATION to scan for shadow bytes for an address you need to read. */
374
375static CORE_ADDR bp_location_placed_address_before_address_max;
376
377/* Maximum offset plus alignment between
378 bp_target_info.PLACED_ADDRESS + bp_target_info.SHADOW_LEN and ADDRESS for
379 the current elements of BP_LOCATION which get a valid result from
380 bp_location_has_shadow. You can use it for roughly limiting the subrange of
381 BP_LOCATION to scan for shadow bytes for an address you need to read. */
382
383static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 384
20874c92 385/* The locations that no longer correspond to any breakpoint,
876fa593 386 unlinked from bp_location array, but for which a hit
20874c92
VP
387 may still be reported by a target. */
388VEC(bp_location_p) *moribund_locations = NULL;
389
c906108c
SS
390/* Number of last breakpoint made. */
391
392int breakpoint_count;
393
1042e4c0
SS
394/* Number of last tracepoint made. */
395
396int tracepoint_count;
397
468d015d
JJ
398/* Return whether a breakpoint is an active enabled breakpoint. */
399static int
400breakpoint_enabled (struct breakpoint *b)
401{
0d381245 402 return (b->enable_state == bp_enabled);
468d015d
JJ
403}
404
c906108c
SS
405/* Set breakpoint count to NUM. */
406
407void
fba45db2 408set_breakpoint_count (int num)
c906108c
SS
409{
410 breakpoint_count = num;
4fa62494 411 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
412}
413
414/* Used in run_command to zero the hit count when a new run starts. */
415
416void
fba45db2 417clear_breakpoint_hit_counts (void)
c906108c
SS
418{
419 struct breakpoint *b;
420
421 ALL_BREAKPOINTS (b)
422 b->hit_count = 0;
423}
424
425/* Default address, symtab and line to put a breakpoint at
426 for "break" command with no arg.
427 if default_breakpoint_valid is zero, the other three are
428 not valid, and "break" with no arg is an error.
429
430 This set by print_stack_frame, which calls set_default_breakpoint. */
431
432int default_breakpoint_valid;
433CORE_ADDR default_breakpoint_address;
434struct symtab *default_breakpoint_symtab;
435int default_breakpoint_line;
6c95b8df
PA
436struct program_space *default_breakpoint_pspace;
437
c906108c
SS
438\f
439/* *PP is a string denoting a breakpoint. Get the number of the breakpoint.
440 Advance *PP after the string and any trailing whitespace.
441
442 Currently the string can either be a number or "$" followed by the name
443 of a convenience variable. Making it an expression wouldn't work well
5c44784c 444 for map_breakpoint_numbers (e.g. "4 + 5 + 6").
40c03ae8
EZ
445
446 If the string is a NULL pointer, that denotes the last breakpoint.
5c44784c
JM
447
448 TRAILER is a character which can be found after the number; most
449 commonly this is `-'. If you don't want a trailer, use \0. */
c906108c 450static int
fba45db2 451get_number_trailer (char **pp, int trailer)
c906108c 452{
5c44784c 453 int retval = 0; /* default */
c906108c
SS
454 char *p = *pp;
455
456 if (p == NULL)
457 /* Empty line means refer to the last breakpoint. */
458 return breakpoint_count;
459 else if (*p == '$')
460 {
461 /* Make a copy of the name, so we can null-terminate it
c5aa993b 462 to pass to lookup_internalvar(). */
c906108c
SS
463 char *varname;
464 char *start = ++p;
4fa62494 465 LONGEST val;
c906108c
SS
466
467 while (isalnum (*p) || *p == '_')
468 p++;
469 varname = (char *) alloca (p - start + 1);
470 strncpy (varname, start, p - start);
471 varname[p - start] = '\0';
4fa62494
UW
472 if (get_internalvar_integer (lookup_internalvar (varname), &val))
473 retval = (int) val;
5c44784c
JM
474 else
475 {
a3f17187 476 printf_filtered (_("Convenience variable must have integer value.\n"));
5c44784c
JM
477 retval = 0;
478 }
c906108c
SS
479 }
480 else
481 {
482 if (*p == '-')
483 ++p;
484 while (*p >= '0' && *p <= '9')
485 ++p;
486 if (p == *pp)
487 /* There is no number here. (e.g. "cond a == b"). */
5c44784c
JM
488 {
489 /* Skip non-numeric token */
490 while (*p && !isspace((int) *p))
491 ++p;
492 /* Return zero, which caller must interpret as error. */
493 retval = 0;
494 }
495 else
496 retval = atoi (*pp);
497 }
498 if (!(isspace (*p) || *p == '\0' || *p == trailer))
499 {
500 /* Trailing junk: return 0 and let caller print error msg. */
501 while (!(isspace (*p) || *p == '\0' || *p == trailer))
502 ++p;
503 retval = 0;
c906108c 504 }
c906108c
SS
505 while (isspace (*p))
506 p++;
507 *pp = p;
508 return retval;
509}
5c44784c 510
11cf8741 511
5c44784c
JM
512/* Like get_number_trailer, but don't allow a trailer. */
513int
fba45db2 514get_number (char **pp)
5c44784c
JM
515{
516 return get_number_trailer (pp, '\0');
517}
518
519/* Parse a number or a range.
520 * A number will be of the form handled by get_number.
521 * A range will be of the form <number1> - <number2>, and
522 * will represent all the integers between number1 and number2,
523 * inclusive.
524 *
525 * While processing a range, this fuction is called iteratively;
526 * At each call it will return the next value in the range.
527 *
528 * At the beginning of parsing a range, the char pointer PP will
529 * be advanced past <number1> and left pointing at the '-' token.
530 * Subsequent calls will not advance the pointer until the range
531 * is completed. The call that completes the range will advance
532 * pointer PP past <number2>.
533 */
534
535int
fba45db2 536get_number_or_range (char **pp)
5c44784c
JM
537{
538 static int last_retval, end_value;
539 static char *end_ptr;
540 static int in_range = 0;
541
542 if (**pp != '-')
543 {
544 /* Default case: pp is pointing either to a solo number,
545 or to the first number of a range. */
546 last_retval = get_number_trailer (pp, '-');
547 if (**pp == '-')
548 {
549 char **temp;
550
551 /* This is the start of a range (<number1> - <number2>).
552 Skip the '-', parse and remember the second number,
553 and also remember the end of the final token. */
554
555 temp = &end_ptr;
556 end_ptr = *pp + 1;
557 while (isspace ((int) *end_ptr))
558 end_ptr++; /* skip white space */
559 end_value = get_number (temp);
560 if (end_value < last_retval)
561 {
8a3fe4f8 562 error (_("inverted range"));
5c44784c
JM
563 }
564 else if (end_value == last_retval)
565 {
566 /* degenerate range (number1 == number2). Advance the
567 token pointer so that the range will be treated as a
568 single number. */
569 *pp = end_ptr;
570 }
571 else
572 in_range = 1;
573 }
574 }
575 else if (! in_range)
8a3fe4f8 576 error (_("negative value"));
5c44784c
JM
577 else
578 {
579 /* pp points to the '-' that betokens a range. All
580 number-parsing has already been done. Return the next
581 integer value (one greater than the saved previous value).
582 Do not advance the token pointer 'pp' until the end of range
583 is reached. */
584
585 if (++last_retval == end_value)
586 {
587 /* End of range reached; advance token pointer. */
588 *pp = end_ptr;
589 in_range = 0;
590 }
591 }
592 return last_retval;
593}
594
48cb2d85
VP
595/* Return the breakpoint with the specified number, or NULL
596 if the number does not refer to an existing breakpoint. */
597
598struct breakpoint *
599get_breakpoint (int num)
600{
601 struct breakpoint *b;
602
603 ALL_BREAKPOINTS (b)
604 if (b->number == num)
605 return b;
606
607 return NULL;
608}
5c44784c 609
c906108c
SS
610\f
611/* condition N EXP -- set break condition of breakpoint N to EXP. */
612
613static void
fba45db2 614condition_command (char *arg, int from_tty)
c906108c 615{
52f0bd74 616 struct breakpoint *b;
c906108c 617 char *p;
52f0bd74 618 int bnum;
c906108c
SS
619
620 if (arg == 0)
e2e0b3e5 621 error_no_arg (_("breakpoint number"));
c906108c
SS
622
623 p = arg;
624 bnum = get_number (&p);
5c44784c 625 if (bnum == 0)
8a3fe4f8 626 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
627
628 ALL_BREAKPOINTS (b)
629 if (b->number == bnum)
2f069f6f
JB
630 {
631 struct bp_location *loc = b->loc;
632 for (; loc; loc = loc->next)
633 {
634 if (loc->cond)
635 {
636 xfree (loc->cond);
637 loc->cond = 0;
638 }
639 }
640 if (b->cond_string != NULL)
641 xfree (b->cond_string);
c906108c 642
2f069f6f
JB
643 if (*p == 0)
644 {
645 b->cond_string = NULL;
646 if (from_tty)
647 printf_filtered (_("Breakpoint %d now unconditional.\n"), bnum);
648 }
649 else
650 {
651 arg = p;
652 /* I don't know if it matters whether this is the string the user
653 typed in or the decompiled expression. */
1b36a34b 654 b->cond_string = xstrdup (arg);
2f069f6f
JB
655 b->condition_not_parsed = 0;
656 for (loc = b->loc; loc; loc = loc->next)
657 {
658 arg = p;
659 loc->cond =
660 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
661 if (*arg)
662 error (_("Junk at end of expression"));
663 }
664 }
665 breakpoints_changed ();
383f836e 666 observer_notify_breakpoint_modified (b->number);
2f069f6f
JB
667 return;
668 }
c906108c 669
8a3fe4f8 670 error (_("No breakpoint number %d."), bnum);
c906108c
SS
671}
672
48cb2d85
VP
673/* Set the command list of B to COMMANDS. */
674
675void
676breakpoint_set_commands (struct breakpoint *b, struct command_line *commands)
677{
678 free_command_lines (&b->commands);
679 b->commands = commands;
680 breakpoints_changed ();
681 observer_notify_breakpoint_modified (b->number);
682}
683
c906108c 684static void
fba45db2 685commands_command (char *arg, int from_tty)
c906108c 686{
52f0bd74 687 struct breakpoint *b;
c906108c 688 char *p;
52f0bd74 689 int bnum;
c906108c
SS
690 struct command_line *l;
691
692 /* If we allowed this, we would have problems with when to
693 free the storage, if we change the commands currently
694 being read from. */
695
696 if (executing_breakpoint_commands)
8a3fe4f8 697 error (_("Can't use the \"commands\" command among a breakpoint's commands."));
c906108c
SS
698
699 p = arg;
700 bnum = get_number (&p);
5c44784c 701
c906108c 702 if (p && *p)
8a3fe4f8 703 error (_("Unexpected extra arguments following breakpoint number."));
c5aa993b 704
c906108c
SS
705 ALL_BREAKPOINTS (b)
706 if (b->number == bnum)
9ebf4acf
AC
707 {
708 char *tmpbuf = xstrprintf ("Type commands for when breakpoint %d is hit, one per line.",
709 bnum);
710 struct cleanup *cleanups = make_cleanup (xfree, tmpbuf);
311a4e6b 711 l = read_command_lines (tmpbuf, from_tty, 1);
9ebf4acf 712 do_cleanups (cleanups);
48cb2d85 713 breakpoint_set_commands (b, l);
9ebf4acf 714 return;
c5aa993b 715 }
8a3fe4f8 716 error (_("No breakpoint number %d."), bnum);
c906108c 717}
40c03ae8
EZ
718
719/* Like commands_command, but instead of reading the commands from
720 input stream, takes them from an already parsed command structure.
721
722 This is used by cli-script.c to DTRT with breakpoint commands
723 that are part of if and while bodies. */
724enum command_control_type
725commands_from_control_command (char *arg, struct command_line *cmd)
726{
727 struct breakpoint *b;
728 char *p;
729 int bnum;
730
731 /* If we allowed this, we would have problems with when to
732 free the storage, if we change the commands currently
733 being read from. */
734
735 if (executing_breakpoint_commands)
736 error (_("Can't use the \"commands\" command among a breakpoint's commands."));
737
738 /* An empty string for the breakpoint number means the last
739 breakpoint, but get_number expects a NULL pointer. */
740 if (arg && !*arg)
741 p = NULL;
742 else
743 p = arg;
744 bnum = get_number (&p);
745
746 if (p && *p)
747 error (_("Unexpected extra arguments following breakpoint number."));
748
749 ALL_BREAKPOINTS (b)
750 if (b->number == bnum)
751 {
752 free_command_lines (&b->commands);
753 if (cmd->body_count != 1)
754 error (_("Invalid \"commands\" block structure."));
755 /* We need to copy the commands because if/while will free the
756 list after it finishes execution. */
757 b->commands = copy_command_lines (cmd->body_list[0]);
758 breakpoints_changed ();
383f836e 759 observer_notify_breakpoint_modified (b->number);
40c03ae8 760 return simple_control;
2f069f6f 761 }
40c03ae8
EZ
762 error (_("No breakpoint number %d."), bnum);
763}
876fa593
JK
764
765/* Return non-zero if BL->TARGET_INFO contains valid information. */
766
767static int
768bp_location_has_shadow (struct bp_location *bl)
769{
770 if (bl->loc_type != bp_loc_software_breakpoint)
771 return 0;
772 if (!bl->inserted)
773 return 0;
774 if (bl->target_info.shadow_len == 0)
775 /* bp isn't valid, or doesn't shadow memory. */
776 return 0;
777 return 1;
778}
779
8defab1a 780/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
781 by replacing any memory breakpoints with their shadowed contents.
782
783 The range of shadowed area by each bp_location is:
784 b->address - bp_location_placed_address_before_address_max
785 up to b->address + bp_location_shadow_len_after_address_max
786 The range we were requested to resolve shadows for is:
787 memaddr ... memaddr + len
788 Thus the safe cutoff boundaries for performance optimization are
789 memaddr + len <= b->address - bp_location_placed_address_before_address_max
790 and:
791 b->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 792
8defab1a
DJ
793void
794breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 795{
876fa593
JK
796 /* Left boundary, right boundary and median element of our binary search. */
797 unsigned bc_l, bc_r, bc;
798
799 /* Find BC_L which is a leftmost element which may affect BUF content. It is
800 safe to report lower value but a failure to report higher one. */
801
802 bc_l = 0;
803 bc_r = bp_location_count;
804 while (bc_l + 1 < bc_r)
805 {
806 struct bp_location *b;
807
808 bc = (bc_l + bc_r) / 2;
809 b = bp_location[bc];
810
811 /* Check first B->ADDRESS will not overflow due to the added constant.
812 Then advance the left boundary only if we are sure the BC element can
813 in no way affect the BUF content (MEMADDR to MEMADDR + LEN range).
814
815 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety offset so that
816 we cannot miss a breakpoint with its shadow range tail still reaching
817 MEMADDR. */
c5aa993b 818
876fa593
JK
819 if (b->address + bp_location_shadow_len_after_address_max >= b->address
820 && b->address + bp_location_shadow_len_after_address_max <= memaddr)
821 bc_l = bc;
822 else
823 bc_r = bc;
824 }
825
826 /* Now do full processing of the found relevant range of elements. */
827
828 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 829 {
876fa593
JK
830 struct bp_location *b = bp_location[bc];
831 CORE_ADDR bp_addr = 0;
832 int bp_size = 0;
833 int bptoffset = 0;
834
ffce0d52 835 if (b->owner->type == bp_none)
8a3fe4f8 836 warning (_("reading through apparently deleted breakpoint #%d?"),
ffce0d52
DJ
837 b->owner->number);
838
876fa593
JK
839 /* Performance optimization: any futher element can no longer affect BUF
840 content. */
841
842 if (b->address >= bp_location_placed_address_before_address_max
843 && memaddr + len <= b->address
844 - bp_location_placed_address_before_address_max)
845 break;
846
847 if (!bp_location_has_shadow (b))
c5aa993b 848 continue;
6c95b8df
PA
849 if (!breakpoint_address_match (b->target_info.placed_address_space, 0,
850 current_program_space->aspace, 0))
851 continue;
852
c5aa993b
JM
853 /* Addresses and length of the part of the breakpoint that
854 we need to copy. */
8181d85f
DJ
855 bp_addr = b->target_info.placed_address;
856 bp_size = b->target_info.shadow_len;
8defab1a 857
c5aa993b
JM
858 if (bp_addr + bp_size <= memaddr)
859 /* The breakpoint is entirely before the chunk of memory we
860 are reading. */
861 continue;
8defab1a 862
c5aa993b
JM
863 if (bp_addr >= memaddr + len)
864 /* The breakpoint is entirely after the chunk of memory we are
865 reading. */
866 continue;
c5aa993b 867
8defab1a
DJ
868 /* Offset within shadow_contents. */
869 if (bp_addr < memaddr)
870 {
871 /* Only copy the second part of the breakpoint. */
872 bp_size -= memaddr - bp_addr;
873 bptoffset = memaddr - bp_addr;
874 bp_addr = memaddr;
875 }
c5aa993b 876
8defab1a
DJ
877 if (bp_addr + bp_size > memaddr + len)
878 {
879 /* Only copy the first part of the breakpoint. */
880 bp_size -= (bp_addr + bp_size) - (memaddr + len);
881 }
c5aa993b 882
8defab1a
DJ
883 memcpy (buf + bp_addr - memaddr,
884 b->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 885 }
c906108c 886}
c906108c 887\f
c5aa993b 888
687595f9 889/* A wrapper function for inserting catchpoints. */
9cbc821d 890static void
687595f9
DJ
891insert_catchpoint (struct ui_out *uo, void *args)
892{
893 struct breakpoint *b = (struct breakpoint *) args;
894 int val = -1;
895
fe798b75
JB
896 gdb_assert (b->type == bp_catchpoint);
897 gdb_assert (b->ops != NULL && b->ops->insert != NULL);
898
899 b->ops->insert (b);
687595f9
DJ
900}
901
a5606eee
VP
902static int
903is_hardware_watchpoint (struct breakpoint *bpt)
904{
905 return (bpt->type == bp_hardware_watchpoint
906 || bpt->type == bp_read_watchpoint
907 || bpt->type == bp_access_watchpoint);
908}
7270d8f2 909
fa4727a6
DJ
910/* Find the current value of a watchpoint on EXP. Return the value in
911 *VALP and *RESULTP and the chain of intermediate and final values
912 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
913 not need them.
914
ccc57cf9 915 If a memory error occurs while evaluating the expression, *RESULTP will
fa4727a6
DJ
916 be set to NULL. *RESULTP may be a lazy value, if the result could
917 not be read from memory. It is used to determine whether a value
918 is user-specified (we should watch the whole value) or intermediate
919 (we should watch only the bit used to locate the final value).
920
921 If the final value, or any intermediate value, could not be read
922 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
923 set to any referenced values. *VALP will never be a lazy value.
924 This is the value which we store in struct breakpoint.
925
926 If VAL_CHAIN is non-NULL, *VAL_CHAIN will be released from the
927 value chain. The caller must free the values individually. If
928 VAL_CHAIN is NULL, all generated values will be left on the value
929 chain. */
930
931static void
932fetch_watchpoint_value (struct expression *exp, struct value **valp,
933 struct value **resultp, struct value **val_chain)
934{
935 struct value *mark, *new_mark, *result;
ccc57cf9 936 volatile struct gdb_exception ex;
fa4727a6
DJ
937
938 *valp = NULL;
939 if (resultp)
940 *resultp = NULL;
941 if (val_chain)
942 *val_chain = NULL;
943
944 /* Evaluate the expression. */
945 mark = value_mark ();
946 result = NULL;
ccc57cf9
PA
947
948 TRY_CATCH (ex, RETURN_MASK_ALL)
949 {
950 result = evaluate_expression (exp);
951 }
952 if (ex.reason < 0)
953 {
954 /* Ignore memory errors, we want watchpoints pointing at
955 inaccessible memory to still be created; otherwise, throw the
956 error to some higher catcher. */
957 switch (ex.error)
958 {
959 case MEMORY_ERROR:
960 break;
961 default:
962 throw_exception (ex);
963 break;
964 }
965 }
966
fa4727a6
DJ
967 new_mark = value_mark ();
968 if (mark == new_mark)
969 return;
970 if (resultp)
971 *resultp = result;
972
973 /* Make sure it's not lazy, so that after the target stops again we
974 have a non-lazy previous value to compare with. */
975 if (result != NULL
976 && (!value_lazy (result) || gdb_value_fetch_lazy (result)))
977 *valp = result;
978
979 if (val_chain)
980 {
981 /* Return the chain of intermediate values. We use this to
982 decide which addresses to watch. */
983 *val_chain = new_mark;
984 value_release_to_mark (mark);
985 }
986}
987
f6bc2008
PA
988/* Assuming that B is a watchpoint: returns true if the current thread
989 and its running state are safe to evaluate or update watchpoint B.
990 Watchpoints on local expressions need to be evaluated in the
991 context of the thread that was current when the watchpoint was
992 created, and, that thread needs to be stopped to be able to select
993 the correct frame context. Watchpoints on global expressions can
994 be evaluated on any thread, and in any state. It is presently left
995 to the target allowing memory accesses when threads are
996 running. */
997
998static int
999watchpoint_in_thread_scope (struct breakpoint *b)
1000{
1001 return (ptid_equal (b->watchpoint_thread, null_ptid)
1002 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1003 && !is_executing (inferior_ptid)));
1004}
1005
567e1b4e
JB
1006/* Assuming that B is a watchpoint:
1007 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1008 - Evaluate expression and store the result in B->val
567e1b4e
JB
1009 - Evaluate the condition if there is one, and store the result
1010 in b->loc->cond.
a5606eee
VP
1011 - Update the list of values that must be watched in B->loc.
1012
bfa149ac 1013 If the watchpoint disposition is disp_del_at_next_stop, then do nothing.
a79d3c27
JK
1014 If this is local watchpoint that is out of scope, delete it.
1015
1016 Even with `set breakpoint always-inserted on' the watchpoints are removed
1017 + inserted on each stop here. Normal breakpoints must never be removed
1018 because they might be missed by a running thread when debugging in non-stop
1019 mode. On the other hand, hardware watchpoints (is_hardware_watchpoint;
1020 processed here) are specific to each LWP since they are stored in each LWP's
1021 hardware debug registers. Therefore, such LWP must be stopped first in
1022 order to be able to modify its hardware watchpoints.
1023
1024 Hardware watchpoints must be reset exactly once after being presented to the
1025 user. It cannot be done sooner, because it would reset the data used to
1026 present the watchpoint hit to the user. And it must not be done later
1027 because it could display the same single watchpoint hit during multiple GDB
1028 stops. Note that the latter is relevant only to the hardware watchpoint
1029 types bp_read_watchpoint and bp_access_watchpoint. False hit by
1030 bp_hardware_watchpoint is not user-visible - its hit is suppressed if the
1031 memory content has not changed.
1032
1033 The following constraints influence the location where we can reset hardware
1034 watchpoints:
1035
1036 * target_stopped_by_watchpoint and target_stopped_data_address are called
1037 several times when GDB stops.
1038
1039 [linux]
1040 * Multiple hardware watchpoints can be hit at the same time, causing GDB to
1041 stop. GDB only presents one hardware watchpoint hit at a time as the
1042 reason for stopping, and all the other hits are presented later, one after
1043 the other, each time the user requests the execution to be resumed.
1044 Execution is not resumed for the threads still having pending hit event
1045 stored in LWP_INFO->STATUS. While the watchpoint is already removed from
1046 the inferior on the first stop the thread hit event is kept being reported
1047 from its cached value by linux_nat_stopped_data_address until the real
1048 thread resume happens after the watchpoint gets presented and thus its
1049 LWP_INFO->STATUS gets reset.
1050
1051 Therefore the hardware watchpoint hit can get safely reset on the watchpoint
1052 removal from inferior. */
1053
b40ce68a 1054static void
a5606eee 1055update_watchpoint (struct breakpoint *b, int reparse)
7270d8f2 1056{
a5606eee 1057 int within_current_scope;
a5606eee
VP
1058 struct frame_id saved_frame_id;
1059 struct bp_location *loc;
66076460 1060 int frame_saved;
a5606eee
VP
1061 bpstat bs;
1062
f6bc2008
PA
1063 /* If this is a local watchpoint, we only want to check if the
1064 watchpoint frame is in scope if the current thread is the thread
1065 that was used to create the watchpoint. */
1066 if (!watchpoint_in_thread_scope (b))
1067 return;
1068
876fa593 1069 /* We don't free locations. They are stored in bp_location array and
567e1b4e
JB
1070 update_global_locations will eventually delete them and remove
1071 breakpoints if needed. */
a5606eee
VP
1072 b->loc = NULL;
1073
1074 if (b->disposition == disp_del_at_next_stop)
1075 return;
1076
66076460 1077 frame_saved = 0;
a5606eee
VP
1078
1079 /* Determine if the watchpoint is within scope. */
1080 if (b->exp_valid_block == NULL)
1081 within_current_scope = 1;
1082 else
1083 {
1084 struct frame_info *fi;
66076460
DJ
1085
1086 /* Save the current frame's ID so we can restore it after
1087 evaluating the watchpoint expression on its own frame. */
1088 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1089 took a frame parameter, so that we didn't have to change the
1090 selected frame. */
1091 frame_saved = 1;
1092 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1093
a5606eee
VP
1094 fi = frame_find_by_id (b->watchpoint_frame);
1095 within_current_scope = (fi != NULL);
1096 if (within_current_scope)
1097 select_frame (fi);
1098 }
1099
1100 if (within_current_scope && reparse)
1101 {
1102 char *s;
1103 if (b->exp)
1104 {
1105 xfree (b->exp);
1106 b->exp = NULL;
1107 }
1108 s = b->exp_string;
1109 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1110 /* If the meaning of expression itself changed, the old value is
1111 no longer relevant. We don't want to report a watchpoint hit
1112 to the user when the old value and the new value may actually
1113 be completely different objects. */
1114 value_free (b->val);
fa4727a6
DJ
1115 b->val = NULL;
1116 b->val_valid = 0;
a5606eee 1117 }
a5606eee
VP
1118
1119 /* If we failed to parse the expression, for example because
1120 it refers to a global variable in a not-yet-loaded shared library,
1121 don't try to insert watchpoint. We don't automatically delete
1122 such watchpoint, though, since failure to parse expression
1123 is different from out-of-scope watchpoint. */
2d134ed3
PA
1124 if ( !target_has_execution)
1125 {
1126 /* Without execution, memory can't change. No use to try and
1127 set watchpoint locations. The watchpoint will be reset when
1128 the target gains execution, through breakpoint_re_set. */
1129 }
1130 else if (within_current_scope && b->exp)
a5606eee 1131 {
fa4727a6 1132 struct value *val_chain, *v, *result, *next;
2d134ed3 1133 struct program_space *frame_pspace;
a5606eee 1134
fa4727a6 1135 fetch_watchpoint_value (b->exp, &v, &result, &val_chain);
a5606eee 1136
a5606eee
VP
1137 /* Avoid setting b->val if it's already set. The meaning of
1138 b->val is 'the last value' user saw, and we should update
1139 it only if we reported that last value to user. As it
1140 happens, the code that reports it updates b->val directly. */
fa4727a6
DJ
1141 if (!b->val_valid)
1142 {
1143 b->val = v;
1144 b->val_valid = 1;
1145 }
a5606eee 1146
db2ad4c3
JK
1147 /* Change the type of breakpoint between hardware assisted or an
1148 ordinary watchpoint depending on the hardware support and free
1149 hardware slots. REPARSE is set when the inferior is started. */
1150 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1151 && reparse)
1152 {
ca2d49e8 1153 int i, mem_cnt, other_type_used;
db2ad4c3
JK
1154
1155 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
1156 &other_type_used);
1157 mem_cnt = can_use_hardware_watchpoint (val_chain);
1158
ca2d49e8 1159 if (!mem_cnt)
db2ad4c3
JK
1160 b->type = bp_watchpoint;
1161 else
ca2d49e8 1162 {
d92524f1 1163 int target_resources_ok = target_can_use_hardware_watchpoint
ca2d49e8
SL
1164 (bp_hardware_watchpoint, i + mem_cnt, other_type_used);
1165 if (target_resources_ok <= 0)
1166 b->type = bp_watchpoint;
1167 else
1168 b->type = bp_hardware_watchpoint;
1169 }
db2ad4c3
JK
1170 }
1171
2d134ed3
PA
1172 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1173
a5606eee 1174 /* Look at each value on the value chain. */
fa4727a6 1175 for (v = val_chain; v; v = next)
a5606eee
VP
1176 {
1177 /* If it's a memory location, and GDB actually needed
1178 its contents to evaluate the expression, then we
fa4727a6
DJ
1179 must watch it. If the first value returned is
1180 still lazy, that means an error occurred reading it;
1181 watch it anyway in case it becomes readable. */
a5606eee 1182 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1183 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1184 {
1185 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1186
a5606eee
VP
1187 /* We only watch structs and arrays if user asked
1188 for it explicitly, never if they just happen to
1189 appear in the middle of some value chain. */
fa4727a6 1190 if (v == result
a5606eee
VP
1191 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1192 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1193 {
1194 CORE_ADDR addr;
1195 int len, type;
1196 struct bp_location *loc, **tmp;
1197
42ae5230 1198 addr = value_address (v);
a5606eee
VP
1199 len = TYPE_LENGTH (value_type (v));
1200 type = hw_write;
1201 if (b->type == bp_read_watchpoint)
1202 type = hw_read;
1203 else if (b->type == bp_access_watchpoint)
1204 type = hw_access;
1205
39d61571 1206 loc = allocate_bp_location (b);
a5606eee
VP
1207 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1208 ;
1209 *tmp = loc;
a6d9a66e 1210 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1211
1212 loc->pspace = frame_pspace;
a5606eee
VP
1213 loc->address = addr;
1214 loc->length = len;
1215 loc->watchpoint_type = type;
1216 }
1217 }
1218
1219 next = value_next (v);
1220 if (v != b->val)
1221 value_free (v);
1222 }
1223
2ec93238
MK
1224 /* We just regenerated the list of breakpoint locations.
1225 The new location does not have its condition field set to anything
1226 and therefore, we must always reparse the cond_string, independently
1227 of the value of the reparse flag. */
1228 if (b->cond_string != NULL)
a5606eee
VP
1229 {
1230 char *s = b->cond_string;
a5606eee
VP
1231 b->loc->cond = parse_exp_1 (&s, b->exp_valid_block, 0);
1232 }
1233 }
1234 else if (!within_current_scope)
7270d8f2 1235 {
a5606eee 1236 printf_filtered (_("\
567e1b4e 1237Watchpoint %d deleted because the program has left the block \n\
a5606eee
VP
1238in which its expression is valid.\n"),
1239 b->number);
1240 if (b->related_breakpoint)
1241 b->related_breakpoint->disposition = disp_del_at_next_stop;
1242 b->disposition = disp_del_at_next_stop;
7270d8f2 1243 }
a5606eee
VP
1244
1245 /* Restore the selected frame. */
66076460
DJ
1246 if (frame_saved)
1247 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1248}
1249
a5606eee 1250
74960c60
VP
1251/* Returns 1 iff breakpoint location should be
1252 inserted in the inferior. */
1253static int
1254should_be_inserted (struct bp_location *bpt)
1255{
1256 if (!breakpoint_enabled (bpt->owner))
1257 return 0;
1258
1259 if (bpt->owner->disposition == disp_del_at_next_stop)
1260 return 0;
1261
1262 if (!bpt->enabled || bpt->shlib_disabled || bpt->duplicate)
1263 return 0;
1264
56710373
PA
1265 /* This is set for example, when we're attached to the parent of a
1266 vfork, and have detached from the child. The child is running
1267 free, and we expect it to do an exec or exit, at which point the
1268 OS makes the parent schedulable again (and the target reports
1269 that the vfork is done). Until the child is done with the shared
1270 memory region, do not insert breakpoints in the parent, otherwise
1271 the child could still trip on the parent's breakpoints. Since
1272 the parent is blocked anyway, it won't miss any breakpoint. */
1273 if (bpt->pspace->breakpoints_not_allowed)
1274 return 0;
1275
1042e4c0
SS
1276 /* Tracepoints are inserted by the target at a time of its choosing,
1277 not by us. */
1278 if (bpt->owner->type == bp_tracepoint)
1279 return 0;
1280
74960c60
VP
1281 return 1;
1282}
1283
879bfdc2
DJ
1284/* Insert a low-level "breakpoint" of some type. BPT is the breakpoint.
1285 Any error messages are printed to TMP_ERROR_STREAM; and DISABLED_BREAKS,
fa3a767f 1286 and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2
DJ
1287
1288 NOTE drow/2003-09-09: This routine could be broken down to an object-style
1289 method for each breakpoint or catchpoint type. */
26bb91f3 1290static int
879bfdc2 1291insert_bp_location (struct bp_location *bpt,
26bb91f3 1292 struct ui_file *tmp_error_stream,
fa3a767f 1293 int *disabled_breaks,
26bb91f3 1294 int *hw_breakpoint_error)
879bfdc2
DJ
1295{
1296 int val = 0;
1297
74960c60 1298 if (!should_be_inserted (bpt) || bpt->inserted)
879bfdc2
DJ
1299 return 0;
1300
8181d85f
DJ
1301 /* Initialize the target-specific information. */
1302 memset (&bpt->target_info, 0, sizeof (bpt->target_info));
1303 bpt->target_info.placed_address = bpt->address;
6c95b8df 1304 bpt->target_info.placed_address_space = bpt->pspace->aspace;
8181d85f 1305
879bfdc2
DJ
1306 if (bpt->loc_type == bp_loc_software_breakpoint
1307 || bpt->loc_type == bp_loc_hardware_breakpoint)
1308 {
765dc015
VP
1309 if (bpt->owner->type != bp_hardware_breakpoint)
1310 {
1311 /* If the explicitly specified breakpoint type
1312 is not hardware breakpoint, check the memory map to see
1313 if the breakpoint address is in read only memory or not.
1314 Two important cases are:
1315 - location type is not hardware breakpoint, memory
1316 is readonly. We change the type of the location to
1317 hardware breakpoint.
1318 - location type is hardware breakpoint, memory is read-write.
1319 This means we've previously made the location hardware one, but
1320 then the memory map changed, so we undo.
1321
1322 When breakpoints are removed, remove_breakpoints will
1323 use location types we've just set here, the only possible
1324 problem is that memory map has changed during running program,
1325 but it's not going to work anyway with current gdb. */
1326 struct mem_region *mr
1327 = lookup_mem_region (bpt->target_info.placed_address);
1328
1329 if (mr)
1330 {
1331 if (automatic_hardware_breakpoints)
1332 {
1333 int changed = 0;
1334 enum bp_loc_type new_type;
1335
1336 if (mr->attrib.mode != MEM_RW)
1337 new_type = bp_loc_hardware_breakpoint;
1338 else
1339 new_type = bp_loc_software_breakpoint;
1340
1341 if (new_type != bpt->loc_type)
1342 {
1343 static int said = 0;
1344 bpt->loc_type = new_type;
1345 if (!said)
1346 {
1347 fprintf_filtered (gdb_stdout, _("\
0767c96d 1348Note: automatically using hardware breakpoints for read-only addresses.\n"));
765dc015
VP
1349 said = 1;
1350 }
1351 }
1352 }
1353 else if (bpt->loc_type == bp_loc_software_breakpoint
1354 && mr->attrib.mode != MEM_RW)
1355 warning (_("cannot set software breakpoint at readonly address %s"),
5af949e3 1356 paddress (bpt->gdbarch, bpt->address));
765dc015
VP
1357 }
1358 }
1359
879bfdc2
DJ
1360 /* First check to see if we have to handle an overlay. */
1361 if (overlay_debugging == ovly_off
1362 || bpt->section == NULL
1363 || !(section_is_overlay (bpt->section)))
1364 {
1365 /* No overlay handling: just set the breakpoint. */
1366
1367 if (bpt->loc_type == bp_loc_hardware_breakpoint)
a6d9a66e
UW
1368 val = target_insert_hw_breakpoint (bpt->gdbarch,
1369 &bpt->target_info);
879bfdc2 1370 else
a6d9a66e
UW
1371 val = target_insert_breakpoint (bpt->gdbarch,
1372 &bpt->target_info);
879bfdc2
DJ
1373 }
1374 else
1375 {
1376 /* This breakpoint is in an overlay section.
1377 Shall we set a breakpoint at the LMA? */
1378 if (!overlay_events_enabled)
1379 {
1380 /* Yes -- overlay event support is not active,
1381 so we must try to set a breakpoint at the LMA.
1382 This will not work for a hardware breakpoint. */
1383 if (bpt->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1384 warning (_("hardware breakpoint %d not supported in overlay!"),
879bfdc2
DJ
1385 bpt->owner->number);
1386 else
1387 {
1388 CORE_ADDR addr = overlay_unmapped_address (bpt->address,
1389 bpt->section);
1390 /* Set a software (trap) breakpoint at the LMA. */
8181d85f
DJ
1391 bpt->overlay_target_info = bpt->target_info;
1392 bpt->overlay_target_info.placed_address = addr;
a6d9a66e
UW
1393 val = target_insert_breakpoint (bpt->gdbarch,
1394 &bpt->overlay_target_info);
879bfdc2 1395 if (val != 0)
99361f52
DE
1396 fprintf_unfiltered (tmp_error_stream,
1397 "Overlay breakpoint %d failed: in ROM?\n",
879bfdc2
DJ
1398 bpt->owner->number);
1399 }
1400 }
1401 /* Shall we set a breakpoint at the VMA? */
1402 if (section_is_mapped (bpt->section))
1403 {
1404 /* Yes. This overlay section is mapped into memory. */
1405 if (bpt->loc_type == bp_loc_hardware_breakpoint)
a6d9a66e
UW
1406 val = target_insert_hw_breakpoint (bpt->gdbarch,
1407 &bpt->target_info);
879bfdc2 1408 else
a6d9a66e
UW
1409 val = target_insert_breakpoint (bpt->gdbarch,
1410 &bpt->target_info);
879bfdc2
DJ
1411 }
1412 else
1413 {
1414 /* No. This breakpoint will not be inserted.
1415 No error, but do not mark the bp as 'inserted'. */
1416 return 0;
1417 }
1418 }
1419
1420 if (val)
1421 {
1422 /* Can't set the breakpoint. */
6c95b8df 1423 if (solib_name_from_address (bpt->pspace, bpt->address))
879bfdc2
DJ
1424 {
1425 /* See also: disable_breakpoints_in_shlibs. */
1426 val = 0;
0d381245 1427 bpt->shlib_disabled = 1;
879bfdc2
DJ
1428 if (!*disabled_breaks)
1429 {
1430 fprintf_unfiltered (tmp_error_stream,
1431 "Cannot insert breakpoint %d.\n",
1432 bpt->owner->number);
1433 fprintf_unfiltered (tmp_error_stream,
1434 "Temporarily disabling shared library breakpoints:\n");
1435 }
1436 *disabled_breaks = 1;
1437 fprintf_unfiltered (tmp_error_stream,
1438 "breakpoint #%d\n", bpt->owner->number);
1439 }
1440 else
879bfdc2 1441 {
879bfdc2
DJ
1442 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1443 {
1444 *hw_breakpoint_error = 1;
1445 fprintf_unfiltered (tmp_error_stream,
1446 "Cannot insert hardware breakpoint %d.\n",
1447 bpt->owner->number);
1448 }
1449 else
1450 {
1451 fprintf_unfiltered (tmp_error_stream,
1452 "Cannot insert breakpoint %d.\n",
1453 bpt->owner->number);
1454 fprintf_filtered (tmp_error_stream,
1455 "Error accessing memory address ");
5af949e3
UW
1456 fputs_filtered (paddress (bpt->gdbarch, bpt->address),
1457 tmp_error_stream);
879bfdc2
DJ
1458 fprintf_filtered (tmp_error_stream, ": %s.\n",
1459 safe_strerror (val));
1460 }
1461
1462 }
1463 }
1464 else
1465 bpt->inserted = 1;
1466
1467 return val;
1468 }
1469
1470 else if (bpt->loc_type == bp_loc_hardware_watchpoint
1471 /* NOTE drow/2003-09-08: This state only exists for removing
1472 watchpoints. It's not clear that it's necessary... */
1473 && bpt->owner->disposition != disp_del_at_next_stop)
1474 {
a5606eee
VP
1475 val = target_insert_watchpoint (bpt->address,
1476 bpt->length,
1477 bpt->watchpoint_type);
1478 bpt->inserted = (val != -1);
879bfdc2
DJ
1479 }
1480
fe798b75 1481 else if (bpt->owner->type == bp_catchpoint)
879bfdc2 1482 {
71fff37b
AC
1483 struct gdb_exception e = catch_exception (uiout, insert_catchpoint,
1484 bpt->owner, RETURN_MASK_ERROR);
9cbc821d
AC
1485 exception_fprintf (gdb_stderr, e, "warning: inserting catchpoint %d: ",
1486 bpt->owner->number);
1487 if (e.reason < 0)
879bfdc2
DJ
1488 bpt->owner->enable_state = bp_disabled;
1489 else
1490 bpt->inserted = 1;
1640b821
DJ
1491
1492 /* We've already printed an error message if there was a problem
1493 inserting this catchpoint, and we've disabled the catchpoint,
1494 so just return success. */
1495 return 0;
879bfdc2
DJ
1496 }
1497
1498 return 0;
1499}
1500
6c95b8df
PA
1501/* This function is called when program space PSPACE is about to be
1502 deleted. It takes care of updating breakpoints to not reference
1503 PSPACE anymore. */
1504
1505void
1506breakpoint_program_space_exit (struct program_space *pspace)
1507{
1508 struct breakpoint *b, *b_temp;
876fa593 1509 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1510
1511 /* Remove any breakpoint that was set through this program space. */
1512 ALL_BREAKPOINTS_SAFE (b, b_temp)
1513 {
1514 if (b->pspace == pspace)
1515 delete_breakpoint (b);
1516 }
1517
1518 /* Breakpoints set through other program spaces could have locations
1519 bound to PSPACE as well. Remove those. */
876fa593 1520 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1521 {
1522 struct bp_location *tmp;
1523
1524 if (loc->pspace == pspace)
1525 {
1526 if (loc->owner->loc == loc)
1527 loc->owner->loc = loc->next;
1528 else
1529 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1530 if (tmp->next == loc)
1531 {
1532 tmp->next = loc->next;
1533 break;
1534 }
1535 }
1536 }
1537
1538 /* Now update the global location list to permanently delete the
1539 removed locations above. */
1540 update_global_location_list (0);
1541}
1542
74960c60
VP
1543/* Make sure all breakpoints are inserted in inferior.
1544 Throws exception on any error.
1545 A breakpoint that is already inserted won't be inserted
1546 again, so calling this function twice is safe. */
1547void
1548insert_breakpoints (void)
1549{
1550 struct breakpoint *bpt;
1551
1552 ALL_BREAKPOINTS (bpt)
1553 if (is_hardware_watchpoint (bpt))
1554 update_watchpoint (bpt, 0 /* don't reparse. */);
1555
b60e7edf 1556 update_global_location_list (1);
74960c60 1557
c35b1492
PA
1558 /* update_global_location_list does not insert breakpoints when
1559 always_inserted_mode is not enabled. Explicitly insert them
1560 now. */
1561 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1562 insert_breakpoint_locations ();
1563}
1564
c906108c
SS
1565/* insert_breakpoints is used when starting or continuing the program.
1566 remove_breakpoints is used when the program stops.
1567 Both return zero if successful,
1568 or an `errno' value if could not write the inferior. */
1569
74960c60
VP
1570static void
1571insert_breakpoint_locations (void)
c906108c 1572{
a5606eee 1573 struct breakpoint *bpt;
876fa593 1574 struct bp_location *b, **bp_tmp;
e236ba44 1575 int error = 0;
c906108c
SS
1576 int val = 0;
1577 int disabled_breaks = 0;
81d0cc19 1578 int hw_breakpoint_error = 0;
c906108c 1579
81d0cc19 1580 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1581 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1582
81d0cc19
GS
1583 /* Explicitly mark the warning -- this will only be printed if
1584 there was an error. */
1585 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1586
1587 save_current_space_and_thread ();
1588
876fa593 1589 ALL_BP_LOCATIONS (b, bp_tmp)
879bfdc2 1590 {
6c95b8df
PA
1591 struct thread_info *tp;
1592 CORE_ADDR last_addr;
1593
74960c60 1594 if (!should_be_inserted (b) || b->inserted)
879bfdc2
DJ
1595 continue;
1596
f365de73
AS
1597 /* There is no point inserting thread-specific breakpoints if the
1598 thread no longer exists. */
1599 if (b->owner->thread != -1
1600 && !valid_thread_id (b->owner->thread))
1601 continue;
1602
6c95b8df
PA
1603 switch_to_program_space_and_thread (b->pspace);
1604
1605 /* For targets that support global breakpoints, there's no need
1606 to select an inferior to insert breakpoint to. In fact, even
1607 if we aren't attached to any process yet, we should still
1608 insert breakpoints. */
1609 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1610 && ptid_equal (inferior_ptid, null_ptid))
1611 continue;
1612
879bfdc2 1613 val = insert_bp_location (b, tmp_error_stream,
fa3a767f 1614 &disabled_breaks,
879bfdc2
DJ
1615 &hw_breakpoint_error);
1616 if (val)
e236ba44 1617 error = val;
879bfdc2 1618 }
c906108c 1619
a5606eee
VP
1620 /* If we failed to insert all locations of a watchpoint,
1621 remove them, as half-inserted watchpoint is of limited use. */
1622 ALL_BREAKPOINTS (bpt)
1623 {
1624 int some_failed = 0;
1625 struct bp_location *loc;
1626
1627 if (!is_hardware_watchpoint (bpt))
1628 continue;
1629
d6b74ac4 1630 if (!breakpoint_enabled (bpt))
a5606eee 1631 continue;
74960c60
VP
1632
1633 if (bpt->disposition == disp_del_at_next_stop)
1634 continue;
a5606eee
VP
1635
1636 for (loc = bpt->loc; loc; loc = loc->next)
56710373 1637 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
1638 {
1639 some_failed = 1;
1640 break;
1641 }
1642 if (some_failed)
1643 {
1644 for (loc = bpt->loc; loc; loc = loc->next)
1645 if (loc->inserted)
1646 remove_breakpoint (loc, mark_uninserted);
1647
1648 hw_breakpoint_error = 1;
1649 fprintf_unfiltered (tmp_error_stream,
1650 "Could not insert hardware watchpoint %d.\n",
1651 bpt->number);
1652 error = -1;
1653 }
1654 }
1655
e236ba44 1656 if (error)
81d0cc19
GS
1657 {
1658 /* If a hardware breakpoint or watchpoint was inserted, add a
1659 message about possibly exhausted resources. */
879bfdc2 1660 if (hw_breakpoint_error)
81d0cc19 1661 {
c6510018
MS
1662 fprintf_unfiltered (tmp_error_stream,
1663 "Could not insert hardware breakpoints:\n\
1664You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 1665 }
81d0cc19
GS
1666 target_terminal_ours_for_output ();
1667 error_stream (tmp_error_stream);
1668 }
f7545552
TT
1669
1670 do_cleanups (cleanups);
c906108c
SS
1671}
1672
c906108c 1673int
fba45db2 1674remove_breakpoints (void)
c906108c 1675{
876fa593 1676 struct bp_location *b, **bp_tmp;
3a1bae8e 1677 int val = 0;
c906108c 1678
876fa593 1679 ALL_BP_LOCATIONS (b, bp_tmp)
c5aa993b 1680 {
0bde7532 1681 if (b->inserted)
3a1bae8e 1682 val |= remove_breakpoint (b, mark_uninserted);
c5aa993b 1683 }
3a1bae8e 1684 return val;
c906108c
SS
1685}
1686
6c95b8df
PA
1687/* Remove breakpoints of process PID. */
1688
1689int
1690remove_breakpoints_pid (int pid)
1691{
876fa593 1692 struct bp_location *b, **b_tmp;
6c95b8df
PA
1693 int val;
1694 struct inferior *inf = find_inferior_pid (pid);
1695
876fa593 1696 ALL_BP_LOCATIONS (b, b_tmp)
6c95b8df
PA
1697 {
1698 if (b->pspace != inf->pspace)
1699 continue;
1700
1701 if (b->inserted)
1702 {
1703 val = remove_breakpoint (b, mark_uninserted);
1704 if (val != 0)
1705 return val;
1706 }
1707 }
1708 return 0;
1709}
1710
692590c1 1711int
80ce1ecb 1712remove_hw_watchpoints (void)
692590c1 1713{
876fa593 1714 struct bp_location *b, **bp_tmp;
3a1bae8e 1715 int val = 0;
692590c1 1716
876fa593 1717 ALL_BP_LOCATIONS (b, bp_tmp)
692590c1 1718 {
0bde7532 1719 if (b->inserted && b->loc_type == bp_loc_hardware_watchpoint)
3a1bae8e 1720 val |= remove_breakpoint (b, mark_uninserted);
692590c1 1721 }
3a1bae8e 1722 return val;
692590c1
MS
1723}
1724
c906108c 1725int
fba45db2 1726reattach_breakpoints (int pid)
c906108c 1727{
6c95b8df 1728 struct cleanup *old_chain;
876fa593 1729 struct bp_location *b, **bp_tmp;
c906108c 1730 int val;
a4954f26 1731 struct ui_file *tmp_error_stream = mem_fileopen ();
fa3a767f 1732 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
1733 struct inferior *inf;
1734 struct thread_info *tp;
1735
1736 tp = any_live_thread_of_process (pid);
1737 if (tp == NULL)
1738 return 1;
1739
1740 inf = find_inferior_pid (pid);
1741 old_chain = save_inferior_ptid ();
1742
1743 inferior_ptid = tp->ptid;
a4954f26
DJ
1744
1745 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 1746
876fa593 1747 ALL_BP_LOCATIONS (b, bp_tmp)
c5aa993b 1748 {
6c95b8df
PA
1749 if (b->pspace != inf->pspace)
1750 continue;
1751
0bde7532 1752 if (b->inserted)
c5aa993b 1753 {
a4954f26
DJ
1754 b->inserted = 0;
1755 val = insert_bp_location (b, tmp_error_stream,
fa3a767f 1756 &dummy1, &dummy2);
c5aa993b
JM
1757 if (val != 0)
1758 {
ce696e05 1759 do_cleanups (old_chain);
c5aa993b
JM
1760 return val;
1761 }
1762 }
1763 }
ce696e05 1764 do_cleanups (old_chain);
c906108c
SS
1765 return 0;
1766}
1767
e58b0e63
PA
1768static int internal_breakpoint_number = -1;
1769
e62c965a 1770static struct breakpoint *
a6d9a66e
UW
1771create_internal_breakpoint (struct gdbarch *gdbarch,
1772 CORE_ADDR address, enum bptype type)
e62c965a 1773{
e62c965a
PP
1774 struct symtab_and_line sal;
1775 struct breakpoint *b;
1776
1777 init_sal (&sal); /* initialize to zeroes */
1778
1779 sal.pc = address;
1780 sal.section = find_pc_overlay (sal.pc);
6c95b8df 1781 sal.pspace = current_program_space;
e62c965a 1782
a6d9a66e 1783 b = set_raw_breakpoint (gdbarch, sal, type);
e62c965a
PP
1784 b->number = internal_breakpoint_number--;
1785 b->disposition = disp_donttouch;
1786
1787 return b;
1788}
1789
1790static void
69de3c6a 1791create_overlay_event_breakpoint (char *func_name)
e62c965a 1792{
69de3c6a 1793 struct objfile *objfile;
e62c965a 1794
69de3c6a
PP
1795 ALL_OBJFILES (objfile)
1796 {
1797 struct breakpoint *b;
1798 struct minimal_symbol *m;
1799
1800 m = lookup_minimal_symbol_text (func_name, objfile);
1801 if (m == NULL)
1802 continue;
e62c965a 1803
a6d9a66e
UW
1804 b = create_internal_breakpoint (get_objfile_arch (objfile),
1805 SYMBOL_VALUE_ADDRESS (m),
69de3c6a
PP
1806 bp_overlay_event);
1807 b->addr_string = xstrdup (func_name);
e62c965a 1808
69de3c6a
PP
1809 if (overlay_debugging == ovly_auto)
1810 {
1811 b->enable_state = bp_enabled;
1812 overlay_events_enabled = 1;
1813 }
1814 else
1815 {
1816 b->enable_state = bp_disabled;
1817 overlay_events_enabled = 0;
1818 }
e62c965a
PP
1819 }
1820 update_global_location_list (1);
1821}
1822
0fd8e87f
UW
1823static void
1824create_longjmp_master_breakpoint (char *func_name)
1825{
6c95b8df 1826 struct program_space *pspace;
0fd8e87f 1827 struct objfile *objfile;
6c95b8df
PA
1828 struct cleanup *old_chain;
1829
1830 old_chain = save_current_program_space ();
0fd8e87f 1831
6c95b8df 1832 ALL_PSPACES (pspace)
0fd8e87f
UW
1833 ALL_OBJFILES (objfile)
1834 {
1835 struct breakpoint *b;
1836 struct minimal_symbol *m;
1837
1838 if (!gdbarch_get_longjmp_target_p (get_objfile_arch (objfile)))
1839 continue;
1840
6c95b8df
PA
1841 set_current_program_space (pspace);
1842
0fd8e87f
UW
1843 m = lookup_minimal_symbol_text (func_name, objfile);
1844 if (m == NULL)
1845 continue;
1846
a6d9a66e
UW
1847 b = create_internal_breakpoint (get_objfile_arch (objfile),
1848 SYMBOL_VALUE_ADDRESS (m),
0fd8e87f
UW
1849 bp_longjmp_master);
1850 b->addr_string = xstrdup (func_name);
1851 b->enable_state = bp_disabled;
1852 }
1853 update_global_location_list (1);
6c95b8df
PA
1854
1855 do_cleanups (old_chain);
0fd8e87f
UW
1856}
1857
c906108c 1858void
fba45db2 1859update_breakpoints_after_exec (void)
c906108c 1860{
c5aa993b
JM
1861 struct breakpoint *b;
1862 struct breakpoint *temp;
876fa593 1863 struct bp_location *bploc, **bplocp_tmp;
c906108c 1864
25b22b0a
PA
1865 /* We're about to delete breakpoints from GDB's lists. If the
1866 INSERTED flag is true, GDB will try to lift the breakpoints by
1867 writing the breakpoints' "shadow contents" back into memory. The
1868 "shadow contents" are NOT valid after an exec, so GDB should not
1869 do that. Instead, the target is responsible from marking
1870 breakpoints out as soon as it detects an exec. We don't do that
1871 here instead, because there may be other attempts to delete
1872 breakpoints after detecting an exec and before reaching here. */
876fa593 1873 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
1874 if (bploc->pspace == current_program_space)
1875 gdb_assert (!bploc->inserted);
c906108c
SS
1876
1877 ALL_BREAKPOINTS_SAFE (b, temp)
c5aa993b 1878 {
6c95b8df
PA
1879 if (b->pspace != current_program_space)
1880 continue;
1881
c5aa993b
JM
1882 /* Solib breakpoints must be explicitly reset after an exec(). */
1883 if (b->type == bp_shlib_event)
1884 {
1885 delete_breakpoint (b);
1886 continue;
1887 }
c906108c 1888
4efc6507
DE
1889 /* JIT breakpoints must be explicitly reset after an exec(). */
1890 if (b->type == bp_jit_event)
1891 {
1892 delete_breakpoint (b);
1893 continue;
1894 }
1895
1900040c 1896 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
1897 as must overlay event and longjmp master breakpoints. */
1898 if (b->type == bp_thread_event || b->type == bp_overlay_event
1899 || b->type == bp_longjmp_master)
c4093a6a
JM
1900 {
1901 delete_breakpoint (b);
1902 continue;
1903 }
1904
c5aa993b
JM
1905 /* Step-resume breakpoints are meaningless after an exec(). */
1906 if (b->type == bp_step_resume)
1907 {
1908 delete_breakpoint (b);
1909 continue;
1910 }
1911
611c83ae
PA
1912 /* Longjmp and longjmp-resume breakpoints are also meaningless
1913 after an exec. */
1914 if (b->type == bp_longjmp || b->type == bp_longjmp_resume)
1915 {
1916 delete_breakpoint (b);
1917 continue;
1918 }
1919
ce78b96d
JB
1920 if (b->type == bp_catchpoint)
1921 {
1922 /* For now, none of the bp_catchpoint breakpoints need to
1923 do anything at this point. In the future, if some of
1924 the catchpoints need to something, we will need to add
1925 a new method, and call this method from here. */
1926 continue;
1927 }
1928
c5aa993b
JM
1929 /* bp_finish is a special case. The only way we ought to be able
1930 to see one of these when an exec() has happened, is if the user
1931 caught a vfork, and then said "finish". Ordinarily a finish just
1932 carries them to the call-site of the current callee, by setting
1933 a temporary bp there and resuming. But in this case, the finish
1934 will carry them entirely through the vfork & exec.
1935
1936 We don't want to allow a bp_finish to remain inserted now. But
1937 we can't safely delete it, 'cause finish_command has a handle to
1938 the bp on a bpstat, and will later want to delete it. There's a
1939 chance (and I've seen it happen) that if we delete the bp_finish
1940 here, that its storage will get reused by the time finish_command
1941 gets 'round to deleting the "use to be a bp_finish" breakpoint.
1942 We really must allow finish_command to delete a bp_finish.
1943
53a5351d
JM
1944 In the absense of a general solution for the "how do we know
1945 it's safe to delete something others may have handles to?"
1946 problem, what we'll do here is just uninsert the bp_finish, and
1947 let finish_command delete it.
1948
1949 (We know the bp_finish is "doomed" in the sense that it's
1950 momentary, and will be deleted as soon as finish_command sees
1951 the inferior stopped. So it doesn't matter that the bp's
1952 address is probably bogus in the new a.out, unlike e.g., the
1953 solib breakpoints.) */
c5aa993b 1954
c5aa993b
JM
1955 if (b->type == bp_finish)
1956 {
1957 continue;
1958 }
1959
1960 /* Without a symbolic address, we have little hope of the
1961 pre-exec() address meaning the same thing in the post-exec()
1962 a.out. */
1963 if (b->addr_string == NULL)
1964 {
1965 delete_breakpoint (b);
1966 continue;
1967 }
c5aa993b 1968 }
1900040c 1969 /* FIXME what about longjmp breakpoints? Re-create them here? */
69de3c6a 1970 create_overlay_event_breakpoint ("_ovly_debug_event");
0fd8e87f
UW
1971 create_longjmp_master_breakpoint ("longjmp");
1972 create_longjmp_master_breakpoint ("_longjmp");
1973 create_longjmp_master_breakpoint ("siglongjmp");
1974 create_longjmp_master_breakpoint ("_siglongjmp");
c906108c
SS
1975}
1976
1977int
fba45db2 1978detach_breakpoints (int pid)
c906108c 1979{
876fa593 1980 struct bp_location *b, **bp_tmp;
3a1bae8e 1981 int val = 0;
ce696e05 1982 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 1983 struct inferior *inf = current_inferior ();
c5aa993b 1984
39f77062 1985 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 1986 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 1987
6c95b8df 1988 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 1989 inferior_ptid = pid_to_ptid (pid);
876fa593 1990 ALL_BP_LOCATIONS (b, bp_tmp)
c5aa993b 1991 {
6c95b8df
PA
1992 if (b->pspace != inf->pspace)
1993 continue;
1994
0bde7532 1995 if (b->inserted)
6c95b8df 1996 val |= remove_breakpoint_1 (b, mark_inserted);
c5aa993b 1997 }
ce696e05 1998 do_cleanups (old_chain);
3a1bae8e 1999 return val;
c906108c
SS
2000}
2001
6c95b8df
PA
2002/* Remove the breakpoint location B from the current address space.
2003 Note that this is used to detach breakpoints from a child fork.
2004 When we get here, the child isn't in the inferior list, and neither
2005 do we have objects to represent its address space --- we should
2006 *not* look at b->pspace->aspace here. */
2007
c906108c 2008static int
6c95b8df 2009remove_breakpoint_1 (struct bp_location *b, insertion_state_t is)
c906108c
SS
2010{
2011 int val;
6c95b8df 2012 struct cleanup *old_chain;
c5aa993b 2013
0bde7532 2014 if (b->owner->enable_state == bp_permanent)
c2c6d25f
JM
2015 /* Permanent breakpoints cannot be inserted or removed. */
2016 return 0;
2017
74960c60
VP
2018 /* The type of none suggests that owner is actually deleted.
2019 This should not ever happen. */
2020 gdb_assert (b->owner->type != bp_none);
0bde7532
DJ
2021
2022 if (b->loc_type == bp_loc_software_breakpoint
2023 || b->loc_type == bp_loc_hardware_breakpoint)
c906108c 2024 {
c02f5703
MS
2025 /* "Normal" instruction breakpoint: either the standard
2026 trap-instruction bp (bp_breakpoint), or a
2027 bp_hardware_breakpoint. */
2028
2029 /* First check to see if we have to handle an overlay. */
2030 if (overlay_debugging == ovly_off
0bde7532
DJ
2031 || b->section == NULL
2032 || !(section_is_overlay (b->section)))
c02f5703
MS
2033 {
2034 /* No overlay handling: just remove the breakpoint. */
2035
0bde7532 2036 if (b->loc_type == bp_loc_hardware_breakpoint)
a6d9a66e 2037 val = target_remove_hw_breakpoint (b->gdbarch, &b->target_info);
c02f5703 2038 else
a6d9a66e 2039 val = target_remove_breakpoint (b->gdbarch, &b->target_info);
c02f5703 2040 }
c906108c
SS
2041 else
2042 {
c02f5703
MS
2043 /* This breakpoint is in an overlay section.
2044 Did we set a breakpoint at the LMA? */
2045 if (!overlay_events_enabled)
2046 {
2047 /* Yes -- overlay event support is not active, so we
2048 should have set a breakpoint at the LMA. Remove it.
2049 */
c02f5703
MS
2050 /* Ignore any failures: if the LMA is in ROM, we will
2051 have already warned when we failed to insert it. */
0bde7532 2052 if (b->loc_type == bp_loc_hardware_breakpoint)
a6d9a66e
UW
2053 target_remove_hw_breakpoint (b->gdbarch,
2054 &b->overlay_target_info);
c02f5703 2055 else
a6d9a66e
UW
2056 target_remove_breakpoint (b->gdbarch,
2057 &b->overlay_target_info);
c02f5703
MS
2058 }
2059 /* Did we set a breakpoint at the VMA?
2060 If so, we will have marked the breakpoint 'inserted'. */
0bde7532 2061 if (b->inserted)
c906108c 2062 {
c02f5703
MS
2063 /* Yes -- remove it. Previously we did not bother to
2064 remove the breakpoint if the section had been
2065 unmapped, but let's not rely on that being safe. We
2066 don't know what the overlay manager might do. */
0bde7532 2067 if (b->loc_type == bp_loc_hardware_breakpoint)
a6d9a66e
UW
2068 val = target_remove_hw_breakpoint (b->gdbarch,
2069 &b->target_info);
aa67235e
UW
2070
2071 /* However, we should remove *software* breakpoints only
2072 if the section is still mapped, or else we overwrite
2073 wrong code with the saved shadow contents. */
2074 else if (section_is_mapped (b->section))
a6d9a66e
UW
2075 val = target_remove_breakpoint (b->gdbarch,
2076 &b->target_info);
aa67235e
UW
2077 else
2078 val = 0;
c906108c 2079 }
c02f5703
MS
2080 else
2081 {
2082 /* No -- not inserted, so no need to remove. No error. */
2083 val = 0;
2084 }
c906108c 2085 }
879d1e6b
UW
2086
2087 /* In some cases, we might not be able to remove a breakpoint
2088 in a shared library that has already been removed, but we
2089 have not yet processed the shlib unload event. */
6c95b8df 2090 if (val && solib_name_from_address (b->pspace, b->address))
879d1e6b
UW
2091 val = 0;
2092
c906108c
SS
2093 if (val)
2094 return val;
0bde7532 2095 b->inserted = (is == mark_inserted);
c906108c 2096 }
a5606eee 2097 else if (b->loc_type == bp_loc_hardware_watchpoint)
c906108c 2098 {
278cd55f
AC
2099 struct value *v;
2100 struct value *n;
c5aa993b 2101
0bde7532 2102 b->inserted = (is == mark_inserted);
a5606eee
VP
2103 val = target_remove_watchpoint (b->address, b->length,
2104 b->watchpoint_type);
2e70b7b9 2105
c906108c 2106 /* Failure to remove any of the hardware watchpoints comes here. */
0bde7532 2107 if ((is == mark_uninserted) && (b->inserted))
8a3fe4f8 2108 warning (_("Could not remove hardware watchpoint %d."),
0bde7532 2109 b->owner->number);
c906108c 2110 }
ce78b96d
JB
2111 else if (b->owner->type == bp_catchpoint
2112 && breakpoint_enabled (b->owner)
2113 && !b->duplicate)
2114 {
2115 gdb_assert (b->owner->ops != NULL && b->owner->ops->remove != NULL);
2116
2117 val = b->owner->ops->remove (b->owner);
2118 if (val)
2119 return val;
2120 b->inserted = (is == mark_inserted);
2121 }
c906108c
SS
2122
2123 return 0;
2124}
2125
6c95b8df
PA
2126static int
2127remove_breakpoint (struct bp_location *b, insertion_state_t is)
2128{
2129 int ret;
2130 struct cleanup *old_chain;
2131
2132 if (b->owner->enable_state == bp_permanent)
2133 /* Permanent breakpoints cannot be inserted or removed. */
2134 return 0;
2135
2136 /* The type of none suggests that owner is actually deleted.
2137 This should not ever happen. */
2138 gdb_assert (b->owner->type != bp_none);
2139
2140 old_chain = save_current_space_and_thread ();
2141
2142 switch_to_program_space_and_thread (b->pspace);
2143
2144 ret = remove_breakpoint_1 (b, is);
2145
2146 do_cleanups (old_chain);
2147 return ret;
2148}
2149
c906108c
SS
2150/* Clear the "inserted" flag in all breakpoints. */
2151
25b22b0a 2152void
fba45db2 2153mark_breakpoints_out (void)
c906108c 2154{
876fa593 2155 struct bp_location *bpt, **bptp_tmp;
c906108c 2156
876fa593 2157 ALL_BP_LOCATIONS (bpt, bptp_tmp)
6c95b8df
PA
2158 if (bpt->pspace == current_program_space)
2159 bpt->inserted = 0;
c906108c
SS
2160}
2161
53a5351d
JM
2162/* Clear the "inserted" flag in all breakpoints and delete any
2163 breakpoints which should go away between runs of the program.
c906108c
SS
2164
2165 Plus other such housekeeping that has to be done for breakpoints
2166 between runs.
2167
53a5351d
JM
2168 Note: this function gets called at the end of a run (by
2169 generic_mourn_inferior) and when a run begins (by
2170 init_wait_for_inferior). */
c906108c
SS
2171
2172
2173
2174void
fba45db2 2175breakpoint_init_inferior (enum inf_context context)
c906108c 2176{
52f0bd74 2177 struct breakpoint *b, *temp;
876fa593 2178 struct bp_location *bpt, **bptp_tmp;
1c5cfe86 2179 int ix;
6c95b8df 2180 struct program_space *pspace = current_program_space;
c906108c 2181
50c71eaf
PA
2182 /* If breakpoint locations are shared across processes, then there's
2183 nothing to do. */
2567c7d9 2184 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2185 return;
2186
876fa593 2187 ALL_BP_LOCATIONS (bpt, bptp_tmp)
6c95b8df
PA
2188 {
2189 if (bpt->pspace == pspace
2190 && bpt->owner->enable_state != bp_permanent)
514f746b 2191 bpt->inserted = 0;
6c95b8df 2192 }
075f6582 2193
c906108c 2194 ALL_BREAKPOINTS_SAFE (b, temp)
c5aa993b 2195 {
6c95b8df
PA
2196 if (b->loc && b->loc->pspace != pspace)
2197 continue;
2198
c5aa993b
JM
2199 switch (b->type)
2200 {
2201 case bp_call_dummy:
2202 case bp_watchpoint_scope:
c906108c 2203
c5aa993b
JM
2204 /* If the call dummy breakpoint is at the entry point it will
2205 cause problems when the inferior is rerun, so we better
2206 get rid of it.
c906108c 2207
c5aa993b
JM
2208 Also get rid of scope breakpoints. */
2209 delete_breakpoint (b);
2210 break;
c906108c 2211
c5aa993b
JM
2212 case bp_watchpoint:
2213 case bp_hardware_watchpoint:
2214 case bp_read_watchpoint:
2215 case bp_access_watchpoint:
c906108c 2216
c5aa993b
JM
2217 /* Likewise for watchpoints on local expressions. */
2218 if (b->exp_valid_block != NULL)
2219 delete_breakpoint (b);
967af18d 2220 else if (context == inf_starting)
c860120c
PM
2221 {
2222 /* Reset val field to force reread of starting value
2223 in insert_breakpoints. */
2224 if (b->val)
2225 value_free (b->val);
2226 b->val = NULL;
fa4727a6 2227 b->val_valid = 0;
c860120c 2228 }
c5aa993b
JM
2229 break;
2230 default:
c5aa993b
JM
2231 break;
2232 }
2233 }
1c5cfe86
PA
2234
2235 /* Get rid of the moribund locations. */
2236 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bpt); ++ix)
2237 free_bp_location (bpt);
2238 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2239}
2240
6c95b8df
PA
2241/* These functions concern about actual breakpoints inserted in the
2242 target --- to e.g. check if we need to do decr_pc adjustment or if
2243 we need to hop over the bkpt --- so we check for address space
2244 match, not program space. */
2245
c2c6d25f
JM
2246/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2247 exists at PC. It returns ordinary_breakpoint_here if it's an
2248 ordinary breakpoint, or permanent_breakpoint_here if it's a
2249 permanent breakpoint.
2250 - When continuing from a location with an ordinary breakpoint, we
2251 actually single step once before calling insert_breakpoints.
2252 - When continuing from a localion with a permanent breakpoint, we
2253 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2254 the target, to advance the PC past the breakpoint. */
c906108c 2255
c2c6d25f 2256enum breakpoint_here
6c95b8df 2257breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2258{
876fa593 2259 struct bp_location *bpt, **bptp_tmp;
c2c6d25f 2260 int any_breakpoint_here = 0;
c906108c 2261
876fa593 2262 ALL_BP_LOCATIONS (bpt, bptp_tmp)
075f6582
DJ
2263 {
2264 if (bpt->loc_type != bp_loc_software_breakpoint
2265 && bpt->loc_type != bp_loc_hardware_breakpoint)
2266 continue;
2267
468d015d 2268 if ((breakpoint_enabled (bpt->owner)
075f6582 2269 || bpt->owner->enable_state == bp_permanent)
6c95b8df
PA
2270 && breakpoint_address_match (bpt->pspace->aspace, bpt->address,
2271 aspace, pc))
075f6582
DJ
2272 {
2273 if (overlay_debugging
2274 && section_is_overlay (bpt->section)
2275 && !section_is_mapped (bpt->section))
2276 continue; /* unmapped overlay -- can't be a match */
2277 else if (bpt->owner->enable_state == bp_permanent)
2278 return permanent_breakpoint_here;
2279 else
2280 any_breakpoint_here = 1;
2281 }
2282 }
c906108c 2283
c2c6d25f 2284 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2285}
2286
1c5cfe86
PA
2287/* Return true if there's a moribund breakpoint at PC. */
2288
2289int
6c95b8df 2290moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2291{
2292 struct bp_location *loc;
2293 int ix;
2294
2295 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
6c95b8df
PA
2296 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
2297 aspace, pc))
1c5cfe86
PA
2298 return 1;
2299
2300 return 0;
2301}
c2c6d25f 2302
c36b740a 2303/* Returns non-zero if there's a breakpoint inserted at PC, which is
876fa593 2304 inserted using regular breakpoint_chain / bp_location array mechanism.
c36b740a
VP
2305 This does not check for single-step breakpoints, which are
2306 inserted and removed using direct target manipulation. */
c906108c
SS
2307
2308int
6c95b8df 2309regular_breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2310{
876fa593 2311 struct bp_location *bpt, **bptp_tmp;
c906108c 2312
876fa593 2313 ALL_BP_LOCATIONS (bpt, bptp_tmp)
c5aa993b 2314 {
075f6582
DJ
2315 if (bpt->loc_type != bp_loc_software_breakpoint
2316 && bpt->loc_type != bp_loc_hardware_breakpoint)
2317 continue;
2318
2319 if (bpt->inserted
6c95b8df
PA
2320 && breakpoint_address_match (bpt->pspace->aspace, bpt->address,
2321 aspace, pc))
075f6582
DJ
2322 {
2323 if (overlay_debugging
2324 && section_is_overlay (bpt->section)
2325 && !section_is_mapped (bpt->section))
2326 continue; /* unmapped overlay -- can't be a match */
2327 else
2328 return 1;
2329 }
c5aa993b 2330 }
c36b740a
VP
2331 return 0;
2332}
2333
2334/* Returns non-zero iff there's either regular breakpoint
2335 or a single step breakpoint inserted at PC. */
2336
2337int
6c95b8df 2338breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2339{
6c95b8df 2340 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2341 return 1;
c906108c 2342
6c95b8df 2343 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2344 return 1;
2345
c906108c
SS
2346 return 0;
2347}
2348
4fa8626c
DJ
2349/* This function returns non-zero iff there is a software breakpoint
2350 inserted at PC. */
2351
2352int
6c95b8df 2353software_breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4fa8626c 2354{
876fa593 2355 struct bp_location *bpt, **bptp_tmp;
4fa8626c
DJ
2356 int any_breakpoint_here = 0;
2357
876fa593 2358 ALL_BP_LOCATIONS (bpt, bptp_tmp)
4fa8626c
DJ
2359 {
2360 if (bpt->loc_type != bp_loc_software_breakpoint)
2361 continue;
2362
0d381245 2363 if (bpt->inserted
6c95b8df
PA
2364 && breakpoint_address_match (bpt->pspace->aspace, bpt->address,
2365 aspace, pc))
4fa8626c
DJ
2366 {
2367 if (overlay_debugging
2368 && section_is_overlay (bpt->section)
2369 && !section_is_mapped (bpt->section))
2370 continue; /* unmapped overlay -- can't be a match */
2371 else
2372 return 1;
2373 }
2374 }
2375
1aafd4da 2376 /* Also check for software single-step breakpoints. */
6c95b8df 2377 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2378 return 1;
2379
4fa8626c
DJ
2380 return 0;
2381}
2382
9093389c
PA
2383int
2384hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2385 CORE_ADDR addr, ULONGEST len)
2386{
2387 struct breakpoint *bpt;
2388
2389 ALL_BREAKPOINTS (bpt)
2390 {
2391 struct bp_location *loc;
2392
2393 if (bpt->type != bp_hardware_watchpoint
2394 && bpt->type != bp_access_watchpoint)
2395 continue;
2396
2397 if (!breakpoint_enabled (bpt))
2398 continue;
2399
2400 for (loc = bpt->loc; loc; loc = loc->next)
2401 if (loc->pspace->aspace == aspace && loc->inserted)
2402 {
2403 CORE_ADDR l, h;
2404
2405 /* Check for intersection. */
2406 l = max (loc->address, addr);
2407 h = min (loc->address + loc->length, addr + len);
2408 if (l < h)
2409 return 1;
2410 }
2411 }
2412 return 0;
2413}
2414
075f6582
DJ
2415/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2416 PC is valid for process/thread PTID. */
c906108c
SS
2417
2418int
6c95b8df
PA
2419breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2420 ptid_t ptid)
c906108c 2421{
876fa593 2422 struct bp_location *bpt, **bptp_tmp;
4a306c9a 2423 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 2424 int thread = -1;
4a306c9a 2425 int task = 0;
a6f1cd96 2426
876fa593 2427 ALL_BP_LOCATIONS (bpt, bptp_tmp)
c5aa993b 2428 {
075f6582
DJ
2429 if (bpt->loc_type != bp_loc_software_breakpoint
2430 && bpt->loc_type != bp_loc_hardware_breakpoint)
2431 continue;
2432
a6f1cd96
JB
2433 if (!breakpoint_enabled (bpt->owner)
2434 && bpt->owner->enable_state != bp_permanent)
2435 continue;
2436
6c95b8df
PA
2437 if (!breakpoint_address_match (bpt->pspace->aspace, bpt->address,
2438 aspace, pc))
a6f1cd96
JB
2439 continue;
2440
2441 if (bpt->owner->thread != -1)
075f6582 2442 {
a6f1cd96
JB
2443 /* This is a thread-specific breakpoint. Check that ptid
2444 matches that thread. If thread hasn't been computed yet,
2445 it is now time to do so. */
2446 if (thread == -1)
2447 thread = pid_to_thread_id (ptid);
2448 if (bpt->owner->thread != thread)
2449 continue;
075f6582 2450 }
a6f1cd96 2451
4a306c9a
JB
2452 if (bpt->owner->task != 0)
2453 {
2454 /* This is a task-specific breakpoint. Check that ptid
2455 matches that task. If task hasn't been computed yet,
2456 it is now time to do so. */
2457 if (task == 0)
2458 task = ada_get_task_number (ptid);
2459 if (bpt->owner->task != task)
2460 continue;
2461 }
2462
a6f1cd96
JB
2463 if (overlay_debugging
2464 && section_is_overlay (bpt->section)
2465 && !section_is_mapped (bpt->section))
2466 continue; /* unmapped overlay -- can't be a match */
2467
2468 return 1;
c5aa993b 2469 }
c906108c
SS
2470
2471 return 0;
2472}
c906108c 2473\f
c5aa993b 2474
c906108c
SS
2475/* bpstat stuff. External routines' interfaces are documented
2476 in breakpoint.h. */
2477
2478int
fba45db2 2479ep_is_catchpoint (struct breakpoint *ep)
c906108c 2480{
533be4dd 2481 return (ep->type == bp_catchpoint);
c906108c
SS
2482}
2483
198757a8
VP
2484void
2485bpstat_free (bpstat bs)
2486{
2487 if (bs->old_val != NULL)
2488 value_free (bs->old_val);
2489 free_command_lines (&bs->commands);
2490 xfree (bs);
2491}
2492
c906108c
SS
2493/* Clear a bpstat so that it says we are not at any breakpoint.
2494 Also free any storage that is part of a bpstat. */
2495
2496void
fba45db2 2497bpstat_clear (bpstat *bsp)
c906108c
SS
2498{
2499 bpstat p;
2500 bpstat q;
2501
2502 if (bsp == 0)
2503 return;
2504 p = *bsp;
2505 while (p != NULL)
2506 {
2507 q = p->next;
198757a8 2508 bpstat_free (p);
c906108c
SS
2509 p = q;
2510 }
2511 *bsp = NULL;
2512}
2513
2514/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
2515 is part of the bpstat is copied as well. */
2516
2517bpstat
fba45db2 2518bpstat_copy (bpstat bs)
c906108c
SS
2519{
2520 bpstat p = NULL;
2521 bpstat tmp;
2522 bpstat retval = NULL;
2523
2524 if (bs == NULL)
2525 return bs;
2526
2527 for (; bs != NULL; bs = bs->next)
2528 {
2529 tmp = (bpstat) xmalloc (sizeof (*tmp));
2530 memcpy (tmp, bs, sizeof (*tmp));
31cc81e9
DJ
2531 if (bs->commands != NULL)
2532 tmp->commands = copy_command_lines (bs->commands);
2533 if (bs->old_val != NULL)
3c3185ac
JK
2534 {
2535 tmp->old_val = value_copy (bs->old_val);
2536 release_value (tmp->old_val);
2537 }
31cc81e9 2538
c906108c
SS
2539 if (p == NULL)
2540 /* This is the first thing in the chain. */
2541 retval = tmp;
2542 else
2543 p->next = tmp;
2544 p = tmp;
2545 }
2546 p->next = NULL;
2547 return retval;
2548}
2549
2550/* Find the bpstat associated with this breakpoint */
2551
2552bpstat
fba45db2 2553bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 2554{
c5aa993b
JM
2555 if (bsp == NULL)
2556 return NULL;
c906108c 2557
c5aa993b
JM
2558 for (; bsp != NULL; bsp = bsp->next)
2559 {
4f8d1dc6 2560 if (bsp->breakpoint_at && bsp->breakpoint_at->owner == breakpoint)
c5aa993b
JM
2561 return bsp;
2562 }
c906108c
SS
2563 return NULL;
2564}
2565
2566/* Find a step_resume breakpoint associated with this bpstat.
2567 (If there are multiple step_resume bp's on the list, this function
2568 will arbitrarily pick one.)
2569
2570 It is an error to use this function if BPSTAT doesn't contain a
2571 step_resume breakpoint.
2572
2573 See wait_for_inferior's use of this function. */
2574struct breakpoint *
fba45db2 2575bpstat_find_step_resume_breakpoint (bpstat bsp)
c906108c 2576{
8601f500
MS
2577 int current_thread;
2578
8a3fe4f8 2579 gdb_assert (bsp != NULL);
c906108c 2580
8601f500
MS
2581 current_thread = pid_to_thread_id (inferior_ptid);
2582
c906108c
SS
2583 for (; bsp != NULL; bsp = bsp->next)
2584 {
059fb39f
PM
2585 if ((bsp->breakpoint_at != NULL)
2586 && (bsp->breakpoint_at->owner->type == bp_step_resume)
2587 && (bsp->breakpoint_at->owner->thread == current_thread
2588 || bsp->breakpoint_at->owner->thread == -1))
4f8d1dc6 2589 return bsp->breakpoint_at->owner;
c906108c
SS
2590 }
2591
8a3fe4f8 2592 internal_error (__FILE__, __LINE__, _("No step_resume breakpoint found."));
c906108c
SS
2593}
2594
2595
8671a17b 2596/* Put in *NUM the breakpoint number of the first breakpoint we are stopped
c906108c
SS
2597 at. *BSP upon return is a bpstat which points to the remaining
2598 breakpoints stopped at (but which is not guaranteed to be good for
2599 anything but further calls to bpstat_num).
8671a17b
PA
2600 Return 0 if passed a bpstat which does not indicate any breakpoints.
2601 Return -1 if stopped at a breakpoint that has been deleted since
2602 we set it.
2603 Return 1 otherwise. */
c906108c
SS
2604
2605int
8671a17b 2606bpstat_num (bpstat *bsp, int *num)
c906108c
SS
2607{
2608 struct breakpoint *b;
2609
2610 if ((*bsp) == NULL)
2611 return 0; /* No more breakpoint values */
8671a17b 2612
4f8d1dc6
VP
2613 /* We assume we'll never have several bpstats that
2614 correspond to a single breakpoint -- otherwise,
2615 this function might return the same number more
2616 than once and this will look ugly. */
2617 b = (*bsp)->breakpoint_at ? (*bsp)->breakpoint_at->owner : NULL;
8671a17b
PA
2618 *bsp = (*bsp)->next;
2619 if (b == NULL)
2620 return -1; /* breakpoint that's been deleted since */
2621
2622 *num = b->number; /* We have its number */
2623 return 1;
c906108c
SS
2624}
2625
2626/* Modify BS so that the actions will not be performed. */
2627
2628void
fba45db2 2629bpstat_clear_actions (bpstat bs)
c906108c
SS
2630{
2631 for (; bs != NULL; bs = bs->next)
2632 {
c2b8ed2c 2633 free_command_lines (&bs->commands);
c906108c
SS
2634 if (bs->old_val != NULL)
2635 {
2636 value_free (bs->old_val);
2637 bs->old_val = NULL;
2638 }
2639 }
2640}
2641
f3b1572e
PA
2642/* Called when a command is about to proceed the inferior. */
2643
2644static void
2645breakpoint_about_to_proceed (void)
2646{
2647 if (!ptid_equal (inferior_ptid, null_ptid))
2648 {
2649 struct thread_info *tp = inferior_thread ();
2650
2651 /* Allow inferior function calls in breakpoint commands to not
2652 interrupt the command list. When the call finishes
2653 successfully, the inferior will be standing at the same
2654 breakpoint as if nothing happened. */
2655 if (tp->in_infcall)
2656 return;
2657 }
2658
2659 breakpoint_proceeded = 1;
2660}
2661
c906108c 2662/* Stub for cleaning up our state if we error-out of a breakpoint command */
c906108c 2663static void
4efb68b1 2664cleanup_executing_breakpoints (void *ignore)
c906108c
SS
2665{
2666 executing_breakpoint_commands = 0;
2667}
2668
2669/* Execute all the commands associated with all the breakpoints at this
2670 location. Any of these commands could cause the process to proceed
2671 beyond this point, etc. We look out for such changes by checking
347bddb7 2672 the global "breakpoint_proceeded" after each command.
c906108c 2673
347bddb7
PA
2674 Returns true if a breakpoint command resumed the inferior. In that
2675 case, it is the caller's responsibility to recall it again with the
2676 bpstat of the current thread. */
2677
2678static int
2679bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
2680{
2681 bpstat bs;
2682 struct cleanup *old_chain;
347bddb7 2683 int again = 0;
c906108c
SS
2684
2685 /* Avoid endless recursion if a `source' command is contained
2686 in bs->commands. */
2687 if (executing_breakpoint_commands)
347bddb7 2688 return 0;
c906108c
SS
2689
2690 executing_breakpoint_commands = 1;
2691 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
2692
c906108c
SS
2693 /* This pointer will iterate over the list of bpstat's. */
2694 bs = *bsp;
2695
2696 breakpoint_proceeded = 0;
2697 for (; bs != NULL; bs = bs->next)
2698 {
6c50ab1c
JB
2699 struct command_line *cmd;
2700 struct cleanup *this_cmd_tree_chain;
2701
2702 /* Take ownership of the BSP's command tree, if it has one.
2703
2704 The command tree could legitimately contain commands like
2705 'step' and 'next', which call clear_proceed_status, which
2706 frees stop_bpstat's command tree. To make sure this doesn't
2707 free the tree we're executing out from under us, we need to
2708 take ownership of the tree ourselves. Since a given bpstat's
2709 commands are only executed once, we don't need to copy it; we
2710 can clear the pointer in the bpstat, and make sure we free
2711 the tree when we're done. */
c906108c 2712 cmd = bs->commands;
6c50ab1c
JB
2713 bs->commands = 0;
2714 this_cmd_tree_chain = make_cleanup_free_command_lines (&cmd);
2715
c906108c
SS
2716 while (cmd != NULL)
2717 {
2718 execute_control_command (cmd);
2719
2720 if (breakpoint_proceeded)
2721 break;
2722 else
2723 cmd = cmd->next;
2724 }
6c50ab1c
JB
2725
2726 /* We can free this command tree now. */
2727 do_cleanups (this_cmd_tree_chain);
2728
c906108c 2729 if (breakpoint_proceeded)
32c1e744
VP
2730 {
2731 if (target_can_async_p ())
347bddb7
PA
2732 /* If we are in async mode, then the target might be still
2733 running, not stopped at any breakpoint, so nothing for
2734 us to do here -- just return to the event loop. */
2735 ;
32c1e744
VP
2736 else
2737 /* In sync mode, when execute_control_command returns
2738 we're already standing on the next breakpoint.
347bddb7
PA
2739 Breakpoint commands for that stop were not run, since
2740 execute_command does not run breakpoint commands --
2741 only command_line_handler does, but that one is not
2742 involved in execution of breakpoint commands. So, we
2743 can now execute breakpoint commands. It should be
2744 noted that making execute_command do bpstat actions is
2745 not an option -- in this case we'll have recursive
2746 invocation of bpstat for each breakpoint with a
2747 command, and can easily blow up GDB stack. Instead, we
2748 return true, which will trigger the caller to recall us
2749 with the new stop_bpstat. */
2750 again = 1;
2751 break;
32c1e744 2752 }
c906108c 2753 }
c2b8ed2c 2754 do_cleanups (old_chain);
347bddb7
PA
2755 return again;
2756}
2757
2758void
2759bpstat_do_actions (void)
2760{
2761 /* Do any commands attached to breakpoint we are stopped at. */
2762 while (!ptid_equal (inferior_ptid, null_ptid)
2763 && target_has_execution
2764 && !is_exited (inferior_ptid)
2765 && !is_executing (inferior_ptid))
2766 /* Since in sync mode, bpstat_do_actions may resume the inferior,
2767 and only return when it is stopped at the next breakpoint, we
2768 keep doing breakpoint actions until it returns false to
2769 indicate the inferior was not resumed. */
2770 if (!bpstat_do_actions_1 (&inferior_thread ()->stop_bpstat))
2771 break;
c906108c
SS
2772}
2773
fa4727a6
DJ
2774/* Print out the (old or new) value associated with a watchpoint. */
2775
2776static void
2777watchpoint_value_print (struct value *val, struct ui_file *stream)
2778{
2779 if (val == NULL)
2780 fprintf_unfiltered (stream, _("<unreadable>"));
2781 else
79a45b7d
TT
2782 {
2783 struct value_print_options opts;
2784 get_user_print_options (&opts);
2785 value_print (val, stream, &opts);
2786 }
fa4727a6
DJ
2787}
2788
e514a9d6 2789/* This is the normal print function for a bpstat. In the future,
c906108c 2790 much of this logic could (should?) be moved to bpstat_stop_status,
e514a9d6
JM
2791 by having it set different print_it values.
2792
2793 Current scheme: When we stop, bpstat_print() is called. It loops
2794 through the bpstat list of things causing this stop, calling the
2795 print_bp_stop_message function on each one. The behavior of the
2796 print_bp_stop_message function depends on the print_it field of
2797 bpstat. If such field so indicates, call this function here.
2798
2799 Return values from this routine (ultimately used by bpstat_print()
2800 and normal_stop() to decide what to do):
2801 PRINT_NOTHING: Means we already printed all we needed to print,
2802 don't print anything else.
2803 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
2804 that something to be followed by a location.
2805 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
2806 that something to be followed by a location.
2807 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
2808 analysis. */
c906108c 2809
917317f4 2810static enum print_stop_action
fba45db2 2811print_it_typical (bpstat bs)
c906108c 2812{
f7545552 2813 struct cleanup *old_chain;
4f8d1dc6 2814 struct breakpoint *b;
89f9893c 2815 const struct bp_location *bl;
8b93c638 2816 struct ui_stream *stb;
f7545552
TT
2817 int bp_temp = 0;
2818 enum print_stop_action result;
2819
c906108c
SS
2820 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
2821 which has since been deleted. */
e514a9d6 2822 if (bs->breakpoint_at == NULL)
917317f4 2823 return PRINT_UNKNOWN;
0d381245
VP
2824 bl = bs->breakpoint_at;
2825 b = bl->owner;
c906108c 2826
f7545552
TT
2827 stb = ui_out_stream_new (uiout);
2828 old_chain = make_cleanup_ui_out_stream_delete (stb);
2829
4f8d1dc6 2830 switch (b->type)
c906108c 2831 {
e514a9d6
JM
2832 case bp_breakpoint:
2833 case bp_hardware_breakpoint:
2cec12e5 2834 bp_temp = bs->breakpoint_at->owner->disposition == disp_del;
0d381245
VP
2835 if (bl->address != bl->requested_address)
2836 breakpoint_adjustment_warning (bl->requested_address,
2837 bl->address,
4f8d1dc6
VP
2838 b->number, 1);
2839 annotate_breakpoint (b->number);
2cec12e5
AR
2840 if (bp_temp)
2841 ui_out_text (uiout, "\nTemporary breakpoint ");
2842 else
2843 ui_out_text (uiout, "\nBreakpoint ");
9dc5e2a9 2844 if (ui_out_is_mi_like_p (uiout))
2cec12e5
AR
2845 {
2846 ui_out_field_string (uiout, "reason",
2847 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
2848 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
2849 }
4f8d1dc6 2850 ui_out_field_int (uiout, "bkptno", b->number);
8b93c638 2851 ui_out_text (uiout, ", ");
f7545552 2852 result = PRINT_SRC_AND_LOC;
e514a9d6
JM
2853 break;
2854
2855 case bp_shlib_event:
917317f4
JM
2856 /* Did we stop because the user set the stop_on_solib_events
2857 variable? (If so, we report this as a generic, "Stopped due
2858 to shlib event" message.) */
a3f17187 2859 printf_filtered (_("Stopped due to shared library event\n"));
f7545552 2860 result = PRINT_NOTHING;
e514a9d6
JM
2861 break;
2862
c4093a6a
JM
2863 case bp_thread_event:
2864 /* Not sure how we will get here.
2865 GDB should not stop for these breakpoints. */
a3f17187 2866 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
f7545552 2867 result = PRINT_NOTHING;
c4093a6a
JM
2868 break;
2869
1900040c
MS
2870 case bp_overlay_event:
2871 /* By analogy with the thread event, GDB should not stop for these. */
a3f17187 2872 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
f7545552 2873 result = PRINT_NOTHING;
1900040c
MS
2874 break;
2875
0fd8e87f
UW
2876 case bp_longjmp_master:
2877 /* These should never be enabled. */
2878 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
2879 result = PRINT_NOTHING;
2880 break;
2881
e514a9d6
JM
2882 case bp_watchpoint:
2883 case bp_hardware_watchpoint:
fa4727a6
DJ
2884 annotate_watchpoint (b->number);
2885 if (ui_out_is_mi_like_p (uiout))
2886 ui_out_field_string
2887 (uiout, "reason",
2888 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
2889 mention (b);
f7545552 2890 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
fa4727a6
DJ
2891 ui_out_text (uiout, "\nOld value = ");
2892 watchpoint_value_print (bs->old_val, stb->stream);
2893 ui_out_field_stream (uiout, "old", stb);
2894 ui_out_text (uiout, "\nNew value = ");
2895 watchpoint_value_print (b->val, stb->stream);
2896 ui_out_field_stream (uiout, "new", stb);
fa4727a6 2897 ui_out_text (uiout, "\n");
e514a9d6 2898 /* More than one watchpoint may have been triggered. */
f7545552 2899 result = PRINT_UNKNOWN;
e514a9d6
JM
2900 break;
2901
2902 case bp_read_watchpoint:
9dc5e2a9 2903 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
2904 ui_out_field_string
2905 (uiout, "reason",
2906 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
4f8d1dc6 2907 mention (b);
f7545552 2908 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 2909 ui_out_text (uiout, "\nValue = ");
fa4727a6 2910 watchpoint_value_print (b->val, stb->stream);
8b93c638 2911 ui_out_field_stream (uiout, "value", stb);
8b93c638 2912 ui_out_text (uiout, "\n");
f7545552 2913 result = PRINT_UNKNOWN;
e514a9d6
JM
2914 break;
2915
2916 case bp_access_watchpoint:
fa4727a6 2917 if (bs->old_val != NULL)
8b93c638 2918 {
4f8d1dc6 2919 annotate_watchpoint (b->number);
9dc5e2a9 2920 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
2921 ui_out_field_string
2922 (uiout, "reason",
2923 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
4f8d1dc6 2924 mention (b);
f7545552 2925 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 2926 ui_out_text (uiout, "\nOld value = ");
fa4727a6 2927 watchpoint_value_print (bs->old_val, stb->stream);
8b93c638 2928 ui_out_field_stream (uiout, "old", stb);
8b93c638
JM
2929 ui_out_text (uiout, "\nNew value = ");
2930 }
2931 else
2932 {
4f8d1dc6 2933 mention (b);
9dc5e2a9 2934 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
2935 ui_out_field_string
2936 (uiout, "reason",
2937 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
f7545552 2938 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638
JM
2939 ui_out_text (uiout, "\nValue = ");
2940 }
fa4727a6 2941 watchpoint_value_print (b->val, stb->stream);
8b93c638 2942 ui_out_field_stream (uiout, "new", stb);
8b93c638 2943 ui_out_text (uiout, "\n");
f7545552 2944 result = PRINT_UNKNOWN;
e514a9d6 2945 break;
4ce44c66 2946
e514a9d6
JM
2947 /* Fall through, we don't deal with these types of breakpoints
2948 here. */
2949
11cf8741 2950 case bp_finish:
9dc5e2a9 2951 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
2952 ui_out_field_string
2953 (uiout, "reason",
2954 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
f7545552 2955 result = PRINT_UNKNOWN;
8b93c638
JM
2956 break;
2957
e514a9d6 2958 case bp_until:
9dc5e2a9 2959 if (ui_out_is_mi_like_p (uiout))
1fbc2a49
NR
2960 ui_out_field_string
2961 (uiout, "reason",
2962 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
f7545552 2963 result = PRINT_UNKNOWN;
8b93c638
JM
2964 break;
2965
c2d11a7d 2966 case bp_none:
e514a9d6
JM
2967 case bp_longjmp:
2968 case bp_longjmp_resume:
2969 case bp_step_resume:
e514a9d6
JM
2970 case bp_watchpoint_scope:
2971 case bp_call_dummy:
1042e4c0 2972 case bp_tracepoint:
4efc6507 2973 case bp_jit_event:
e514a9d6 2974 default:
f7545552
TT
2975 result = PRINT_UNKNOWN;
2976 break;
e514a9d6 2977 }
f7545552
TT
2978
2979 do_cleanups (old_chain);
2980 return result;
e514a9d6
JM
2981}
2982
2983/* Generic routine for printing messages indicating why we
2984 stopped. The behavior of this function depends on the value
2985 'print_it' in the bpstat structure. Under some circumstances we
2986 may decide not to print anything here and delegate the task to
2987 normal_stop(). */
2988
2989static enum print_stop_action
2990print_bp_stop_message (bpstat bs)
2991{
2992 switch (bs->print_it)
2993 {
2994 case print_it_noop:
2995 /* Nothing should be printed for this bpstat entry. */
2996 return PRINT_UNKNOWN;
2997 break;
2998
2999 case print_it_done:
3000 /* We still want to print the frame, but we already printed the
3001 relevant messages. */
3002 return PRINT_SRC_AND_LOC;
3003 break;
3004
3005 case print_it_normal:
4f8d1dc6 3006 {
89f9893c 3007 const struct bp_location *bl = bs->breakpoint_at;
4f8d1dc6
VP
3008 struct breakpoint *b = bl ? bl->owner : NULL;
3009
3010 /* Normal case. Call the breakpoint's print_it method, or
3011 print_it_typical. */
3012 /* FIXME: how breakpoint can ever be NULL here? */
3013 if (b != NULL && b->ops != NULL && b->ops->print_it != NULL)
3014 return b->ops->print_it (b);
3015 else
3016 return print_it_typical (bs);
3017 }
3018 break;
3086aeae 3019
e514a9d6 3020 default:
8e65ff28 3021 internal_error (__FILE__, __LINE__,
e2e0b3e5 3022 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3023 break;
c906108c 3024 }
c906108c
SS
3025}
3026
e514a9d6
JM
3027/* Print a message indicating what happened. This is called from
3028 normal_stop(). The input to this routine is the head of the bpstat
3029 list - a list of the eventpoints that caused this stop. This
3030 routine calls the generic print routine for printing a message
3031 about reasons for stopping. This will print (for example) the
3032 "Breakpoint n," part of the output. The return value of this
3033 routine is one of:
c906108c 3034
917317f4
JM
3035 PRINT_UNKNOWN: Means we printed nothing
3036 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
c5aa993b
JM
3037 code to print the location. An example is
3038 "Breakpoint 1, " which should be followed by
3039 the location.
917317f4 3040 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3041 to also print the location part of the message.
3042 An example is the catch/throw messages, which
917317f4
JM
3043 don't require a location appended to the end.
3044 PRINT_NOTHING: We have done some printing and we don't need any
3045 further info to be printed.*/
c906108c 3046
917317f4 3047enum print_stop_action
fba45db2 3048bpstat_print (bpstat bs)
c906108c
SS
3049{
3050 int val;
c5aa993b 3051
c906108c 3052 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3053 (Currently all watchpoints go on the bpstat whether hit or not.
3054 That probably could (should) be changed, provided care is taken
c906108c 3055 with respect to bpstat_explains_signal). */
e514a9d6
JM
3056 for (; bs; bs = bs->next)
3057 {
3058 val = print_bp_stop_message (bs);
3059 if (val == PRINT_SRC_ONLY
3060 || val == PRINT_SRC_AND_LOC
3061 || val == PRINT_NOTHING)
3062 return val;
3063 }
c906108c 3064
e514a9d6
JM
3065 /* We reached the end of the chain, or we got a null BS to start
3066 with and nothing was printed. */
917317f4 3067 return PRINT_UNKNOWN;
c906108c
SS
3068}
3069
3070/* Evaluate the expression EXP and return 1 if value is zero.
3071 This is used inside a catch_errors to evaluate the breakpoint condition.
3072 The argument is a "struct expression *" that has been cast to char * to
3073 make it pass through catch_errors. */
3074
3075static int
4efb68b1 3076breakpoint_cond_eval (void *exp)
c906108c 3077{
278cd55f 3078 struct value *mark = value_mark ();
c5aa993b 3079 int i = !value_true (evaluate_expression ((struct expression *) exp));
c906108c
SS
3080 value_free_to_mark (mark);
3081 return i;
3082}
3083
3084/* Allocate a new bpstat and chain it to the current one. */
3085
3086static bpstat
89f9893c 3087bpstat_alloc (const struct bp_location *bl, bpstat cbs /* Current "bs" value */ )
c906108c
SS
3088{
3089 bpstat bs;
3090
3091 bs = (bpstat) xmalloc (sizeof (*bs));
3092 cbs->next = bs;
4f8d1dc6 3093 bs->breakpoint_at = bl;
c906108c
SS
3094 /* If the condition is false, etc., don't do the commands. */
3095 bs->commands = NULL;
3096 bs->old_val = NULL;
3097 bs->print_it = print_it_normal;
3098 return bs;
3099}
3100\f
d983da9c
DJ
3101/* The target has stopped with waitstatus WS. Check if any hardware
3102 watchpoints have triggered, according to the target. */
3103
3104int
3105watchpoints_triggered (struct target_waitstatus *ws)
3106{
d92524f1 3107 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3108 CORE_ADDR addr;
3109 struct breakpoint *b;
3110
3111 if (!stopped_by_watchpoint)
3112 {
3113 /* We were not stopped by a watchpoint. Mark all watchpoints
3114 as not triggered. */
3115 ALL_BREAKPOINTS (b)
3116 if (b->type == bp_hardware_watchpoint
3117 || b->type == bp_read_watchpoint
3118 || b->type == bp_access_watchpoint)
3119 b->watchpoint_triggered = watch_triggered_no;
3120
3121 return 0;
3122 }
3123
3124 if (!target_stopped_data_address (&current_target, &addr))
3125 {
3126 /* We were stopped by a watchpoint, but we don't know where.
3127 Mark all watchpoints as unknown. */
3128 ALL_BREAKPOINTS (b)
3129 if (b->type == bp_hardware_watchpoint
3130 || b->type == bp_read_watchpoint
3131 || b->type == bp_access_watchpoint)
3132 b->watchpoint_triggered = watch_triggered_unknown;
3133
3134 return stopped_by_watchpoint;
3135 }
3136
3137 /* The target could report the data address. Mark watchpoints
3138 affected by this data address as triggered, and all others as not
3139 triggered. */
3140
3141 ALL_BREAKPOINTS (b)
3142 if (b->type == bp_hardware_watchpoint
3143 || b->type == bp_read_watchpoint
3144 || b->type == bp_access_watchpoint)
3145 {
a5606eee 3146 struct bp_location *loc;
d983da9c
DJ
3147 struct value *v;
3148
3149 b->watchpoint_triggered = watch_triggered_no;
a5606eee
VP
3150 for (loc = b->loc; loc; loc = loc->next)
3151 /* Exact match not required. Within range is
3152 sufficient. */
5009afc5
AS
3153 if (target_watchpoint_addr_within_range (&current_target,
3154 addr, loc->address,
3155 loc->length))
a5606eee
VP
3156 {
3157 b->watchpoint_triggered = watch_triggered_yes;
3158 break;
3159 }
d983da9c
DJ
3160 }
3161
3162 return 1;
3163}
3164
c906108c
SS
3165/* Possible return values for watchpoint_check (this can't be an enum
3166 because of check_errors). */
3167/* The watchpoint has been deleted. */
3168#define WP_DELETED 1
3169/* The value has changed. */
3170#define WP_VALUE_CHANGED 2
3171/* The value has not changed. */
3172#define WP_VALUE_NOT_CHANGED 3
3173
3174#define BP_TEMPFLAG 1
3175#define BP_HARDWAREFLAG 2
3176
553e4c11
JB
3177/* Evaluate watchpoint condition expression and check if its value changed.
3178
3179 P should be a pointer to struct bpstat, but is defined as a void *
3180 in order for this function to be usable with catch_errors. */
c906108c
SS
3181
3182static int
4efb68b1 3183watchpoint_check (void *p)
c906108c
SS
3184{
3185 bpstat bs = (bpstat) p;
3186 struct breakpoint *b;
3187 struct frame_info *fr;
3188 int within_current_scope;
3189
4f8d1dc6 3190 b = bs->breakpoint_at->owner;
c906108c 3191
f6bc2008
PA
3192 /* If this is a local watchpoint, we only want to check if the
3193 watchpoint frame is in scope if the current thread is the thread
3194 that was used to create the watchpoint. */
3195 if (!watchpoint_in_thread_scope (b))
3196 return WP_VALUE_NOT_CHANGED;
3197
c906108c
SS
3198 if (b->exp_valid_block == NULL)
3199 within_current_scope = 1;
3200 else
3201 {
edb3359d
DJ
3202 struct frame_info *frame = get_current_frame ();
3203 struct gdbarch *frame_arch = get_frame_arch (frame);
3204 CORE_ADDR frame_pc = get_frame_pc (frame);
3205
101dcfbe 3206 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3207 within_current_scope = (fr != NULL);
69fbadd5
DJ
3208
3209 /* If we've gotten confused in the unwinder, we might have
3210 returned a frame that can't describe this variable. */
edb3359d
DJ
3211 if (within_current_scope)
3212 {
3213 struct symbol *function;
3214
3215 function = get_frame_function (fr);
3216 if (function == NULL
3217 || !contained_in (b->exp_valid_block,
3218 SYMBOL_BLOCK_VALUE (function)))
3219 within_current_scope = 0;
3220 }
69fbadd5 3221
c12260ac
CV
3222 /* in_function_epilogue_p() returns a non-zero value if we're still
3223 in the function but the stack frame has already been invalidated.
3224 Since we can't rely on the values of local variables after the
3225 stack has been destroyed, we are treating the watchpoint in that
edb3359d
DJ
3226 state as `not changed' without further checking. Don't mark
3227 watchpoints as changed if the current frame is in an epilogue -
3228 even if they are in some other frame, our view of the stack
3229 is likely to be wrong. */
3230 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
3231 return WP_VALUE_NOT_CHANGED;
fb14de7b 3232
edb3359d 3233 if (within_current_scope)
c906108c
SS
3234 /* If we end up stopping, the current frame will get selected
3235 in normal_stop. So this call to select_frame won't affect
3236 the user. */
0f7d239c 3237 select_frame (fr);
c906108c 3238 }
c5aa993b 3239
c906108c
SS
3240 if (within_current_scope)
3241 {
3242 /* We use value_{,free_to_}mark because it could be a
3243 *long* time before we return to the command level and
c5aa993b
JM
3244 call free_all_values. We can't call free_all_values because
3245 we might be in the middle of evaluating a function call. */
c906108c 3246
278cd55f 3247 struct value *mark = value_mark ();
fa4727a6
DJ
3248 struct value *new_val;
3249
3250 fetch_watchpoint_value (b->exp, &new_val, NULL, NULL);
218d2fc6
TJB
3251
3252 /* We use value_equal_contents instead of value_equal because the latter
3253 coerces an array to a pointer, thus comparing just the address of the
3254 array instead of its contents. This is not what we want. */
fa4727a6 3255 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3256 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3257 {
fa4727a6
DJ
3258 if (new_val != NULL)
3259 {
3260 release_value (new_val);
3261 value_free_to_mark (mark);
3262 }
c906108c
SS
3263 bs->old_val = b->val;
3264 b->val = new_val;
fa4727a6 3265 b->val_valid = 1;
c906108c
SS
3266 /* We will stop here */
3267 return WP_VALUE_CHANGED;
3268 }
3269 else
3270 {
3271 /* Nothing changed, don't do anything. */
3272 value_free_to_mark (mark);
3273 /* We won't stop here */
3274 return WP_VALUE_NOT_CHANGED;
3275 }
3276 }
3277 else
3278 {
3279 /* This seems like the only logical thing to do because
c5aa993b
JM
3280 if we temporarily ignored the watchpoint, then when
3281 we reenter the block in which it is valid it contains
3282 garbage (in the case of a function, it may have two
3283 garbage values, one before and one after the prologue).
3284 So we can't even detect the first assignment to it and
3285 watch after that (since the garbage may or may not equal
3286 the first value assigned). */
4ce44c66
JM
3287 /* We print all the stop information in print_it_typical(), but
3288 in this case, by the time we call print_it_typical() this bp
3289 will be deleted already. So we have no choice but print the
3290 information here. */
9dc5e2a9 3291 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3292 ui_out_field_string
3293 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3294 ui_out_text (uiout, "\nWatchpoint ");
4f8d1dc6 3295 ui_out_field_int (uiout, "wpnum", b->number);
8b93c638
JM
3296 ui_out_text (uiout, " deleted because the program has left the block in\n\
3297which its expression is valid.\n");
4ce44c66 3298
c906108c 3299 if (b->related_breakpoint)
b5de0fa7
EZ
3300 b->related_breakpoint->disposition = disp_del_at_next_stop;
3301 b->disposition = disp_del_at_next_stop;
c906108c
SS
3302
3303 return WP_DELETED;
3304 }
3305}
3306
18a18393
VP
3307/* Return true if it looks like target has stopped due to hitting
3308 breakpoint location BL. This function does not check if we
3309 should stop, only if BL explains the stop. */
3310static int
6c95b8df
PA
3311bpstat_check_location (const struct bp_location *bl,
3312 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3313{
3314 struct breakpoint *b = bl->owner;
3315
e8595ef6
SS
3316 /* By definition, the inferior does not report stops at
3317 tracepoints. */
3318 if (b->type == bp_tracepoint)
3319 return 0;
3320
18a18393
VP
3321 if (b->type != bp_watchpoint
3322 && b->type != bp_hardware_watchpoint
3323 && b->type != bp_read_watchpoint
3324 && b->type != bp_access_watchpoint
3325 && b->type != bp_hardware_breakpoint
fe798b75 3326 && b->type != bp_catchpoint) /* a non-watchpoint bp */
18a18393 3327 {
6c95b8df
PA
3328 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
3329 aspace, bp_addr))
18a18393
VP
3330 return 0;
3331 if (overlay_debugging /* unmapped overlay section */
3332 && section_is_overlay (bl->section)
3333 && !section_is_mapped (bl->section))
3334 return 0;
3335 }
3336
3337 /* Continuable hardware watchpoints are treated as non-existent if the
3338 reason we stopped wasn't a hardware watchpoint (we didn't stop on
3339 some data address). Otherwise gdb won't stop on a break instruction
3340 in the code (not from a breakpoint) when a hardware watchpoint has
3341 been defined. Also skip watchpoints which we know did not trigger
3342 (did not match the data address). */
3343
3344 if ((b->type == bp_hardware_watchpoint
3345 || b->type == bp_read_watchpoint
3346 || b->type == bp_access_watchpoint)
3347 && b->watchpoint_triggered == watch_triggered_no)
3348 return 0;
3349
3350 if (b->type == bp_hardware_breakpoint)
3351 {
3352 if (bl->address != bp_addr)
3353 return 0;
3354 if (overlay_debugging /* unmapped overlay section */
3355 && section_is_overlay (bl->section)
3356 && !section_is_mapped (bl->section))
3357 return 0;
3358 }
ce78b96d 3359
ce78b96d
JB
3360 if (b->type == bp_catchpoint)
3361 {
3362 gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
3363 if (!b->ops->breakpoint_hit (b))
3364 return 0;
3365 }
3366
18a18393
VP
3367 return 1;
3368}
3369
3370/* If BS refers to a watchpoint, determine if the watched values
3371 has actually changed, and we should stop. If not, set BS->stop
3372 to 0. */
3373static void
3374bpstat_check_watchpoint (bpstat bs)
3375{
3376 const struct bp_location *bl = bs->breakpoint_at;
3377 struct breakpoint *b = bl->owner;
3378
3379 if (b->type == bp_watchpoint
3380 || b->type == bp_read_watchpoint
3381 || b->type == bp_access_watchpoint
3382 || b->type == bp_hardware_watchpoint)
3383 {
3384 CORE_ADDR addr;
3385 struct value *v;
3386 int must_check_value = 0;
3387
3388 if (b->type == bp_watchpoint)
3389 /* For a software watchpoint, we must always check the
3390 watched value. */
3391 must_check_value = 1;
3392 else if (b->watchpoint_triggered == watch_triggered_yes)
3393 /* We have a hardware watchpoint (read, write, or access)
3394 and the target earlier reported an address watched by
3395 this watchpoint. */
3396 must_check_value = 1;
3397 else if (b->watchpoint_triggered == watch_triggered_unknown
3398 && b->type == bp_hardware_watchpoint)
3399 /* We were stopped by a hardware watchpoint, but the target could
3400 not report the data address. We must check the watchpoint's
3401 value. Access and read watchpoints are out of luck; without
3402 a data address, we can't figure it out. */
3403 must_check_value = 1;
3404
3405 if (must_check_value)
3406 {
3407 char *message = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3408 b->number);
3409 struct cleanup *cleanups = make_cleanup (xfree, message);
3410 int e = catch_errors (watchpoint_check, bs, message,
3411 RETURN_MASK_ALL);
3412 do_cleanups (cleanups);
3413 switch (e)
3414 {
3415 case WP_DELETED:
3416 /* We've already printed what needs to be printed. */
3417 bs->print_it = print_it_done;
3418 /* Stop. */
3419 break;
3420 case WP_VALUE_CHANGED:
3421 if (b->type == bp_read_watchpoint)
3422 {
3423 /* Don't stop: read watchpoints shouldn't fire if
3424 the value has changed. This is for targets
3425 which cannot set read-only watchpoints. */
3426 bs->print_it = print_it_noop;
3427 bs->stop = 0;
3428 }
3429 break;
3430 case WP_VALUE_NOT_CHANGED:
3431 if (b->type == bp_hardware_watchpoint
3432 || b->type == bp_watchpoint)
3433 {
3434 /* Don't stop: write watchpoints shouldn't fire if
3435 the value hasn't changed. */
3436 bs->print_it = print_it_noop;
3437 bs->stop = 0;
3438 }
3439 /* Stop. */
3440 break;
3441 default:
3442 /* Can't happen. */
3443 case 0:
3444 /* Error from catch_errors. */
3445 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
3446 if (b->related_breakpoint)
3447 b->related_breakpoint->disposition = disp_del_at_next_stop;
3448 b->disposition = disp_del_at_next_stop;
3449 /* We've already printed what needs to be printed. */
3450 bs->print_it = print_it_done;
3451 break;
3452 }
3453 }
3454 else /* must_check_value == 0 */
3455 {
3456 /* This is a case where some watchpoint(s) triggered, but
3457 not at the address of this watchpoint, or else no
3458 watchpoint triggered after all. So don't print
3459 anything for this watchpoint. */
3460 bs->print_it = print_it_noop;
3461 bs->stop = 0;
3462 }
3463 }
3464}
3465
3466
3467/* Check conditions (condition proper, frame, thread and ignore count)
3468 of breakpoint referred to by BS. If we should not stop for this
3469 breakpoint, set BS->stop to 0. */
3470static void
3471bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3472{
3473 int thread_id = pid_to_thread_id (ptid);
3474 const struct bp_location *bl = bs->breakpoint_at;
3475 struct breakpoint *b = bl->owner;
3476
3477 if (frame_id_p (b->frame_id)
edb3359d 3478 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
3479 bs->stop = 0;
3480 else if (bs->stop)
3481 {
3482 int value_is_zero = 0;
3483
3484 /* If this is a scope breakpoint, mark the associated
3485 watchpoint as triggered so that we will handle the
3486 out-of-scope event. We'll get to the watchpoint next
3487 iteration. */
3488 if (b->type == bp_watchpoint_scope)
3489 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
3490
3491 if (bl->cond && bl->owner->disposition != disp_del_at_next_stop)
3492 {
c5bc3a77
DJ
3493 /* We use value_mark and value_free_to_mark because it could
3494 be a long time before we return to the command level and
3495 call free_all_values. We can't call free_all_values
3496 because we might be in the middle of evaluating a
3497 function call. */
3498 struct value *mark = value_mark ();
3499
edb3359d
DJ
3500 /* Need to select the frame, with all that implies so that
3501 the conditions will have the right context. Because we
3502 use the frame, we will not see an inlined function's
3503 variables when we arrive at a breakpoint at the start
3504 of the inlined function; the current frame will be the
3505 call site. */
18a18393
VP
3506 select_frame (get_current_frame ());
3507 value_is_zero
3508 = catch_errors (breakpoint_cond_eval, (bl->cond),
3509 "Error in testing breakpoint condition:\n",
3510 RETURN_MASK_ALL);
3511 /* FIXME-someday, should give breakpoint # */
c5bc3a77 3512 value_free_to_mark (mark);
18a18393
VP
3513 }
3514 if (bl->cond && value_is_zero)
3515 {
3516 bs->stop = 0;
3517 }
3518 else if (b->thread != -1 && b->thread != thread_id)
3519 {
3520 bs->stop = 0;
3521 }
3522 else if (b->ignore_count > 0)
3523 {
3524 b->ignore_count--;
3525 annotate_ignore_count_change ();
3526 bs->stop = 0;
3527 /* Increase the hit count even though we don't
3528 stop. */
3529 ++(b->hit_count);
3530 }
3531 }
3532}
3533
3534
9709f61c 3535/* Get a bpstat associated with having just stopped at address
d983da9c 3536 BP_ADDR in thread PTID.
c906108c 3537
d983da9c 3538 Determine whether we stopped at a breakpoint, etc, or whether we
c906108c
SS
3539 don't understand this stop. Result is a chain of bpstat's such that:
3540
c5aa993b 3541 if we don't understand the stop, the result is a null pointer.
c906108c 3542
c5aa993b 3543 if we understand why we stopped, the result is not null.
c906108c 3544
c5aa993b
JM
3545 Each element of the chain refers to a particular breakpoint or
3546 watchpoint at which we have stopped. (We may have stopped for
3547 several reasons concurrently.)
c906108c 3548
c5aa993b
JM
3549 Each element of the chain has valid next, breakpoint_at,
3550 commands, FIXME??? fields. */
c906108c
SS
3551
3552bpstat
6c95b8df
PA
3553bpstat_stop_status (struct address_space *aspace,
3554 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 3555{
0d381245 3556 struct breakpoint *b = NULL;
876fa593 3557 struct bp_location *bl, **blp_tmp;
20874c92 3558 struct bp_location *loc;
c906108c
SS
3559 /* Root of the chain of bpstat's */
3560 struct bpstats root_bs[1];
3561 /* Pointer to the last thing in the chain currently. */
3562 bpstat bs = root_bs;
20874c92 3563 int ix;
429374b8 3564 int need_remove_insert;
c906108c 3565
429374b8
JK
3566 /* ALL_BP_LOCATIONS iteration would break across
3567 update_global_location_list possibly executed by
3568 bpstat_check_breakpoint_conditions's inferior call. */
c5aa993b 3569
429374b8
JK
3570 ALL_BREAKPOINTS (b)
3571 {
3572 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
3573 continue;
a5606eee 3574
429374b8
JK
3575 for (bl = b->loc; bl != NULL; bl = bl->next)
3576 {
3577 /* For hardware watchpoints, we look only at the first location.
3578 The watchpoint_check function will work on entire expression,
3579 not the individual locations. For read watchopints, the
3580 watchpoints_triggered function have checked all locations
3581 already. */
3582 if (b->type == bp_hardware_watchpoint && bl != b->loc)
3583 break;
18a18393 3584
429374b8
JK
3585 if (bl->shlib_disabled)
3586 continue;
c5aa993b 3587
429374b8
JK
3588 if (!bpstat_check_location (bl, aspace, bp_addr))
3589 continue;
c5aa993b 3590
429374b8 3591 /* Come here if it's a watchpoint, or if the break address matches */
c5aa993b 3592
429374b8 3593 bs = bpstat_alloc (bl, bs); /* Alloc a bpstat to explain stop */
c5aa993b 3594
429374b8
JK
3595 /* Assume we stop. Should we find watchpoint that is not actually
3596 triggered, or if condition of breakpoint is false, we'll reset
3597 'stop' to 0. */
3598 bs->stop = 1;
3599 bs->print = 1;
d983da9c 3600
429374b8
JK
3601 bpstat_check_watchpoint (bs);
3602 if (!bs->stop)
3603 continue;
18a18393 3604
429374b8
JK
3605 if (b->type == bp_thread_event || b->type == bp_overlay_event
3606 || b->type == bp_longjmp_master)
3607 /* We do not stop for these. */
3608 bs->stop = 0;
3609 else
3610 bpstat_check_breakpoint_conditions (bs, ptid);
3611
3612 if (bs->stop)
3613 {
3614 ++(b->hit_count);
c906108c 3615
429374b8
JK
3616 /* We will stop here */
3617 if (b->disposition == disp_disable)
3618 {
3619 if (b->enable_state != bp_permanent)
3620 b->enable_state = bp_disabled;
3621 update_global_location_list (0);
3622 }
3623 if (b->silent)
3624 bs->print = 0;
3625 bs->commands = b->commands;
3626 if (bs->commands
3627 && (strcmp ("silent", bs->commands->line) == 0
3628 || (xdb_commands && strcmp ("Q",
3629 bs->commands->line) == 0)))
3630 {
3631 bs->commands = bs->commands->next;
3632 bs->print = 0;
3633 }
3634 bs->commands = copy_command_lines (bs->commands);
3635 }
3636
3637 /* Print nothing for this entry if we dont stop or dont print. */
3638 if (bs->stop == 0 || bs->print == 0)
3639 bs->print_it = print_it_noop;
3640 }
3641 }
876fa593 3642
20874c92
VP
3643 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3644 {
6c95b8df
PA
3645 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
3646 aspace, bp_addr))
20874c92
VP
3647 {
3648 bs = bpstat_alloc (loc, bs);
3649 /* For hits of moribund locations, we should just proceed. */
3650 bs->stop = 0;
3651 bs->print = 0;
3652 bs->print_it = print_it_noop;
3653 }
3654 }
3655
c906108c
SS
3656 bs->next = NULL; /* Terminate the chain */
3657 bs = root_bs->next; /* Re-grab the head of the chain */
3658
d983da9c
DJ
3659 /* If we aren't stopping, the value of some hardware watchpoint may
3660 not have changed, but the intermediate memory locations we are
3661 watching may have. Don't bother if we're stopping; this will get
3662 done later. */
3663 for (bs = root_bs->next; bs != NULL; bs = bs->next)
3664 if (bs->stop)
3665 break;
3666
d832cb68 3667 need_remove_insert = 0;
d983da9c
DJ
3668 if (bs == NULL)
3669 for (bs = root_bs->next; bs != NULL; bs = bs->next)
3670 if (!bs->stop
20874c92 3671 && bs->breakpoint_at->owner
2d134ed3 3672 && is_hardware_watchpoint (bs->breakpoint_at->owner))
d983da9c 3673 {
2d134ed3
PA
3674 update_watchpoint (bs->breakpoint_at->owner, 0 /* don't reparse. */);
3675 /* Updating watchpoints invalidates bs->breakpoint_at.
3676 Prevent further code from trying to use it. */
a5606eee 3677 bs->breakpoint_at = NULL;
d832cb68 3678 need_remove_insert = 1;
d983da9c
DJ
3679 }
3680
d832cb68 3681 if (need_remove_insert)
2d134ed3 3682 update_global_location_list (1);
d832cb68 3683
d983da9c 3684 return root_bs->next;
c906108c
SS
3685}
3686\f
3687/* Tell what to do about this bpstat. */
3688struct bpstat_what
fba45db2 3689bpstat_what (bpstat bs)
c906108c
SS
3690{
3691 /* Classify each bpstat as one of the following. */
c5aa993b
JM
3692 enum class
3693 {
3694 /* This bpstat element has no effect on the main_action. */
3695 no_effect = 0,
3696
3697 /* There was a watchpoint, stop but don't print. */
3698 wp_silent,
c906108c 3699
c5aa993b
JM
3700 /* There was a watchpoint, stop and print. */
3701 wp_noisy,
c906108c 3702
c5aa993b
JM
3703 /* There was a breakpoint but we're not stopping. */
3704 bp_nostop,
c906108c 3705
c5aa993b
JM
3706 /* There was a breakpoint, stop but don't print. */
3707 bp_silent,
c906108c 3708
c5aa993b
JM
3709 /* There was a breakpoint, stop and print. */
3710 bp_noisy,
c906108c 3711
c5aa993b
JM
3712 /* We hit the longjmp breakpoint. */
3713 long_jump,
c906108c 3714
c5aa993b
JM
3715 /* We hit the longjmp_resume breakpoint. */
3716 long_resume,
c906108c 3717
c5aa993b
JM
3718 /* We hit the step_resume breakpoint. */
3719 step_resume,
c906108c 3720
c5aa993b
JM
3721 /* We hit the shared library event breakpoint. */
3722 shlib_event,
c906108c 3723
4efc6507
DE
3724 /* We hit the jit event breakpoint. */
3725 jit_event,
3726
c5aa993b
JM
3727 /* This is just used to count how many enums there are. */
3728 class_last
c906108c
SS
3729 };
3730
3731 /* Here is the table which drives this routine. So that we can
3732 format it pretty, we define some abbreviations for the
3733 enum bpstat_what codes. */
3734#define kc BPSTAT_WHAT_KEEP_CHECKING
3735#define ss BPSTAT_WHAT_STOP_SILENT
3736#define sn BPSTAT_WHAT_STOP_NOISY
3737#define sgl BPSTAT_WHAT_SINGLE
3738#define slr BPSTAT_WHAT_SET_LONGJMP_RESUME
3739#define clr BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
c906108c 3740#define sr BPSTAT_WHAT_STEP_RESUME
c906108c 3741#define shl BPSTAT_WHAT_CHECK_SHLIBS
4efc6507 3742#define jit BPSTAT_WHAT_CHECK_JIT
c906108c
SS
3743
3744/* "Can't happen." Might want to print an error message.
3745 abort() is not out of the question, but chances are GDB is just
3746 a bit confused, not unusable. */
3747#define err BPSTAT_WHAT_STOP_NOISY
3748
3749 /* Given an old action and a class, come up with a new action. */
3750 /* One interesting property of this table is that wp_silent is the same
3751 as bp_silent and wp_noisy is the same as bp_noisy. That is because
3752 after stopping, the check for whether to step over a breakpoint
3753 (BPSTAT_WHAT_SINGLE type stuff) is handled in proceed() without
53a5351d
JM
3754 reference to how we stopped. We retain separate wp_silent and
3755 bp_silent codes in case we want to change that someday.
43ff13b4
JM
3756
3757 Another possibly interesting property of this table is that
3758 there's a partial ordering, priority-like, of the actions. Once
3759 you've decided that some action is appropriate, you'll never go
3760 back and decide something of a lower priority is better. The
3761 ordering is:
3762
4efc6507
DE
3763 kc < jit clr sgl shl slr sn sr ss
3764 sgl < jit shl slr sn sr ss
3765 slr < jit err shl sn sr ss
3766 clr < jit err shl sn sr ss
3767 ss < jit shl sn sr
3768 sn < jit shl sr
3769 jit < shl sr
d81191fc 3770 shl < sr
4d5b2cd7 3771 sr <
c5aa993b 3772
43ff13b4
JM
3773 What I think this means is that we don't need a damned table
3774 here. If you just put the rows and columns in the right order,
3775 it'd look awfully regular. We could simply walk the bpstat list
3776 and choose the highest priority action we find, with a little
78b6a731 3777 logic to handle the 'err' cases. */
c906108c
SS
3778
3779 /* step_resume entries: a step resume breakpoint overrides another
3780 breakpoint of signal handling (see comment in wait_for_inferior
fcf70625 3781 at where we set the step_resume breakpoint). */
c906108c
SS
3782
3783 static const enum bpstat_what_main_action
c5aa993b
JM
3784 table[(int) class_last][(int) BPSTAT_WHAT_LAST] =
3785 {
3786 /* old action */
4efc6507
DE
3787 /* kc ss sn sgl slr clr sr shl jit */
3788/* no_effect */ {kc, ss, sn, sgl, slr, clr, sr, shl, jit},
3789/* wp_silent */ {ss, ss, sn, ss, ss, ss, sr, shl, jit},
3790/* wp_noisy */ {sn, sn, sn, sn, sn, sn, sr, shl, jit},
3791/* bp_nostop */ {sgl, ss, sn, sgl, slr, slr, sr, shl, jit},
3792/* bp_silent */ {ss, ss, sn, ss, ss, ss, sr, shl, jit},
3793/* bp_noisy */ {sn, sn, sn, sn, sn, sn, sr, shl, jit},
3794/* long_jump */ {slr, ss, sn, slr, slr, err, sr, shl, jit},
3795/* long_resume */ {clr, ss, sn, err, err, err, sr, shl, jit},
3796/* step_resume */ {sr, sr, sr, sr, sr, sr, sr, sr, sr },
3797/* shlib */ {shl, shl, shl, shl, shl, shl, sr, shl, shl},
3798/* jit_event */ {jit, jit, jit, jit, jit, jit, sr, jit, jit}
c5aa993b 3799 };
c906108c
SS
3800
3801#undef kc
3802#undef ss
3803#undef sn
3804#undef sgl
3805#undef slr
3806#undef clr
c906108c
SS
3807#undef err
3808#undef sr
3809#undef ts
3810#undef shl
4efc6507 3811#undef jit
c906108c
SS
3812 enum bpstat_what_main_action current_action = BPSTAT_WHAT_KEEP_CHECKING;
3813 struct bpstat_what retval;
3814
3815 retval.call_dummy = 0;
3816 for (; bs != NULL; bs = bs->next)
3817 {
3818 enum class bs_class = no_effect;
3819 if (bs->breakpoint_at == NULL)
3820 /* I suspect this can happen if it was a momentary breakpoint
3821 which has since been deleted. */
3822 continue;
20874c92
VP
3823 if (bs->breakpoint_at->owner == NULL)
3824 bs_class = bp_nostop;
3825 else
4f8d1dc6 3826 switch (bs->breakpoint_at->owner->type)
c906108c
SS
3827 {
3828 case bp_none:
3829 continue;
3830
3831 case bp_breakpoint:
3832 case bp_hardware_breakpoint:
3833 case bp_until:
3834 case bp_finish:
3835 if (bs->stop)
3836 {
3837 if (bs->print)
3838 bs_class = bp_noisy;
3839 else
3840 bs_class = bp_silent;
3841 }
3842 else
3843 bs_class = bp_nostop;
3844 break;
3845 case bp_watchpoint:
3846 case bp_hardware_watchpoint:
3847 case bp_read_watchpoint:
3848 case bp_access_watchpoint:
3849 if (bs->stop)
3850 {
3851 if (bs->print)
3852 bs_class = wp_noisy;
3853 else
3854 bs_class = wp_silent;
3855 }
3856 else
53a5351d
JM
3857 /* There was a watchpoint, but we're not stopping.
3858 This requires no further action. */
c906108c
SS
3859 bs_class = no_effect;
3860 break;
3861 case bp_longjmp:
3862 bs_class = long_jump;
3863 break;
3864 case bp_longjmp_resume:
3865 bs_class = long_resume;
3866 break;
3867 case bp_step_resume:
3868 if (bs->stop)
3869 {
3870 bs_class = step_resume;
3871 }
3872 else
3873 /* It is for the wrong frame. */
3874 bs_class = bp_nostop;
3875 break;
c906108c
SS
3876 case bp_watchpoint_scope:
3877 bs_class = bp_nostop;
3878 break;
c5aa993b
JM
3879 case bp_shlib_event:
3880 bs_class = shlib_event;
3881 break;
4efc6507
DE
3882 case bp_jit_event:
3883 bs_class = jit_event;
3884 break;
c4093a6a 3885 case bp_thread_event:
1900040c 3886 case bp_overlay_event:
0fd8e87f 3887 case bp_longjmp_master:
c4093a6a
JM
3888 bs_class = bp_nostop;
3889 break;
ce78b96d 3890 case bp_catchpoint:
c5aa993b
JM
3891 if (bs->stop)
3892 {
3893 if (bs->print)
3894 bs_class = bp_noisy;
3895 else
3896 bs_class = bp_silent;
3897 }
3898 else
53a5351d
JM
3899 /* There was a catchpoint, but we're not stopping.
3900 This requires no further action. */
c5aa993b
JM
3901 bs_class = no_effect;
3902 break;
c906108c 3903 case bp_call_dummy:
53a5351d
JM
3904 /* Make sure the action is stop (silent or noisy),
3905 so infrun.c pops the dummy frame. */
c906108c
SS
3906 bs_class = bp_silent;
3907 retval.call_dummy = 1;
3908 break;
1042e4c0
SS
3909 case bp_tracepoint:
3910 /* Tracepoint hits should not be reported back to GDB, and
3911 if one got through somehow, it should have been filtered
3912 out already. */
3913 internal_error (__FILE__, __LINE__,
3914 _("bpstat_what: bp_tracepoint encountered"));
3915 break;
c906108c 3916 }
c5aa993b 3917 current_action = table[(int) bs_class][(int) current_action];
c906108c
SS
3918 }
3919 retval.main_action = current_action;
3920 return retval;
3921}
3922
3923/* Nonzero if we should step constantly (e.g. watchpoints on machines
3924 without hardware support). This isn't related to a specific bpstat,
3925 just to things like whether watchpoints are set. */
3926
c5aa993b 3927int
fba45db2 3928bpstat_should_step (void)
c906108c
SS
3929{
3930 struct breakpoint *b;
3931 ALL_BREAKPOINTS (b)
717a8278 3932 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 3933 return 1;
c906108c
SS
3934 return 0;
3935}
3936
67822962
PA
3937int
3938bpstat_causes_stop (bpstat bs)
3939{
3940 for (; bs != NULL; bs = bs->next)
3941 if (bs->stop)
3942 return 1;
3943
3944 return 0;
3945}
3946
c906108c 3947\f
c5aa993b 3948
859825b8
JK
3949/* Print the LOC location out of the list of B->LOC locations. */
3950
0d381245
VP
3951static void print_breakpoint_location (struct breakpoint *b,
3952 struct bp_location *loc,
3953 char *wrap_indent,
3954 struct ui_stream *stb)
3955{
6c95b8df
PA
3956 struct cleanup *old_chain = save_current_program_space ();
3957
859825b8
JK
3958 if (loc != NULL && loc->shlib_disabled)
3959 loc = NULL;
3960
6c95b8df
PA
3961 if (loc != NULL)
3962 set_current_program_space (loc->pspace);
3963
859825b8 3964 if (b->source_file && loc)
0d381245
VP
3965 {
3966 struct symbol *sym
3967 = find_pc_sect_function (loc->address, loc->section);
3968 if (sym)
3969 {
3970 ui_out_text (uiout, "in ");
3971 ui_out_field_string (uiout, "func",
3972 SYMBOL_PRINT_NAME (sym));
3973 ui_out_wrap_hint (uiout, wrap_indent);
3974 ui_out_text (uiout, " at ");
3975 }
3976 ui_out_field_string (uiout, "file", b->source_file);
3977 ui_out_text (uiout, ":");
3978
3979 if (ui_out_is_mi_like_p (uiout))
3980 {
3981 struct symtab_and_line sal = find_pc_line (loc->address, 0);
3982 char *fullname = symtab_to_fullname (sal.symtab);
3983
3984 if (fullname)
3985 ui_out_field_string (uiout, "fullname", fullname);
3986 }
3987
3988 ui_out_field_int (uiout, "line", b->line_number);
3989 }
859825b8 3990 else if (loc)
0d381245 3991 {
22e722e1
DJ
3992 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
3993 demangle, "");
0d381245
VP
3994 ui_out_field_stream (uiout, "at", stb);
3995 }
859825b8
JK
3996 else
3997 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
3998
3999 do_cleanups (old_chain);
0d381245
VP
4000}
4001
c4093a6a 4002/* Print B to gdb_stdout. */
c906108c 4003static void
0d381245
VP
4004print_one_breakpoint_location (struct breakpoint *b,
4005 struct bp_location *loc,
4006 int loc_number,
a6d9a66e 4007 struct bp_location **last_loc,
6c95b8df
PA
4008 int print_address_bits,
4009 int allflag)
c906108c 4010{
52f0bd74
AC
4011 struct command_line *l;
4012 struct symbol *sym;
c4093a6a
JM
4013 struct ep_type_description
4014 {
4015 enum bptype type;
4016 char *description;
4017 };
4018 static struct ep_type_description bptypes[] =
c906108c 4019 {
c5aa993b
JM
4020 {bp_none, "?deleted?"},
4021 {bp_breakpoint, "breakpoint"},
c906108c 4022 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4023 {bp_until, "until"},
4024 {bp_finish, "finish"},
4025 {bp_watchpoint, "watchpoint"},
c906108c 4026 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4027 {bp_read_watchpoint, "read watchpoint"},
4028 {bp_access_watchpoint, "acc watchpoint"},
4029 {bp_longjmp, "longjmp"},
4030 {bp_longjmp_resume, "longjmp resume"},
4031 {bp_step_resume, "step resume"},
c5aa993b
JM
4032 {bp_watchpoint_scope, "watchpoint scope"},
4033 {bp_call_dummy, "call dummy"},
4034 {bp_shlib_event, "shlib events"},
c4093a6a 4035 {bp_thread_event, "thread events"},
1900040c 4036 {bp_overlay_event, "overlay events"},
0fd8e87f 4037 {bp_longjmp_master, "longjmp master"},
ce78b96d 4038 {bp_catchpoint, "catchpoint"},
1042e4c0 4039 {bp_tracepoint, "tracepoint"},
4efc6507 4040 {bp_jit_event, "jit events"},
c5aa993b 4041 };
c4093a6a 4042
c2c6d25f 4043 static char bpenables[] = "nynny";
c906108c 4044 char wrap_indent[80];
8b93c638
JM
4045 struct ui_stream *stb = ui_out_stream_new (uiout);
4046 struct cleanup *old_chain = make_cleanup_ui_out_stream_delete (stb);
3b31d625 4047 struct cleanup *bkpt_chain;
c906108c 4048
0d381245
VP
4049 int header_of_multiple = 0;
4050 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4051 struct value_print_options opts;
4052
4053 get_user_print_options (&opts);
0d381245
VP
4054
4055 gdb_assert (!loc || loc_number != 0);
4056 /* See comment in print_one_breakpoint concerning
4057 treatment of breakpoints with single disabled
4058 location. */
4059 if (loc == NULL
4060 && (b->loc != NULL
4061 && (b->loc->next != NULL || !b->loc->enabled)))
4062 header_of_multiple = 1;
4063 if (loc == NULL)
4064 loc = b->loc;
4065
c4093a6a 4066 annotate_record ();
3b31d625 4067 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
c4093a6a
JM
4068
4069 /* 1 */
4070 annotate_field (0);
0d381245
VP
4071 if (part_of_multiple)
4072 {
4073 char *formatted;
0c6773c1 4074 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4075 ui_out_field_string (uiout, "number", formatted);
4076 xfree (formatted);
4077 }
4078 else
4079 {
4080 ui_out_field_int (uiout, "number", b->number);
4081 }
c4093a6a
JM
4082
4083 /* 2 */
4084 annotate_field (1);
0d381245
VP
4085 if (part_of_multiple)
4086 ui_out_field_skip (uiout, "type");
4087 else
4088 {
4089 if (((int) b->type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4090 || ((int) b->type != bptypes[(int) b->type].type))
4091 internal_error (__FILE__, __LINE__,
4092 _("bptypes table does not describe type #%d."),
4093 (int) b->type);
4094 ui_out_field_string (uiout, "type", bptypes[(int) b->type].description);
4095 }
c4093a6a
JM
4096
4097 /* 3 */
4098 annotate_field (2);
0d381245
VP
4099 if (part_of_multiple)
4100 ui_out_field_skip (uiout, "disp");
4101 else
2cec12e5 4102 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4103
c4093a6a
JM
4104
4105 /* 4 */
4106 annotate_field (3);
0d381245 4107 if (part_of_multiple)
54e52265 4108 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4109 else
54e52265
VP
4110 ui_out_field_fmt (uiout, "enabled", "%c",
4111 bpenables[(int) b->enable_state]);
4112 ui_out_spaces (uiout, 2);
0d381245 4113
c4093a6a
JM
4114
4115 /* 5 and 6 */
4116 strcpy (wrap_indent, " ");
79a45b7d 4117 if (opts.addressprint)
75ac9d7b 4118 {
a6d9a66e 4119 if (print_address_bits <= 32)
75ac9d7b
MS
4120 strcat (wrap_indent, " ");
4121 else
4122 strcat (wrap_indent, " ");
4123 }
c906108c 4124
3086aeae 4125 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245
VP
4126 {
4127 /* Although the print_one can possibly print
4128 all locations, calling it here is not likely
4129 to get any nice result. So, make sure there's
4130 just one location. */
4131 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4132 b->ops->print_one (b, last_loc);
0d381245 4133 }
3086aeae
DJ
4134 else
4135 switch (b->type)
4136 {
4137 case bp_none:
4138 internal_error (__FILE__, __LINE__,
e2e0b3e5 4139 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4140 break;
c906108c 4141
3086aeae
DJ
4142 case bp_watchpoint:
4143 case bp_hardware_watchpoint:
4144 case bp_read_watchpoint:
4145 case bp_access_watchpoint:
4146 /* Field 4, the address, is omitted (which makes the columns
4147 not line up too nicely with the headers, but the effect
4148 is relatively readable). */
79a45b7d 4149 if (opts.addressprint)
3086aeae
DJ
4150 ui_out_field_skip (uiout, "addr");
4151 annotate_field (5);
fa8a61dc 4152 ui_out_field_string (uiout, "what", b->exp_string);
3086aeae
DJ
4153 break;
4154
3086aeae
DJ
4155 case bp_breakpoint:
4156 case bp_hardware_breakpoint:
4157 case bp_until:
4158 case bp_finish:
4159 case bp_longjmp:
4160 case bp_longjmp_resume:
4161 case bp_step_resume:
3086aeae
DJ
4162 case bp_watchpoint_scope:
4163 case bp_call_dummy:
4164 case bp_shlib_event:
4165 case bp_thread_event:
4166 case bp_overlay_event:
0fd8e87f 4167 case bp_longjmp_master:
1042e4c0 4168 case bp_tracepoint:
4efc6507 4169 case bp_jit_event:
79a45b7d 4170 if (opts.addressprint)
3086aeae
DJ
4171 {
4172 annotate_field (4);
54e52265 4173 if (header_of_multiple)
0d381245 4174 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4175 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4176 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4177 else
5af949e3
UW
4178 ui_out_field_core_addr (uiout, "addr",
4179 loc->gdbarch, loc->address);
3086aeae
DJ
4180 }
4181 annotate_field (5);
0d381245
VP
4182 if (!header_of_multiple)
4183 print_breakpoint_location (b, loc, wrap_indent, stb);
4184 if (b->loc)
a6d9a66e 4185 *last_loc = b->loc;
3086aeae
DJ
4186 break;
4187 }
c906108c 4188
6c95b8df
PA
4189
4190 /* For backward compatibility, don't display inferiors unless there
4191 are several. */
4192 if (loc != NULL
4193 && !header_of_multiple
4194 && (allflag
4195 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4196 && (number_of_program_spaces () > 1
4197 || number_of_inferiors () > 1)
4198 && loc->owner->type != bp_catchpoint)))
4199 {
4200 struct inferior *inf;
4201 int first = 1;
4202
4203 for (inf = inferior_list; inf != NULL; inf = inf->next)
4204 {
4205 if (inf->pspace == loc->pspace)
4206 {
4207 if (first)
4208 {
4209 first = 0;
4210 ui_out_text (uiout, " inf ");
4211 }
4212 else
4213 ui_out_text (uiout, ", ");
4214 ui_out_text (uiout, plongest (inf->num));
4215 }
4216 }
4217 }
4218
4a306c9a 4219 if (!part_of_multiple)
c4093a6a 4220 {
4a306c9a
JB
4221 if (b->thread != -1)
4222 {
4223 /* FIXME: This seems to be redundant and lost here; see the
4224 "stop only in" line a little further down. */
4225 ui_out_text (uiout, " thread ");
4226 ui_out_field_int (uiout, "thread", b->thread);
4227 }
4228 else if (b->task != 0)
4229 {
4230 ui_out_text (uiout, " task ");
4231 ui_out_field_int (uiout, "task", b->task);
4232 }
c4093a6a
JM
4233 }
4234
8b93c638 4235 ui_out_text (uiout, "\n");
c4093a6a 4236
0d381245 4237 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4238 {
4239 annotate_field (6);
8b93c638 4240 ui_out_text (uiout, "\tstop only in stack frame at ");
818dd999
AC
4241 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
4242 the frame ID. */
5af949e3
UW
4243 ui_out_field_core_addr (uiout, "frame",
4244 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4245 ui_out_text (uiout, "\n");
c4093a6a
JM
4246 }
4247
0d381245 4248 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
c4093a6a 4249 {
f7f9143b
JB
4250 /* We do not print the condition for Ada exception catchpoints
4251 because the condition is an internal implementation detail
4252 that we do not want to expose to the user. */
c4093a6a 4253 annotate_field (7);
1042e4c0
SS
4254 if (b->type == bp_tracepoint)
4255 ui_out_text (uiout, "\ttrace only if ");
4256 else
4257 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4258 ui_out_field_string (uiout, "cond", b->cond_string);
4259 ui_out_text (uiout, "\n");
4260 }
4261
0d381245 4262 if (!part_of_multiple && b->thread != -1)
c4093a6a
JM
4263 {
4264 /* FIXME should make an annotation for this */
8b93c638
JM
4265 ui_out_text (uiout, "\tstop only in thread ");
4266 ui_out_field_int (uiout, "thread", b->thread);
4267 ui_out_text (uiout, "\n");
c4093a6a
JM
4268 }
4269
63c715c6 4270 if (!part_of_multiple && b->hit_count)
c4093a6a
JM
4271 {
4272 /* FIXME should make an annotation for this */
8b93c638
JM
4273 if (ep_is_catchpoint (b))
4274 ui_out_text (uiout, "\tcatchpoint");
4275 else
4276 ui_out_text (uiout, "\tbreakpoint");
4277 ui_out_text (uiout, " already hit ");
4278 ui_out_field_int (uiout, "times", b->hit_count);
4279 if (b->hit_count == 1)
4280 ui_out_text (uiout, " time\n");
4281 else
4282 ui_out_text (uiout, " times\n");
c4093a6a
JM
4283 }
4284
fb40c209
AC
4285 /* Output the count also if it is zero, but only if this is
4286 mi. FIXME: Should have a better test for this. */
9dc5e2a9 4287 if (ui_out_is_mi_like_p (uiout))
63c715c6 4288 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4289 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4290
0d381245 4291 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4292 {
4293 annotate_field (8);
8b93c638
JM
4294 ui_out_text (uiout, "\tignore next ");
4295 ui_out_field_int (uiout, "ignore", b->ignore_count);
4296 ui_out_text (uiout, " hits\n");
c4093a6a 4297 }
059fb39f
PM
4298
4299 l = b->commands;
4300 if (!part_of_multiple && l)
c4093a6a 4301 {
3b31d625
EZ
4302 struct cleanup *script_chain;
4303
c4093a6a 4304 annotate_field (9);
3b31d625 4305 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4306 print_command_lines (uiout, l, 4);
3b31d625 4307 do_cleanups (script_chain);
c4093a6a 4308 }
d24317b4 4309
1042e4c0
SS
4310 if (!part_of_multiple && b->pass_count)
4311 {
4312 annotate_field (10);
4313 ui_out_text (uiout, "\tpass count ");
4314 ui_out_field_int (uiout, "pass", b->pass_count);
4315 ui_out_text (uiout, " \n");
4316 }
4317
4318 if (!part_of_multiple && b->step_count)
4319 {
4320 annotate_field (11);
4321 ui_out_text (uiout, "\tstep count ");
4322 ui_out_field_int (uiout, "step", b->step_count);
4323 ui_out_text (uiout, " \n");
4324 }
4325
4326 if (!part_of_multiple && b->actions)
4327 {
4328 struct action_line *action;
4329 annotate_field (12);
4330 for (action = b->actions; action; action = action->next)
4331 {
4332 ui_out_text (uiout, " A\t");
4333 ui_out_text (uiout, action->action);
4334 ui_out_text (uiout, "\n");
4335 }
4336 }
4337
d24317b4
VP
4338 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4339 {
4340 if (b->addr_string)
4341 ui_out_field_string (uiout, "original-location", b->addr_string);
4342 else if (b->exp_string)
4343 ui_out_field_string (uiout, "original-location", b->exp_string);
4344 }
4345
3b31d625 4346 do_cleanups (bkpt_chain);
8b93c638 4347 do_cleanups (old_chain);
c4093a6a 4348}
c5aa993b 4349
0d381245
VP
4350static void
4351print_one_breakpoint (struct breakpoint *b,
6c95b8df
PA
4352 struct bp_location **last_loc, int print_address_bits,
4353 int allflag)
0d381245 4354{
6c95b8df
PA
4355 print_one_breakpoint_location (b, NULL, 0, last_loc,
4356 print_address_bits, allflag);
0d381245
VP
4357
4358 /* If this breakpoint has custom print function,
4359 it's already printed. Otherwise, print individual
4360 locations, if any. */
4361 if (b->ops == NULL || b->ops->print_one == NULL)
4362 {
4363 /* If breakpoint has a single location that is
4364 disabled, we print it as if it had
4365 several locations, since otherwise it's hard to
4366 represent "breakpoint enabled, location disabled"
a5606eee
VP
4367 situation.
4368 Note that while hardware watchpoints have
4369 several locations internally, that's no a property
4370 exposed to user. */
0d381245 4371 if (b->loc
a5606eee 4372 && !is_hardware_watchpoint (b)
0d381245 4373 && (b->loc->next || !b->loc->enabled)
a5606eee 4374 && !ui_out_is_mi_like_p (uiout))
0d381245
VP
4375 {
4376 struct bp_location *loc;
4377 int n = 1;
4378 for (loc = b->loc; loc; loc = loc->next, ++n)
a6d9a66e 4379 print_one_breakpoint_location (b, loc, n, last_loc,
6c95b8df 4380 print_address_bits, allflag);
0d381245
VP
4381 }
4382 }
4383}
4384
a6d9a66e
UW
4385static int
4386breakpoint_address_bits (struct breakpoint *b)
4387{
4388 int print_address_bits = 0;
4389 struct bp_location *loc;
4390
4391 for (loc = b->loc; loc; loc = loc->next)
4392 {
c561c276 4393 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
4394 if (addr_bit > print_address_bits)
4395 print_address_bits = addr_bit;
4396 }
4397
4398 return print_address_bits;
4399}
0d381245 4400
c4093a6a
JM
4401struct captured_breakpoint_query_args
4402 {
4403 int bnum;
4404 };
c5aa993b 4405
c4093a6a 4406static int
2b65245e 4407do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
4408{
4409 struct captured_breakpoint_query_args *args = data;
52f0bd74 4410 struct breakpoint *b;
a6d9a66e 4411 struct bp_location *dummy_loc = NULL;
c4093a6a
JM
4412 ALL_BREAKPOINTS (b)
4413 {
4414 if (args->bnum == b->number)
c5aa993b 4415 {
a6d9a66e 4416 int print_address_bits = breakpoint_address_bits (b);
6c95b8df 4417 print_one_breakpoint (b, &dummy_loc, print_address_bits, 0);
c4093a6a 4418 return GDB_RC_OK;
c5aa993b 4419 }
c4093a6a
JM
4420 }
4421 return GDB_RC_NONE;
4422}
c5aa993b 4423
c4093a6a 4424enum gdb_rc
ce43223b 4425gdb_breakpoint_query (struct ui_out *uiout, int bnum, char **error_message)
c4093a6a
JM
4426{
4427 struct captured_breakpoint_query_args args;
4428 args.bnum = bnum;
4429 /* For the moment we don't trust print_one_breakpoint() to not throw
4430 an error. */
b0b13bb4
DJ
4431 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
4432 error_message, RETURN_MASK_ALL) < 0)
4433 return GDB_RC_FAIL;
4434 else
4435 return GDB_RC_OK;
c4093a6a 4436}
c5aa993b 4437
7f3b0473
AC
4438/* Return non-zero if B is user settable (breakpoints, watchpoints,
4439 catchpoints, et.al.). */
4440
4441static int
4442user_settable_breakpoint (const struct breakpoint *b)
4443{
4444 return (b->type == bp_breakpoint
ce78b96d 4445 || b->type == bp_catchpoint
7f3b0473 4446 || b->type == bp_hardware_breakpoint
1042e4c0 4447 || b->type == bp_tracepoint
7f3b0473
AC
4448 || b->type == bp_watchpoint
4449 || b->type == bp_read_watchpoint
4450 || b->type == bp_access_watchpoint
4451 || b->type == bp_hardware_watchpoint);
4452}
4453
4454/* Print information on user settable breakpoint (watchpoint, etc)
4455 number BNUM. If BNUM is -1 print all user settable breakpoints.
4456 If ALLFLAG is non-zero, include non- user settable breakpoints. */
c906108c 4457
c4093a6a 4458static void
fba45db2 4459breakpoint_1 (int bnum, int allflag)
c4093a6a 4460{
52f0bd74 4461 struct breakpoint *b;
a6d9a66e 4462 struct bp_location *last_loc = NULL;
7f3b0473 4463 int nr_printable_breakpoints;
3b31d625 4464 struct cleanup *bkpttbl_chain;
79a45b7d 4465 struct value_print_options opts;
a6d9a66e 4466 int print_address_bits = 0;
c4093a6a 4467
79a45b7d
TT
4468 get_user_print_options (&opts);
4469
a6d9a66e
UW
4470 /* Compute the number of rows in the table, as well as the
4471 size required for address fields. */
7f3b0473
AC
4472 nr_printable_breakpoints = 0;
4473 ALL_BREAKPOINTS (b)
4474 if (bnum == -1
4475 || bnum == b->number)
4476 {
4477 if (allflag || user_settable_breakpoint (b))
a6d9a66e
UW
4478 {
4479 int addr_bit = breakpoint_address_bits (b);
4480 if (addr_bit > print_address_bits)
4481 print_address_bits = addr_bit;
4482
4483 nr_printable_breakpoints++;
4484 }
7f3b0473
AC
4485 }
4486
79a45b7d 4487 if (opts.addressprint)
3b31d625
EZ
4488 bkpttbl_chain
4489 = make_cleanup_ui_out_table_begin_end (uiout, 6, nr_printable_breakpoints,
4490 "BreakpointTable");
8b93c638 4491 else
3b31d625
EZ
4492 bkpttbl_chain
4493 = make_cleanup_ui_out_table_begin_end (uiout, 5, nr_printable_breakpoints,
4494 "BreakpointTable");
8b93c638 4495
7f3b0473 4496 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
4497 annotate_breakpoints_headers ();
4498 if (nr_printable_breakpoints > 0)
4499 annotate_field (0);
0d381245 4500 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
4501 if (nr_printable_breakpoints > 0)
4502 annotate_field (1);
4503 ui_out_table_header (uiout, 14, ui_left, "type", "Type"); /* 2 */
4504 if (nr_printable_breakpoints > 0)
4505 annotate_field (2);
4506 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
4507 if (nr_printable_breakpoints > 0)
4508 annotate_field (3);
54e52265 4509 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 4510 if (opts.addressprint)
7f3b0473 4511 {
d7faa9e7
AC
4512 if (nr_printable_breakpoints > 0)
4513 annotate_field (4);
a6d9a66e 4514 if (print_address_bits <= 32)
b25959ec 4515 ui_out_table_header (uiout, 10, ui_left, "addr", "Address");/* 5 */
7f3b0473 4516 else
b25959ec 4517 ui_out_table_header (uiout, 18, ui_left, "addr", "Address");/* 5 */
7f3b0473 4518 }
d7faa9e7
AC
4519 if (nr_printable_breakpoints > 0)
4520 annotate_field (5);
4521 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
4522 ui_out_table_body (uiout);
4523 if (nr_printable_breakpoints > 0)
4524 annotate_breakpoints_table ();
7f3b0473 4525
c4093a6a
JM
4526 ALL_BREAKPOINTS (b)
4527 if (bnum == -1
4528 || bnum == b->number)
4529 {
4530 /* We only print out user settable breakpoints unless the
4531 allflag is set. */
7f3b0473 4532 if (allflag || user_settable_breakpoint (b))
6c95b8df 4533 print_one_breakpoint (b, &last_loc, print_address_bits, allflag);
c4093a6a
JM
4534 }
4535
3b31d625 4536 do_cleanups (bkpttbl_chain);
698384cd 4537
7f3b0473 4538 if (nr_printable_breakpoints == 0)
c906108c 4539 {
8b93c638
JM
4540 if (bnum == -1)
4541 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
4542 else
4543 ui_out_message (uiout, 0, "No breakpoint or watchpoint number %d.\n",
4544 bnum);
c906108c
SS
4545 }
4546 else
c4093a6a 4547 {
a6d9a66e
UW
4548 if (last_loc && !server_command)
4549 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 4550 }
c906108c 4551
c4093a6a
JM
4552 /* FIXME? Should this be moved up so that it is only called when
4553 there have been breakpoints? */
c906108c
SS
4554 annotate_breakpoints_table_end ();
4555}
4556
c906108c 4557static void
fba45db2 4558breakpoints_info (char *bnum_exp, int from_tty)
c906108c
SS
4559{
4560 int bnum = -1;
4561
4562 if (bnum_exp)
bb518678 4563 bnum = parse_and_eval_long (bnum_exp);
c906108c
SS
4564
4565 breakpoint_1 (bnum, 0);
4566}
4567
7a292a7a 4568static void
fba45db2 4569maintenance_info_breakpoints (char *bnum_exp, int from_tty)
c906108c
SS
4570{
4571 int bnum = -1;
4572
4573 if (bnum_exp)
bb518678 4574 bnum = parse_and_eval_long (bnum_exp);
c906108c
SS
4575
4576 breakpoint_1 (bnum, 1);
4577}
4578
0d381245 4579static int
714835d5 4580breakpoint_has_pc (struct breakpoint *b,
6c95b8df 4581 struct program_space *pspace,
714835d5 4582 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
4583{
4584 struct bp_location *bl = b->loc;
4585 for (; bl; bl = bl->next)
4586 {
6c95b8df
PA
4587 if (bl->pspace == pspace
4588 && bl->address == pc
0d381245
VP
4589 && (!overlay_debugging || bl->section == section))
4590 return 1;
4591 }
4592 return 0;
4593}
4594
6c95b8df
PA
4595/* Print a message describing any breakpoints set at PC. This
4596 concerns with logical breakpoints, so we match program spaces, not
4597 address spaces. */
c906108c
SS
4598
4599static void
6c95b8df
PA
4600describe_other_breakpoints (struct gdbarch *gdbarch,
4601 struct program_space *pspace, CORE_ADDR pc,
5af949e3 4602 struct obj_section *section, int thread)
c906108c 4603{
52f0bd74
AC
4604 int others = 0;
4605 struct breakpoint *b;
c906108c
SS
4606
4607 ALL_BREAKPOINTS (b)
6c95b8df 4608 others += breakpoint_has_pc (b, pspace, pc, section);
c906108c
SS
4609 if (others > 0)
4610 {
a3f17187
AC
4611 if (others == 1)
4612 printf_filtered (_("Note: breakpoint "));
4613 else /* if (others == ???) */
4614 printf_filtered (_("Note: breakpoints "));
c906108c 4615 ALL_BREAKPOINTS (b)
6c95b8df 4616 if (breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
4617 {
4618 others--;
4619 printf_filtered ("%d", b->number);
4620 if (b->thread == -1 && thread != -1)
4621 printf_filtered (" (all threads)");
4622 else if (b->thread != -1)
4623 printf_filtered (" (thread %d)", b->thread);
4624 printf_filtered ("%s%s ",
059fb39f 4625 ((b->enable_state == bp_disabled
8bea4e01
UW
4626 || b->enable_state == bp_call_disabled
4627 || b->enable_state == bp_startup_disabled)
0d381245
VP
4628 ? " (disabled)"
4629 : b->enable_state == bp_permanent
4630 ? " (permanent)"
4631 : ""),
4632 (others > 1) ? ","
4633 : ((others == 1) ? " and" : ""));
4634 }
a3f17187 4635 printf_filtered (_("also set at pc "));
5af949e3 4636 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
4637 printf_filtered (".\n");
4638 }
4639}
4640\f
4641/* Set the default place to put a breakpoint
4642 for the `break' command with no arguments. */
4643
4644void
6c95b8df
PA
4645set_default_breakpoint (int valid, struct program_space *pspace,
4646 CORE_ADDR addr, struct symtab *symtab,
fba45db2 4647 int line)
c906108c
SS
4648{
4649 default_breakpoint_valid = valid;
6c95b8df 4650 default_breakpoint_pspace = pspace;
c906108c
SS
4651 default_breakpoint_address = addr;
4652 default_breakpoint_symtab = symtab;
4653 default_breakpoint_line = line;
4654}
4655
e4f237da
KB
4656/* Return true iff it is meaningful to use the address member of
4657 BPT. For some breakpoint types, the address member is irrelevant
4658 and it makes no sense to attempt to compare it to other addresses
4659 (or use it for any other purpose either).
4660
4661 More specifically, each of the following breakpoint types will always
876fa593
JK
4662 have a zero valued address and we don't want to mark breakpoints of any of
4663 these types to be a duplicate of an actual breakpoint at address zero:
e4f237da
KB
4664
4665 bp_watchpoint
2d134ed3
PA
4666 bp_catchpoint
4667
4668*/
e4f237da
KB
4669
4670static int
4671breakpoint_address_is_meaningful (struct breakpoint *bpt)
4672{
4673 enum bptype type = bpt->type;
4674
2d134ed3
PA
4675 return (type != bp_watchpoint && type != bp_catchpoint);
4676}
4677
4678/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
4679 true if LOC1 and LOC2 represent the same watchpoint location. */
4680
4681static int
4682watchpoint_locations_match (struct bp_location *loc1, struct bp_location *loc2)
4683{
4684 return (loc1->owner->type == loc2->owner->type
4685 && loc1->pspace->aspace == loc2->pspace->aspace
4686 && loc1->address == loc2->address
4687 && loc1->length == loc2->length);
e4f237da
KB
4688}
4689
6c95b8df
PA
4690/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
4691 same breakpoint location. In most targets, this can only be true
4692 if ASPACE1 matches ASPACE2. On targets that have global
4693 breakpoints, the address space doesn't really matter. */
4694
4695static int
4696breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
4697 struct address_space *aspace2, CORE_ADDR addr2)
4698{
4699 return ((gdbarch_has_global_breakpoints (target_gdbarch)
4700 || aspace1 == aspace2)
4701 && addr1 == addr2);
4702}
4703
2d134ed3
PA
4704/* Assuming LOC1 and LOC2's types' have meaningful target addresses
4705 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
4706 represent the same location. */
4707
4708static int
4709breakpoint_locations_match (struct bp_location *loc1, struct bp_location *loc2)
4710{
4711 int hw_point1 = is_hardware_watchpoint (loc1->owner);
4712 int hw_point2 = is_hardware_watchpoint (loc2->owner);
4713
4714 if (hw_point1 != hw_point2)
4715 return 0;
4716 else if (hw_point1)
4717 return watchpoint_locations_match (loc1, loc2);
4718 else
4719 return breakpoint_address_match (loc1->pspace->aspace, loc1->address,
4720 loc2->pspace->aspace, loc2->address);
4721}
4722
76897487
KB
4723static void
4724breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
4725 int bnum, int have_bnum)
4726{
4727 char astr1[40];
4728 char astr2[40];
4729
bb599908
PH
4730 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
4731 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 4732 if (have_bnum)
8a3fe4f8 4733 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
4734 bnum, astr1, astr2);
4735 else
8a3fe4f8 4736 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
4737}
4738
4739/* Adjust a breakpoint's address to account for architectural constraints
4740 on breakpoint placement. Return the adjusted address. Note: Very
4741 few targets require this kind of adjustment. For most targets,
4742 this function is simply the identity function. */
4743
4744static CORE_ADDR
a6d9a66e
UW
4745adjust_breakpoint_address (struct gdbarch *gdbarch,
4746 CORE_ADDR bpaddr, enum bptype bptype)
76897487 4747{
a6d9a66e 4748 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
4749 {
4750 /* Very few targets need any kind of breakpoint adjustment. */
4751 return bpaddr;
4752 }
88f7da05
KB
4753 else if (bptype == bp_watchpoint
4754 || bptype == bp_hardware_watchpoint
4755 || bptype == bp_read_watchpoint
4756 || bptype == bp_access_watchpoint
fe798b75 4757 || bptype == bp_catchpoint)
88f7da05
KB
4758 {
4759 /* Watchpoints and the various bp_catch_* eventpoints should not
4760 have their addresses modified. */
4761 return bpaddr;
4762 }
76897487
KB
4763 else
4764 {
4765 CORE_ADDR adjusted_bpaddr;
4766
4767 /* Some targets have architectural constraints on the placement
4768 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 4769 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
4770
4771 /* An adjusted breakpoint address can significantly alter
4772 a user's expectations. Print a warning if an adjustment
4773 is required. */
4774 if (adjusted_bpaddr != bpaddr)
4775 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
4776
4777 return adjusted_bpaddr;
4778 }
4779}
4780
7cc221ef
DJ
4781/* Allocate a struct bp_location. */
4782
26bb91f3 4783static struct bp_location *
39d61571 4784allocate_bp_location (struct breakpoint *bpt)
7cc221ef
DJ
4785{
4786 struct bp_location *loc, *loc_p;
4787
4788 loc = xmalloc (sizeof (struct bp_location));
4789 memset (loc, 0, sizeof (*loc));
4790
e049a4b5 4791 loc->owner = bpt;
511a6cd4 4792 loc->cond = NULL;
0d381245
VP
4793 loc->shlib_disabled = 0;
4794 loc->enabled = 1;
e049a4b5 4795
39d61571 4796 switch (bpt->type)
e049a4b5
DJ
4797 {
4798 case bp_breakpoint:
1042e4c0 4799 case bp_tracepoint:
e049a4b5
DJ
4800 case bp_until:
4801 case bp_finish:
4802 case bp_longjmp:
4803 case bp_longjmp_resume:
4804 case bp_step_resume:
e049a4b5
DJ
4805 case bp_watchpoint_scope:
4806 case bp_call_dummy:
4807 case bp_shlib_event:
4808 case bp_thread_event:
4809 case bp_overlay_event:
4efc6507 4810 case bp_jit_event:
0fd8e87f 4811 case bp_longjmp_master:
e049a4b5
DJ
4812 loc->loc_type = bp_loc_software_breakpoint;
4813 break;
4814 case bp_hardware_breakpoint:
4815 loc->loc_type = bp_loc_hardware_breakpoint;
4816 break;
4817 case bp_hardware_watchpoint:
4818 case bp_read_watchpoint:
4819 case bp_access_watchpoint:
4820 loc->loc_type = bp_loc_hardware_watchpoint;
4821 break;
4822 case bp_watchpoint:
ce78b96d 4823 case bp_catchpoint:
e049a4b5
DJ
4824 loc->loc_type = bp_loc_other;
4825 break;
4826 default:
e2e0b3e5 4827 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
4828 }
4829
7cc221ef
DJ
4830 return loc;
4831}
4832
fe3f5fa8
VP
4833static void free_bp_location (struct bp_location *loc)
4834{
4835 if (loc->cond)
4836 xfree (loc->cond);
74960c60
VP
4837
4838 if (loc->function_name)
4839 xfree (loc->function_name);
4840
fe3f5fa8
VP
4841 xfree (loc);
4842}
4843
0d381245
VP
4844/* Helper to set_raw_breakpoint below. Creates a breakpoint
4845 that has type BPTYPE and has no locations as yet. */
63c252f8 4846/* This function is used in gdbtk sources and thus can not be made static. */
c906108c 4847
c40e75cd 4848static struct breakpoint *
a6d9a66e
UW
4849set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
4850 enum bptype bptype)
c906108c 4851{
52f0bd74 4852 struct breakpoint *b, *b1;
c906108c
SS
4853
4854 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
4855 memset (b, 0, sizeof (*b));
2219d63c 4856
4d28f7a8 4857 b->type = bptype;
a6d9a66e 4858 b->gdbarch = gdbarch;
c906108c
SS
4859 b->language = current_language->la_language;
4860 b->input_radix = input_radix;
4861 b->thread = -1;
b5de0fa7 4862 b->enable_state = bp_enabled;
c906108c
SS
4863 b->next = 0;
4864 b->silent = 0;
4865 b->ignore_count = 0;
4866 b->commands = NULL;
818dd999 4867 b->frame_id = null_frame_id;
3a3e9ee3 4868 b->forked_inferior_pid = null_ptid;
c906108c 4869 b->exec_pathname = NULL;
a96d9b2e 4870 b->syscalls_to_be_caught = NULL;
3086aeae 4871 b->ops = NULL;
0d381245 4872 b->condition_not_parsed = 0;
c906108c
SS
4873
4874 /* Add this breakpoint to the end of the chain
4875 so that a list of breakpoints will come out in order
4876 of increasing numbers. */
4877
4878 b1 = breakpoint_chain;
4879 if (b1 == 0)
4880 breakpoint_chain = b;
4881 else
4882 {
4883 while (b1->next)
4884 b1 = b1->next;
4885 b1->next = b;
4886 }
0d381245
VP
4887 return b;
4888}
4889
4890/* Initialize loc->function_name. */
4891static void
4892set_breakpoint_location_function (struct bp_location *loc)
4893{
4894 if (loc->owner->type == bp_breakpoint
1042e4c0
SS
4895 || loc->owner->type == bp_hardware_breakpoint
4896 || loc->owner->type == bp_tracepoint)
0d381245
VP
4897 {
4898 find_pc_partial_function (loc->address, &(loc->function_name),
4899 NULL, NULL);
4900 if (loc->function_name)
4901 loc->function_name = xstrdup (loc->function_name);
4902 }
4903}
4904
a6d9a66e
UW
4905/* Attempt to determine architecture of location identified by SAL. */
4906static struct gdbarch *
4907get_sal_arch (struct symtab_and_line sal)
4908{
4909 if (sal.section)
4910 return get_objfile_arch (sal.section->objfile);
4911 if (sal.symtab)
4912 return get_objfile_arch (sal.symtab->objfile);
4913
4914 return NULL;
4915}
4916
0d381245
VP
4917/* set_raw_breakpoint is a low level routine for allocating and
4918 partially initializing a breakpoint of type BPTYPE. The newly
4919 created breakpoint's address, section, source file name, and line
4920 number are provided by SAL. The newly created and partially
4921 initialized breakpoint is added to the breakpoint chain and
4922 is also returned as the value of this function.
4923
4924 It is expected that the caller will complete the initialization of
4925 the newly created breakpoint struct as well as output any status
4926 information regarding the creation of a new breakpoint. In
4927 particular, set_raw_breakpoint does NOT set the breakpoint
4928 number! Care should be taken to not allow an error to occur
4929 prior to completing the initialization of the breakpoint. If this
4930 should happen, a bogus breakpoint will be left on the chain. */
4931
63c252f8 4932struct breakpoint *
a6d9a66e
UW
4933set_raw_breakpoint (struct gdbarch *gdbarch,
4934 struct symtab_and_line sal, enum bptype bptype)
0d381245 4935{
a6d9a66e 4936 struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch, bptype);
0d381245 4937 CORE_ADDR adjusted_address;
a6d9a66e
UW
4938 struct gdbarch *loc_gdbarch;
4939
4940 loc_gdbarch = get_sal_arch (sal);
4941 if (!loc_gdbarch)
4942 loc_gdbarch = b->gdbarch;
0d381245 4943
6c95b8df
PA
4944 if (bptype != bp_catchpoint)
4945 gdb_assert (sal.pspace != NULL);
4946
0d381245
VP
4947 /* Adjust the breakpoint's address prior to allocating a location.
4948 Once we call allocate_bp_location(), that mostly uninitialized
4949 location will be placed on the location chain. Adjustment of the
8defab1a 4950 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
4951 not want its scan of the location chain to find a breakpoint and
4952 location that's only been partially initialized. */
a6d9a66e 4953 adjusted_address = adjust_breakpoint_address (loc_gdbarch, sal.pc, b->type);
0d381245 4954
39d61571 4955 b->loc = allocate_bp_location (b);
a6d9a66e 4956 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
4957 b->loc->requested_address = sal.pc;
4958 b->loc->address = adjusted_address;
6c95b8df
PA
4959 b->loc->pspace = sal.pspace;
4960
4961 /* Store the program space that was used to set the breakpoint, for
4962 breakpoint resetting. */
4963 b->pspace = sal.pspace;
0d381245
VP
4964
4965 if (sal.symtab == NULL)
4966 b->source_file = NULL;
4967 else
1b36a34b 4968 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
4969 b->loc->section = sal.section;
4970 b->line_number = sal.line;
4971
4972 set_breakpoint_location_function (b->loc);
c906108c 4973
c906108c
SS
4974 breakpoints_changed ();
4975
4976 return b;
4977}
4978
c2c6d25f
JM
4979
4980/* Note that the breakpoint object B describes a permanent breakpoint
4981 instruction, hard-wired into the inferior's code. */
4982void
4983make_breakpoint_permanent (struct breakpoint *b)
4984{
0d381245 4985 struct bp_location *bl;
b5de0fa7 4986 b->enable_state = bp_permanent;
c2c6d25f 4987
0d381245
VP
4988 /* By definition, permanent breakpoints are already present in the code.
4989 Mark all locations as inserted. For now, make_breakpoint_permanent
4990 is called in just one place, so it's hard to say if it's reasonable
4991 to have permanent breakpoint with multiple locations or not,
4992 but it's easy to implmement. */
4993 for (bl = b->loc; bl; bl = bl->next)
4994 bl->inserted = 1;
c2c6d25f
JM
4995}
4996
53a5351d 4997/* Call this routine when stepping and nexting to enable a breakpoint
0fd8e87f 4998 if we do a longjmp() in THREAD. When we hit that breakpoint, call
c906108c
SS
4999 set_longjmp_resume_breakpoint() to figure out where we are going. */
5000
5001void
0fd8e87f 5002set_longjmp_breakpoint (int thread)
c906108c 5003{
0fd8e87f
UW
5004 struct breakpoint *b, *temp;
5005
5006 /* To avoid having to rescan all objfile symbols at every step,
5007 we maintain a list of continually-inserted but always disabled
5008 longjmp "master" breakpoints. Here, we simply create momentary
5009 clones of those and enable them for the requested thread. */
5010 ALL_BREAKPOINTS_SAFE (b, temp)
6c95b8df
PA
5011 if (b->pspace == current_program_space
5012 && b->type == bp_longjmp_master)
0fd8e87f
UW
5013 {
5014 struct breakpoint *clone = clone_momentary_breakpoint (b);
5015 clone->type = bp_longjmp;
5016 clone->thread = thread;
5017 }
c906108c
SS
5018}
5019
611c83ae 5020/* Delete all longjmp breakpoints from THREAD. */
c906108c 5021void
611c83ae 5022delete_longjmp_breakpoint (int thread)
c906108c 5023{
611c83ae 5024 struct breakpoint *b, *temp;
c906108c 5025
611c83ae
PA
5026 ALL_BREAKPOINTS_SAFE (b, temp)
5027 if (b->type == bp_longjmp)
5028 {
5029 if (b->thread == thread)
5030 delete_breakpoint (b);
5031 }
c906108c
SS
5032}
5033
1900040c
MS
5034void
5035enable_overlay_breakpoints (void)
5036{
52f0bd74 5037 struct breakpoint *b;
1900040c
MS
5038
5039 ALL_BREAKPOINTS (b)
5040 if (b->type == bp_overlay_event)
5041 {
5042 b->enable_state = bp_enabled;
b60e7edf 5043 update_global_location_list (1);
c02f5703 5044 overlay_events_enabled = 1;
1900040c
MS
5045 }
5046}
5047
5048void
5049disable_overlay_breakpoints (void)
5050{
52f0bd74 5051 struct breakpoint *b;
1900040c
MS
5052
5053 ALL_BREAKPOINTS (b)
5054 if (b->type == bp_overlay_event)
5055 {
5056 b->enable_state = bp_disabled;
b60e7edf 5057 update_global_location_list (0);
c02f5703 5058 overlay_events_enabled = 0;
1900040c
MS
5059 }
5060}
5061
c4093a6a 5062struct breakpoint *
a6d9a66e 5063create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
5064{
5065 struct breakpoint *b;
c4093a6a 5066
a6d9a66e 5067 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
c4093a6a 5068
b5de0fa7 5069 b->enable_state = bp_enabled;
c4093a6a 5070 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
5071 b->addr_string
5072 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 5073
b60e7edf 5074 update_global_location_list_nothrow (1);
74960c60 5075
c4093a6a
JM
5076 return b;
5077}
5078
5079void
5080remove_thread_event_breakpoints (void)
5081{
5082 struct breakpoint *b, *temp;
5083
5084 ALL_BREAKPOINTS_SAFE (b, temp)
6c95b8df
PA
5085 if (b->type == bp_thread_event
5086 && b->loc->pspace == current_program_space)
c4093a6a
JM
5087 delete_breakpoint (b);
5088}
5089
0101ce28
JJ
5090struct captured_parse_breakpoint_args
5091 {
5092 char **arg_p;
5093 struct symtabs_and_lines *sals_p;
5094 char ***addr_string_p;
5095 int *not_found_ptr;
5096 };
5097
5098struct lang_and_radix
5099 {
5100 enum language lang;
5101 int radix;
5102 };
5103
4efc6507
DE
5104/* Create a breakpoint for JIT code registration and unregistration. */
5105
5106struct breakpoint *
5107create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5108{
5109 struct breakpoint *b;
5110
5111 b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
5112 update_global_location_list_nothrow (1);
5113 return b;
5114}
0101ce28 5115
cae688ec
JJ
5116void
5117remove_solib_event_breakpoints (void)
5118{
5119 struct breakpoint *b, *temp;
5120
5121 ALL_BREAKPOINTS_SAFE (b, temp)
6c95b8df
PA
5122 if (b->type == bp_shlib_event
5123 && b->loc->pspace == current_program_space)
cae688ec
JJ
5124 delete_breakpoint (b);
5125}
5126
5127struct breakpoint *
a6d9a66e 5128create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
5129{
5130 struct breakpoint *b;
5131
a6d9a66e 5132 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
b60e7edf 5133 update_global_location_list_nothrow (1);
cae688ec
JJ
5134 return b;
5135}
5136
5137/* Disable any breakpoints that are on code in shared libraries. Only
5138 apply to enabled breakpoints, disabled ones can just stay disabled. */
5139
5140void
cb851954 5141disable_breakpoints_in_shlibs (void)
cae688ec 5142{
876fa593 5143 struct bp_location *loc, **locp_tmp;
cae688ec 5144
876fa593 5145 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 5146 {
0d381245
VP
5147 struct breakpoint *b = loc->owner;
5148 /* We apply the check to all breakpoints, including disabled
5149 for those with loc->duplicate set. This is so that when breakpoint
5150 becomes enabled, or the duplicate is removed, gdb will try to insert
5151 all breakpoints. If we don't set shlib_disabled here, we'll try
5152 to insert those breakpoints and fail. */
1042e4c0
SS
5153 if (((b->type == bp_breakpoint)
5154 || (b->type == bp_hardware_breakpoint)
5155 || (b->type == bp_tracepoint))
6c95b8df 5156 && loc->pspace == current_program_space
0d381245 5157 && !loc->shlib_disabled
a77053c2 5158#ifdef PC_SOLIB
0d381245 5159 && PC_SOLIB (loc->address)
a77053c2 5160#else
6c95b8df 5161 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
5162#endif
5163 )
0d381245
VP
5164 {
5165 loc->shlib_disabled = 1;
5166 }
cae688ec
JJ
5167 }
5168}
5169
84acb35a
JJ
5170/* Disable any breakpoints that are in in an unloaded shared library. Only
5171 apply to enabled breakpoints, disabled ones can just stay disabled. */
5172
75149521 5173static void
84acb35a
JJ
5174disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
5175{
876fa593 5176 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
5177 int disabled_shlib_breaks = 0;
5178
c86cf029
VP
5179 /* SunOS a.out shared libraries are always mapped, so do not
5180 disable breakpoints; they will only be reported as unloaded
5181 through clear_solib when GDB discards its shared library
5182 list. See clear_solib for more information. */
5183 if (exec_bfd != NULL
5184 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
5185 return;
5186
876fa593 5187 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 5188 {
0d381245
VP
5189 struct breakpoint *b = loc->owner;
5190 if ((loc->loc_type == bp_loc_hardware_breakpoint
5191 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 5192 && solib->pspace == loc->pspace
e2dd7057
PP
5193 && !loc->shlib_disabled
5194 && (b->type == bp_breakpoint || b->type == bp_hardware_breakpoint)
5195 && solib_contains_address_p (solib, loc->address))
84acb35a 5196 {
e2dd7057
PP
5197 loc->shlib_disabled = 1;
5198 /* At this point, we cannot rely on remove_breakpoint
5199 succeeding so we must mark the breakpoint as not inserted
5200 to prevent future errors occurring in remove_breakpoints. */
5201 loc->inserted = 0;
5202 if (!disabled_shlib_breaks)
5203 {
5204 target_terminal_ours_for_output ();
5205 warning (_("Temporarily disabling breakpoints for unloaded shared library \"%s\""),
5206 solib->so_name);
84acb35a 5207 }
e2dd7057 5208 disabled_shlib_breaks = 1;
84acb35a
JJ
5209 }
5210 }
84acb35a
JJ
5211}
5212
ce78b96d
JB
5213/* FORK & VFORK catchpoints. */
5214
5215/* Implement the "insert" breakpoint_ops method for fork catchpoints. */
5216
c906108c 5217static void
ce78b96d
JB
5218insert_catch_fork (struct breakpoint *b)
5219{
5220 target_insert_fork_catchpoint (PIDGET (inferior_ptid));
5221}
5222
5223/* Implement the "remove" breakpoint_ops method for fork catchpoints. */
5224
5225static int
5226remove_catch_fork (struct breakpoint *b)
5227{
5228 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
5229}
5230
5231/* Implement the "breakpoint_hit" breakpoint_ops method for fork
5232 catchpoints. */
5233
5234static int
5235breakpoint_hit_catch_fork (struct breakpoint *b)
5236{
5237 return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
5238}
5239
5240/* Implement the "print_it" breakpoint_ops method for fork catchpoints. */
5241
5242static enum print_stop_action
5243print_it_catch_fork (struct breakpoint *b)
5244{
5245 annotate_catchpoint (b->number);
5246 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
5247 b->number, ptid_get_pid (b->forked_inferior_pid));
5248 return PRINT_SRC_AND_LOC;
5249}
5250
5251/* Implement the "print_one" breakpoint_ops method for fork catchpoints. */
5252
5253static void
a6d9a66e 5254print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 5255{
79a45b7d
TT
5256 struct value_print_options opts;
5257
5258 get_user_print_options (&opts);
5259
ce78b96d
JB
5260 /* Field 4, the address, is omitted (which makes the columns
5261 not line up too nicely with the headers, but the effect
5262 is relatively readable). */
79a45b7d 5263 if (opts.addressprint)
ce78b96d
JB
5264 ui_out_field_skip (uiout, "addr");
5265 annotate_field (5);
5266 ui_out_text (uiout, "fork");
5267 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
5268 {
5269 ui_out_text (uiout, ", process ");
5270 ui_out_field_int (uiout, "what",
5271 ptid_get_pid (b->forked_inferior_pid));
5272 ui_out_spaces (uiout, 1);
5273 }
5274}
5275
5276/* Implement the "print_mention" breakpoint_ops method for fork
5277 catchpoints. */
5278
5279static void
5280print_mention_catch_fork (struct breakpoint *b)
5281{
5282 printf_filtered (_("Catchpoint %d (fork)"), b->number);
5283}
5284
5285/* The breakpoint_ops structure to be used in fork catchpoints. */
5286
5287static struct breakpoint_ops catch_fork_breakpoint_ops =
5288{
5289 insert_catch_fork,
5290 remove_catch_fork,
5291 breakpoint_hit_catch_fork,
5292 print_it_catch_fork,
5293 print_one_catch_fork,
5294 print_mention_catch_fork
5295};
5296
5297/* Implement the "insert" breakpoint_ops method for vfork catchpoints. */
5298
5299static void
5300insert_catch_vfork (struct breakpoint *b)
5301{
5302 target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
5303}
5304
5305/* Implement the "remove" breakpoint_ops method for vfork catchpoints. */
5306
5307static int
5308remove_catch_vfork (struct breakpoint *b)
5309{
5310 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
5311}
5312
5313/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
5314 catchpoints. */
5315
5316static int
5317breakpoint_hit_catch_vfork (struct breakpoint *b)
5318{
5319 return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
5320}
5321
5322/* Implement the "print_it" breakpoint_ops method for vfork catchpoints. */
5323
5324static enum print_stop_action
5325print_it_catch_vfork (struct breakpoint *b)
5326{
5327 annotate_catchpoint (b->number);
5328 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
5329 b->number, ptid_get_pid (b->forked_inferior_pid));
5330 return PRINT_SRC_AND_LOC;
5331}
5332
5333/* Implement the "print_one" breakpoint_ops method for vfork catchpoints. */
5334
5335static void
a6d9a66e 5336print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 5337{
79a45b7d
TT
5338 struct value_print_options opts;
5339
5340 get_user_print_options (&opts);
ce78b96d
JB
5341 /* Field 4, the address, is omitted (which makes the columns
5342 not line up too nicely with the headers, but the effect
5343 is relatively readable). */
79a45b7d 5344 if (opts.addressprint)
ce78b96d
JB
5345 ui_out_field_skip (uiout, "addr");
5346 annotate_field (5);
5347 ui_out_text (uiout, "vfork");
5348 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
5349 {
5350 ui_out_text (uiout, ", process ");
5351 ui_out_field_int (uiout, "what",
5352 ptid_get_pid (b->forked_inferior_pid));
5353 ui_out_spaces (uiout, 1);
5354 }
5355}
5356
5357/* Implement the "print_mention" breakpoint_ops method for vfork
5358 catchpoints. */
5359
5360static void
5361print_mention_catch_vfork (struct breakpoint *b)
5362{
5363 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
5364}
5365
5366/* The breakpoint_ops structure to be used in vfork catchpoints. */
5367
5368static struct breakpoint_ops catch_vfork_breakpoint_ops =
5369{
5370 insert_catch_vfork,
5371 remove_catch_vfork,
5372 breakpoint_hit_catch_vfork,
5373 print_it_catch_vfork,
5374 print_one_catch_vfork,
5375 print_mention_catch_vfork
5376};
5377
a96d9b2e
SDJ
5378/* Implement the "insert" breakpoint_ops method for syscall
5379 catchpoints. */
5380
5381static void
5382insert_catch_syscall (struct breakpoint *b)
5383{
5384 struct inferior *inf = current_inferior ();
5385
5386 ++inf->total_syscalls_count;
5387 if (!b->syscalls_to_be_caught)
5388 ++inf->any_syscall_count;
5389 else
5390 {
5391 int i, iter;
5392 for (i = 0;
5393 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
5394 i++)
5395 {
5396 int elem;
5397 if (iter >= VEC_length (int, inf->syscalls_counts))
5398 {
5399 int old_size = VEC_length (int, inf->syscalls_counts);
5400 uintptr_t vec_addr_offset = old_size * ((uintptr_t) sizeof (int));
5401 uintptr_t vec_addr;
5402 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
5403 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
5404 vec_addr_offset;
5405 memset ((void *) vec_addr, 0,
5406 (iter + 1 - old_size) * sizeof (int));
5407 }
5408 elem = VEC_index (int, inf->syscalls_counts, iter);
5409 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
5410 }
5411 }
5412
5413 target_set_syscall_catchpoint (PIDGET (inferior_ptid),
5414 inf->total_syscalls_count != 0,
5415 inf->any_syscall_count,
5416 VEC_length (int, inf->syscalls_counts),
5417 VEC_address (int, inf->syscalls_counts));
5418}
5419
5420/* Implement the "remove" breakpoint_ops method for syscall
5421 catchpoints. */
5422
5423static int
5424remove_catch_syscall (struct breakpoint *b)
5425{
5426 struct inferior *inf = current_inferior ();
5427
5428 --inf->total_syscalls_count;
5429 if (!b->syscalls_to_be_caught)
5430 --inf->any_syscall_count;
5431 else
5432 {
5433 int i, iter;
5434 for (i = 0;
5435 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
5436 i++)
5437 {
5438 int elem;
5439 if (iter >= VEC_length (int, inf->syscalls_counts))
5440 /* Shouldn't happen. */
5441 continue;
5442 elem = VEC_index (int, inf->syscalls_counts, iter);
5443 VEC_replace (int, inf->syscalls_counts, iter, --elem);
5444 }
5445 }
5446
5447 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
5448 inf->total_syscalls_count != 0,
5449 inf->any_syscall_count,
5450 VEC_length (int, inf->syscalls_counts),
5451 VEC_address (int, inf->syscalls_counts));
5452}
5453
5454/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
5455 catchpoints. */
5456
5457static int
5458breakpoint_hit_catch_syscall (struct breakpoint *b)
5459{
5460 /* We must check if we are catching specific syscalls in this breakpoint.
5461 If we are, then we must guarantee that the called syscall is the same
5462 syscall we are catching. */
5463 int syscall_number = 0;
5464
5465 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
5466 return 0;
5467
5468 /* Now, checking if the syscall is the same. */
5469 if (b->syscalls_to_be_caught)
5470 {
5471 int i, iter;
5472 for (i = 0;
5473 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
5474 i++)
5475 if (syscall_number == iter)
5476 break;
5477 /* Not the same. */
5478 if (!iter)
5479 return 0;
5480 }
5481
5482 return 1;
5483}
5484
5485/* Implement the "print_it" breakpoint_ops method for syscall
5486 catchpoints. */
5487
5488static enum print_stop_action
5489print_it_catch_syscall (struct breakpoint *b)
5490{
5491 /* These are needed because we want to know in which state a
5492 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
5493 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
5494 must print "called syscall" or "returned from syscall". */
5495 ptid_t ptid;
5496 struct target_waitstatus last;
5497 struct syscall s;
5498 struct cleanup *old_chain;
5499 char *syscall_id;
5500
5501 get_last_target_status (&ptid, &last);
5502
5503 get_syscall_by_number (last.value.syscall_number, &s);
5504
5505 annotate_catchpoint (b->number);
5506
5507 if (s.name == NULL)
5508 syscall_id = xstrprintf ("%d", last.value.syscall_number);
5509 else
5510 syscall_id = xstrprintf ("'%s'", s.name);
5511
5512 old_chain = make_cleanup (xfree, syscall_id);
5513
5514 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
5515 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
5516 b->number, syscall_id);
5517 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
5518 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
5519 b->number, syscall_id);
5520
5521 do_cleanups (old_chain);
5522
5523 return PRINT_SRC_AND_LOC;
5524}
5525
5526/* Implement the "print_one" breakpoint_ops method for syscall
5527 catchpoints. */
5528
5529static void
5530print_one_catch_syscall (struct breakpoint *b,
5531 struct bp_location **last_loc)
5532{
5533 struct value_print_options opts;
5534
5535 get_user_print_options (&opts);
5536 /* Field 4, the address, is omitted (which makes the columns
5537 not line up too nicely with the headers, but the effect
5538 is relatively readable). */
5539 if (opts.addressprint)
5540 ui_out_field_skip (uiout, "addr");
5541 annotate_field (5);
5542
5543 if (b->syscalls_to_be_caught
5544 && VEC_length (int, b->syscalls_to_be_caught) > 1)
5545 ui_out_text (uiout, "syscalls \"");
5546 else
5547 ui_out_text (uiout, "syscall \"");
5548
5549 if (b->syscalls_to_be_caught)
5550 {
5551 int i, iter;
5552 char *text = xstrprintf ("%s", "");
5553 for (i = 0;
5554 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
5555 i++)
5556 {
5557 char *x = text;
5558 struct syscall s;
5559 get_syscall_by_number (iter, &s);
5560
5561 if (s.name != NULL)
5562 text = xstrprintf ("%s%s, ", text, s.name);
5563 else
5564 text = xstrprintf ("%s%d, ", text, iter);
5565
5566 /* We have to xfree the last 'text' (now stored at 'x')
5567 because xstrprintf dinamically allocates new space for it
5568 on every call. */
5569 xfree (x);
5570 }
5571 /* Remove the last comma. */
5572 text[strlen (text) - 2] = '\0';
5573 ui_out_field_string (uiout, "what", text);
5574 }
5575 else
5576 ui_out_field_string (uiout, "what", "<any syscall>");
5577 ui_out_text (uiout, "\" ");
5578}
5579
5580/* Implement the "print_mention" breakpoint_ops method for syscall
5581 catchpoints. */
5582
5583static void
5584print_mention_catch_syscall (struct breakpoint *b)
5585{
5586 if (b->syscalls_to_be_caught)
5587 {
5588 int i, iter;
5589
5590 if (VEC_length (int, b->syscalls_to_be_caught) > 1)
5591 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
5592 else
5593 printf_filtered (_("Catchpoint %d (syscall"), b->number);
5594
5595 for (i = 0;
5596 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
5597 i++)
5598 {
5599 struct syscall s;
5600 get_syscall_by_number (iter, &s);
5601
5602 if (s.name)
5603 printf_filtered (" '%s' [%d]", s.name, s.number);
5604 else
5605 printf_filtered (" %d", s.number);
5606 }
5607 printf_filtered (")");
5608 }
5609 else
5610 printf_filtered (_("Catchpoint %d (any syscall)"),
5611 b->number);
5612}
5613
5614/* The breakpoint_ops structure to be used in syscall catchpoints. */
5615
5616static struct breakpoint_ops catch_syscall_breakpoint_ops =
5617{
5618 insert_catch_syscall,
5619 remove_catch_syscall,
5620 breakpoint_hit_catch_syscall,
5621 print_it_catch_syscall,
5622 print_one_catch_syscall,
5623 print_mention_catch_syscall
5624};
5625
5626/* Returns non-zero if 'b' is a syscall catchpoint. */
5627
5628static int
5629syscall_catchpoint_p (struct breakpoint *b)
5630{
5631 return (b->ops == &catch_syscall_breakpoint_ops);
5632}
5633
5634/* Create a new breakpoint of the bp_catchpoint kind and return it,
5635 but does NOT mention it nor update the global location list.
5636 This is useful if you need to fill more fields in the
5637 struct breakpoint before calling mention.
ce78b96d
JB
5638
5639 If TEMPFLAG is non-zero, then make the breakpoint temporary.
5640 If COND_STRING is not NULL, then store it in the breakpoint.
5641 OPS, if not NULL, is the breakpoint_ops structure associated
5642 to the catchpoint. */
5643
5644static struct breakpoint *
a96d9b2e
SDJ
5645create_catchpoint_without_mention (struct gdbarch *gdbarch, int tempflag,
5646 char *cond_string,
5647 struct breakpoint_ops *ops)
c906108c 5648{
c5aa993b
JM
5649 struct symtab_and_line sal;
5650 struct breakpoint *b;
c5aa993b 5651
fe39c653 5652 init_sal (&sal);
6c95b8df 5653 sal.pspace = current_program_space;
c5aa993b 5654
a6d9a66e 5655 b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
c906108c
SS
5656 set_breakpoint_count (breakpoint_count + 1);
5657 b->number = breakpoint_count;
ce78b96d 5658
1b36a34b 5659 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
ce78b96d 5660 b->thread = -1;
c906108c 5661 b->addr_string = NULL;
b5de0fa7
EZ
5662 b->enable_state = bp_enabled;
5663 b->disposition = tempflag ? disp_del : disp_donttouch;
ce78b96d 5664 b->ops = ops;
c5aa993b 5665
a96d9b2e
SDJ
5666 return b;
5667}
5668
5669/* Create a new breakpoint of the bp_catchpoint kind and return it.
5670
5671 If TEMPFLAG is non-zero, then make the breakpoint temporary.
5672 If COND_STRING is not NULL, then store it in the breakpoint.
5673 OPS, if not NULL, is the breakpoint_ops structure associated
5674 to the catchpoint. */
5675
5676static struct breakpoint *
5677create_catchpoint (struct gdbarch *gdbarch, int tempflag,
5678 char *cond_string, struct breakpoint_ops *ops)
5679{
5680 struct breakpoint *b =
5681 create_catchpoint_without_mention (gdbarch, tempflag, cond_string, ops);
5682
c906108c 5683 mention (b);
ce78b96d 5684 update_global_location_list (1);
c906108c 5685
ce78b96d 5686 return b;
c906108c 5687}
c5aa993b 5688
9b70b993 5689static void
a6d9a66e
UW
5690create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
5691 int tempflag, char *cond_string,
ce78b96d 5692 struct breakpoint_ops *ops)
c906108c 5693{
a6d9a66e
UW
5694 struct breakpoint *b
5695 = create_catchpoint (gdbarch, tempflag, cond_string, ops);
ce78b96d
JB
5696
5697 /* FIXME: We should put this information in a breakpoint private data
5698 area. */
5699 b->forked_inferior_pid = null_ptid;
c906108c
SS
5700}
5701
fe798b75
JB
5702/* Exec catchpoints. */
5703
9b70b993 5704static void
fe798b75 5705insert_catch_exec (struct breakpoint *b)
c906108c 5706{
fe798b75
JB
5707 target_insert_exec_catchpoint (PIDGET (inferior_ptid));
5708}
c906108c 5709
fe798b75
JB
5710static int
5711remove_catch_exec (struct breakpoint *b)
5712{
5713 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
5714}
c906108c 5715
fe798b75
JB
5716static int
5717breakpoint_hit_catch_exec (struct breakpoint *b)
5718{
5719 return inferior_has_execd (inferior_ptid, &b->exec_pathname);
5720}
c906108c 5721
fe798b75
JB
5722static enum print_stop_action
5723print_it_catch_exec (struct breakpoint *b)
5724{
5725 annotate_catchpoint (b->number);
5726 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
5727 b->exec_pathname);
5728 return PRINT_SRC_AND_LOC;
c906108c
SS
5729}
5730
fe798b75 5731static void
a6d9a66e 5732print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75
JB
5733{
5734 struct value_print_options opts;
5735
5736 get_user_print_options (&opts);
5737
5738 /* Field 4, the address, is omitted (which makes the columns
5739 not line up too nicely with the headers, but the effect
5740 is relatively readable). */
5741 if (opts.addressprint)
5742 ui_out_field_skip (uiout, "addr");
5743 annotate_field (5);
5744 ui_out_text (uiout, "exec");
5745 if (b->exec_pathname != NULL)
5746 {
5747 ui_out_text (uiout, ", program \"");
5748 ui_out_field_string (uiout, "what", b->exec_pathname);
5749 ui_out_text (uiout, "\" ");
5750 }
5751}
5752
5753static void
5754print_mention_catch_exec (struct breakpoint *b)
5755{
5756 printf_filtered (_("Catchpoint %d (exec)"), b->number);
5757}
5758
5759static struct breakpoint_ops catch_exec_breakpoint_ops =
5760{
5761 insert_catch_exec,
5762 remove_catch_exec,
5763 breakpoint_hit_catch_exec,
5764 print_it_catch_exec,
5765 print_one_catch_exec,
5766 print_mention_catch_exec
5767};
5768
a96d9b2e
SDJ
5769static void
5770create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
5771 struct breakpoint_ops *ops)
5772{
5773 struct gdbarch *gdbarch = get_current_arch ();
5774 struct breakpoint *b =
5775 create_catchpoint_without_mention (gdbarch, tempflag, NULL, ops);
5776
5777 b->syscalls_to_be_caught = filter;
5778
5779 /* Now, we have to mention the breakpoint and update the global
5780 location list. */
5781 mention (b);
5782 update_global_location_list (1);
5783}
5784
c906108c 5785static int
fba45db2 5786hw_breakpoint_used_count (void)
c906108c 5787{
52f0bd74 5788 struct breakpoint *b;
c906108c
SS
5789 int i = 0;
5790
5791 ALL_BREAKPOINTS (b)
c5aa993b 5792 {
d6b74ac4 5793 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
c5aa993b
JM
5794 i++;
5795 }
c906108c
SS
5796
5797 return i;
5798}
5799
5800static int
fba45db2 5801hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 5802{
52f0bd74 5803 struct breakpoint *b;
c906108c
SS
5804 int i = 0;
5805
5806 *other_type_used = 0;
5807 ALL_BREAKPOINTS (b)
c5aa993b 5808 {
468d015d 5809 if (breakpoint_enabled (b))
c5aa993b
JM
5810 {
5811 if (b->type == type)
5812 i++;
059fb39f
PM
5813 else if ((b->type == bp_hardware_watchpoint
5814 || b->type == bp_read_watchpoint
5815 || b->type == bp_access_watchpoint))
c5aa993b
JM
5816 *other_type_used = 1;
5817 }
5818 }
c906108c
SS
5819 return i;
5820}
5821
c906108c 5822void
fba45db2 5823disable_watchpoints_before_interactive_call_start (void)
c906108c 5824{
c5aa993b 5825 struct breakpoint *b;
c906108c
SS
5826
5827 ALL_BREAKPOINTS (b)
c5aa993b
JM
5828 {
5829 if (((b->type == bp_watchpoint)
5830 || (b->type == bp_hardware_watchpoint)
5831 || (b->type == bp_read_watchpoint)
dfdfb3ca 5832 || (b->type == bp_access_watchpoint))
468d015d 5833 && breakpoint_enabled (b))
c5aa993b 5834 {
b5de0fa7 5835 b->enable_state = bp_call_disabled;
b60e7edf 5836 update_global_location_list (0);
c5aa993b
JM
5837 }
5838 }
c906108c
SS
5839}
5840
5841void
fba45db2 5842enable_watchpoints_after_interactive_call_stop (void)
c906108c 5843{
c5aa993b 5844 struct breakpoint *b;
c906108c
SS
5845
5846 ALL_BREAKPOINTS (b)
c5aa993b
JM
5847 {
5848 if (((b->type == bp_watchpoint)
5849 || (b->type == bp_hardware_watchpoint)
5850 || (b->type == bp_read_watchpoint)
dfdfb3ca 5851 || (b->type == bp_access_watchpoint))
b5de0fa7 5852 && (b->enable_state == bp_call_disabled))
c5aa993b 5853 {
b5de0fa7 5854 b->enable_state = bp_enabled;
b60e7edf 5855 update_global_location_list (1);
c5aa993b
JM
5856 }
5857 }
c906108c
SS
5858}
5859
8bea4e01
UW
5860void
5861disable_breakpoints_before_startup (void)
5862{
5863 struct breakpoint *b;
5864 int found = 0;
5865
5866 ALL_BREAKPOINTS (b)
5867 {
6c95b8df
PA
5868 if (b->pspace != current_program_space)
5869 continue;
5870
8bea4e01
UW
5871 if ((b->type == bp_breakpoint
5872 || b->type == bp_hardware_breakpoint)
5873 && breakpoint_enabled (b))
5874 {
5875 b->enable_state = bp_startup_disabled;
5876 found = 1;
5877 }
5878 }
5879
5880 if (found)
5881 update_global_location_list (0);
5882
6c95b8df 5883 current_program_space->executing_startup = 1;
8bea4e01
UW
5884}
5885
5886void
5887enable_breakpoints_after_startup (void)
5888{
5889 struct breakpoint *b;
5890 int found = 0;
5891
6c95b8df 5892 current_program_space->executing_startup = 0;
8bea4e01
UW
5893
5894 ALL_BREAKPOINTS (b)
5895 {
6c95b8df
PA
5896 if (b->pspace != current_program_space)
5897 continue;
5898
8bea4e01
UW
5899 if ((b->type == bp_breakpoint
5900 || b->type == bp_hardware_breakpoint)
5901 && b->enable_state == bp_startup_disabled)
5902 {
5903 b->enable_state = bp_enabled;
5904 found = 1;
5905 }
5906 }
5907
5908 if (found)
5909 breakpoint_re_set ();
5910}
5911
c906108c
SS
5912
5913/* Set a breakpoint that will evaporate an end of command
5914 at address specified by SAL.
5915 Restrict it to frame FRAME if FRAME is nonzero. */
5916
5917struct breakpoint *
a6d9a66e
UW
5918set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
5919 struct frame_id frame_id, enum bptype type)
c906108c 5920{
52f0bd74 5921 struct breakpoint *b;
edb3359d
DJ
5922
5923 /* If FRAME_ID is valid, it should be a real frame, not an inlined
5924 one. */
5925 gdb_assert (!frame_id_inlined_p (frame_id));
5926
a6d9a66e 5927 b = set_raw_breakpoint (gdbarch, sal, type);
b5de0fa7
EZ
5928 b->enable_state = bp_enabled;
5929 b->disposition = disp_donttouch;
818dd999 5930 b->frame_id = frame_id;
c906108c
SS
5931
5932 /* If we're debugging a multi-threaded program, then we
5933 want momentary breakpoints to be active in only a
5934 single thread of control. */
39f77062
KB
5935 if (in_thread_list (inferior_ptid))
5936 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 5937
b60e7edf 5938 update_global_location_list_nothrow (1);
74960c60 5939
c906108c
SS
5940 return b;
5941}
611c83ae 5942
e58b0e63
PA
5943/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
5944 ORIG is NULL. */
5945
5946struct breakpoint *
5947clone_momentary_breakpoint (struct breakpoint *orig)
5948{
5949 struct breakpoint *copy;
5950
5951 /* If there's nothing to clone, then return nothing. */
5952 if (orig == NULL)
5953 return NULL;
5954
a6d9a66e 5955 copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
e58b0e63
PA
5956 copy->loc = allocate_bp_location (copy);
5957 set_breakpoint_location_function (copy->loc);
5958
a6d9a66e 5959 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
5960 copy->loc->requested_address = orig->loc->requested_address;
5961 copy->loc->address = orig->loc->address;
5962 copy->loc->section = orig->loc->section;
6c95b8df 5963 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
5964
5965 if (orig->source_file == NULL)
5966 copy->source_file = NULL;
5967 else
5968 copy->source_file = xstrdup (orig->source_file);
5969
5970 copy->line_number = orig->line_number;
5971 copy->frame_id = orig->frame_id;
5972 copy->thread = orig->thread;
6c95b8df 5973 copy->pspace = orig->pspace;
e58b0e63
PA
5974
5975 copy->enable_state = bp_enabled;
5976 copy->disposition = disp_donttouch;
5977 copy->number = internal_breakpoint_number--;
5978
5979 update_global_location_list_nothrow (0);
5980 return copy;
5981}
5982
611c83ae 5983struct breakpoint *
a6d9a66e
UW
5984set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
5985 enum bptype type)
611c83ae
PA
5986{
5987 struct symtab_and_line sal;
5988
5989 sal = find_pc_line (pc, 0);
5990 sal.pc = pc;
5991 sal.section = find_pc_overlay (pc);
5992 sal.explicit_pc = 1;
5993
a6d9a66e 5994 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 5995}
c906108c 5996\f
c5aa993b 5997
c906108c
SS
5998/* Tell the user we have just set a breakpoint B. */
5999
6000static void
fba45db2 6001mention (struct breakpoint *b)
c906108c
SS
6002{
6003 int say_where = 0;
fa8a61dc 6004 struct cleanup *ui_out_chain;
79a45b7d
TT
6005 struct value_print_options opts;
6006
6007 get_user_print_options (&opts);
8b93c638 6008
9a4105ab
AC
6009 /* FIXME: This is misplaced; mention() is called by things (like
6010 hitting a watchpoint) other than breakpoint creation. It should
6011 be possible to clean this up and at the same time replace the
7f4b89d1 6012 random calls to breakpoint_changed with this hook. */
383f836e 6013 observer_notify_breakpoint_created (b->number);
c906108c 6014
3086aeae
DJ
6015 if (b->ops != NULL && b->ops->print_mention != NULL)
6016 b->ops->print_mention (b);
6017 else
6018 switch (b->type)
6019 {
6020 case bp_none:
a3f17187 6021 printf_filtered (_("(apparently deleted?) Eventpoint %d: "), b->number);
3086aeae
DJ
6022 break;
6023 case bp_watchpoint:
6024 ui_out_text (uiout, "Watchpoint ");
6025 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
6026 ui_out_field_int (uiout, "number", b->number);
6027 ui_out_text (uiout, ": ");
fa8a61dc 6028 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
6029 do_cleanups (ui_out_chain);
6030 break;
6031 case bp_hardware_watchpoint:
6032 ui_out_text (uiout, "Hardware watchpoint ");
6033 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
6034 ui_out_field_int (uiout, "number", b->number);
6035 ui_out_text (uiout, ": ");
fa8a61dc 6036 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
6037 do_cleanups (ui_out_chain);
6038 break;
6039 case bp_read_watchpoint:
6040 ui_out_text (uiout, "Hardware read watchpoint ");
6041 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
6042 ui_out_field_int (uiout, "number", b->number);
6043 ui_out_text (uiout, ": ");
fa8a61dc 6044 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
6045 do_cleanups (ui_out_chain);
6046 break;
6047 case bp_access_watchpoint:
6048 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
6049 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
6050 ui_out_field_int (uiout, "number", b->number);
6051 ui_out_text (uiout, ": ");
fa8a61dc 6052 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
6053 do_cleanups (ui_out_chain);
6054 break;
6055 case bp_breakpoint:
6056 if (ui_out_is_mi_like_p (uiout))
6057 {
6058 say_where = 0;
6059 break;
6060 }
2cec12e5
AR
6061 if (b->disposition == disp_del)
6062 printf_filtered (_("Temporary breakpoint"));
6063 else
6064 printf_filtered (_("Breakpoint"));
6065 printf_filtered (_(" %d"), b->number);
3086aeae
DJ
6066 say_where = 1;
6067 break;
6068 case bp_hardware_breakpoint:
6069 if (ui_out_is_mi_like_p (uiout))
6070 {
6071 say_where = 0;
6072 break;
6073 }
a3f17187 6074 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
3086aeae
DJ
6075 say_where = 1;
6076 break;
1042e4c0
SS
6077 case bp_tracepoint:
6078 if (ui_out_is_mi_like_p (uiout))
6079 {
6080 say_where = 0;
6081 break;
6082 }
6083 printf_filtered (_("Tracepoint"));
6084 printf_filtered (_(" %d"), b->number);
6085 say_where = 1;
6086 break;
3086aeae
DJ
6087
6088 case bp_until:
6089 case bp_finish:
6090 case bp_longjmp:
6091 case bp_longjmp_resume:
6092 case bp_step_resume:
3086aeae
DJ
6093 case bp_call_dummy:
6094 case bp_watchpoint_scope:
6095 case bp_shlib_event:
6096 case bp_thread_event:
6097 case bp_overlay_event:
4efc6507 6098 case bp_jit_event:
0fd8e87f 6099 case bp_longjmp_master:
3086aeae
DJ
6100 break;
6101 }
c906108c 6102
c906108c
SS
6103 if (say_where)
6104 {
a3f17187
AC
6105 /* i18n: cagney/2005-02-11: Below needs to be merged into a
6106 single string. */
0d381245 6107 if (b->loc == NULL)
c906108c 6108 {
a3f17187 6109 printf_filtered (_(" (%s) pending."), b->addr_string);
0101ce28
JJ
6110 }
6111 else
6112 {
79a45b7d 6113 if (opts.addressprint || b->source_file == NULL)
0101ce28
JJ
6114 {
6115 printf_filtered (" at ");
5af949e3
UW
6116 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
6117 gdb_stdout);
0101ce28
JJ
6118 }
6119 if (b->source_file)
6120 printf_filtered (": file %s, line %d.",
6121 b->source_file, b->line_number);
0d381245
VP
6122
6123 if (b->loc->next)
6124 {
6125 struct bp_location *loc = b->loc;
6126 int n = 0;
6127 for (; loc; loc = loc->next)
6128 ++n;
6129 printf_filtered (" (%d locations)", n);
6130 }
6131
c906108c 6132 }
c906108c 6133 }
9dc5e2a9 6134 if (ui_out_is_mi_like_p (uiout))
fb40c209 6135 return;
c906108c
SS
6136 printf_filtered ("\n");
6137}
c906108c 6138\f
c5aa993b 6139
0d381245 6140static struct bp_location *
39d61571 6141add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
6142 const struct symtab_and_line *sal)
6143{
6144 struct bp_location *loc, **tmp;
6145
39d61571 6146 loc = allocate_bp_location (b);
0d381245
VP
6147 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
6148 ;
6149 *tmp = loc;
a6d9a66e
UW
6150 loc->gdbarch = get_sal_arch (*sal);
6151 if (!loc->gdbarch)
6152 loc->gdbarch = b->gdbarch;
0d381245 6153 loc->requested_address = sal->pc;
a6d9a66e
UW
6154 loc->address = adjust_breakpoint_address (loc->gdbarch,
6155 loc->requested_address, b->type);
6c95b8df
PA
6156 loc->pspace = sal->pspace;
6157 gdb_assert (loc->pspace != NULL);
0d381245
VP
6158 loc->section = sal->section;
6159
6160 set_breakpoint_location_function (loc);
6161 return loc;
6162}
514f746b
AR
6163\f
6164
6165/* Return 1 if LOC is pointing to a permanent breakpoint,
6166 return 0 otherwise. */
6167
6168static int
6169bp_loc_is_permanent (struct bp_location *loc)
6170{
6171 int len;
6172 CORE_ADDR addr;
6173 const gdb_byte *brk;
6174 gdb_byte *target_mem;
939c61fa
JK
6175 struct cleanup *cleanup;
6176 int retval = 0;
514f746b
AR
6177
6178 gdb_assert (loc != NULL);
6179
6180 addr = loc->address;
a6d9a66e 6181 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 6182
939c61fa
JK
6183 /* Software breakpoints unsupported? */
6184 if (brk == NULL)
6185 return 0;
6186
514f746b
AR
6187 target_mem = alloca (len);
6188
939c61fa
JK
6189 /* Enable the automatic memory restoration from breakpoints while
6190 we read the memory. Otherwise we could say about our temporary
6191 breakpoints they are permanent. */
6c95b8df
PA
6192 cleanup = save_current_space_and_thread ();
6193
6194 switch_to_program_space_and_thread (loc->pspace);
6195 make_show_memory_breakpoints_cleanup (0);
939c61fa 6196
514f746b
AR
6197 if (target_read_memory (loc->address, target_mem, len) == 0
6198 && memcmp (target_mem, brk, len) == 0)
939c61fa 6199 retval = 1;
514f746b 6200
939c61fa
JK
6201 do_cleanups (cleanup);
6202
6203 return retval;
514f746b
AR
6204}
6205
6206
c3f6f71d 6207
018d34a4
VP
6208/* Create a breakpoint with SAL as location. Use ADDR_STRING
6209 as textual description of the location, and COND_STRING
db107f19 6210 as condition expression. */
018d34a4
VP
6211
6212static void
a6d9a66e
UW
6213create_breakpoint (struct gdbarch *gdbarch,
6214 struct symtabs_and_lines sals, char *addr_string,
018d34a4
VP
6215 char *cond_string,
6216 enum bptype type, enum bpdisp disposition,
4a306c9a 6217 int thread, int task, int ignore_count,
41447f92 6218 struct breakpoint_ops *ops, int from_tty, int enabled)
018d34a4 6219{
0d381245
VP
6220 struct breakpoint *b = NULL;
6221 int i;
018d34a4
VP
6222
6223 if (type == bp_hardware_breakpoint)
6224 {
6225 int i = hw_breakpoint_used_count ();
6226 int target_resources_ok =
d92524f1 6227 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
6228 i + 1, 0);
6229 if (target_resources_ok == 0)
6230 error (_("No hardware breakpoint support in the target."));
6231 else if (target_resources_ok < 0)
6232 error (_("Hardware breakpoints used exceeds limit."));
6233 }
6234
6c95b8df
PA
6235 gdb_assert (sals.nelts > 0);
6236
0d381245
VP
6237 for (i = 0; i < sals.nelts; ++i)
6238 {
6239 struct symtab_and_line sal = sals.sals[i];
6240 struct bp_location *loc;
6241
6242 if (from_tty)
5af949e3
UW
6243 {
6244 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
6245 if (!loc_gdbarch)
6246 loc_gdbarch = gdbarch;
6247
6248 describe_other_breakpoints (loc_gdbarch,
6c95b8df 6249 sal.pspace, sal.pc, sal.section, thread);
5af949e3 6250 }
0d381245
VP
6251
6252 if (i == 0)
6253 {
a6d9a66e 6254 b = set_raw_breakpoint (gdbarch, sal, type);
0d381245
VP
6255 set_breakpoint_count (breakpoint_count + 1);
6256 b->number = breakpoint_count;
6257 b->thread = thread;
4a306c9a 6258 b->task = task;
018d34a4 6259
0d381245
VP
6260 b->cond_string = cond_string;
6261 b->ignore_count = ignore_count;
41447f92 6262 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 6263 b->disposition = disposition;
018d34a4 6264
6c95b8df
PA
6265 b->pspace = sals.sals[0].pspace;
6266
6267 if (enabled && b->pspace->executing_startup
8bea4e01
UW
6268 && (b->type == bp_breakpoint
6269 || b->type == bp_hardware_breakpoint))
6270 b->enable_state = bp_startup_disabled;
6271
0d381245
VP
6272 loc = b->loc;
6273 }
6274 else
018d34a4 6275 {
39d61571 6276 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
6277 }
6278
514f746b
AR
6279 if (bp_loc_is_permanent (loc))
6280 make_breakpoint_permanent (b);
6281
0d381245
VP
6282 if (b->cond_string)
6283 {
6284 char *arg = b->cond_string;
d32a6982 6285 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 6286 if (*arg)
db107f19 6287 error (_("Garbage %s follows condition"), arg);
018d34a4 6288 }
0d381245 6289 }
018d34a4
VP
6290
6291 if (addr_string)
6292 b->addr_string = addr_string;
6293 else
6294 /* addr_string has to be used or breakpoint_re_set will delete
6295 me. */
5af949e3
UW
6296 b->addr_string
6297 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
018d34a4 6298
604133b5 6299 b->ops = ops;
018d34a4
VP
6300 mention (b);
6301}
6302
ed0616c6
VP
6303/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
6304 elements to fill the void space. */
2c0b251b
PA
6305static void
6306remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
6307{
6308 int i = index_to_remove+1;
6309 int last_index = sal->nelts-1;
6310
6311 for (;i <= last_index; ++i)
6312 sal->sals[i-1] = sal->sals[i];
6313
6314 --(sal->nelts);
6315}
6316
6c95b8df
PA
6317/* If appropriate, obtains all sals that correspond to the same file
6318 and line as SAL, in all program spaces. Users debugging with IDEs,
6319 will want to set a breakpoint at foo.c:line, and not really care
6320 about program spaces. This is done only if SAL does not have
6321 explicit PC and has line and file information. If we got just a
6322 single expanded sal, return the original.
ed0616c6 6323
6c95b8df
PA
6324 Otherwise, if SAL.explicit_line is not set, filter out all sals for
6325 which the name of enclosing function is different from SAL. This
6326 makes sure that if we have breakpoint originally set in template
6327 instantiation, say foo<int>(), we won't expand SAL to locations at
6328 the same line in all existing instantiations of 'foo'. */
ed0616c6 6329
2c0b251b 6330static struct symtabs_and_lines
ed0616c6
VP
6331expand_line_sal_maybe (struct symtab_and_line sal)
6332{
6333 struct symtabs_and_lines expanded;
6334 CORE_ADDR original_pc = sal.pc;
6335 char *original_function = NULL;
6336 int found;
6337 int i;
6c95b8df 6338 struct cleanup *old_chain;
ed0616c6
VP
6339
6340 /* If we have explicit pc, don't expand.
6341 If we have no line number, we can't expand. */
6342 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
6343 {
6344 expanded.nelts = 1;
6345 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
6346 expanded.sals[0] = sal;
6347 return expanded;
6348 }
6349
6350 sal.pc = 0;
6c95b8df
PA
6351
6352 old_chain = save_current_space_and_thread ();
6353
6354 switch_to_program_space_and_thread (sal.pspace);
6355
ed0616c6 6356 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
6357
6358 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 6359 expanded = expand_line_sal (sal);
6c95b8df 6360
ed0616c6
VP
6361 if (expanded.nelts == 1)
6362 {
3dba1c98
JB
6363 /* We had one sal, we got one sal. Return that sal, adjusting it
6364 past the function prologue if necessary. */
ed0616c6
VP
6365 xfree (expanded.sals);
6366 expanded.nelts = 1;
6367 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
6368 sal.pc = original_pc;
6369 expanded.sals[0] = sal;
3dba1c98 6370 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 6371 do_cleanups (old_chain);
ed0616c6
VP
6372 return expanded;
6373 }
6374
6375 if (!sal.explicit_line)
6376 {
6377 CORE_ADDR func_addr, func_end;
6378 for (i = 0; i < expanded.nelts; ++i)
6379 {
6380 CORE_ADDR pc = expanded.sals[i].pc;
6381 char *this_function;
6c95b8df
PA
6382
6383 /* We need to switch threads as well since we're about to
6384 read memory. */
6385 switch_to_program_space_and_thread (expanded.sals[i].pspace);
6386
ed0616c6
VP
6387 if (find_pc_partial_function (pc, &this_function,
6388 &func_addr, &func_end))
6389 {
059fb39f
PM
6390 if (this_function
6391 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
6392 {
6393 remove_sal (&expanded, i);
6394 --i;
6395 }
6396 else if (func_addr == pc)
6397 {
6398 /* We're at beginning of a function, and should
6399 skip prologue. */
6400 struct symbol *sym = find_pc_function (pc);
6401 if (sym)
6402 expanded.sals[i] = find_function_start_sal (sym, 1);
6403 else
d80b854b
UW
6404 {
6405 /* Since find_pc_partial_function returned true,
6406 we should really always find the section here. */
6407 struct obj_section *section = find_pc_section (pc);
6408 if (section)
6409 {
6410 struct gdbarch *gdbarch
6411 = get_objfile_arch (section->objfile);
6412 expanded.sals[i].pc
6413 = gdbarch_skip_prologue (gdbarch, pc);
6414 }
6415 }
ed0616c6
VP
6416 }
6417 }
6418 }
6419 }
3daf8fe5
JG
6420 else
6421 {
6422 for (i = 0; i < expanded.nelts; ++i)
6423 {
6424 /* If this SAL corresponds to a breakpoint inserted using a
6425 line number, then skip the function prologue if necessary. */
6426 skip_prologue_sal (&expanded.sals[i]);
6427 }
6428 }
ed0616c6 6429
6c95b8df
PA
6430 do_cleanups (old_chain);
6431
ed0616c6
VP
6432 if (expanded.nelts <= 1)
6433 {
6434 /* This is un ugly workaround. If we get zero
6435 expanded sals then something is really wrong.
6436 Fix that by returnign the original sal. */
6437 xfree (expanded.sals);
6438 expanded.nelts = 1;
6439 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
6440 sal.pc = original_pc;
6441 expanded.sals[0] = sal;
6442 return expanded;
6443 }
6444
6445 if (original_pc)
6446 {
6447 found = 0;
6448 for (i = 0; i < expanded.nelts; ++i)
6449 if (expanded.sals[i].pc == original_pc)
6450 {
6451 found = 1;
6452 break;
6453 }
6454 gdb_assert (found);
6455 }
6456
6457 return expanded;
6458}
6459
018d34a4
VP
6460/* Add SALS.nelts breakpoints to the breakpoint table. For each
6461 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
6462 value. COND_STRING, if not NULL, specified the condition to be
6463 used for all breakpoints. Essentially the only case where
6464 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
6465 function. In that case, it's still not possible to specify
6466 separate conditions for different overloaded functions, so
6467 we take just a single condition string.
6468
c3f6f71d 6469 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 6470 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
6471 array contents). If the function fails (error() is called), the
6472 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
6473 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
6474
6475static void
a6d9a66e
UW
6476create_breakpoints (struct gdbarch *gdbarch,
6477 struct symtabs_and_lines sals, char **addr_string,
018d34a4 6478 char *cond_string,
c3f6f71d 6479 enum bptype type, enum bpdisp disposition,
4a306c9a 6480 int thread, int task, int ignore_count,
41447f92
VP
6481 struct breakpoint_ops *ops, int from_tty,
6482 int enabled)
c906108c 6483{
018d34a4
VP
6484 int i;
6485 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 6486 {
ed0616c6
VP
6487 struct symtabs_and_lines expanded =
6488 expand_line_sal_maybe (sals.sals[i]);
0d381245 6489
a6d9a66e 6490 create_breakpoint (gdbarch, expanded, addr_string[i],
018d34a4 6491 cond_string, type, disposition,
4a306c9a 6492 thread, task, ignore_count, ops, from_tty, enabled);
c3f6f71d 6493 }
c3f6f71d 6494}
c906108c 6495
c3f6f71d
JM
6496/* Parse ARG which is assumed to be a SAL specification possibly
6497 followed by conditionals. On return, SALS contains an array of SAL
6498 addresses found. ADDR_STRING contains a vector of (canonical)
6499 address strings. ARG points to the end of the SAL. */
c906108c 6500
b9362cc7 6501static void
c3f6f71d
JM
6502parse_breakpoint_sals (char **address,
6503 struct symtabs_and_lines *sals,
0101ce28
JJ
6504 char ***addr_string,
6505 int *not_found_ptr)
c3f6f71d
JM
6506{
6507 char *addr_start = *address;
6508 *addr_string = NULL;
6509 /* If no arg given, or if first arg is 'if ', use the default
6510 breakpoint. */
6511 if ((*address) == NULL
6512 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
6513 {
6514 if (default_breakpoint_valid)
6515 {
c3f6f71d 6516 struct symtab_and_line sal;
fe39c653 6517 init_sal (&sal); /* initialize to zeroes */
c3f6f71d 6518 sals->sals = (struct symtab_and_line *)
c906108c
SS
6519 xmalloc (sizeof (struct symtab_and_line));
6520 sal.pc = default_breakpoint_address;
6521 sal.line = default_breakpoint_line;
6522 sal.symtab = default_breakpoint_symtab;
6c95b8df 6523 sal.pspace = default_breakpoint_pspace;
c5aa993b 6524 sal.section = find_pc_overlay (sal.pc);
00903456
JK
6525
6526 /* "break" without arguments is equivalent to "break *PC" where PC is
6527 the default_breakpoint_address. So make sure to set
6528 sal.explicit_pc to prevent GDB from trying to expand the list of
6529 sals to include all other instances with the same symtab and line.
6530 */
6531 sal.explicit_pc = 1;
6532
c3f6f71d
JM
6533 sals->sals[0] = sal;
6534 sals->nelts = 1;
c906108c
SS
6535 }
6536 else
8a3fe4f8 6537 error (_("No default breakpoint address now."));
c906108c
SS
6538 }
6539 else
6540 {
c906108c 6541 /* Force almost all breakpoints to be in terms of the
c5aa993b
JM
6542 current_source_symtab (which is decode_line_1's default). This
6543 should produce the results we want almost all of the time while
1aeae86e
AF
6544 leaving default_breakpoint_* alone.
6545 ObjC: However, don't match an Objective-C method name which
6546 may have a '+' or '-' succeeded by a '[' */
0378c332 6547
c214a6fd 6548 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 6549
c906108c 6550 if (default_breakpoint_valid
0378c332 6551 && (!cursal.symtab
1aeae86e
AF
6552 || ((strchr ("+-", (*address)[0]) != NULL)
6553 && ((*address)[1] != '['))))
c3f6f71d 6554 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
0101ce28
JJ
6555 default_breakpoint_line, addr_string,
6556 not_found_ptr);
c906108c 6557 else
0101ce28
JJ
6558 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
6559 addr_string, not_found_ptr);
c906108c 6560 }
c3f6f71d
JM
6561 /* For any SAL that didn't have a canonical string, fill one in. */
6562 if (sals->nelts > 0 && *addr_string == NULL)
6563 *addr_string = xcalloc (sals->nelts, sizeof (char **));
6564 if (addr_start != (*address))
c906108c 6565 {
c3f6f71d
JM
6566 int i;
6567 for (i = 0; i < sals->nelts; i++)
c906108c 6568 {
c3f6f71d
JM
6569 /* Add the string if not present. */
6570 if ((*addr_string)[i] == NULL)
6571 (*addr_string)[i] = savestring (addr_start, (*address) - addr_start);
c906108c
SS
6572 }
6573 }
c3f6f71d 6574}
c906108c 6575
c906108c 6576
c3f6f71d
JM
6577/* Convert each SAL into a real PC. Verify that the PC can be
6578 inserted as a breakpoint. If it can't throw an error. */
c906108c 6579
b9362cc7 6580static void
c3f6f71d
JM
6581breakpoint_sals_to_pc (struct symtabs_and_lines *sals,
6582 char *address)
6583{
6584 int i;
6585 for (i = 0; i < sals->nelts; i++)
ee53e872 6586 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
6587}
6588
05ff989b 6589static void
0101ce28
JJ
6590do_captured_parse_breakpoint (struct ui_out *ui, void *data)
6591{
6592 struct captured_parse_breakpoint_args *args = data;
6593
6594 parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p,
6595 args->not_found_ptr);
0101ce28
JJ
6596}
6597
018d34a4
VP
6598/* Given TOK, a string specification of condition and thread, as
6599 accepted by the 'break' command, extract the condition
6600 string and thread number and set *COND_STRING and *THREAD.
6601 PC identifies the context at which the condition should be parsed.
6602 If no condition is found, *COND_STRING is set to NULL.
6603 If no thread is found, *THREAD is set to -1. */
6604static void
6605find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 6606 char **cond_string, int *thread, int *task)
018d34a4
VP
6607{
6608 *cond_string = NULL;
6609 *thread = -1;
6610 while (tok && *tok)
6611 {
6612 char *end_tok;
6613 int toklen;
6614 char *cond_start = NULL;
6615 char *cond_end = NULL;
6616 while (*tok == ' ' || *tok == '\t')
6617 tok++;
6618
6619 end_tok = tok;
6620
6621 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
6622 end_tok++;
6623
6624 toklen = end_tok - tok;
6625
6626 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
6627 {
f7545552
TT
6628 struct expression *expr;
6629
018d34a4 6630 tok = cond_start = end_tok + 1;
f7545552
TT
6631 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
6632 xfree (expr);
018d34a4
VP
6633 cond_end = tok;
6634 *cond_string = savestring (cond_start,
6635 cond_end - cond_start);
6636 }
6637 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
6638 {
6639 char *tmptok;
6640
6641 tok = end_tok + 1;
6642 tmptok = tok;
6643 *thread = strtol (tok, &tok, 0);
6644 if (tok == tmptok)
6645 error (_("Junk after thread keyword."));
6646 if (!valid_thread_id (*thread))
6647 error (_("Unknown thread %d."), *thread);
6648 }
4a306c9a
JB
6649 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
6650 {
6651 char *tmptok;
6652
6653 tok = end_tok + 1;
6654 tmptok = tok;
6655 *task = strtol (tok, &tok, 0);
6656 if (tok == tmptok)
6657 error (_("Junk after task keyword."));
6658 if (!valid_task_id (*task))
b6199126 6659 error (_("Unknown task %d."), *task);
4a306c9a 6660 }
018d34a4
VP
6661 else
6662 error (_("Junk at end of arguments."));
6663 }
6664}
6665
72b2ff0e
VP
6666/* Set a breakpoint. This function is shared between
6667 CLI and MI functions for setting a breakpoint.
6668 This function has two major modes of operations,
6669 selected by the PARSE_CONDITION_AND_THREAD parameter.
6670 If non-zero, the function will parse arg, extracting
6671 breakpoint location, address and thread. Otherwise,
6672 ARG is just the location of breakpoint, with condition
6673 and thread specified by the COND_STRING and THREAD
6674 parameters. */
0101ce28 6675
98deb0da 6676static void
a6d9a66e
UW
6677break_command_really (struct gdbarch *gdbarch,
6678 char *arg, char *cond_string, int thread,
60c46647 6679 int parse_condition_and_thread,
1042e4c0 6680 int tempflag, int hardwareflag, int traceflag,
60c46647
VP
6681 int ignore_count,
6682 enum auto_boolean pending_break_support,
604133b5 6683 struct breakpoint_ops *ops,
41447f92
VP
6684 int from_tty,
6685 int enabled)
c3f6f71d 6686{
71fff37b 6687 struct gdb_exception e;
c3f6f71d 6688 struct symtabs_and_lines sals;
0101ce28 6689 struct symtab_and_line pending_sal;
0101ce28
JJ
6690 char *copy_arg;
6691 char *err_msg;
c3f6f71d
JM
6692 char *addr_start = arg;
6693 char **addr_string;
6694 struct cleanup *old_chain;
80c99de1 6695 struct cleanup *bkpt_chain = NULL;
0101ce28 6696 struct captured_parse_breakpoint_args parse_args;
05ff989b 6697 int i;
0101ce28 6698 int pending = 0;
0101ce28 6699 int not_found = 0;
1042e4c0 6700 enum bptype type_wanted;
4a306c9a 6701 int task = 0;
c3f6f71d 6702
c3f6f71d
JM
6703 sals.sals = NULL;
6704 sals.nelts = 0;
6705 addr_string = NULL;
c3f6f71d 6706
0101ce28
JJ
6707 parse_args.arg_p = &arg;
6708 parse_args.sals_p = &sals;
6709 parse_args.addr_string_p = &addr_string;
6710 parse_args.not_found_ptr = &not_found;
6711
05ff989b
AC
6712 e = catch_exception (uiout, do_captured_parse_breakpoint,
6713 &parse_args, RETURN_MASK_ALL);
0101ce28
JJ
6714
6715 /* If caller is interested in rc value from parse, set value. */
05ff989b 6716 switch (e.reason)
0101ce28 6717 {
05ff989b 6718 case RETURN_QUIT:
98deb0da 6719 throw_exception (e);
05ff989b
AC
6720 case RETURN_ERROR:
6721 switch (e.error)
0101ce28 6722 {
05ff989b 6723 case NOT_FOUND_ERROR:
0101ce28 6724
05ff989b
AC
6725 /* If pending breakpoint support is turned off, throw
6726 error. */
fa8d40ab
JJ
6727
6728 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
6729 throw_exception (e);
6730
6731 exception_print (gdb_stderr, e);
fa8d40ab 6732
05ff989b
AC
6733 /* If pending breakpoint support is auto query and the user
6734 selects no, then simply return the error code. */
059fb39f
PM
6735 if (pending_break_support == AUTO_BOOLEAN_AUTO
6736 && !nquery ("Make breakpoint pending on future shared library load? "))
98deb0da 6737 return;
fa8d40ab 6738
05ff989b
AC
6739 /* At this point, either the user was queried about setting
6740 a pending breakpoint and selected yes, or pending
6741 breakpoint behavior is on and thus a pending breakpoint
6742 is defaulted on behalf of the user. */
0101ce28
JJ
6743 copy_arg = xstrdup (addr_start);
6744 addr_string = &copy_arg;
6745 sals.nelts = 1;
6746 sals.sals = &pending_sal;
6747 pending_sal.pc = 0;
6748 pending = 1;
05ff989b
AC
6749 break;
6750 default:
98deb0da 6751 throw_exception (e);
0101ce28 6752 }
05ff989b
AC
6753 default:
6754 if (!sals.nelts)
98deb0da 6755 return;
0101ce28 6756 }
c3f6f71d
JM
6757
6758 /* Create a chain of things that always need to be cleaned up. */
6759 old_chain = make_cleanup (null_cleanup, 0);
6760
0101ce28
JJ
6761 if (!pending)
6762 {
6763 /* Make sure that all storage allocated to SALS gets freed. */
6764 make_cleanup (xfree, sals.sals);
6765
6766 /* Cleanup the addr_string array but not its contents. */
6767 make_cleanup (xfree, addr_string);
6768 }
c3f6f71d 6769
c3f6f71d
JM
6770 /* ----------------------------- SNIP -----------------------------
6771 Anything added to the cleanup chain beyond this point is assumed
6772 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
6773 then the memory is not reclaimed. */
6774 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d
JM
6775
6776 /* Mark the contents of the addr_string for cleanup. These go on
80c99de1 6777 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
6778 for (i = 0; i < sals.nelts; i++)
6779 {
6780 if (addr_string[i] != NULL)
b8c9b27d 6781 make_cleanup (xfree, addr_string[i]);
c3f6f71d
JM
6782 }
6783
6784 /* Resolve all line numbers to PC's and verify that the addresses
6785 are ok for the target. */
0101ce28
JJ
6786 if (!pending)
6787 breakpoint_sals_to_pc (&sals, addr_start);
c3f6f71d 6788
1042e4c0
SS
6789 type_wanted = (traceflag
6790 ? bp_tracepoint
6791 : (hardwareflag ? bp_hardware_breakpoint : bp_breakpoint));
6792
c3f6f71d
JM
6793 /* Verify that condition can be parsed, before setting any
6794 breakpoints. Allocate a separate condition expression for each
6795 breakpoint. */
0101ce28 6796 if (!pending)
c3f6f71d 6797 {
2f069f6f 6798 if (parse_condition_and_thread)
72b2ff0e
VP
6799 {
6800 /* Here we only parse 'arg' to separate condition
6801 from thread number, so parsing in context of first
6802 sal is OK. When setting the breakpoint we'll
6803 re-parse it in context of each sal. */
6804 cond_string = NULL;
6805 thread = -1;
4a306c9a
JB
6806 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
6807 &thread, &task);
72b2ff0e
VP
6808 if (cond_string)
6809 make_cleanup (xfree, cond_string);
6810 }
2f069f6f 6811 else
72b2ff0e
VP
6812 {
6813 /* Create a private copy of condition string. */
6814 if (cond_string)
6815 {
6816 cond_string = xstrdup (cond_string);
6817 make_cleanup (xfree, cond_string);
6818 }
6819 }
a6d9a66e 6820 create_breakpoints (gdbarch, sals, addr_string, cond_string, type_wanted,
0101ce28 6821 tempflag ? disp_del : disp_donttouch,
4a306c9a 6822 thread, task, ignore_count, ops, from_tty, enabled);
c906108c 6823 }
0101ce28
JJ
6824 else
6825 {
fe3f5fa8 6826 struct symtab_and_line sal = {0};
0101ce28
JJ
6827 struct breakpoint *b;
6828
0101ce28
JJ
6829 make_cleanup (xfree, copy_arg);
6830
a6d9a66e 6831 b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
0101ce28
JJ
6832 set_breakpoint_count (breakpoint_count + 1);
6833 b->number = breakpoint_count;
72b2ff0e 6834 b->thread = -1;
018d34a4 6835 b->addr_string = addr_string[0];
72b2ff0e 6836 b->cond_string = NULL;
0101ce28 6837 b->ignore_count = ignore_count;
0101ce28 6838 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 6839 b->condition_not_parsed = 1;
604133b5 6840 b->ops = ops;
41447f92 6841 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 6842 b->pspace = current_program_space;
74960c60 6843
6c95b8df 6844 if (enabled && b->pspace->executing_startup
8bea4e01
UW
6845 && (b->type == bp_breakpoint
6846 || b->type == bp_hardware_breakpoint))
6847 b->enable_state = bp_startup_disabled;
6848
0101ce28
JJ
6849 mention (b);
6850 }
6851
c3f6f71d 6852 if (sals.nelts > 1)
8a3fe4f8
AC
6853 warning (_("Multiple breakpoints were set.\n"
6854 "Use the \"delete\" command to delete unwanted breakpoints."));
80c99de1
PA
6855 /* That's it. Discard the cleanups for data inserted into the
6856 breakpoint. */
6857 discard_cleanups (bkpt_chain);
6858 /* But cleanup everything else. */
c3f6f71d 6859 do_cleanups (old_chain);
217dc9e2 6860
80c99de1 6861 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 6862 update_global_location_list (1);
c3f6f71d 6863}
c906108c 6864
72b2ff0e
VP
6865/* Set a breakpoint.
6866 ARG is a string describing breakpoint address,
6867 condition, and thread.
6868 FLAG specifies if a breakpoint is hardware on,
6869 and if breakpoint is temporary, using BP_HARDWARE_FLAG
6870 and BP_TEMPFLAG. */
6871
98deb0da 6872static void
72b2ff0e 6873break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 6874{
72b2ff0e
VP
6875 int hardwareflag = flag & BP_HARDWAREFLAG;
6876 int tempflag = flag & BP_TEMPFLAG;
c3f6f71d 6877
a6d9a66e
UW
6878 break_command_really (get_current_arch (),
6879 arg,
98deb0da 6880 NULL, 0, 1 /* parse arg */,
1042e4c0 6881 tempflag, hardwareflag, 0 /* traceflag */,
98deb0da 6882 0 /* Ignore count */,
604133b5
AR
6883 pending_break_support,
6884 NULL /* breakpoint_ops */,
41447f92
VP
6885 from_tty,
6886 1 /* enabled */);
c906108c
SS
6887}
6888
72b2ff0e 6889
98deb0da 6890void
a6d9a66e
UW
6891set_breakpoint (struct gdbarch *gdbarch,
6892 char *address, char *condition,
c3f6f71d 6893 int hardwareflag, int tempflag,
ce43223b 6894 int thread, int ignore_count,
41447f92 6895 int pending, int enabled)
c3f6f71d 6896{
a6d9a66e
UW
6897 break_command_really (gdbarch,
6898 address, condition, thread,
98deb0da 6899 0 /* condition and thread are valid. */,
1042e4c0 6900 tempflag, hardwareflag, 0 /* traceflag */,
98deb0da
VP
6901 ignore_count,
6902 pending
6903 ? AUTO_BOOLEAN_TRUE : AUTO_BOOLEAN_FALSE,
41447f92 6904 NULL, 0, enabled);
c3f6f71d
JM
6905}
6906
6a048695
JB
6907/* Adjust SAL to the first instruction past the function prologue.
6908 The end of the prologue is determined using the line table from
3daf8fe5
JG
6909 the debugging information. explicit_pc and explicit_line are
6910 not modified.
6a048695
JB
6911
6912 If SAL is already past the prologue, then do nothing. */
6913
6914static void
6915skip_prologue_sal (struct symtab_and_line *sal)
6916{
6c95b8df 6917 struct symbol *sym;
6a048695 6918 struct symtab_and_line start_sal;
6c95b8df 6919 struct cleanup *old_chain;
6a048695 6920
6c95b8df 6921 old_chain = save_current_space_and_thread ();
6a048695 6922
6c95b8df
PA
6923 sym = find_pc_function (sal->pc);
6924 if (sym != NULL)
3daf8fe5 6925 {
6c95b8df
PA
6926 start_sal = find_function_start_sal (sym, 1);
6927 if (sal->pc < start_sal.pc)
6928 {
6929 start_sal.explicit_line = sal->explicit_line;
6930 start_sal.explicit_pc = sal->explicit_pc;
6931 *sal = start_sal;
6932 }
3daf8fe5 6933 }
6c95b8df
PA
6934
6935 do_cleanups (old_chain);
6a048695 6936}
c3f6f71d 6937
c906108c
SS
6938/* Helper function for break_command_1 and disassemble_command. */
6939
6940void
fba45db2 6941resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
6942{
6943 CORE_ADDR pc;
6944
6945 if (sal->pc == 0 && sal->symtab != NULL)
6946 {
6947 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 6948 error (_("No line %d in file \"%s\"."),
c906108c
SS
6949 sal->line, sal->symtab->filename);
6950 sal->pc = pc;
6a048695
JB
6951
6952 /* If this SAL corresponds to a breakpoint inserted using
6953 a line number, then skip the function prologue if necessary. */
6954 if (sal->explicit_line)
56ef84b1
TT
6955 {
6956 /* Preserve the original line number. */
6957 int saved_line = sal->line;
6958 skip_prologue_sal (sal);
6959 sal->line = saved_line;
6960 }
c906108c
SS
6961 }
6962
6963 if (sal->section == 0 && sal->symtab != NULL)
6964 {
6965 struct blockvector *bv;
c5aa993b
JM
6966 struct block *b;
6967 struct symbol *sym;
c906108c 6968
801e3a5b 6969 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
6970 if (bv != NULL)
6971 {
7f0df278 6972 sym = block_linkage_function (b);
c906108c
SS
6973 if (sym != NULL)
6974 {
6975 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 6976 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
6977 }
6978 else
6979 {
6980 /* It really is worthwhile to have the section, so we'll just
c5aa993b
JM
6981 have to look harder. This case can be executed if we have
6982 line numbers but no functions (as can happen in assembly
6983 source). */
c906108c 6984
c5aa993b 6985 struct minimal_symbol *msym;
6c95b8df
PA
6986 struct cleanup *old_chain = save_current_space_and_thread ();
6987
6988 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
6989
6990 msym = lookup_minimal_symbol_by_pc (sal->pc);
6991 if (msym)
714835d5 6992 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
6993
6994 do_cleanups (old_chain);
c906108c
SS
6995 }
6996 }
6997 }
6998}
6999
7000void
fba45db2 7001break_command (char *arg, int from_tty)
c906108c 7002{
db107f19 7003 break_command_1 (arg, 0, from_tty);
c906108c
SS
7004}
7005
c906108c 7006void
fba45db2 7007tbreak_command (char *arg, int from_tty)
c906108c 7008{
db107f19 7009 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
7010}
7011
c906108c 7012static void
fba45db2 7013hbreak_command (char *arg, int from_tty)
c906108c 7014{
db107f19 7015 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
7016}
7017
7018static void
fba45db2 7019thbreak_command (char *arg, int from_tty)
c906108c 7020{
db107f19 7021 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
7022}
7023
7024static void
fba45db2 7025stop_command (char *arg, int from_tty)
c906108c 7026{
a3f17187 7027 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 7028Usage: stop in <function | address>\n\
a3f17187 7029 stop at <line>\n"));
c906108c
SS
7030}
7031
7032static void
fba45db2 7033stopin_command (char *arg, int from_tty)
c906108c
SS
7034{
7035 int badInput = 0;
7036
c5aa993b 7037 if (arg == (char *) NULL)
c906108c
SS
7038 badInput = 1;
7039 else if (*arg != '*')
7040 {
7041 char *argptr = arg;
7042 int hasColon = 0;
7043
53a5351d
JM
7044 /* look for a ':'. If this is a line number specification, then
7045 say it is bad, otherwise, it should be an address or
7046 function/method name */
c906108c 7047 while (*argptr && !hasColon)
c5aa993b
JM
7048 {
7049 hasColon = (*argptr == ':');
7050 argptr++;
7051 }
c906108c
SS
7052
7053 if (hasColon)
c5aa993b 7054 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 7055 else
c5aa993b 7056 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
7057 }
7058
7059 if (badInput)
a3f17187 7060 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 7061 else
db107f19 7062 break_command_1 (arg, 0, from_tty);
c906108c
SS
7063}
7064
7065static void
fba45db2 7066stopat_command (char *arg, int from_tty)
c906108c
SS
7067{
7068 int badInput = 0;
7069
c5aa993b 7070 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
7071 badInput = 1;
7072 else
7073 {
7074 char *argptr = arg;
7075 int hasColon = 0;
7076
7077 /* look for a ':'. If there is a '::' then get out, otherwise
c5aa993b 7078 it is probably a line number. */
c906108c 7079 while (*argptr && !hasColon)
c5aa993b
JM
7080 {
7081 hasColon = (*argptr == ':');
7082 argptr++;
7083 }
c906108c
SS
7084
7085 if (hasColon)
c5aa993b 7086 badInput = (*argptr == ':'); /* we have class::method */
c906108c 7087 else
c5aa993b 7088 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
7089 }
7090
7091 if (badInput)
a3f17187 7092 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 7093 else
db107f19 7094 break_command_1 (arg, 0, from_tty);
c906108c
SS
7095}
7096
53a5351d
JM
7097/* accessflag: hw_write: watch write,
7098 hw_read: watch read,
7099 hw_access: watch access (read or write) */
c906108c 7100static void
fba45db2 7101watch_command_1 (char *arg, int accessflag, int from_tty)
c906108c 7102{
a6d9a66e 7103 struct gdbarch *gdbarch = get_current_arch ();
d983da9c 7104 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c
SS
7105 struct expression *exp;
7106 struct block *exp_valid_block;
7107 struct value *val, *mark;
7108 struct frame_info *frame;
c906108c
SS
7109 char *exp_start = NULL;
7110 char *exp_end = NULL;
37e4754d 7111 char *tok, *id_tok_start, *end_tok;
c906108c
SS
7112 int toklen;
7113 char *cond_start = NULL;
7114 char *cond_end = NULL;
c906108c
SS
7115 int i, other_type_used, target_resources_ok = 0;
7116 enum bptype bp_type;
7117 int mem_cnt = 0;
37e4754d 7118 int thread = -1;
c906108c 7119
37e4754d
LM
7120 /* Make sure that we actually have parameters to parse. */
7121 if (arg != NULL && arg[0] != '\0')
7122 {
7123 toklen = strlen (arg); /* Size of argument list. */
7124
7125 /* Points tok to the end of the argument list. */
7126 tok = arg + toklen - 1;
7127
7128 /* Go backwards in the parameters list. Skip the last parameter.
7129 If we're expecting a 'thread <thread_num>' parameter, this should
7130 be the thread identifier. */
7131 while (tok > arg && (*tok == ' ' || *tok == '\t'))
7132 tok--;
7133 while (tok > arg && (*tok != ' ' && *tok != '\t'))
7134 tok--;
7135
7136 /* Points end_tok to the beginning of the last token. */
7137 id_tok_start = tok + 1;
7138
7139 /* Go backwards in the parameters list. Skip one more parameter.
7140 If we're expecting a 'thread <thread_num>' parameter, we should
7141 reach a "thread" token. */
7142 while (tok > arg && (*tok == ' ' || *tok == '\t'))
7143 tok--;
7144
7145 end_tok = tok;
7146
7147 while (tok > arg && (*tok != ' ' && *tok != '\t'))
7148 tok--;
7149
7150 /* Move the pointer forward to skip the whitespace and
7151 calculate the length of the token. */
7152 tok++;
7153 toklen = end_tok - tok;
7154
7155 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7156 {
7157 /* At this point we've found a "thread" token, which means
7158 the user is trying to set a watchpoint that triggers
7159 only in a specific thread. */
7160 char *endp;
7161
7162 /* Extract the thread ID from the next token. */
7163 thread = strtol (id_tok_start, &endp, 0);
7164
7165 /* Check if the user provided a valid numeric value for the
7166 thread ID. */
7167 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
7168 error (_("Invalid thread ID specification %s."), id_tok_start);
7169
7170 /* Check if the thread actually exists. */
7171 if (!valid_thread_id (thread))
7172 error (_("Unknown thread %d."), thread);
7173
7174 /* Truncate the string and get rid of the thread <thread_num>
7175 parameter before the parameter list is parsed by the
7176 evaluate_expression() function. */
7177 *tok = '\0';
7178 }
7179 }
7180
7181 /* Parse the rest of the arguments. */
c906108c
SS
7182 innermost_block = NULL;
7183 exp_start = arg;
7184 exp = parse_exp_1 (&arg, 0, 0);
7185 exp_end = arg;
fa8a61dc
TT
7186 /* Remove trailing whitespace from the expression before saving it.
7187 This makes the eventual display of the expression string a bit
7188 prettier. */
7189 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
7190 --exp_end;
7191
c906108c
SS
7192 exp_valid_block = innermost_block;
7193 mark = value_mark ();
fa4727a6
DJ
7194 fetch_watchpoint_value (exp, &val, NULL, NULL);
7195 if (val != NULL)
7196 release_value (val);
c906108c
SS
7197
7198 tok = arg;
7199 while (*tok == ' ' || *tok == '\t')
7200 tok++;
7201 end_tok = tok;
7202
7203 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
7204 end_tok++;
7205
7206 toklen = end_tok - tok;
7207 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7208 {
2d134ed3
PA
7209 struct expression *cond;
7210
c906108c
SS
7211 tok = cond_start = end_tok + 1;
7212 cond = parse_exp_1 (&tok, 0, 0);
2d134ed3 7213 xfree (cond);
c906108c
SS
7214 cond_end = tok;
7215 }
7216 if (*tok)
8a3fe4f8 7217 error (_("Junk at end of command."));
c906108c 7218
53a5351d 7219 if (accessflag == hw_read)
c5aa993b 7220 bp_type = bp_read_watchpoint;
53a5351d 7221 else if (accessflag == hw_access)
c5aa993b
JM
7222 bp_type = bp_access_watchpoint;
7223 else
7224 bp_type = bp_hardware_watchpoint;
c906108c
SS
7225
7226 mem_cnt = can_use_hardware_watchpoint (val);
7227 if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 7228 error (_("Expression cannot be implemented with read/access watchpoint."));
c5aa993b
JM
7229 if (mem_cnt != 0)
7230 {
7231 i = hw_watchpoint_used_count (bp_type, &other_type_used);
53a5351d 7232 target_resources_ok =
d92524f1 7233 target_can_use_hardware_watchpoint (bp_type, i + mem_cnt,
53a5351d 7234 other_type_used);
c5aa993b 7235 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 7236 error (_("Target does not support this type of hardware watchpoint."));
53a5351d 7237
c5aa993b 7238 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 7239 error (_("Target can only support one kind of HW watchpoint at a time."));
c5aa993b 7240 }
c906108c 7241
4d28f7a8
KB
7242 /* Change the type of breakpoint to an ordinary watchpoint if a hardware
7243 watchpoint could not be set. */
7244 if (!mem_cnt || target_resources_ok <= 0)
7245 bp_type = bp_watchpoint;
7246
d983da9c 7247 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
7248
7249 /* If the expression is "local", then set up a "watchpoint scope"
7250 breakpoint at the point where we've left the scope of the watchpoint
7251 expression. Create the scope breakpoint before the watchpoint, so
7252 that we will encounter it first in bpstat_stop_status. */
edb3359d 7253 if (innermost_block && frame)
d983da9c 7254 {
edb3359d
DJ
7255 if (frame_id_p (frame_unwind_caller_id (frame)))
7256 {
7257 scope_breakpoint
a6d9a66e
UW
7258 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
7259 frame_unwind_caller_pc (frame),
edb3359d 7260 bp_watchpoint_scope);
d983da9c 7261
edb3359d 7262 scope_breakpoint->enable_state = bp_enabled;
d983da9c 7263
edb3359d
DJ
7264 /* Automatically delete the breakpoint when it hits. */
7265 scope_breakpoint->disposition = disp_del;
d983da9c 7266
edb3359d
DJ
7267 /* Only break in the proper frame (help with recursion). */
7268 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 7269
edb3359d 7270 /* Set the address at which we will stop. */
a6d9a66e
UW
7271 scope_breakpoint->loc->gdbarch
7272 = frame_unwind_caller_arch (frame);
edb3359d
DJ
7273 scope_breakpoint->loc->requested_address
7274 = frame_unwind_caller_pc (frame);
7275 scope_breakpoint->loc->address
a6d9a66e
UW
7276 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
7277 scope_breakpoint->loc->requested_address,
edb3359d
DJ
7278 scope_breakpoint->type);
7279 }
d983da9c
DJ
7280 }
7281
c906108c 7282 /* Now set up the breakpoint. */
2d134ed3 7283 b = set_raw_breakpoint_without_location (NULL, bp_type);
c906108c
SS
7284 set_breakpoint_count (breakpoint_count + 1);
7285 b->number = breakpoint_count;
37e4754d 7286 b->thread = thread;
b5de0fa7 7287 b->disposition = disp_donttouch;
c906108c
SS
7288 b->exp = exp;
7289 b->exp_valid_block = exp_valid_block;
7290 b->exp_string = savestring (exp_start, exp_end - exp_start);
7291 b->val = val;
fa4727a6 7292 b->val_valid = 1;
c906108c
SS
7293 if (cond_start)
7294 b->cond_string = savestring (cond_start, cond_end - cond_start);
7295 else
7296 b->cond_string = 0;
c5aa993b 7297
c906108c 7298 if (frame)
f6bc2008
PA
7299 {
7300 b->watchpoint_frame = get_frame_id (frame);
7301 b->watchpoint_thread = inferior_ptid;
7302 }
c906108c 7303 else
f6bc2008
PA
7304 {
7305 b->watchpoint_frame = null_frame_id;
7306 b->watchpoint_thread = null_ptid;
7307 }
c906108c 7308
d983da9c 7309 if (scope_breakpoint != NULL)
c906108c 7310 {
d983da9c
DJ
7311 /* The scope breakpoint is related to the watchpoint. We will
7312 need to act on them together. */
7313 b->related_breakpoint = scope_breakpoint;
7314 scope_breakpoint->related_breakpoint = b;
c906108c 7315 }
d983da9c 7316
c906108c 7317 value_free_to_mark (mark);
2d134ed3
PA
7318
7319 /* Finally update the new watchpoint. This creates the locations
7320 that should be inserted. */
7321 update_watchpoint (b, 1);
7322
c906108c 7323 mention (b);
b60e7edf 7324 update_global_location_list (1);
c906108c
SS
7325}
7326
7327/* Return count of locations need to be watched and can be handled
7328 in hardware. If the watchpoint can not be handled
7329 in hardware return zero. */
7330
c906108c 7331static int
fba45db2 7332can_use_hardware_watchpoint (struct value *v)
c906108c
SS
7333{
7334 int found_memory_cnt = 0;
2e70b7b9 7335 struct value *head = v;
c906108c
SS
7336
7337 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 7338 if (!can_use_hw_watchpoints)
c906108c 7339 return 0;
c5aa993b 7340
5c44784c
JM
7341 /* Make sure that the value of the expression depends only upon
7342 memory contents, and values computed from them within GDB. If we
7343 find any register references or function calls, we can't use a
7344 hardware watchpoint.
7345
7346 The idea here is that evaluating an expression generates a series
7347 of values, one holding the value of every subexpression. (The
7348 expression a*b+c has five subexpressions: a, b, a*b, c, and
7349 a*b+c.) GDB's values hold almost enough information to establish
7350 the criteria given above --- they identify memory lvalues,
7351 register lvalues, computed values, etcetera. So we can evaluate
7352 the expression, and then scan the chain of values that leaves
7353 behind to decide whether we can detect any possible change to the
7354 expression's final value using only hardware watchpoints.
7355
7356 However, I don't think that the values returned by inferior
7357 function calls are special in any way. So this function may not
7358 notice that an expression involving an inferior function call
7359 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 7360 for (; v; v = value_next (v))
c906108c 7361 {
5c44784c 7362 if (VALUE_LVAL (v) == lval_memory)
c906108c 7363 {
d69fe07e 7364 if (value_lazy (v))
5c44784c
JM
7365 /* A lazy memory lvalue is one that GDB never needed to fetch;
7366 we either just used its address (e.g., `a' in `a.b') or
7367 we never needed it at all (e.g., `a' in `a,b'). */
7368 ;
53a5351d 7369 else
5c44784c
JM
7370 {
7371 /* Ahh, memory we actually used! Check if we can cover
7372 it with hardware watchpoints. */
df407dfe 7373 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
7374
7375 /* We only watch structs and arrays if user asked for it
7376 explicitly, never if they just happen to appear in a
7377 middle of some value chain. */
7378 if (v == head
7379 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
7380 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
7381 {
42ae5230 7382 CORE_ADDR vaddr = value_address (v);
df407dfe 7383 int len = TYPE_LENGTH (value_type (v));
2e70b7b9 7384
d92524f1 7385 if (!target_region_ok_for_hw_watchpoint (vaddr, len))
2e70b7b9
MS
7386 return 0;
7387 else
7388 found_memory_cnt++;
7389 }
5c44784c 7390 }
c5aa993b 7391 }
5086187c
AC
7392 else if (VALUE_LVAL (v) != not_lval
7393 && deprecated_value_modifiable (v) == 0)
53a5351d 7394 return 0; /* ??? What does this represent? */
5086187c 7395 else if (VALUE_LVAL (v) == lval_register)
53a5351d 7396 return 0; /* cannot watch a register with a HW watchpoint */
c906108c
SS
7397 }
7398
7399 /* The expression itself looks suitable for using a hardware
7400 watchpoint, but give the target machine a chance to reject it. */
7401 return found_memory_cnt;
7402}
7403
8b93c638 7404void
fba45db2 7405watch_command_wrapper (char *arg, int from_tty)
8b93c638
JM
7406{
7407 watch_command (arg, from_tty);
7408}
8926118c 7409
c5aa993b 7410static void
fba45db2 7411watch_command (char *arg, int from_tty)
c906108c 7412{
53a5351d 7413 watch_command_1 (arg, hw_write, from_tty);
c906108c
SS
7414}
7415
8b93c638 7416void
fba45db2 7417rwatch_command_wrapper (char *arg, int from_tty)
8b93c638
JM
7418{
7419 rwatch_command (arg, from_tty);
7420}
8926118c 7421
c5aa993b 7422static void
fba45db2 7423rwatch_command (char *arg, int from_tty)
c906108c 7424{
53a5351d 7425 watch_command_1 (arg, hw_read, from_tty);
c906108c
SS
7426}
7427
8b93c638 7428void
fba45db2 7429awatch_command_wrapper (char *arg, int from_tty)
8b93c638
JM
7430{
7431 awatch_command (arg, from_tty);
7432}
8926118c 7433
c5aa993b 7434static void
fba45db2 7435awatch_command (char *arg, int from_tty)
c906108c 7436{
53a5351d 7437 watch_command_1 (arg, hw_access, from_tty);
c906108c 7438}
c906108c 7439\f
c5aa993b 7440
43ff13b4 7441/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
7442 because it uses the mechanisms of breakpoints. */
7443
bfec99b2
PA
7444struct until_break_command_continuation_args
7445{
7446 struct breakpoint *breakpoint;
7447 struct breakpoint *breakpoint2;
7448};
7449
43ff13b4
JM
7450/* This function is called by fetch_inferior_event via the
7451 cmd_continuation pointer, to complete the until command. It takes
7452 care of cleaning up the temporary breakpoints set up by the until
7453 command. */
c2c6d25f 7454static void
604ead4a 7455until_break_command_continuation (void *arg)
43ff13b4 7456{
bfec99b2
PA
7457 struct until_break_command_continuation_args *a = arg;
7458
7459 delete_breakpoint (a->breakpoint);
7460 if (a->breakpoint2)
7461 delete_breakpoint (a->breakpoint2);
43ff13b4
JM
7462}
7463
c906108c 7464void
ae66c1fc 7465until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
7466{
7467 struct symtabs_and_lines sals;
7468 struct symtab_and_line sal;
206415a3 7469 struct frame_info *frame = get_selected_frame (NULL);
c906108c 7470 struct breakpoint *breakpoint;
f107f563 7471 struct breakpoint *breakpoint2 = NULL;
c906108c
SS
7472 struct cleanup *old_chain;
7473
7474 clear_proceed_status ();
7475
7476 /* Set a breakpoint where the user wants it and at return from
7477 this function */
c5aa993b 7478
c906108c
SS
7479 if (default_breakpoint_valid)
7480 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
68219205 7481 default_breakpoint_line, (char ***) NULL, NULL);
c906108c 7482 else
53a5351d 7483 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
68219205 7484 0, (char ***) NULL, NULL);
c5aa993b 7485
c906108c 7486 if (sals.nelts != 1)
8a3fe4f8 7487 error (_("Couldn't get information on specified line."));
c5aa993b 7488
c906108c 7489 sal = sals.sals[0];
b8c9b27d 7490 xfree (sals.sals); /* malloc'd, so freed */
c5aa993b 7491
c906108c 7492 if (*arg)
8a3fe4f8 7493 error (_("Junk at end of arguments."));
c5aa993b 7494
c906108c 7495 resolve_sal_pc (&sal);
c5aa993b 7496
ae66c1fc
EZ
7497 if (anywhere)
7498 /* If the user told us to continue until a specified location,
7499 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
7500 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
7501 null_frame_id, bp_until);
ae66c1fc 7502 else
edb3359d 7503 /* Otherwise, specify the selected frame, because we want to stop only
ae66c1fc 7504 at the very same frame. */
a6d9a66e
UW
7505 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
7506 get_stack_frame_id (frame),
ae66c1fc 7507 bp_until);
c5aa993b 7508
f107f563 7509 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 7510
ae66c1fc
EZ
7511 /* Keep within the current frame, or in frames called by the current
7512 one. */
edb3359d
DJ
7513
7514 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 7515 {
edb3359d
DJ
7516 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
7517 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
7518 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
7519 sal,
edb3359d 7520 frame_unwind_caller_id (frame),
f107f563
VP
7521 bp_until);
7522 make_cleanup_delete_breakpoint (breakpoint2);
c906108c 7523 }
c5aa993b 7524
c906108c 7525 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563
VP
7526
7527 /* If we are running asynchronously, and proceed call above has actually
7528 managed to start the target, arrange for breakpoints to be
7529 deleted when the target stops. Otherwise, we're already stopped and
7530 delete breakpoints via cleanup chain. */
7531
8ea051c5 7532 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 7533 {
bfec99b2
PA
7534 struct until_break_command_continuation_args *args;
7535 args = xmalloc (sizeof (*args));
f107f563 7536
bfec99b2
PA
7537 args->breakpoint = breakpoint;
7538 args->breakpoint2 = breakpoint2;
f107f563
VP
7539
7540 discard_cleanups (old_chain);
95e54da7
PA
7541 add_continuation (inferior_thread (),
7542 until_break_command_continuation, args,
604ead4a 7543 xfree);
f107f563
VP
7544 }
7545 else
c5aa993b 7546 do_cleanups (old_chain);
c906108c 7547}
ae66c1fc 7548
c906108c 7549static void
fba45db2 7550ep_skip_leading_whitespace (char **s)
c906108c 7551{
c5aa993b
JM
7552 if ((s == NULL) || (*s == NULL))
7553 return;
7554 while (isspace (**s))
7555 *s += 1;
c906108c 7556}
c5aa993b 7557
c906108c
SS
7558/* This function attempts to parse an optional "if <cond>" clause
7559 from the arg string. If one is not found, it returns NULL.
c5aa993b 7560
c906108c
SS
7561 Else, it returns a pointer to the condition string. (It does not
7562 attempt to evaluate the string against a particular block.) And,
7563 it updates arg to point to the first character following the parsed
7564 if clause in the arg string. */
53a5351d 7565
c906108c 7566static char *
fba45db2 7567ep_parse_optional_if_clause (char **arg)
c906108c 7568{
c5aa993b
JM
7569 char *cond_string;
7570
7571 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 7572 return NULL;
c5aa993b 7573
c906108c
SS
7574 /* Skip the "if" keyword. */
7575 (*arg) += 2;
c5aa993b 7576
c906108c
SS
7577 /* Skip any extra leading whitespace, and record the start of the
7578 condition string. */
7579 ep_skip_leading_whitespace (arg);
7580 cond_string = *arg;
c5aa993b 7581
c906108c
SS
7582 /* Assume that the condition occupies the remainder of the arg string. */
7583 (*arg) += strlen (cond_string);
c5aa993b 7584
c906108c
SS
7585 return cond_string;
7586}
c5aa993b 7587
c906108c
SS
7588/* This function attempts to parse an optional filename from the arg
7589 string. If one is not found, it returns NULL.
c5aa993b 7590
c906108c
SS
7591 Else, it returns a pointer to the parsed filename. (This function
7592 makes no attempt to verify that a file of that name exists, or is
7593 accessible.) And, it updates arg to point to the first character
7594 following the parsed filename in the arg string.
c5aa993b 7595
c906108c
SS
7596 Note that clients needing to preserve the returned filename for
7597 future access should copy it to their own buffers. */
7598static char *
fba45db2 7599ep_parse_optional_filename (char **arg)
c906108c 7600{
c5aa993b
JM
7601 static char filename[1024];
7602 char *arg_p = *arg;
7603 int i;
7604 char c;
7605
c906108c
SS
7606 if ((*arg_p == '\0') || isspace (*arg_p))
7607 return NULL;
c5aa993b
JM
7608
7609 for (i = 0;; i++)
c906108c
SS
7610 {
7611 c = *arg_p;
7612 if (isspace (c))
c5aa993b 7613 c = '\0';
c906108c
SS
7614 filename[i] = c;
7615 if (c == '\0')
c5aa993b 7616 break;
c906108c
SS
7617 arg_p++;
7618 }
7619 *arg = arg_p;
c5aa993b 7620
c906108c
SS
7621 return filename;
7622}
c5aa993b 7623
c906108c
SS
7624/* Commands to deal with catching events, such as signals, exceptions,
7625 process start/exit, etc. */
c5aa993b
JM
7626
7627typedef enum
7628{
44feb3ce
TT
7629 catch_fork_temporary, catch_vfork_temporary,
7630 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
7631}
7632catch_fork_kind;
7633
c906108c 7634static void
44feb3ce 7635catch_fork_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
c906108c 7636{
a6d9a66e 7637 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 7638 char *cond_string = NULL;
44feb3ce
TT
7639 catch_fork_kind fork_kind;
7640 int tempflag;
7641
7642 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
7643 tempflag = (fork_kind == catch_fork_temporary
7644 || fork_kind == catch_vfork_temporary);
c5aa993b 7645
44feb3ce
TT
7646 if (!arg)
7647 arg = "";
c906108c 7648 ep_skip_leading_whitespace (&arg);
c5aa993b 7649
c906108c 7650 /* The allowed syntax is:
c5aa993b
JM
7651 catch [v]fork
7652 catch [v]fork if <cond>
7653
c906108c
SS
7654 First, check if there's an if clause. */
7655 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 7656
c906108c 7657 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 7658 error (_("Junk at end of arguments."));
c5aa993b 7659
c906108c
SS
7660 /* If this target supports it, create a fork or vfork catchpoint
7661 and enable reporting of such events. */
c5aa993b
JM
7662 switch (fork_kind)
7663 {
44feb3ce
TT
7664 case catch_fork_temporary:
7665 case catch_fork_permanent:
a6d9a66e 7666 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 7667 &catch_fork_breakpoint_ops);
c906108c 7668 break;
44feb3ce
TT
7669 case catch_vfork_temporary:
7670 case catch_vfork_permanent:
a6d9a66e 7671 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 7672 &catch_vfork_breakpoint_ops);
c906108c 7673 break;
c5aa993b 7674 default:
8a3fe4f8 7675 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 7676 break;
c5aa993b 7677 }
c906108c
SS
7678}
7679
7680static void
44feb3ce 7681catch_exec_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
c906108c 7682{
a6d9a66e 7683 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 7684 int tempflag;
c5aa993b 7685 char *cond_string = NULL;
c906108c 7686
44feb3ce
TT
7687 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7688
7689 if (!arg)
7690 arg = "";
c906108c
SS
7691 ep_skip_leading_whitespace (&arg);
7692
7693 /* The allowed syntax is:
c5aa993b
JM
7694 catch exec
7695 catch exec if <cond>
c906108c
SS
7696
7697 First, check if there's an if clause. */
7698 cond_string = ep_parse_optional_if_clause (&arg);
7699
7700 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 7701 error (_("Junk at end of arguments."));
c906108c
SS
7702
7703 /* If this target supports it, create an exec catchpoint
7704 and enable reporting of such events. */
a6d9a66e
UW
7705 create_catchpoint (gdbarch, tempflag, cond_string,
7706 &catch_exec_breakpoint_ops);
c906108c 7707}
c5aa993b 7708
3086aeae
DJ
7709static enum print_stop_action
7710print_exception_catchpoint (struct breakpoint *b)
7711{
ade92717 7712 int bp_temp, bp_throw;
3086aeae 7713
ade92717 7714 annotate_catchpoint (b->number);
3086aeae 7715
ade92717
AR
7716 bp_throw = strstr (b->addr_string, "throw") != NULL;
7717 if (b->loc->address != b->loc->requested_address)
7718 breakpoint_adjustment_warning (b->loc->requested_address,
7719 b->loc->address,
7720 b->number, 1);
df2b6d2d 7721 bp_temp = b->disposition == disp_del;
ade92717
AR
7722 ui_out_text (uiout,
7723 bp_temp ? "Temporary catchpoint "
7724 : "Catchpoint ");
7725 if (!ui_out_is_mi_like_p (uiout))
7726 ui_out_field_int (uiout, "bkptno", b->number);
7727 ui_out_text (uiout,
c0b37c48
AR
7728 bp_throw ? " (exception thrown), "
7729 : " (exception caught), ");
ade92717
AR
7730 if (ui_out_is_mi_like_p (uiout))
7731 {
7732 ui_out_field_string (uiout, "reason",
7733 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
7734 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7735 ui_out_field_int (uiout, "bkptno", b->number);
7736 }
3086aeae
DJ
7737 return PRINT_SRC_AND_LOC;
7738}
7739
7740static void
a6d9a66e 7741print_one_exception_catchpoint (struct breakpoint *b, struct bp_location **last_loc)
3086aeae 7742{
79a45b7d
TT
7743 struct value_print_options opts;
7744 get_user_print_options (&opts);
7745 if (opts.addressprint)
3086aeae
DJ
7746 {
7747 annotate_field (4);
604133b5
AR
7748 if (b->loc == NULL || b->loc->shlib_disabled)
7749 ui_out_field_string (uiout, "addr", "<PENDING>");
7750 else
5af949e3
UW
7751 ui_out_field_core_addr (uiout, "addr",
7752 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
7753 }
7754 annotate_field (5);
604133b5 7755 if (b->loc)
a6d9a66e 7756 *last_loc = b->loc;
3086aeae
DJ
7757 if (strstr (b->addr_string, "throw") != NULL)
7758 ui_out_field_string (uiout, "what", "exception throw");
7759 else
7760 ui_out_field_string (uiout, "what", "exception catch");
7761}
7762
7763static void
7764print_mention_exception_catchpoint (struct breakpoint *b)
7765{
ade92717
AR
7766 int bp_temp;
7767 int bp_throw;
7768
df2b6d2d 7769 bp_temp = b->disposition == disp_del;
ade92717
AR
7770 bp_throw = strstr (b->addr_string, "throw") != NULL;
7771 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
7772 : _("Catchpoint "));
7773 ui_out_field_int (uiout, "bkptno", b->number);
7774 ui_out_text (uiout, bp_throw ? _(" (throw)")
7775 : _(" (catch)"));
3086aeae
DJ
7776}
7777
7778static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
ce78b96d
JB
7779 NULL, /* insert */
7780 NULL, /* remove */
7781 NULL, /* breakpoint_hit */
3086aeae
DJ
7782 print_exception_catchpoint,
7783 print_one_exception_catchpoint,
7784 print_mention_exception_catchpoint
7785};
7786
7787static int
7788handle_gnu_v3_exceptions (int tempflag, char *cond_string,
7789 enum exception_event_kind ex_event, int from_tty)
7790{
604133b5
AR
7791 char *trigger_func_name;
7792
3086aeae 7793 if (ex_event == EX_EVENT_CATCH)
604133b5 7794 trigger_func_name = "__cxa_begin_catch";
3086aeae 7795 else
604133b5 7796 trigger_func_name = "__cxa_throw";
3086aeae 7797
a6d9a66e
UW
7798 break_command_really (get_current_arch (),
7799 trigger_func_name, cond_string, -1,
604133b5 7800 0 /* condition and thread are valid. */,
1042e4c0 7801 tempflag, 0, 0,
604133b5
AR
7802 0,
7803 AUTO_BOOLEAN_TRUE /* pending */,
41447f92
VP
7804 &gnu_v3_exception_catchpoint_ops, from_tty,
7805 1 /* enabled */);
3086aeae 7806
3086aeae
DJ
7807 return 1;
7808}
7809
c5aa993b 7810/* Deal with "catch catch" and "catch throw" commands */
c906108c
SS
7811
7812static void
fba45db2
KB
7813catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
7814 int tempflag, int from_tty)
c906108c 7815{
c5aa993b
JM
7816 char *cond_string = NULL;
7817 struct symtab_and_line *sal = NULL;
7818
44feb3ce
TT
7819 if (!arg)
7820 arg = "";
c906108c 7821 ep_skip_leading_whitespace (&arg);
c5aa993b 7822
c906108c
SS
7823 cond_string = ep_parse_optional_if_clause (&arg);
7824
7825 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 7826 error (_("Junk at end of arguments."));
c906108c 7827
059fb39f
PM
7828 if (ex_event != EX_EVENT_THROW
7829 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 7830 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 7831
3086aeae
DJ
7832 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
7833 return;
7834
8a3fe4f8 7835 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
7836}
7837
44feb3ce
TT
7838/* Implementation of "catch catch" command. */
7839
7840static void
7841catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
7842{
7843 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7844 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
7845}
7846
7847/* Implementation of "catch throw" command. */
7848
7849static void
7850catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
7851{
7852 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7853 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
7854}
7855
f7f9143b
JB
7856/* Create a breakpoint struct for Ada exception catchpoints. */
7857
7858static void
a6d9a66e
UW
7859create_ada_exception_breakpoint (struct gdbarch *gdbarch,
7860 struct symtab_and_line sal,
f7f9143b
JB
7861 char *addr_string,
7862 char *exp_string,
7863 char *cond_string,
7864 struct expression *cond,
7865 struct breakpoint_ops *ops,
7866 int tempflag,
7867 int from_tty)
7868{
7869 struct breakpoint *b;
7870
7871 if (from_tty)
7872 {
5af949e3
UW
7873 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7874 if (!loc_gdbarch)
7875 loc_gdbarch = gdbarch;
7876
6c95b8df
PA
7877 describe_other_breakpoints (loc_gdbarch,
7878 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
7879 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
7880 version for exception catchpoints, because two catchpoints
7881 used for different exception names will use the same address.
7882 In this case, a "breakpoint ... also set at..." warning is
7883 unproductive. Besides. the warning phrasing is also a bit
7884 inapropriate, we should use the word catchpoint, and tell
7885 the user what type of catchpoint it is. The above is good
7886 enough for now, though. */
7887 }
7888
a6d9a66e 7889 b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
f7f9143b
JB
7890 set_breakpoint_count (breakpoint_count + 1);
7891
7892 b->enable_state = bp_enabled;
7893 b->disposition = tempflag ? disp_del : disp_donttouch;
7894 b->number = breakpoint_count;
7895 b->ignore_count = 0;
511a6cd4 7896 b->loc->cond = cond;
f7f9143b
JB
7897 b->addr_string = addr_string;
7898 b->language = language_ada;
7899 b->cond_string = cond_string;
7900 b->exp_string = exp_string;
7901 b->thread = -1;
7902 b->ops = ops;
f7f9143b
JB
7903
7904 mention (b);
b60e7edf 7905 update_global_location_list (1);
f7f9143b
JB
7906}
7907
7908/* Implement the "catch exception" command. */
7909
7910static void
44feb3ce
TT
7911catch_ada_exception_command (char *arg, int from_tty,
7912 struct cmd_list_element *command)
f7f9143b 7913{
a6d9a66e 7914 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 7915 int tempflag;
f7f9143b
JB
7916 struct symtab_and_line sal;
7917 enum bptype type;
7918 char *addr_string = NULL;
7919 char *exp_string = NULL;
7920 char *cond_string = NULL;
7921 struct expression *cond = NULL;
7922 struct breakpoint_ops *ops = NULL;
7923
44feb3ce
TT
7924 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7925
7926 if (!arg)
7927 arg = "";
f7f9143b
JB
7928 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
7929 &cond_string, &cond, &ops);
a6d9a66e 7930 create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
f7f9143b
JB
7931 cond_string, cond, ops, tempflag,
7932 from_tty);
7933}
7934
a96d9b2e
SDJ
7935/* Cleanup function for a syscall filter list. */
7936static void
7937clean_up_filters (void *arg)
7938{
7939 VEC(int) *iter = *(VEC(int) **) arg;
7940 VEC_free (int, iter);
7941}
7942
7943/* Splits the argument using space as delimiter. Returns an xmalloc'd
7944 filter list, or NULL if no filtering is required. */
7945static VEC(int) *
7946catch_syscall_split_args (char *arg)
7947{
7948 VEC(int) *result = NULL;
7949 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
7950
7951 while (*arg != '\0')
7952 {
7953 int i, syscall_number;
7954 char *endptr;
7955 char cur_name[128];
7956 struct syscall s;
7957
7958 /* Skip whitespace. */
7959 while (isspace (*arg))
7960 arg++;
7961
7962 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
7963 cur_name[i] = arg[i];
7964 cur_name[i] = '\0';
7965 arg += i;
7966
7967 /* Check if the user provided a syscall name or a number. */
7968 syscall_number = (int) strtol (cur_name, &endptr, 0);
7969 if (*endptr == '\0')
bccd0dd2 7970 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
7971 else
7972 {
7973 /* We have a name. Let's check if it's valid and convert it
7974 to a number. */
7975 get_syscall_by_name (cur_name, &s);
7976
7977 if (s.number == UNKNOWN_SYSCALL)
7978 /* Here we have to issue an error instead of a warning, because
7979 GDB cannot do anything useful if there's no syscall number to
7980 be caught. */
7981 error (_("Unknown syscall name '%s'."), cur_name);
7982 }
7983
7984 /* Ok, it's valid. */
7985 VEC_safe_push (int, result, s.number);
7986 }
7987
7988 discard_cleanups (cleanup);
7989 return result;
7990}
7991
7992/* Implement the "catch syscall" command. */
7993
7994static void
7995catch_syscall_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
7996{
7997 int tempflag;
7998 VEC(int) *filter;
7999 struct syscall s;
8000 struct gdbarch *gdbarch = get_current_arch ();
8001
8002 /* Checking if the feature if supported. */
8003 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
8004 error (_("The feature 'catch syscall' is not supported on \
8005this architeture yet."));
8006
8007 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8008
8009 ep_skip_leading_whitespace (&arg);
8010
8011 /* We need to do this first "dummy" translation in order
8012 to get the syscall XML file loaded or, most important,
8013 to display a warning to the user if there's no XML file
8014 for his/her architecture. */
8015 get_syscall_by_number (0, &s);
8016
8017 /* The allowed syntax is:
8018 catch syscall
8019 catch syscall <name | number> [<name | number> ... <name | number>]
8020
8021 Let's check if there's a syscall name. */
8022
8023 if (arg != NULL)
8024 filter = catch_syscall_split_args (arg);
8025 else
8026 filter = NULL;
8027
8028 create_syscall_event_catchpoint (tempflag, filter,
8029 &catch_syscall_breakpoint_ops);
8030}
8031
f7f9143b
JB
8032/* Implement the "catch assert" command. */
8033
8034static void
44feb3ce 8035catch_assert_command (char *arg, int from_tty, struct cmd_list_element *command)
f7f9143b 8036{
a6d9a66e 8037 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 8038 int tempflag;
f7f9143b
JB
8039 struct symtab_and_line sal;
8040 char *addr_string = NULL;
8041 struct breakpoint_ops *ops = NULL;
8042
44feb3ce
TT
8043 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8044
8045 if (!arg)
8046 arg = "";
f7f9143b 8047 sal = ada_decode_assert_location (arg, &addr_string, &ops);
a6d9a66e
UW
8048 create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
8049 ops, tempflag, from_tty);
f7f9143b
JB
8050}
8051
c906108c 8052static void
fba45db2 8053catch_command (char *arg, int from_tty)
c906108c 8054{
44feb3ce 8055 error (_("Catch requires an event name."));
c906108c
SS
8056}
8057\f
8058
8059static void
fba45db2 8060tcatch_command (char *arg, int from_tty)
c906108c 8061{
44feb3ce 8062 error (_("Catch requires an event name."));
c906108c
SS
8063}
8064
80f8a6eb 8065/* Delete breakpoints by address or line. */
c906108c
SS
8066
8067static void
fba45db2 8068clear_command (char *arg, int from_tty)
c906108c 8069{
d6e956e5
VP
8070 struct breakpoint *b;
8071 VEC(breakpoint_p) *found = 0;
8072 int ix;
c906108c
SS
8073 int default_match;
8074 struct symtabs_and_lines sals;
8075 struct symtab_and_line sal;
c906108c
SS
8076 int i;
8077
8078 if (arg)
8079 {
8080 sals = decode_line_spec (arg, 1);
8081 default_match = 0;
8082 }
8083 else
8084 {
c5aa993b 8085 sals.sals = (struct symtab_and_line *)
c906108c 8086 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 8087 make_cleanup (xfree, sals.sals);
fe39c653 8088 init_sal (&sal); /* initialize to zeroes */
c906108c
SS
8089 sal.line = default_breakpoint_line;
8090 sal.symtab = default_breakpoint_symtab;
8091 sal.pc = default_breakpoint_address;
6c95b8df 8092 sal.pspace = default_breakpoint_pspace;
c906108c 8093 if (sal.symtab == 0)
8a3fe4f8 8094 error (_("No source file specified."));
c906108c
SS
8095
8096 sals.sals[0] = sal;
8097 sals.nelts = 1;
8098
8099 default_match = 1;
8100 }
8101
ed0616c6
VP
8102 /* We don't call resolve_sal_pc here. That's not
8103 as bad as it seems, because all existing breakpoints
8104 typically have both file/line and pc set. So, if
8105 clear is given file/line, we can match this to existing
8106 breakpoint without obtaining pc at all.
8107
8108 We only support clearing given the address explicitly
8109 present in breakpoint table. Say, we've set breakpoint
8110 at file:line. There were several PC values for that file:line,
8111 due to optimization, all in one block.
8112 We've picked one PC value. If "clear" is issued with another
8113 PC corresponding to the same file:line, the breakpoint won't
8114 be cleared. We probably can still clear the breakpoint, but
8115 since the other PC value is never presented to user, user
8116 can only find it by guessing, and it does not seem important
8117 to support that. */
8118
c906108c 8119 /* For each line spec given, delete bps which correspond
80f8a6eb
MS
8120 to it. Do it in two passes, solely to preserve the current
8121 behavior that from_tty is forced true if we delete more than
8122 one breakpoint. */
c906108c 8123
80f8a6eb 8124 found = NULL;
c906108c
SS
8125 for (i = 0; i < sals.nelts; i++)
8126 {
8127 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
8128 If line given (pc == 0), clear all bpts on specified line.
8129 If defaulting, clear all bpts on default line
c906108c 8130 or at default pc.
c5aa993b
JM
8131
8132 defaulting sal.pc != 0 tests to do
8133
8134 0 1 pc
8135 1 1 pc _and_ line
8136 0 0 line
8137 1 0 <can't happen> */
c906108c
SS
8138
8139 sal = sals.sals[i];
c906108c 8140
d6e956e5
VP
8141 /* Find all matching breakpoints and add them to
8142 'found'. */
8143 ALL_BREAKPOINTS (b)
c5aa993b 8144 {
0d381245 8145 int match = 0;
80f8a6eb
MS
8146 /* Are we going to delete b? */
8147 if (b->type != bp_none
8148 && b->type != bp_watchpoint
8149 && b->type != bp_hardware_watchpoint
8150 && b->type != bp_read_watchpoint
0d381245
VP
8151 && b->type != bp_access_watchpoint)
8152 {
8153 struct bp_location *loc = b->loc;
8154 for (; loc; loc = loc->next)
8155 {
6c95b8df
PA
8156 int pc_match = sal.pc
8157 && (loc->pspace == sal.pspace)
0d381245
VP
8158 && (loc->address == sal.pc)
8159 && (!section_is_overlay (loc->section)
8160 || loc->section == sal.section);
8161 int line_match = ((default_match || (0 == sal.pc))
8162 && b->source_file != NULL
8163 && sal.symtab != NULL
6c95b8df 8164 && sal.pspace == loc->pspace
0d381245
VP
8165 && strcmp (b->source_file, sal.symtab->filename) == 0
8166 && b->line_number == sal.line);
8167 if (pc_match || line_match)
8168 {
8169 match = 1;
8170 break;
8171 }
8172 }
8173 }
8174
8175 if (match)
d6e956e5 8176 VEC_safe_push(breakpoint_p, found, b);
c906108c 8177 }
80f8a6eb
MS
8178 }
8179 /* Now go thru the 'found' chain and delete them. */
d6e956e5 8180 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
8181 {
8182 if (arg)
8a3fe4f8 8183 error (_("No breakpoint at %s."), arg);
80f8a6eb 8184 else
8a3fe4f8 8185 error (_("No breakpoint at this line."));
80f8a6eb 8186 }
c906108c 8187
d6e956e5 8188 if (VEC_length(breakpoint_p, found) > 1)
80f8a6eb
MS
8189 from_tty = 1; /* Always report if deleted more than one */
8190 if (from_tty)
a3f17187 8191 {
d6e956e5 8192 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
8193 printf_unfiltered (_("Deleted breakpoint "));
8194 else
8195 printf_unfiltered (_("Deleted breakpoints "));
8196 }
80f8a6eb 8197 breakpoints_changed ();
d6e956e5
VP
8198
8199 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 8200 {
c5aa993b 8201 if (from_tty)
d6e956e5
VP
8202 printf_unfiltered ("%d ", b->number);
8203 delete_breakpoint (b);
c906108c 8204 }
80f8a6eb
MS
8205 if (from_tty)
8206 putchar_unfiltered ('\n');
c906108c
SS
8207}
8208\f
8209/* Delete breakpoint in BS if they are `delete' breakpoints and
8210 all breakpoints that are marked for deletion, whether hit or not.
8211 This is called after any breakpoint is hit, or after errors. */
8212
8213void
fba45db2 8214breakpoint_auto_delete (bpstat bs)
c906108c
SS
8215{
8216 struct breakpoint *b, *temp;
8217
8218 for (; bs; bs = bs->next)
20874c92
VP
8219 if (bs->breakpoint_at
8220 && bs->breakpoint_at->owner
8221 && bs->breakpoint_at->owner->disposition == disp_del
c906108c 8222 && bs->stop)
4f8d1dc6 8223 delete_breakpoint (bs->breakpoint_at->owner);
c906108c
SS
8224
8225 ALL_BREAKPOINTS_SAFE (b, temp)
c5aa993b 8226 {
b5de0fa7 8227 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
8228 delete_breakpoint (b);
8229 }
c906108c
SS
8230}
8231
494cfb0f 8232/* A comparison function for bp_location AP and BP being interfaced to qsort.
876fa593
JK
8233 Sort elements primarily by their ADDRESS (no matter what does
8234 breakpoint_address_is_meaningful say for its OWNER), secondarily by ordering
8235 first bp_permanent OWNERed elements and terciarily just ensuring the array
8236 is sorted stable way despite qsort being an instable algorithm. */
8237
8238static int
494cfb0f 8239bp_location_compare (const void *ap, const void *bp)
876fa593 8240{
494cfb0f
JK
8241 struct bp_location *a = *(void **) ap;
8242 struct bp_location *b = *(void **) bp;
876fa593
JK
8243 int a_perm = a->owner->enable_state == bp_permanent;
8244 int b_perm = b->owner->enable_state == bp_permanent;
8245
8246 if (a->address != b->address)
8247 return (a->address > b->address) - (a->address < b->address);
8248
8249 /* Sort permanent breakpoints first. */
8250 if (a_perm != b_perm)
8251 return (a_perm < b_perm) - (a_perm > b_perm);
8252
8253 /* Make the user-visible order stable across GDB runs. Locations of the same
8254 breakpoint can be sorted in arbitrary order. */
8255
8256 if (a->owner->number != b->owner->number)
8257 return (a->owner->number > b->owner->number)
8258 - (a->owner->number < b->owner->number);
8259
8260 return (a > b) - (a < b);
8261}
8262
876fa593
JK
8263/* Set bp_location_placed_address_before_address_max and
8264 bp_location_shadow_len_after_address_max according to the current content of
8265 the bp_location array. */
f7545552
TT
8266
8267static void
876fa593 8268bp_location_target_extensions_update (void)
f7545552 8269{
876fa593
JK
8270 struct bp_location *bl, **blp_tmp;
8271
8272 bp_location_placed_address_before_address_max = 0;
8273 bp_location_shadow_len_after_address_max = 0;
8274
8275 ALL_BP_LOCATIONS (bl, blp_tmp)
8276 {
8277 CORE_ADDR start, end, addr;
8278
8279 if (!bp_location_has_shadow (bl))
8280 continue;
8281
8282 start = bl->target_info.placed_address;
8283 end = start + bl->target_info.shadow_len;
8284
8285 gdb_assert (bl->address >= start);
8286 addr = bl->address - start;
8287 if (addr > bp_location_placed_address_before_address_max)
8288 bp_location_placed_address_before_address_max = addr;
8289
8290 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
8291
8292 gdb_assert (bl->address < end);
8293 addr = end - bl->address;
8294 if (addr > bp_location_shadow_len_after_address_max)
8295 bp_location_shadow_len_after_address_max = addr;
8296 }
f7545552
TT
8297}
8298
4cd9bd08 8299/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
8300 into the inferior, only remove already-inserted locations that no
8301 longer should be inserted. Functions that delete a breakpoint or
8302 breakpoints should pass false, so that deleting a breakpoint
8303 doesn't have the side effect of inserting the locations of other
8304 breakpoints that are marked not-inserted, but should_be_inserted
8305 returns true on them.
8306
8307 This behaviour is useful is situations close to tear-down -- e.g.,
8308 after an exec, while the target still has execution, but breakpoint
8309 shadows of the previous executable image should *NOT* be restored
8310 to the new image; or before detaching, where the target still has
8311 execution and wants to delete breakpoints from GDB's lists, and all
8312 breakpoints had already been removed from the inferior. */
8313
0d381245 8314static void
b60e7edf 8315update_global_location_list (int should_insert)
0d381245 8316{
74960c60 8317 struct breakpoint *b;
876fa593 8318 struct bp_location **locp, *loc;
f7545552
TT
8319 struct cleanup *cleanups;
8320
2d134ed3
PA
8321 /* Used in the duplicates detection below. When iterating over all
8322 bp_locations, points to the first bp_location of a given address.
8323 Breakpoints and watchpoints of different types are never
8324 duplicates of each other. Keep one pointer for each type of
8325 breakpoint/watchpoint, so we only need to loop over all locations
8326 once. */
8327 struct bp_location *bp_loc_first; /* breakpoint */
8328 struct bp_location *wp_loc_first; /* hardware watchpoint */
8329 struct bp_location *awp_loc_first; /* access watchpoint */
8330 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593
JK
8331
8332 /* Saved former bp_location array which we compare against the newly built
8333 bp_location from the current state of ALL_BREAKPOINTS. */
8334 struct bp_location **old_location, **old_locp;
8335 unsigned old_location_count;
8336
8337 old_location = bp_location;
8338 old_location_count = bp_location_count;
8339 bp_location = NULL;
8340 bp_location_count = 0;
8341 cleanups = make_cleanup (xfree, old_location);
0d381245 8342
74960c60 8343 ALL_BREAKPOINTS (b)
876fa593
JK
8344 for (loc = b->loc; loc; loc = loc->next)
8345 bp_location_count++;
8346
8347 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
8348 locp = bp_location;
8349 ALL_BREAKPOINTS (b)
8350 for (loc = b->loc; loc; loc = loc->next)
8351 *locp++ = loc;
8352 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 8353 bp_location_compare);
876fa593
JK
8354
8355 bp_location_target_extensions_update ();
74960c60
VP
8356
8357 /* Identify bp_location instances that are no longer present in the new
8358 list, and therefore should be freed. Note that it's not necessary that
8359 those locations should be removed from inferior -- if there's another
8360 location at the same address (previously marked as duplicate),
876fa593
JK
8361 we don't need to remove/insert the location.
8362
8363 LOCP is kept in sync with OLD_LOCP, each pointing to the current and
8364 former bp_location array state respectively. */
8365
8366 locp = bp_location;
8367 for (old_locp = old_location; old_locp < old_location + old_location_count;
8368 old_locp++)
74960c60 8369 {
876fa593 8370 struct bp_location *old_loc = *old_locp;
c7d46a38 8371 struct bp_location **loc2p;
876fa593
JK
8372
8373 /* Tells if 'old_loc' is found amoung the new locations. If not, we
74960c60 8374 have to free it. */
c7d46a38 8375 int found_object = 0;
20874c92
VP
8376 /* Tells if the location should remain inserted in the target. */
8377 int keep_in_target = 0;
8378 int removed = 0;
876fa593
JK
8379
8380 /* Skip LOCP entries which will definitely never be needed. Stop either
8381 at or being the one matching OLD_LOC. */
8382 while (locp < bp_location + bp_location_count
c7d46a38 8383 && (*locp)->address < old_loc->address)
876fa593 8384 locp++;
c7d46a38
PA
8385
8386 for (loc2p = locp;
8387 (loc2p < bp_location + bp_location_count
8388 && (*loc2p)->address == old_loc->address);
8389 loc2p++)
8390 {
8391 if (*loc2p == old_loc)
8392 {
8393 found_object = 1;
8394 break;
8395 }
8396 }
74960c60
VP
8397
8398 /* If this location is no longer present, and inserted, look if there's
8399 maybe a new location at the same address. If so, mark that one
8400 inserted, and don't remove this one. This is needed so that we
8401 don't have a time window where a breakpoint at certain location is not
8402 inserted. */
8403
876fa593 8404 if (old_loc->inserted)
0d381245 8405 {
74960c60 8406 /* If the location is inserted now, we might have to remove it. */
74960c60 8407
876fa593 8408 if (found_object && should_be_inserted (old_loc))
74960c60
VP
8409 {
8410 /* The location is still present in the location list, and still
8411 should be inserted. Don't do anything. */
20874c92 8412 keep_in_target = 1;
74960c60
VP
8413 }
8414 else
8415 {
8416 /* The location is either no longer present, or got disabled.
8417 See if there's another location at the same address, in which
8418 case we don't need to remove this one from the target. */
876fa593
JK
8419
8420 if (breakpoint_address_is_meaningful (old_loc->owner))
8421 {
876fa593 8422 for (loc2p = locp;
c7d46a38
PA
8423 (loc2p < bp_location + bp_location_count
8424 && (*loc2p)->address == old_loc->address);
876fa593
JK
8425 loc2p++)
8426 {
8427 struct bp_location *loc2 = *loc2p;
8428
2d134ed3 8429 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38
PA
8430 {
8431 /* For the sake of should_be_inserted.
8432 Duplicates check below will fix up this later. */
8433 loc2->duplicate = 0;
8434 if (loc2 != old_loc && should_be_inserted (loc2))
8435 {
8436 loc2->inserted = 1;
8437 loc2->target_info = old_loc->target_info;
8438 keep_in_target = 1;
8439 break;
8440 }
876fa593
JK
8441 }
8442 }
8443 }
74960c60
VP
8444 }
8445
20874c92
VP
8446 if (!keep_in_target)
8447 {
876fa593 8448 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92
VP
8449 {
8450 /* This is just about all we can do. We could keep this
8451 location on the global list, and try to remove it next
8452 time, but there's no particular reason why we will
8453 succeed next time.
8454
876fa593 8455 Note that at this point, old_loc->owner is still valid,
20874c92
VP
8456 as delete_breakpoint frees the breakpoint only
8457 after calling us. */
8458 printf_filtered (_("warning: Error removing breakpoint %d\n"),
876fa593 8459 old_loc->owner->number);
20874c92
VP
8460 }
8461 removed = 1;
8462 }
0d381245 8463 }
74960c60
VP
8464
8465 if (!found_object)
1c5cfe86 8466 {
db82e815
PA
8467 if (removed && non_stop
8468 && breakpoint_address_is_meaningful (old_loc->owner)
8469 && !is_hardware_watchpoint (old_loc->owner))
20874c92 8470 {
db82e815
PA
8471 /* This location was removed from the target. In
8472 non-stop mode, a race condition is possible where
8473 we've removed a breakpoint, but stop events for that
8474 breakpoint are already queued and will arrive later.
8475 We apply an heuristic to be able to distinguish such
8476 SIGTRAPs from other random SIGTRAPs: we keep this
8477 breakpoint location for a bit, and will retire it
8478 after we see some number of events. The theory here
8479 is that reporting of events should, "on the average",
8480 be fair, so after a while we'll see events from all
8481 threads that have anything of interest, and no longer
8482 need to keep this breakpoint location around. We
8483 don't hold locations forever so to reduce chances of
8484 mistaking a non-breakpoint SIGTRAP for a breakpoint
8485 SIGTRAP.
8486
8487 The heuristic failing can be disastrous on
8488 decr_pc_after_break targets.
8489
8490 On decr_pc_after_break targets, like e.g., x86-linux,
8491 if we fail to recognize a late breakpoint SIGTRAP,
8492 because events_till_retirement has reached 0 too
8493 soon, we'll fail to do the PC adjustment, and report
8494 a random SIGTRAP to the user. When the user resumes
8495 the inferior, it will most likely immediately crash
8496 with SIGILL/SIGBUS/SEGSEGV, or worse, get silently
8497 corrupted, because of being resumed e.g., in the
8498 middle of a multi-byte instruction, or skipped a
8499 one-byte instruction. This was actually seen happen
8500 on native x86-linux, and should be less rare on
8501 targets that do not support new thread events, like
8502 remote, due to the heuristic depending on
8503 thread_count.
8504
8505 Mistaking a random SIGTRAP for a breakpoint trap
8506 causes similar symptoms (PC adjustment applied when
8507 it shouldn't), but then again, playing with SIGTRAPs
8508 behind the debugger's back is asking for trouble.
8509
8510 Since hardware watchpoint traps are always
8511 distinguishable from other traps, so we don't need to
8512 apply keep hardware watchpoint moribund locations
8513 around. We simply always ignore hardware watchpoint
8514 traps we can no longer explain. */
8515
876fa593
JK
8516 old_loc->events_till_retirement = 3 * (thread_count () + 1);
8517 old_loc->owner = NULL;
20874c92 8518
876fa593 8519 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
8520 }
8521 else
876fa593 8522 free_bp_location (old_loc);
20874c92 8523 }
74960c60 8524 }
1c5cfe86 8525
2d134ed3
PA
8526 /* Rescan breakpoints at the same address and section, marking the
8527 first one as "first" and any others as "duplicates". This is so
8528 that the bpt instruction is only inserted once. If we have a
8529 permanent breakpoint at the same place as BPT, make that one the
8530 official one, and the rest as duplicates. Permanent breakpoints
8531 are sorted first for the same address.
8532
8533 Do the same for hardware watchpoints, but also considering the
8534 watchpoint's type (regular/access/read) and length. */
876fa593 8535
2d134ed3
PA
8536 bp_loc_first = NULL;
8537 wp_loc_first = NULL;
8538 awp_loc_first = NULL;
8539 rwp_loc_first = NULL;
876fa593 8540 ALL_BP_LOCATIONS (loc, locp)
74960c60 8541 {
876fa593 8542 struct breakpoint *b = loc->owner;
2d134ed3 8543 struct bp_location **loc_first_p;
876fa593
JK
8544
8545 if (b->enable_state == bp_disabled
8546 || b->enable_state == bp_call_disabled
8547 || b->enable_state == bp_startup_disabled
8548 || !loc->enabled
8549 || loc->shlib_disabled
8550 || !breakpoint_address_is_meaningful (b))
8551 continue;
8552
8553 /* Permanent breakpoint should always be inserted. */
8554 if (b->enable_state == bp_permanent && ! loc->inserted)
8555 internal_error (__FILE__, __LINE__,
8556 _("allegedly permanent breakpoint is not "
8557 "actually inserted"));
8558
2d134ed3
PA
8559 if (b->type == bp_hardware_watchpoint)
8560 loc_first_p = &wp_loc_first;
8561 else if (b->type == bp_read_watchpoint)
8562 loc_first_p = &rwp_loc_first;
8563 else if (b->type == bp_access_watchpoint)
8564 loc_first_p = &awp_loc_first;
8565 else
8566 loc_first_p = &bp_loc_first;
8567
8568 if (*loc_first_p == NULL
8569 || (overlay_debugging && loc->section != (*loc_first_p)->section)
8570 || !breakpoint_locations_match (loc, *loc_first_p))
876fa593 8571 {
2d134ed3 8572 *loc_first_p = loc;
876fa593
JK
8573 loc->duplicate = 0;
8574 continue;
8575 }
8576
8577 loc->duplicate = 1;
8578
2d134ed3
PA
8579 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
8580 && b->enable_state != bp_permanent)
876fa593
JK
8581 internal_error (__FILE__, __LINE__,
8582 _("another breakpoint was inserted on top of "
8583 "a permanent breakpoint"));
0d381245 8584 }
74960c60 8585
50c71eaf 8586 if (breakpoints_always_inserted_mode () && should_insert
c35b1492 8587 && (have_live_inferiors ()
2567c7d9 8588 || (gdbarch_has_global_breakpoints (target_gdbarch))))
74960c60 8589 insert_breakpoint_locations ();
f7545552
TT
8590
8591 do_cleanups (cleanups);
74960c60
VP
8592}
8593
20874c92
VP
8594void
8595breakpoint_retire_moribund (void)
8596{
8597 struct bp_location *loc;
8598 int ix;
8599
8600 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
8601 if (--(loc->events_till_retirement) == 0)
8602 {
8603 free_bp_location (loc);
8604 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
8605 --ix;
8606 }
8607}
8608
74960c60 8609static void
b60e7edf 8610update_global_location_list_nothrow (int inserting)
74960c60
VP
8611{
8612 struct gdb_exception e;
8613 TRY_CATCH (e, RETURN_MASK_ERROR)
b60e7edf 8614 update_global_location_list (inserting);
0d381245
VP
8615}
8616
a474d7c2
PA
8617/* Clear BPT from a BPS. */
8618static void
8619bpstat_remove_breakpoint (bpstat bps, struct breakpoint *bpt)
8620{
8621 bpstat bs;
8622 for (bs = bps; bs; bs = bs->next)
8623 if (bs->breakpoint_at && bs->breakpoint_at->owner == bpt)
8624 {
8625 bs->breakpoint_at = NULL;
8626 bs->old_val = NULL;
8627 /* bs->commands will be freed later. */
8628 }
8629}
8630
8631/* Callback for iterate_over_threads. */
8632static int
8633bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
8634{
8635 struct breakpoint *bpt = data;
8636 bpstat_remove_breakpoint (th->stop_bpstat, bpt);
8637 return 0;
8638}
8639
53a5351d
JM
8640/* Delete a breakpoint and clean up all traces of it in the data
8641 structures. */
c906108c
SS
8642
8643void
fba45db2 8644delete_breakpoint (struct breakpoint *bpt)
c906108c 8645{
52f0bd74 8646 struct breakpoint *b;
74960c60 8647 struct bp_location *loc, *next;
c906108c 8648
8a3fe4f8 8649 gdb_assert (bpt != NULL);
c906108c
SS
8650
8651 /* Has this bp already been deleted? This can happen because multiple
8652 lists can hold pointers to bp's. bpstat lists are especial culprits.
8653
8654 One example of this happening is a watchpoint's scope bp. When the
8655 scope bp triggers, we notice that the watchpoint is out of scope, and
8656 delete it. We also delete its scope bp. But the scope bp is marked
8657 "auto-deleting", and is already on a bpstat. That bpstat is then
8658 checked for auto-deleting bp's, which are deleted.
8659
8660 A real solution to this problem might involve reference counts in bp's,
8661 and/or giving them pointers back to their referencing bpstat's, and
8662 teaching delete_breakpoint to only free a bp's storage when no more
1272ad14 8663 references were extent. A cheaper bandaid was chosen. */
c906108c
SS
8664 if (bpt->type == bp_none)
8665 return;
8666
383f836e 8667 observer_notify_breakpoint_deleted (bpt->number);
c906108c 8668
c906108c
SS
8669 if (breakpoint_chain == bpt)
8670 breakpoint_chain = bpt->next;
8671
c906108c
SS
8672 ALL_BREAKPOINTS (b)
8673 if (b->next == bpt)
c5aa993b
JM
8674 {
8675 b->next = bpt->next;
8676 break;
8677 }
c906108c 8678
c906108c 8679 free_command_lines (&bpt->commands);
c906108c 8680 if (bpt->cond_string != NULL)
b8c9b27d 8681 xfree (bpt->cond_string);
c906108c 8682 if (bpt->addr_string != NULL)
b8c9b27d 8683 xfree (bpt->addr_string);
c906108c 8684 if (bpt->exp != NULL)
b8c9b27d 8685 xfree (bpt->exp);
c906108c 8686 if (bpt->exp_string != NULL)
b8c9b27d 8687 xfree (bpt->exp_string);
c906108c
SS
8688 if (bpt->val != NULL)
8689 value_free (bpt->val);
8690 if (bpt->source_file != NULL)
b8c9b27d 8691 xfree (bpt->source_file);
c906108c 8692 if (bpt->exec_pathname != NULL)
b8c9b27d 8693 xfree (bpt->exec_pathname);
a96d9b2e 8694 clean_up_filters (&bpt->syscalls_to_be_caught);
c906108c
SS
8695
8696 /* Be sure no bpstat's are pointing at it after it's been freed. */
8697 /* FIXME, how can we find all bpstat's?
198757a8
VP
8698 We just check stop_bpstat for now. Note that we cannot just
8699 remove bpstats pointing at bpt from the stop_bpstat list
8700 entirely, as breakpoint commands are associated with the bpstat;
8701 if we remove it here, then the later call to
8702 bpstat_do_actions (&stop_bpstat);
8703 in event-top.c won't do anything, and temporary breakpoints
8704 with commands won't work. */
a474d7c2 8705
a474d7c2 8706 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
74960c60
VP
8707
8708 /* Now that breakpoint is removed from breakpoint
8709 list, update the global location list. This
8710 will remove locations that used to belong to
8711 this breakpoint. Do this before freeing
8712 the breakpoint itself, since remove_breakpoint
8713 looks at location's owner. It might be better
8714 design to have location completely self-contained,
8715 but it's not the case now. */
b60e7edf 8716 update_global_location_list (0);
74960c60
VP
8717
8718
c906108c
SS
8719 /* On the chance that someone will soon try again to delete this same
8720 bp, we mark it as deleted before freeing its storage. */
8721 bpt->type = bp_none;
8722
b8c9b27d 8723 xfree (bpt);
c906108c
SS
8724}
8725
4d6140d9
AC
8726static void
8727do_delete_breakpoint_cleanup (void *b)
8728{
8729 delete_breakpoint (b);
8730}
8731
8732struct cleanup *
8733make_cleanup_delete_breakpoint (struct breakpoint *b)
8734{
8735 return make_cleanup (do_delete_breakpoint_cleanup, b);
8736}
8737
c906108c 8738void
fba45db2 8739delete_command (char *arg, int from_tty)
c906108c
SS
8740{
8741 struct breakpoint *b, *temp;
8742
ea9365bb
TT
8743 dont_repeat ();
8744
c906108c
SS
8745 if (arg == 0)
8746 {
8747 int breaks_to_delete = 0;
8748
8749 /* Delete all breakpoints if no argument.
c5aa993b
JM
8750 Do not delete internal or call-dummy breakpoints, these
8751 have to be deleted with an explicit breakpoint number argument. */
8752 ALL_BREAKPOINTS (b)
8753 {
059fb39f
PM
8754 if (b->type != bp_call_dummy
8755 && b->type != bp_shlib_event
4efc6507 8756 && b->type != bp_jit_event
059fb39f
PM
8757 && b->type != bp_thread_event
8758 && b->type != bp_overlay_event
0fd8e87f 8759 && b->type != bp_longjmp_master
059fb39f 8760 && b->number >= 0)
973d738b
DJ
8761 {
8762 breaks_to_delete = 1;
8763 break;
8764 }
c5aa993b 8765 }
c906108c
SS
8766
8767 /* Ask user only if there are some breakpoints to delete. */
8768 if (!from_tty
e2e0b3e5 8769 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 8770 {
c5aa993b
JM
8771 ALL_BREAKPOINTS_SAFE (b, temp)
8772 {
059fb39f
PM
8773 if (b->type != bp_call_dummy
8774 && b->type != bp_shlib_event
8775 && b->type != bp_thread_event
4efc6507 8776 && b->type != bp_jit_event
059fb39f 8777 && b->type != bp_overlay_event
0fd8e87f 8778 && b->type != bp_longjmp_master
059fb39f 8779 && b->number >= 0)
c5aa993b
JM
8780 delete_breakpoint (b);
8781 }
c906108c
SS
8782 }
8783 }
8784 else
8785 map_breakpoint_numbers (arg, delete_breakpoint);
8786}
8787
0d381245
VP
8788static int
8789all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 8790{
0d381245
VP
8791 for (; loc; loc = loc->next)
8792 if (!loc->shlib_disabled)
8793 return 0;
8794 return 1;
fe3f5fa8
VP
8795}
8796
776592bf
DE
8797/* Subroutine of update_breakpoint_locations to simplify it.
8798 Return non-zero if multiple fns in list LOC have the same name.
8799 Null names are ignored. */
8800
8801static int
8802ambiguous_names_p (struct bp_location *loc)
8803{
8804 struct bp_location *l;
8805 htab_t htab = htab_create_alloc (13, htab_hash_string,
8806 (int (*) (const void *, const void *)) streq,
8807 NULL, xcalloc, xfree);
8808
8809 for (l = loc; l != NULL; l = l->next)
8810 {
8811 const char **slot;
8812 const char *name = l->function_name;
8813
8814 /* Allow for some names to be NULL, ignore them. */
8815 if (name == NULL)
8816 continue;
8817
8818 slot = (const char **) htab_find_slot (htab, (const void *) name,
8819 INSERT);
8820 /* NOTE: We can assume slot != NULL here because xcalloc never returns
8821 NULL. */
8822 if (*slot != NULL)
8823 {
8824 htab_delete (htab);
8825 return 1;
8826 }
8827 *slot = name;
8828 }
8829
8830 htab_delete (htab);
8831 return 0;
8832}
8833
fe3f5fa8 8834static void
0d381245
VP
8835update_breakpoint_locations (struct breakpoint *b,
8836 struct symtabs_and_lines sals)
fe3f5fa8
VP
8837{
8838 int i;
8839 char *s;
0d381245
VP
8840 struct bp_location *existing_locations = b->loc;
8841
8842 /* If there's no new locations, and all existing locations
8843 are pending, don't do anything. This optimizes
8844 the common case where all locations are in the same
8845 shared library, that was unloaded. We'd like to
8846 retain the location, so that when the library
8847 is loaded again, we don't loose the enabled/disabled
8848 status of the individual locations. */
8849 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
8850 return;
8851
fe3f5fa8
VP
8852 b->loc = NULL;
8853
0d381245 8854 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 8855 {
0d381245 8856 struct bp_location *new_loc =
39d61571 8857 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 8858
0d381245
VP
8859 /* Reparse conditions, they might contain references to the
8860 old symtab. */
8861 if (b->cond_string != NULL)
8862 {
8863 struct gdb_exception e;
fe3f5fa8 8864
0d381245
VP
8865 s = b->cond_string;
8866 TRY_CATCH (e, RETURN_MASK_ERROR)
8867 {
8868 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
8869 0);
8870 }
8871 if (e.reason < 0)
8872 {
8873 warning (_("failed to reevaluate condition for breakpoint %d: %s"),
8874 b->number, e.message);
8875 new_loc->enabled = 0;
8876 }
8877 }
fe3f5fa8 8878
0d381245
VP
8879 if (b->source_file != NULL)
8880 xfree (b->source_file);
8881 if (sals.sals[i].symtab == NULL)
8882 b->source_file = NULL;
8883 else
1b36a34b 8884 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 8885
0d381245
VP
8886 if (b->line_number == 0)
8887 b->line_number = sals.sals[i].line;
8888 }
fe3f5fa8 8889
514f746b
AR
8890 /* Update locations of permanent breakpoints. */
8891 if (b->enable_state == bp_permanent)
8892 make_breakpoint_permanent (b);
8893
0d381245
VP
8894 /* If possible, carry over 'disable' status from existing breakpoints. */
8895 {
8896 struct bp_location *e = existing_locations;
776592bf
DE
8897 /* If there are multiple breakpoints with the same function name,
8898 e.g. for inline functions, comparing function names won't work.
8899 Instead compare pc addresses; this is just a heuristic as things
8900 may have moved, but in practice it gives the correct answer
8901 often enough until a better solution is found. */
8902 int have_ambiguous_names = ambiguous_names_p (b->loc);
8903
0d381245
VP
8904 for (; e; e = e->next)
8905 {
8906 if (!e->enabled && e->function_name)
8907 {
8908 struct bp_location *l = b->loc;
776592bf
DE
8909 if (have_ambiguous_names)
8910 {
8911 for (; l; l = l->next)
6c95b8df
PA
8912 if (breakpoint_address_match (e->pspace->aspace, e->address,
8913 l->pspace->aspace, l->address))
776592bf
DE
8914 {
8915 l->enabled = 0;
8916 break;
8917 }
8918 }
8919 else
8920 {
8921 for (; l; l = l->next)
8922 if (l->function_name
8923 && strcmp (e->function_name, l->function_name) == 0)
8924 {
8925 l->enabled = 0;
8926 break;
8927 }
8928 }
0d381245
VP
8929 }
8930 }
8931 }
fe3f5fa8 8932
b60e7edf 8933 update_global_location_list (1);
fe3f5fa8
VP
8934}
8935
8936
c906108c
SS
8937/* Reset a breakpoint given it's struct breakpoint * BINT.
8938 The value we return ends up being the return value from catch_errors.
8939 Unused in this case. */
8940
8941static int
4efb68b1 8942breakpoint_re_set_one (void *bint)
c906108c 8943{
53a5351d
JM
8944 /* get past catch_errs */
8945 struct breakpoint *b = (struct breakpoint *) bint;
c906108c
SS
8946 struct value *mark;
8947 int i;
fe3f5fa8
VP
8948 int not_found = 0;
8949 int *not_found_ptr = &not_found;
6c95b8df
PA
8950 struct symtabs_and_lines sals = {0};
8951 struct symtabs_and_lines expanded = {0};
c906108c 8952 char *s;
b5de0fa7 8953 enum enable_state save_enable;
fe3f5fa8 8954 struct gdb_exception e;
6c95b8df 8955 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
8956
8957 switch (b->type)
8958 {
8959 case bp_none:
8a3fe4f8 8960 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
53a5351d 8961 b->number);
c906108c
SS
8962 return 0;
8963 case bp_breakpoint:
8964 case bp_hardware_breakpoint:
1042e4c0 8965 case bp_tracepoint:
8bea4e01
UW
8966 /* Do not attempt to re-set breakpoints disabled during startup. */
8967 if (b->enable_state == bp_startup_disabled)
8968 return 0;
8969
c906108c
SS
8970 if (b->addr_string == NULL)
8971 {
8972 /* Anything without a string can't be re-set. */
8973 delete_breakpoint (b);
8974 return 0;
8975 }
c906108c
SS
8976
8977 set_language (b->language);
8978 input_radix = b->input_radix;
8979 s = b->addr_string;
6c95b8df
PA
8980
8981 save_current_space_and_thread ();
8982 switch_to_program_space_and_thread (b->pspace);
8983
fe3f5fa8 8984 TRY_CATCH (e, RETURN_MASK_ERROR)
c906108c 8985 {
fe3f5fa8
VP
8986 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, (char ***) NULL,
8987 not_found_ptr);
8988 }
8989 if (e.reason < 0)
8990 {
8991 int not_found_and_ok = 0;
8992 /* For pending breakpoints, it's expected that parsing
8993 will fail until the right shared library is loaded.
8994 User has already told to create pending breakpoints and
8995 don't need extra messages. If breakpoint is in bp_shlib_disabled
8996 state, then user already saw the message about that breakpoint
8997 being disabled, and don't want to see more errors. */
0d381245
VP
8998 if (not_found
8999 && (b->condition_not_parsed
9000 || (b->loc && b->loc->shlib_disabled)
9001 || b->enable_state == bp_disabled))
fe3f5fa8
VP
9002 not_found_and_ok = 1;
9003
9004 if (!not_found_and_ok)
c906108c 9005 {
fe3f5fa8
VP
9006 /* We surely don't want to warn about the same breakpoint
9007 10 times. One solution, implemented here, is disable
9008 the breakpoint on error. Another solution would be to
9009 have separate 'warning emitted' flag. Since this
9010 happens only when a binary has changed, I don't know
9011 which approach is better. */
9012 b->enable_state = bp_disabled;
9013 throw_exception (e);
c906108c 9014 }
fe3f5fa8 9015 }
c906108c 9016
6c95b8df 9017 if (!not_found)
fe3f5fa8 9018 {
6c95b8df
PA
9019 gdb_assert (sals.nelts == 1);
9020
9021 resolve_sal_pc (&sals.sals[0]);
9022 if (b->condition_not_parsed && s && s[0])
9023 {
9024 char *cond_string = 0;
9025 int thread = -1;
9026 int task = 0;
9027
9028 find_condition_and_thread (s, sals.sals[0].pc,
9029 &cond_string, &thread, &task);
9030 if (cond_string)
9031 b->cond_string = cond_string;
9032 b->thread = thread;
9033 b->task = task;
9034 b->condition_not_parsed = 0;
9035 }
9036
9037 expanded = expand_line_sal_maybe (sals.sals[0]);
fe3f5fa8 9038 }
6c95b8df
PA
9039
9040 make_cleanup (xfree, sals.sals);
ed0616c6 9041 update_breakpoint_locations (b, expanded);
c906108c
SS
9042 break;
9043
9044 case bp_watchpoint:
9045 case bp_hardware_watchpoint:
9046 case bp_read_watchpoint:
9047 case bp_access_watchpoint:
0b3de036
VP
9048 /* Watchpoint can be either on expression using entirely global variables,
9049 or it can be on local variables.
9050
9051 Watchpoints of the first kind are never auto-deleted, and even persist
9052 across program restarts. Since they can use variables from shared
9053 libraries, we need to reparse expression as libraries are loaded
9054 and unloaded.
9055
9056 Watchpoints on local variables can also change meaning as result
9057 of solib event. For example, if a watchpoint uses both a local and
9058 a global variables in expression, it's a local watchpoint, but
9059 unloading of a shared library will make the expression invalid.
9060 This is not a very common use case, but we still re-evaluate
9061 expression, to avoid surprises to the user.
9062
9063 Note that for local watchpoints, we re-evaluate it only if
9064 watchpoints frame id is still valid. If it's not, it means
9065 the watchpoint is out of scope and will be deleted soon. In fact,
9066 I'm not sure we'll ever be called in this case.
9067
9068 If a local watchpoint's frame id is still valid, then
9069 b->exp_valid_block is likewise valid, and we can safely use it.
9070
9071 Don't do anything about disabled watchpoints, since they will
9072 be reevaluated again when enabled. */
a5606eee 9073 update_watchpoint (b, 1 /* reparse */);
c906108c 9074 break;
c5aa993b
JM
9075 /* We needn't really do anything to reset these, since the mask
9076 that requests them is unaffected by e.g., new libraries being
9077 loaded. */
ce78b96d 9078 case bp_catchpoint:
c906108c 9079 break;
c5aa993b 9080
c906108c 9081 default:
a3f17187 9082 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
c906108c 9083 /* fall through */
0fd8e87f
UW
9084 /* Delete overlay event and longjmp master breakpoints; they will be
9085 reset later by breakpoint_re_set. */
1900040c 9086 case bp_overlay_event:
0fd8e87f 9087 case bp_longjmp_master:
c906108c
SS
9088 delete_breakpoint (b);
9089 break;
9090
c5aa993b
JM
9091 /* This breakpoint is special, it's set up when the inferior
9092 starts and we really don't want to touch it. */
c906108c
SS
9093 case bp_shlib_event:
9094
c4093a6a
JM
9095 /* Like bp_shlib_event, this breakpoint type is special.
9096 Once it is set up, we do not want to touch it. */
9097 case bp_thread_event:
9098
c5aa993b
JM
9099 /* Keep temporary breakpoints, which can be encountered when we step
9100 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
9101 Otherwise these should have been blown away via the cleanup chain
9102 or by breakpoint_init_inferior when we rerun the executable. */
c906108c
SS
9103 case bp_until:
9104 case bp_finish:
9105 case bp_watchpoint_scope:
9106 case bp_call_dummy:
9107 case bp_step_resume:
611c83ae
PA
9108 case bp_longjmp:
9109 case bp_longjmp_resume:
4efc6507 9110 case bp_jit_event:
c906108c
SS
9111 break;
9112 }
9113
6c95b8df 9114 do_cleanups (cleanups);
c906108c
SS
9115 return 0;
9116}
9117
69de3c6a 9118/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 9119void
69de3c6a 9120breakpoint_re_set (void)
c906108c
SS
9121{
9122 struct breakpoint *b, *temp;
9123 enum language save_language;
9124 int save_input_radix;
6c95b8df 9125 struct cleanup *old_chain;
c5aa993b 9126
c906108c
SS
9127 save_language = current_language->la_language;
9128 save_input_radix = input_radix;
6c95b8df
PA
9129 old_chain = save_current_program_space ();
9130
c906108c 9131 ALL_BREAKPOINTS_SAFE (b, temp)
c5aa993b 9132 {
53a5351d 9133 /* Format possible error msg */
fe3f5fa8 9134 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
9135 b->number);
9136 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 9137 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 9138 do_cleanups (cleanups);
c5aa993b 9139 }
c906108c
SS
9140 set_language (save_language);
9141 input_radix = save_input_radix;
e62c965a 9142
0756c555 9143 jit_breakpoint_re_set ();
4efc6507 9144
6c95b8df
PA
9145 do_cleanups (old_chain);
9146
69de3c6a 9147 create_overlay_event_breakpoint ("_ovly_debug_event");
0fd8e87f
UW
9148 create_longjmp_master_breakpoint ("longjmp");
9149 create_longjmp_master_breakpoint ("_longjmp");
9150 create_longjmp_master_breakpoint ("siglongjmp");
9151 create_longjmp_master_breakpoint ("_siglongjmp");
c906108c
SS
9152}
9153\f
c906108c
SS
9154/* Reset the thread number of this breakpoint:
9155
9156 - If the breakpoint is for all threads, leave it as-is.
39f77062 9157 - Else, reset it to the current thread for inferior_ptid. */
c906108c 9158void
fba45db2 9159breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
9160{
9161 if (b->thread != -1)
9162 {
39f77062
KB
9163 if (in_thread_list (inferior_ptid))
9164 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
9165
9166 /* We're being called after following a fork. The new fork is
9167 selected as current, and unless this was a vfork will have a
9168 different program space from the original thread. Reset that
9169 as well. */
9170 b->loc->pspace = current_program_space;
c906108c
SS
9171 }
9172}
9173
03ac34d5
MS
9174/* Set ignore-count of breakpoint number BPTNUM to COUNT.
9175 If from_tty is nonzero, it prints a message to that effect,
9176 which ends with a period (no newline). */
9177
c906108c 9178void
fba45db2 9179set_ignore_count (int bptnum, int count, int from_tty)
c906108c 9180{
52f0bd74 9181 struct breakpoint *b;
c906108c
SS
9182
9183 if (count < 0)
9184 count = 0;
9185
9186 ALL_BREAKPOINTS (b)
9187 if (b->number == bptnum)
c5aa993b
JM
9188 {
9189 b->ignore_count = count;
221ea385
KS
9190 if (from_tty)
9191 {
9192 if (count == 0)
a3f17187 9193 printf_filtered (_("Will stop next time breakpoint %d is reached."),
221ea385
KS
9194 bptnum);
9195 else if (count == 1)
a3f17187 9196 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
9197 bptnum);
9198 else
a3f17187 9199 printf_filtered (_("Will ignore next %d crossings of breakpoint %d."),
221ea385
KS
9200 count, bptnum);
9201 }
c5aa993b 9202 breakpoints_changed ();
383f836e 9203 observer_notify_breakpoint_modified (b->number);
c5aa993b
JM
9204 return;
9205 }
c906108c 9206
8a3fe4f8 9207 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
9208}
9209
b2175913
MS
9210void
9211make_breakpoint_silent (struct breakpoint *b)
9212{
9213 /* Silence the breakpoint. */
9214 b->silent = 1;
9215}
9216
c906108c
SS
9217/* Command to set ignore-count of breakpoint N to COUNT. */
9218
9219static void
fba45db2 9220ignore_command (char *args, int from_tty)
c906108c
SS
9221{
9222 char *p = args;
52f0bd74 9223 int num;
c906108c
SS
9224
9225 if (p == 0)
e2e0b3e5 9226 error_no_arg (_("a breakpoint number"));
c5aa993b 9227
c906108c 9228 num = get_number (&p);
5c44784c 9229 if (num == 0)
8a3fe4f8 9230 error (_("bad breakpoint number: '%s'"), args);
c906108c 9231 if (*p == 0)
8a3fe4f8 9232 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
9233
9234 set_ignore_count (num,
9235 longest_to_int (value_as_long (parse_and_eval (p))),
9236 from_tty);
221ea385
KS
9237 if (from_tty)
9238 printf_filtered ("\n");
c906108c
SS
9239}
9240\f
9241/* Call FUNCTION on each of the breakpoints
9242 whose numbers are given in ARGS. */
9243
9244static void
831662b3 9245map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *))
c906108c 9246{
52f0bd74 9247 char *p = args;
c906108c 9248 char *p1;
52f0bd74
AC
9249 int num;
9250 struct breakpoint *b, *tmp;
11cf8741 9251 int match;
c906108c
SS
9252
9253 if (p == 0)
e2e0b3e5 9254 error_no_arg (_("one or more breakpoint numbers"));
c906108c
SS
9255
9256 while (*p)
9257 {
11cf8741 9258 match = 0;
c906108c 9259 p1 = p;
c5aa993b 9260
5c44784c
JM
9261 num = get_number_or_range (&p1);
9262 if (num == 0)
c5aa993b 9263 {
8a3fe4f8 9264 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
9265 }
9266 else
9267 {
9268 ALL_BREAKPOINTS_SAFE (b, tmp)
9269 if (b->number == num)
9270 {
9271 struct breakpoint *related_breakpoint = b->related_breakpoint;
11cf8741 9272 match = 1;
5c44784c
JM
9273 function (b);
9274 if (related_breakpoint)
9275 function (related_breakpoint);
11cf8741 9276 break;
5c44784c 9277 }
11cf8741 9278 if (match == 0)
a3f17187 9279 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 9280 }
c906108c
SS
9281 p = p1;
9282 }
9283}
9284
0d381245
VP
9285static struct bp_location *
9286find_location_by_number (char *number)
9287{
9288 char *dot = strchr (number, '.');
9289 char *p1;
9290 int bp_num;
9291 int loc_num;
9292 struct breakpoint *b;
9293 struct bp_location *loc;
9294
9295 *dot = '\0';
9296
9297 p1 = number;
9298 bp_num = get_number_or_range (&p1);
9299 if (bp_num == 0)
9300 error (_("Bad breakpoint number '%s'"), number);
9301
9302 ALL_BREAKPOINTS (b)
9303 if (b->number == bp_num)
9304 {
9305 break;
9306 }
9307
9308 if (!b || b->number != bp_num)
9309 error (_("Bad breakpoint number '%s'"), number);
9310
9311 p1 = dot+1;
9312 loc_num = get_number_or_range (&p1);
9313 if (loc_num == 0)
9314 error (_("Bad breakpoint location number '%s'"), number);
9315
9316 --loc_num;
9317 loc = b->loc;
9318 for (;loc_num && loc; --loc_num, loc = loc->next)
9319 ;
9320 if (!loc)
9321 error (_("Bad breakpoint location number '%s'"), dot+1);
9322
9323 return loc;
9324}
9325
9326
1900040c
MS
9327/* Set ignore-count of breakpoint number BPTNUM to COUNT.
9328 If from_tty is nonzero, it prints a message to that effect,
9329 which ends with a period (no newline). */
9330
c906108c 9331void
fba45db2 9332disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
9333{
9334 /* Never disable a watchpoint scope breakpoint; we want to
9335 hit them when we leave scope so we can delete both the
9336 watchpoint and its scope breakpoint at that time. */
9337 if (bpt->type == bp_watchpoint_scope)
9338 return;
9339
c2c6d25f 9340 /* You can't disable permanent breakpoints. */
b5de0fa7 9341 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
9342 return;
9343
b5de0fa7 9344 bpt->enable_state = bp_disabled;
c906108c 9345
b60e7edf 9346 update_global_location_list (0);
c906108c 9347
383f836e 9348 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
9349}
9350
c906108c 9351static void
fba45db2 9352disable_command (char *args, int from_tty)
c906108c 9353{
52f0bd74 9354 struct breakpoint *bpt;
c906108c
SS
9355 if (args == 0)
9356 ALL_BREAKPOINTS (bpt)
9357 switch (bpt->type)
c5aa993b
JM
9358 {
9359 case bp_none:
8a3fe4f8 9360 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
53a5351d 9361 bpt->number);
c5aa993b
JM
9362 continue;
9363 case bp_breakpoint:
1042e4c0 9364 case bp_tracepoint:
ce78b96d 9365 case bp_catchpoint:
c5aa993b
JM
9366 case bp_hardware_breakpoint:
9367 case bp_watchpoint:
9368 case bp_hardware_watchpoint:
9369 case bp_read_watchpoint:
9370 case bp_access_watchpoint:
9371 disable_breakpoint (bpt);
9372 default:
9373 continue;
9374 }
0d381245
VP
9375 else if (strchr (args, '.'))
9376 {
9377 struct bp_location *loc = find_location_by_number (args);
9378 if (loc)
9379 loc->enabled = 0;
b60e7edf 9380 update_global_location_list (0);
0d381245 9381 }
c906108c
SS
9382 else
9383 map_breakpoint_numbers (args, disable_breakpoint);
9384}
9385
9386static void
fba45db2 9387do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 9388{
c906108c
SS
9389 int target_resources_ok, other_type_used;
9390 struct value *mark;
9391
9392 if (bpt->type == bp_hardware_breakpoint)
9393 {
9394 int i;
c5aa993b 9395 i = hw_breakpoint_used_count ();
53a5351d 9396 target_resources_ok =
d92524f1 9397 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 9398 i + 1, 0);
c906108c 9399 if (target_resources_ok == 0)
8a3fe4f8 9400 error (_("No hardware breakpoint support in the target."));
c906108c 9401 else if (target_resources_ok < 0)
8a3fe4f8 9402 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
9403 }
9404
059fb39f
PM
9405 if (bpt->type == bp_watchpoint
9406 || bpt->type == bp_hardware_watchpoint
9407 || bpt->type == bp_read_watchpoint
9408 || bpt->type == bp_access_watchpoint)
c906108c 9409 {
dde02812
ES
9410 struct gdb_exception e;
9411
9412 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 9413 {
dde02812 9414 update_watchpoint (bpt, 1 /* reparse */);
c906108c 9415 }
dde02812 9416 if (e.reason < 0)
c5aa993b 9417 {
dde02812
ES
9418 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
9419 bpt->number);
9420 return;
c5aa993b 9421 }
c906108c 9422 }
0101ce28 9423
b4c291bb
KH
9424 if (bpt->enable_state != bp_permanent)
9425 bpt->enable_state = bp_enabled;
9426 bpt->disposition = disposition;
b60e7edf 9427 update_global_location_list (1);
b4c291bb
KH
9428 breakpoints_changed ();
9429
383f836e 9430 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
9431}
9432
fe3f5fa8 9433
c906108c 9434void
fba45db2 9435enable_breakpoint (struct breakpoint *bpt)
c906108c
SS
9436{
9437 do_enable_breakpoint (bpt, bpt->disposition);
9438}
9439
9440/* The enable command enables the specified breakpoints (or all defined
9441 breakpoints) so they once again become (or continue to be) effective
1272ad14 9442 in stopping the inferior. */
c906108c 9443
c906108c 9444static void
fba45db2 9445enable_command (char *args, int from_tty)
c906108c 9446{
52f0bd74 9447 struct breakpoint *bpt;
c906108c
SS
9448 if (args == 0)
9449 ALL_BREAKPOINTS (bpt)
9450 switch (bpt->type)
c5aa993b
JM
9451 {
9452 case bp_none:
8a3fe4f8 9453 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
53a5351d 9454 bpt->number);
c5aa993b
JM
9455 continue;
9456 case bp_breakpoint:
1042e4c0 9457 case bp_tracepoint:
ce78b96d 9458 case bp_catchpoint:
c5aa993b
JM
9459 case bp_hardware_breakpoint:
9460 case bp_watchpoint:
9461 case bp_hardware_watchpoint:
9462 case bp_read_watchpoint:
9463 case bp_access_watchpoint:
9464 enable_breakpoint (bpt);
9465 default:
9466 continue;
9467 }
0d381245
VP
9468 else if (strchr (args, '.'))
9469 {
9470 struct bp_location *loc = find_location_by_number (args);
9471 if (loc)
9472 loc->enabled = 1;
b60e7edf 9473 update_global_location_list (1);
0d381245 9474 }
c906108c
SS
9475 else
9476 map_breakpoint_numbers (args, enable_breakpoint);
9477}
9478
9479static void
fba45db2 9480enable_once_breakpoint (struct breakpoint *bpt)
c906108c 9481{
b5de0fa7 9482 do_enable_breakpoint (bpt, disp_disable);
c906108c
SS
9483}
9484
c906108c 9485static void
fba45db2 9486enable_once_command (char *args, int from_tty)
c906108c
SS
9487{
9488 map_breakpoint_numbers (args, enable_once_breakpoint);
9489}
9490
9491static void
fba45db2 9492enable_delete_breakpoint (struct breakpoint *bpt)
c906108c 9493{
b5de0fa7 9494 do_enable_breakpoint (bpt, disp_del);
c906108c
SS
9495}
9496
c906108c 9497static void
fba45db2 9498enable_delete_command (char *args, int from_tty)
c906108c
SS
9499{
9500 map_breakpoint_numbers (args, enable_delete_breakpoint);
9501}
9502\f
fa8d40ab
JJ
9503static void
9504set_breakpoint_cmd (char *args, int from_tty)
9505{
9506}
9507
9508static void
9509show_breakpoint_cmd (char *args, int from_tty)
9510{
9511}
9512
1f3b5d1b
PP
9513/* Invalidate last known value of any hardware watchpoint if
9514 the memory which that value represents has been written to by
9515 GDB itself. */
9516
9517static void
9518invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
9519 const bfd_byte *data)
9520{
9521 struct breakpoint *bp;
9522
9523 ALL_BREAKPOINTS (bp)
9524 if (bp->enable_state == bp_enabled
9525 && bp->type == bp_hardware_watchpoint
9526 && bp->val_valid && bp->val)
9527 {
9528 struct bp_location *loc;
9529
9530 for (loc = bp->loc; loc != NULL; loc = loc->next)
9531 if (loc->loc_type == bp_loc_hardware_watchpoint
9532 && loc->address + loc->length > addr
9533 && addr + len > loc->address)
9534 {
9535 value_free (bp->val);
9536 bp->val = NULL;
9537 bp->val_valid = 0;
9538 }
9539 }
9540}
9541
c906108c
SS
9542/* Use default_breakpoint_'s, or nothing if they aren't valid. */
9543
9544struct symtabs_and_lines
fba45db2 9545decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
9546{
9547 struct symtabs_and_lines sals;
9548 if (string == 0)
8a3fe4f8 9549 error (_("Empty line specification."));
c906108c
SS
9550 if (default_breakpoint_valid)
9551 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
9552 default_breakpoint_symtab,
9553 default_breakpoint_line,
68219205 9554 (char ***) NULL, NULL);
c906108c
SS
9555 else
9556 sals = decode_line_1 (&string, funfirstline,
68219205 9557 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
c906108c 9558 if (*string)
8a3fe4f8 9559 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
9560 return sals;
9561}
8181d85f
DJ
9562
9563/* Create and insert a raw software breakpoint at PC. Return an
9564 identifier, which should be used to remove the breakpoint later.
9565 In general, places which call this should be using something on the
9566 breakpoint chain instead; this function should be eliminated
9567 someday. */
9568
9569void *
6c95b8df
PA
9570deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
9571 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
9572{
9573 struct bp_target_info *bp_tgt;
9574
6c95b8df 9575 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 9576
6c95b8df 9577 bp_tgt->placed_address_space = aspace;
8181d85f 9578 bp_tgt->placed_address = pc;
6c95b8df 9579
a6d9a66e 9580 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
9581 {
9582 /* Could not insert the breakpoint. */
9583 xfree (bp_tgt);
9584 return NULL;
9585 }
9586
9587 return bp_tgt;
9588}
9589
9590/* Remove a breakpoint BP inserted by deprecated_insert_raw_breakpoint. */
9591
9592int
a6d9a66e 9593deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
9594{
9595 struct bp_target_info *bp_tgt = bp;
9596 int ret;
9597
a6d9a66e 9598 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
9599 xfree (bp_tgt);
9600
9601 return ret;
9602}
9603
9604/* One (or perhaps two) breakpoints used for software single stepping. */
9605
9606static void *single_step_breakpoints[2];
a6d9a66e 9607static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
9608
9609/* Create and insert a breakpoint for software single step. */
9610
9611void
6c95b8df
PA
9612insert_single_step_breakpoint (struct gdbarch *gdbarch,
9613 struct address_space *aspace, CORE_ADDR next_pc)
8181d85f
DJ
9614{
9615 void **bpt_p;
9616
9617 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
9618 {
9619 bpt_p = &single_step_breakpoints[0];
9620 single_step_gdbarch[0] = gdbarch;
9621 }
8181d85f
DJ
9622 else
9623 {
9624 gdb_assert (single_step_breakpoints[1] == NULL);
9625 bpt_p = &single_step_breakpoints[1];
a6d9a66e 9626 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
9627 }
9628
9629 /* NOTE drow/2006-04-11: A future improvement to this function would be
9630 to only create the breakpoints once, and actually put them on the
9631 breakpoint chain. That would let us use set_raw_breakpoint. We could
9632 adjust the addresses each time they were needed. Doing this requires
9633 corresponding changes elsewhere where single step breakpoints are
9634 handled, however. So, for now, we use this. */
9635
6c95b8df 9636 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 9637 if (*bpt_p == NULL)
5af949e3
UW
9638 error (_("Could not insert single-step breakpoint at %s"),
9639 paddress (gdbarch, next_pc));
8181d85f
DJ
9640}
9641
9642/* Remove and delete any breakpoints used for software single step. */
9643
9644void
9645remove_single_step_breakpoints (void)
9646{
9647 gdb_assert (single_step_breakpoints[0] != NULL);
9648
9649 /* See insert_single_step_breakpoint for more about this deprecated
9650 call. */
a6d9a66e
UW
9651 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
9652 single_step_breakpoints[0]);
9653 single_step_gdbarch[0] = NULL;
8181d85f
DJ
9654 single_step_breakpoints[0] = NULL;
9655
9656 if (single_step_breakpoints[1] != NULL)
9657 {
a6d9a66e
UW
9658 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
9659 single_step_breakpoints[1]);
9660 single_step_gdbarch[1] = NULL;
8181d85f
DJ
9661 single_step_breakpoints[1] = NULL;
9662 }
9663}
9664
1aafd4da
UW
9665/* Check whether a software single-step breakpoint is inserted at PC. */
9666
9667static int
6c95b8df 9668single_step_breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
1aafd4da
UW
9669{
9670 int i;
9671
9672 for (i = 0; i < 2; i++)
9673 {
9674 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
9675 if (bp_tgt
9676 && breakpoint_address_match (bp_tgt->placed_address_space,
9677 bp_tgt->placed_address,
9678 aspace, pc))
1aafd4da
UW
9679 return 1;
9680 }
9681
9682 return 0;
9683}
9684
a96d9b2e
SDJ
9685/* Returns 0 if 'bp' is NOT a syscall catchpoint,
9686 non-zero otherwise. */
9687static int
9688is_syscall_catchpoint_enabled (struct breakpoint *bp)
9689{
9690 if (syscall_catchpoint_p (bp)
9691 && bp->enable_state != bp_disabled
9692 && bp->enable_state != bp_call_disabled)
9693 return 1;
9694 else
9695 return 0;
9696}
9697
9698int
9699catch_syscall_enabled (void)
9700{
9701 struct inferior *inf = current_inferior ();
9702
9703 return inf->total_syscalls_count != 0;
9704}
9705
9706int
9707catching_syscall_number (int syscall_number)
9708{
9709 struct breakpoint *bp;
9710
9711 ALL_BREAKPOINTS (bp)
9712 if (is_syscall_catchpoint_enabled (bp))
9713 {
9714 if (bp->syscalls_to_be_caught)
9715 {
9716 int i, iter;
9717 for (i = 0;
9718 VEC_iterate (int, bp->syscalls_to_be_caught, i, iter);
9719 i++)
9720 if (syscall_number == iter)
9721 return 1;
9722 }
9723 else
9724 return 1;
9725 }
9726
9727 return 0;
9728}
9729
9730/* Complete syscall names. Used by "catch syscall". */
9731static char **
9732catch_syscall_completer (struct cmd_list_element *cmd,
9733 char *text, char *word)
9734{
9735 const char **list = get_syscall_names ();
9736 return (list == NULL) ? NULL : complete_on_enum (list, text, word);
9737}
9738
1042e4c0
SS
9739/* Tracepoint-specific operations. */
9740
9741/* Set tracepoint count to NUM. */
9742static void
9743set_tracepoint_count (int num)
9744{
9745 tracepoint_count = num;
4fa62494 9746 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
9747}
9748
9749void
9750trace_command (char *arg, int from_tty)
9751{
a6d9a66e
UW
9752 break_command_really (get_current_arch (),
9753 arg,
1042e4c0
SS
9754 NULL, 0, 1 /* parse arg */,
9755 0 /* tempflag */, 0 /* hardwareflag */,
9756 1 /* traceflag */,
9757 0 /* Ignore count */,
9758 pending_break_support,
9759 NULL,
9760 from_tty,
9761 1 /* enabled */);
9762 set_tracepoint_count (breakpoint_count);
9763}
9764
9765/* Print information on tracepoint number TPNUM_EXP, or all if
9766 omitted. */
9767
9768static void
9769tracepoints_info (char *tpnum_exp, int from_tty)
9770{
9771 struct breakpoint *b;
9772 int tps_to_list = 0;
9773
9774 /* In the no-arguments case, say "No tracepoints" if none found. */
9775 if (tpnum_exp == 0)
9776 {
9777 ALL_TRACEPOINTS (b)
9778 {
9779 if (b->number >= 0)
9780 {
9781 tps_to_list = 1;
9782 break;
9783 }
9784 }
9785 if (!tps_to_list)
9786 {
9787 ui_out_message (uiout, 0, "No tracepoints.\n");
9788 return;
9789 }
9790 }
9791
9792 /* Otherwise be the same as "info break". */
9793 breakpoints_info (tpnum_exp, from_tty);
9794}
9795
9796/* The 'enable trace' command enables tracepoints.
9797 Not supported by all targets. */
9798static void
9799enable_trace_command (char *args, int from_tty)
9800{
9801 enable_command (args, from_tty);
9802}
9803
9804/* The 'disable trace' command disables tracepoints.
9805 Not supported by all targets. */
9806static void
9807disable_trace_command (char *args, int from_tty)
9808{
9809 disable_command (args, from_tty);
9810}
9811
9812/* Remove a tracepoint (or all if no argument) */
9813static void
9814delete_trace_command (char *arg, int from_tty)
9815{
9816 struct breakpoint *b, *temp;
9817
9818 dont_repeat ();
9819
9820 if (arg == 0)
9821 {
9822 int breaks_to_delete = 0;
9823
9824 /* Delete all breakpoints if no argument.
9825 Do not delete internal or call-dummy breakpoints, these
9826 have to be deleted with an explicit breakpoint number argument. */
9827 ALL_TRACEPOINTS (b)
9828 {
9829 if (b->number >= 0)
9830 {
9831 breaks_to_delete = 1;
9832 break;
9833 }
9834 }
9835
9836 /* Ask user only if there are some breakpoints to delete. */
9837 if (!from_tty
9838 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
9839 {
9840 ALL_BREAKPOINTS_SAFE (b, temp)
9841 {
059fb39f
PM
9842 if (b->type == bp_tracepoint
9843 && b->number >= 0)
1042e4c0
SS
9844 delete_breakpoint (b);
9845 }
9846 }
9847 }
9848 else
9849 map_breakpoint_numbers (arg, delete_breakpoint);
9850}
9851
9852/* Set passcount for tracepoint.
9853
9854 First command argument is passcount, second is tracepoint number.
9855 If tracepoint number omitted, apply to most recently defined.
9856 Also accepts special argument "all". */
9857
9858static void
9859trace_pass_command (char *args, int from_tty)
9860{
9861 struct breakpoint *t1 = (struct breakpoint *) -1, *t2;
9862 unsigned int count;
9863 int all = 0;
9864
9865 if (args == 0 || *args == 0)
9866 error (_("passcount command requires an argument (count + optional TP num)"));
9867
9868 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
9869
9870 while (*args && isspace ((int) *args))
9871 args++;
9872
9873 if (*args && strncasecmp (args, "all", 3) == 0)
9874 {
9875 args += 3; /* Skip special argument "all". */
9876 all = 1;
9877 if (*args)
9878 error (_("Junk at end of arguments."));
9879 }
9880 else
9881 t1 = get_tracepoint_by_number (&args, 1, 1);
9882
9883 do
9884 {
9885 if (t1)
9886 {
9887 ALL_TRACEPOINTS (t2)
9888 if (t1 == (struct breakpoint *) -1 || t1 == t2)
9889 {
9890 t2->pass_count = count;
9891 observer_notify_tracepoint_modified (t2->number);
9892 if (from_tty)
9893 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
9894 t2->number, count);
9895 }
9896 if (! all && *args)
9897 t1 = get_tracepoint_by_number (&args, 1, 0);
9898 }
9899 }
9900 while (*args);
9901}
9902
9903struct breakpoint *
9904get_tracepoint (int num)
9905{
9906 struct breakpoint *t;
9907
9908 ALL_TRACEPOINTS (t)
9909 if (t->number == num)
9910 return t;
9911
9912 return NULL;
9913}
9914
9915/* Utility: parse a tracepoint number and look it up in the list.
9916 If MULTI_P is true, there might be a range of tracepoints in ARG.
9917 if OPTIONAL_P is true, then if the argument is missing, the most
9918 recent tracepoint (tracepoint_count) is returned. */
9919struct breakpoint *
9920get_tracepoint_by_number (char **arg, int multi_p, int optional_p)
9921{
9922 extern int tracepoint_count;
9923 struct breakpoint *t;
9924 int tpnum;
9925 char *instring = arg == NULL ? NULL : *arg;
9926
9927 if (arg == NULL || *arg == NULL || ! **arg)
9928 {
9929 if (optional_p)
9930 tpnum = tracepoint_count;
9931 else
9932 error_no_arg (_("tracepoint number"));
9933 }
9934 else
9935 tpnum = multi_p ? get_number_or_range (arg) : get_number (arg);
9936
9937 if (tpnum <= 0)
9938 {
9939 if (instring && *instring)
9940 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
9941 instring);
9942 else
9943 printf_filtered (_("Tracepoint argument missing and no previous tracepoint\n"));
9944 return NULL;
9945 }
9946
9947 ALL_TRACEPOINTS (t)
9948 if (t->number == tpnum)
9949 {
9950 return t;
9951 }
9952
9953 /* FIXME: if we are in the middle of a range we don't want to give
9954 a message. The current interface to get_number_or_range doesn't
9955 allow us to discover this. */
9956 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
9957 return NULL;
9958}
9959
9960/* save-tracepoints command */
9961static void
9962tracepoint_save_command (char *args, int from_tty)
9963{
9964 struct breakpoint *tp;
9965 int any_tp = 0;
9966 struct action_line *line;
9967 FILE *fp;
9968 char *i1 = " ", *i2 = " ";
9969 char *indent, *actionline, *pathname;
9970 char tmp[40];
9971 struct cleanup *cleanup;
9972
9973 if (args == 0 || *args == 0)
9974 error (_("Argument required (file name in which to save tracepoints)"));
9975
9976 /* See if we have anything to save. */
9977 ALL_TRACEPOINTS (tp)
9978 {
9979 any_tp = 1;
9980 break;
9981 }
9982 if (!any_tp)
9983 {
9984 warning (_("save-tracepoints: no tracepoints to save."));
9985 return;
9986 }
9987
9988 pathname = tilde_expand (args);
9989 cleanup = make_cleanup (xfree, pathname);
059fb39f
PM
9990 fp = fopen (pathname, "w");
9991 if (!fp)
1042e4c0
SS
9992 error (_("Unable to open file '%s' for saving tracepoints (%s)"),
9993 args, safe_strerror (errno));
9994 make_cleanup_fclose (fp);
9995
9996 ALL_TRACEPOINTS (tp)
9997 {
9998 if (tp->addr_string)
9999 fprintf (fp, "trace %s\n", tp->addr_string);
10000 else
10001 {
10002 sprintf_vma (tmp, tp->loc->address);
10003 fprintf (fp, "trace *0x%s\n", tmp);
10004 }
10005
10006 if (tp->pass_count)
10007 fprintf (fp, " passcount %d\n", tp->pass_count);
10008
10009 if (tp->actions)
10010 {
10011 fprintf (fp, " actions\n");
10012 indent = i1;
10013 for (line = tp->actions; line; line = line->next)
10014 {
10015 struct cmd_list_element *cmd;
10016
10017 QUIT; /* allow user to bail out with ^C */
10018 actionline = line->action;
10019 while (isspace ((int) *actionline))
10020 actionline++;
10021
10022 fprintf (fp, "%s%s\n", indent, actionline);
10023 if (*actionline != '#') /* skip for comment lines */
10024 {
10025 cmd = lookup_cmd (&actionline, cmdlist, "", -1, 1);
10026 if (cmd == 0)
10027 error (_("Bad action list item: %s"), actionline);
10028 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
10029 indent = i2;
10030 else if (cmd_cfunc_eq (cmd, end_actions_pseudocommand))
10031 indent = i1;
10032 }
10033 }
10034 }
10035 }
10036 do_cleanups (cleanup);
10037 if (from_tty)
10038 printf_filtered (_("Tracepoints saved to file '%s'.\n"), args);
10039 return;
10040}
10041
10042/* Create a vector of all tracepoints. */
10043
10044VEC(breakpoint_p) *
10045all_tracepoints ()
10046{
10047 VEC(breakpoint_p) *tp_vec = 0;
10048 struct breakpoint *tp;
10049
10050 ALL_TRACEPOINTS (tp)
10051 {
10052 VEC_safe_push (breakpoint_p, tp_vec, tp);
10053 }
10054
10055 return tp_vec;
10056}
10057
c906108c 10058\f
31e2b00f
AS
10059/* This help string is used for the break, hbreak, tbreak and thbreak commands.
10060 It is defined as a macro to prevent duplication.
10061 COMMAND should be a string constant containing the name of the command. */
10062#define BREAK_ARGS_HELP(command) \
10063command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
10064LOCATION may be a line number, function name, or \"*\" and an address.\n\
10065If a line number is specified, break at start of code for that line.\n\
10066If a function is specified, break at start of code for that function.\n\
10067If an address is specified, break at that exact address.\n\
10068With no LOCATION, uses current execution address of selected stack frame.\n\
10069This is useful for breaking on return to a stack frame.\n\
10070\n\
10071THREADNUM is the number from \"info threads\".\n\
10072CONDITION is a boolean expression.\n\
10073\n\
10074Multiple breakpoints at one place are permitted, and useful if conditional.\n\
10075\n\
10076Do \"help breakpoints\" for info on other commands dealing with breakpoints."
10077
44feb3ce
TT
10078/* List of subcommands for "catch". */
10079static struct cmd_list_element *catch_cmdlist;
10080
10081/* List of subcommands for "tcatch". */
10082static struct cmd_list_element *tcatch_cmdlist;
10083
10084/* Like add_cmd, but add the command to both the "catch" and "tcatch"
10085 lists, and pass some additional user data to the command function. */
10086static void
10087add_catch_command (char *name, char *docstring,
10088 void (*sfunc) (char *args, int from_tty,
10089 struct cmd_list_element *command),
a96d9b2e
SDJ
10090 char **(*completer) (struct cmd_list_element *cmd,
10091 char *text, char *word),
44feb3ce
TT
10092 void *user_data_catch,
10093 void *user_data_tcatch)
10094{
10095 struct cmd_list_element *command;
10096
10097 command = add_cmd (name, class_breakpoint, NULL, docstring,
10098 &catch_cmdlist);
10099 set_cmd_sfunc (command, sfunc);
10100 set_cmd_context (command, user_data_catch);
a96d9b2e 10101 set_cmd_completer (command, completer);
44feb3ce
TT
10102
10103 command = add_cmd (name, class_breakpoint, NULL, docstring,
10104 &tcatch_cmdlist);
10105 set_cmd_sfunc (command, sfunc);
10106 set_cmd_context (command, user_data_tcatch);
a96d9b2e 10107 set_cmd_completer (command, completer);
44feb3ce
TT
10108}
10109
6c95b8df
PA
10110static void
10111clear_syscall_counts (int pid)
10112{
10113 struct inferior *inf = find_inferior_pid (pid);
10114
10115 inf->total_syscalls_count = 0;
10116 inf->any_syscall_count = 0;
10117 VEC_free (int, inf->syscalls_counts);
10118}
10119
c906108c 10120void
fba45db2 10121_initialize_breakpoint (void)
c906108c 10122{
fa8d40ab
JJ
10123 static struct cmd_list_element *breakpoint_set_cmdlist;
10124 static struct cmd_list_element *breakpoint_show_cmdlist;
c906108c
SS
10125 struct cmd_list_element *c;
10126
84acb35a 10127 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 10128 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 10129 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 10130
c906108c
SS
10131 breakpoint_chain = 0;
10132 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
10133 before a breakpoint is set. */
10134 breakpoint_count = 0;
10135
1042e4c0
SS
10136 tracepoint_count = 0;
10137
1bedd215
AC
10138 add_com ("ignore", class_breakpoint, ignore_command, _("\
10139Set ignore-count of breakpoint number N to COUNT.\n\
10140Usage is `ignore N COUNT'."));
c906108c 10141 if (xdb_commands)
c5aa993b 10142 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 10143
1bedd215
AC
10144 add_com ("commands", class_breakpoint, commands_command, _("\
10145Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
10146Give breakpoint number as argument after \"commands\".\n\
10147With no argument, the targeted breakpoint is the last one set.\n\
10148The commands themselves follow starting on the next line.\n\
10149Type a line containing \"end\" to indicate the end of them.\n\
10150Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 10151then no output is printed when it is hit, except what the commands print."));
c906108c 10152
1bedd215
AC
10153 add_com ("condition", class_breakpoint, condition_command, _("\
10154Specify breakpoint number N to break only if COND is true.\n\
c906108c 10155Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 10156expression to be evaluated whenever breakpoint N is reached."));
c906108c 10157
1bedd215 10158 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 10159Set a temporary breakpoint.\n\
c906108c
SS
10160Like \"break\" except the breakpoint is only temporary,\n\
10161so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
10162by using \"enable delete\" on the breakpoint number.\n\
10163\n"
10164BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 10165 set_cmd_completer (c, location_completer);
c94fdfd0 10166
1bedd215 10167 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
31e2b00f 10168Set a hardware assisted breakpoint.\n\
c906108c 10169Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
10170some target hardware may not have this support.\n\
10171\n"
10172BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 10173 set_cmd_completer (c, location_completer);
c906108c 10174
1bedd215 10175 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 10176Set a temporary hardware assisted breakpoint.\n\
c906108c 10177Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
10178so it will be deleted when hit.\n\
10179\n"
10180BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 10181 set_cmd_completer (c, location_completer);
c906108c 10182
1bedd215
AC
10183 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
10184Enable some breakpoints.\n\
c906108c
SS
10185Give breakpoint numbers (separated by spaces) as arguments.\n\
10186With no subcommand, breakpoints are enabled until you command otherwise.\n\
10187This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 10188With a subcommand you can enable temporarily."),
c906108c
SS
10189 &enablelist, "enable ", 1, &cmdlist);
10190 if (xdb_commands)
1bedd215
AC
10191 add_com ("ab", class_breakpoint, enable_command, _("\
10192Enable some breakpoints.\n\
c906108c
SS
10193Give breakpoint numbers (separated by spaces) as arguments.\n\
10194With no subcommand, breakpoints are enabled until you command otherwise.\n\
10195This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 10196With a subcommand you can enable temporarily."));
c906108c
SS
10197
10198 add_com_alias ("en", "enable", class_breakpoint, 1);
10199
1bedd215
AC
10200 add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
10201Enable some breakpoints.\n\
c906108c
SS
10202Give breakpoint numbers (separated by spaces) as arguments.\n\
10203This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 10204May be abbreviated to simply \"enable\".\n"),
c5aa993b 10205 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 10206
1a966eab
AC
10207 add_cmd ("once", no_class, enable_once_command, _("\
10208Enable breakpoints for one hit. Give breakpoint numbers.\n\
10209If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
10210 &enablebreaklist);
10211
1a966eab
AC
10212 add_cmd ("delete", no_class, enable_delete_command, _("\
10213Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
10214If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
10215 &enablebreaklist);
10216
1a966eab
AC
10217 add_cmd ("delete", no_class, enable_delete_command, _("\
10218Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
10219If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
10220 &enablelist);
10221
1a966eab
AC
10222 add_cmd ("once", no_class, enable_once_command, _("\
10223Enable breakpoints for one hit. Give breakpoint numbers.\n\
10224If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
10225 &enablelist);
10226
1bedd215
AC
10227 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
10228Disable some breakpoints.\n\
c906108c
SS
10229Arguments are breakpoint numbers with spaces in between.\n\
10230To disable all breakpoints, give no argument.\n\
1bedd215 10231A disabled breakpoint is not forgotten, but has no effect until reenabled."),
c906108c
SS
10232 &disablelist, "disable ", 1, &cmdlist);
10233 add_com_alias ("dis", "disable", class_breakpoint, 1);
10234 add_com_alias ("disa", "disable", class_breakpoint, 1);
10235 if (xdb_commands)
1bedd215
AC
10236 add_com ("sb", class_breakpoint, disable_command, _("\
10237Disable some breakpoints.\n\
c906108c
SS
10238Arguments are breakpoint numbers with spaces in between.\n\
10239To disable all breakpoints, give no argument.\n\
1bedd215 10240A disabled breakpoint is not forgotten, but has no effect until reenabled."));
c906108c 10241
1a966eab
AC
10242 add_cmd ("breakpoints", class_alias, disable_command, _("\
10243Disable some breakpoints.\n\
c906108c
SS
10244Arguments are breakpoint numbers with spaces in between.\n\
10245To disable all breakpoints, give no argument.\n\
10246A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1a966eab 10247This command may be abbreviated \"disable\"."),
c906108c
SS
10248 &disablelist);
10249
1bedd215
AC
10250 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
10251Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
10252Arguments are breakpoint numbers with spaces in between.\n\
10253To delete all breakpoints, give no argument.\n\
10254\n\
10255Also a prefix command for deletion of other GDB objects.\n\
1bedd215 10256The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
10257 &deletelist, "delete ", 1, &cmdlist);
10258 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 10259 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 10260 if (xdb_commands)
1bedd215
AC
10261 add_com ("db", class_breakpoint, delete_command, _("\
10262Delete some breakpoints.\n\
c906108c 10263Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 10264To delete all breakpoints, give no argument.\n"));
c906108c 10265
1a966eab
AC
10266 add_cmd ("breakpoints", class_alias, delete_command, _("\
10267Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
10268Arguments are breakpoint numbers with spaces in between.\n\
10269To delete all breakpoints, give no argument.\n\
1a966eab 10270This command may be abbreviated \"delete\"."),
c906108c
SS
10271 &deletelist);
10272
1bedd215
AC
10273 add_com ("clear", class_breakpoint, clear_command, _("\
10274Clear breakpoint at specified line or function.\n\
c906108c
SS
10275Argument may be line number, function name, or \"*\" and an address.\n\
10276If line number is specified, all breakpoints in that line are cleared.\n\
10277If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
10278If an address is specified, breakpoints at that address are cleared.\n\
10279\n\
10280With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
10281is executing in.\n\
10282\n\
1bedd215 10283See also the \"delete\" command which clears breakpoints by number."));
c906108c 10284
1bedd215 10285 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
10286Set breakpoint at specified line or function.\n"
10287BREAK_ARGS_HELP ("break")));
5ba2abeb 10288 set_cmd_completer (c, location_completer);
c94fdfd0 10289
c906108c
SS
10290 add_com_alias ("b", "break", class_run, 1);
10291 add_com_alias ("br", "break", class_run, 1);
10292 add_com_alias ("bre", "break", class_run, 1);
10293 add_com_alias ("brea", "break", class_run, 1);
10294
7681d515
PM
10295 if (xdb_commands)
10296 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
10297
10298 if (dbx_commands)
10299 {
1bedd215
AC
10300 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
10301Break in function/address or break at a line in the current file."),
c5aa993b
JM
10302 &stoplist, "stop ", 1, &cmdlist);
10303 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 10304 _("Break in function or address."), &stoplist);
c5aa993b 10305 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 10306 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
10307 add_com ("status", class_info, breakpoints_info, _("\
10308Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
10309The \"Type\" column indicates one of:\n\
10310\tbreakpoint - normal breakpoint\n\
10311\twatchpoint - watchpoint\n\
10312The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
10313the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
10314breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
10315address and file/line number respectively.\n\
10316\n\
10317Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
10318are set to the address of the last breakpoint listed unless the command\n\
10319is prefixed with \"server \".\n\n\
c906108c 10320Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 10321breakpoint set."));
c906108c
SS
10322 }
10323
1bedd215
AC
10324 add_info ("breakpoints", breakpoints_info, _("\
10325Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
10326The \"Type\" column indicates one of:\n\
10327\tbreakpoint - normal breakpoint\n\
10328\twatchpoint - watchpoint\n\
10329The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
10330the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
10331breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
10332address and file/line number respectively.\n\
10333\n\
10334Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
10335are set to the address of the last breakpoint listed unless the command\n\
10336is prefixed with \"server \".\n\n\
c906108c 10337Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 10338breakpoint set."));
c906108c 10339
6b04bdb7
MS
10340 add_info_alias ("b", "breakpoints", 1);
10341
c906108c 10342 if (xdb_commands)
1bedd215
AC
10343 add_com ("lb", class_breakpoint, breakpoints_info, _("\
10344Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
10345The \"Type\" column indicates one of:\n\
10346\tbreakpoint - normal breakpoint\n\
10347\twatchpoint - watchpoint\n\
10348The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
10349the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
10350breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
10351address and file/line number respectively.\n\
10352\n\
10353Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
10354are set to the address of the last breakpoint listed unless the command\n\
10355is prefixed with \"server \".\n\n\
c906108c 10356Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 10357breakpoint set."));
c906108c 10358
1a966eab
AC
10359 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
10360Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
10361The \"Type\" column indicates one of:\n\
10362\tbreakpoint - normal breakpoint\n\
10363\twatchpoint - watchpoint\n\
10364\tlongjmp - internal breakpoint used to step through longjmp()\n\
10365\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
10366\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
10367\tfinish - internal breakpoint used by the \"finish\" command\n\
10368The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
10369the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
10370breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
10371address and file/line number respectively.\n\
10372\n\
10373Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
10374are set to the address of the last breakpoint listed unless the command\n\
10375is prefixed with \"server \".\n\n\
c906108c 10376Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 10377breakpoint set."),
c906108c
SS
10378 &maintenanceinfolist);
10379
44feb3ce
TT
10380 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
10381Set catchpoints to catch events."),
10382 &catch_cmdlist, "catch ",
10383 0/*allow-unknown*/, &cmdlist);
10384
10385 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
10386Set temporary catchpoints to catch events."),
10387 &tcatch_cmdlist, "tcatch ",
10388 0/*allow-unknown*/, &cmdlist);
10389
10390 /* Add catch and tcatch sub-commands. */
10391 add_catch_command ("catch", _("\
10392Catch an exception, when caught.\n\
10393With an argument, catch only exceptions with the given name."),
10394 catch_catch_command,
a96d9b2e 10395 NULL,
44feb3ce
TT
10396 CATCH_PERMANENT,
10397 CATCH_TEMPORARY);
10398 add_catch_command ("throw", _("\
10399Catch an exception, when thrown.\n\
10400With an argument, catch only exceptions with the given name."),
10401 catch_throw_command,
a96d9b2e 10402 NULL,
44feb3ce
TT
10403 CATCH_PERMANENT,
10404 CATCH_TEMPORARY);
10405 add_catch_command ("fork", _("Catch calls to fork."),
10406 catch_fork_command_1,
a96d9b2e 10407 NULL,
44feb3ce
TT
10408 (void *) (uintptr_t) catch_fork_permanent,
10409 (void *) (uintptr_t) catch_fork_temporary);
10410 add_catch_command ("vfork", _("Catch calls to vfork."),
10411 catch_fork_command_1,
a96d9b2e 10412 NULL,
44feb3ce
TT
10413 (void *) (uintptr_t) catch_vfork_permanent,
10414 (void *) (uintptr_t) catch_vfork_temporary);
10415 add_catch_command ("exec", _("Catch calls to exec."),
10416 catch_exec_command_1,
a96d9b2e
SDJ
10417 NULL,
10418 CATCH_PERMANENT,
10419 CATCH_TEMPORARY);
10420 add_catch_command ("syscall", _("\
10421Catch system calls by their names and/or numbers.\n\
10422Arguments say which system calls to catch. If no arguments\n\
10423are given, every system call will be caught.\n\
10424Arguments, if given, should be one or more system call names\n\
10425(if your system supports that), or system call numbers."),
10426 catch_syscall_command_1,
10427 catch_syscall_completer,
44feb3ce
TT
10428 CATCH_PERMANENT,
10429 CATCH_TEMPORARY);
44feb3ce
TT
10430 add_catch_command ("exception", _("\
10431Catch Ada exceptions, when raised.\n\
10432With an argument, catch only exceptions with the given name."),
10433 catch_ada_exception_command,
a96d9b2e 10434 NULL,
44feb3ce
TT
10435 CATCH_PERMANENT,
10436 CATCH_TEMPORARY);
10437 add_catch_command ("assert", _("\
10438Catch failed Ada assertions, when raised.\n\
10439With an argument, catch only exceptions with the given name."),
10440 catch_assert_command,
a96d9b2e 10441 NULL,
44feb3ce
TT
10442 CATCH_PERMANENT,
10443 CATCH_TEMPORARY);
c5aa993b 10444
1bedd215
AC
10445 c = add_com ("watch", class_breakpoint, watch_command, _("\
10446Set a watchpoint for an expression.\n\
c906108c 10447A watchpoint stops execution of your program whenever the value of\n\
1bedd215 10448an expression changes."));
65d12d83 10449 set_cmd_completer (c, expression_completer);
c906108c 10450
1bedd215
AC
10451 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
10452Set a read watchpoint for an expression.\n\
c906108c 10453A watchpoint stops execution of your program whenever the value of\n\
1bedd215 10454an expression is read."));
65d12d83 10455 set_cmd_completer (c, expression_completer);
c906108c 10456
1bedd215
AC
10457 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
10458Set a watchpoint for an expression.\n\
c906108c 10459A watchpoint stops execution of your program whenever the value of\n\
1bedd215 10460an expression is either read or written."));
65d12d83 10461 set_cmd_completer (c, expression_completer);
c906108c
SS
10462
10463 add_info ("watchpoints", breakpoints_info,
1bedd215 10464 _("Synonym for ``info breakpoints''."));
c906108c
SS
10465
10466
920d2a44
AC
10467 /* XXX: cagney/2005-02-23: This should be a boolean, and should
10468 respond to changes - contrary to the description. */
85c07804
AC
10469 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
10470 &can_use_hw_watchpoints, _("\
10471Set debugger's willingness to use watchpoint hardware."), _("\
10472Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
10473If zero, gdb will not use hardware for new watchpoints, even if\n\
10474such is available. (However, any hardware watchpoints that were\n\
10475created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
10476hardware.)"),
10477 NULL,
920d2a44 10478 show_can_use_hw_watchpoints,
85c07804 10479 &setlist, &showlist);
c906108c
SS
10480
10481 can_use_hw_watchpoints = 1;
fa8d40ab 10482
1042e4c0
SS
10483 /* Tracepoint manipulation commands. */
10484
10485 c = add_com ("trace", class_breakpoint, trace_command, _("\
10486Set a tracepoint at specified line or function.\n\
10487\n"
10488BREAK_ARGS_HELP ("trace") "\n\
10489Do \"help tracepoints\" for info on other tracepoint commands."));
10490 set_cmd_completer (c, location_completer);
10491
10492 add_com_alias ("tp", "trace", class_alias, 0);
10493 add_com_alias ("tr", "trace", class_alias, 1);
10494 add_com_alias ("tra", "trace", class_alias, 1);
10495 add_com_alias ("trac", "trace", class_alias, 1);
10496
10497 add_info ("tracepoints", tracepoints_info, _("\
10498Status of tracepoints, or tracepoint number NUMBER.\n\
10499Convenience variable \"$tpnum\" contains the number of the\n\
10500last tracepoint set."));
10501
10502 add_info_alias ("tp", "tracepoints", 1);
10503
10504 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
10505Delete specified tracepoints.\n\
10506Arguments are tracepoint numbers, separated by spaces.\n\
10507No argument means delete all tracepoints."),
10508 &deletelist);
10509
10510 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
10511Disable specified tracepoints.\n\
10512Arguments are tracepoint numbers, separated by spaces.\n\
10513No argument means disable all tracepoints."),
10514 &disablelist);
10515 deprecate_cmd (c, "disable");
10516
10517 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
10518Enable specified tracepoints.\n\
10519Arguments are tracepoint numbers, separated by spaces.\n\
10520No argument means enable all tracepoints."),
10521 &enablelist);
10522 deprecate_cmd (c, "enable");
10523
10524 add_com ("passcount", class_trace, trace_pass_command, _("\
10525Set the passcount for a tracepoint.\n\
10526The trace will end when the tracepoint has been passed 'count' times.\n\
10527Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
10528if TPNUM is omitted, passcount refers to the last tracepoint defined."));
10529
10530 c = add_com ("save-tracepoints", class_trace, tracepoint_save_command, _("\
10531Save current tracepoint definitions as a script.\n\
10532Use the 'source' command in another debug session to restore them."));
10533 set_cmd_completer (c, filename_completer);
10534
1bedd215 10535 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
10536Breakpoint specific settings\n\
10537Configure various breakpoint-specific variables such as\n\
1bedd215 10538pending breakpoint behavior"),
fa8d40ab
JJ
10539 &breakpoint_set_cmdlist, "set breakpoint ",
10540 0/*allow-unknown*/, &setlist);
1bedd215 10541 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
10542Breakpoint specific settings\n\
10543Configure various breakpoint-specific variables such as\n\
1bedd215 10544pending breakpoint behavior"),
fa8d40ab
JJ
10545 &breakpoint_show_cmdlist, "show breakpoint ",
10546 0/*allow-unknown*/, &showlist);
10547
7915a72c
AC
10548 add_setshow_auto_boolean_cmd ("pending", no_class,
10549 &pending_break_support, _("\
10550Set debugger's behavior regarding pending breakpoints."), _("\
10551Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
10552If on, an unrecognized breakpoint location will cause gdb to create a\n\
10553pending breakpoint. If off, an unrecognized breakpoint location results in\n\
10554an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 10555user-query to see if a pending breakpoint should be created."),
2c5b56ce 10556 NULL,
920d2a44 10557 show_pending_break_support,
6e1d7d6c
AC
10558 &breakpoint_set_cmdlist,
10559 &breakpoint_show_cmdlist);
fa8d40ab
JJ
10560
10561 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
10562
10563 add_setshow_boolean_cmd ("auto-hw", no_class,
10564 &automatic_hardware_breakpoints, _("\
10565Set automatic usage of hardware breakpoints."), _("\
10566Show automatic usage of hardware breakpoints."), _("\
10567If set, the debugger will automatically use hardware breakpoints for\n\
10568breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
10569a warning will be emitted for such breakpoints."),
10570 NULL,
10571 show_automatic_hardware_breakpoints,
10572 &breakpoint_set_cmdlist,
10573 &breakpoint_show_cmdlist);
74960c60 10574
33e5cbd6
PA
10575 add_setshow_enum_cmd ("always-inserted", class_support,
10576 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
10577Set mode for inserting breakpoints."), _("\
10578Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
10579When this mode is off, breakpoints are inserted in inferior when it is\n\
10580resumed, and removed when execution stops. When this mode is on,\n\
10581breakpoints are inserted immediately and removed only when the user\n\
10582deletes the breakpoint. When this mode is auto (which is the default),\n\
10583the behaviour depends on the non-stop setting (see help set non-stop).\n\
10584In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
10585behaves as if always-inserted mode is on; if gdb is controlling the\n\
10586inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
10587 NULL,
10588 &show_always_inserted_mode,
10589 &breakpoint_set_cmdlist,
10590 &breakpoint_show_cmdlist);
765dc015
VP
10591
10592 automatic_hardware_breakpoints = 1;
f3b1572e
PA
10593
10594 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 10595}
This page took 1.520232 seconds and 4 git commands to generate.