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