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