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