1 /* Scheme interface to breakpoints.
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
24 #include "breakpoint.h"
25 #include "cli/cli-cmds.h"
26 #include "gdbthread.h"
27 #include "observable.h"
28 #include "cli/cli-script.h"
30 #include "arch-utils.h"
32 #include "guile-internal.h"
35 /* The <gdb:breakpoint> smob.
36 N.B.: The name of this struct is known to breakpoint.h.
38 Note: Breakpoints are added to gdb using a two step process:
39 1) Call make-breakpoint to create a <gdb:breakpoint> object.
40 2) Call register-breakpoint! to add the breakpoint to gdb.
41 It is done this way so that the constructor, make-breakpoint, doesn't have
42 any side-effects. This means that the smob needs to store everything
43 that was passed to make-breakpoint. */
45 typedef struct gdbscm_breakpoint_object
47 /* This always appears first. */
50 /* Non-zero if this breakpoint was created with make-breakpoint. */
53 /* For breakpoints created with make-breakpoint, these are the parameters
54 that were passed to make-breakpoint. These values are not used except
55 to register the breakpoint with GDB. */
58 /* The string representation of the breakpoint.
59 Space for this lives in GC space. */
62 /* The kind of breakpoint.
63 At the moment this can only be one of bp_breakpoint, bp_watchpoint. */
66 /* If a watchpoint, the kind of watchpoint. */
67 enum target_hw_bp_type access_type
;
69 /* Non-zero if the breakpoint is an "internal" breakpoint. */
72 /* Non-zero if the breakpoint is temporary. */
76 /* The breakpoint number according to gdb.
77 For breakpoints created from Scheme, this has the value -1 until the
78 breakpoint is registered with gdb.
79 This is recorded here because BP will be NULL when deleted. */
82 /* The gdb breakpoint object, or NULL if the breakpoint has not been
83 registered yet, or has been deleted. */
84 struct breakpoint
*bp
;
86 /* Backlink to our containing <gdb:breakpoint> smob.
87 This is needed when we are deleted, we need to unprotect the object
91 /* A stop condition or #f. */
95 static const char breakpoint_smob_name
[] = "gdb:breakpoint";
97 /* The tag Guile knows the breakpoint smob by. */
98 static scm_t_bits breakpoint_smob_tag
;
100 /* Variables used to pass information between the breakpoint_smob
101 constructor and the breakpoint-created hook function. */
102 static SCM pending_breakpoint_scm
= SCM_BOOL_F
;
104 /* Keywords used by create-breakpoint!. */
105 static SCM type_keyword
;
106 static SCM wp_class_keyword
;
107 static SCM internal_keyword
;
108 static SCM temporary_keyword
;
110 /* Administrivia for breakpoint smobs. */
112 /* The smob "free" function for <gdb:breakpoint>. */
115 bpscm_free_breakpoint_smob (SCM self
)
117 breakpoint_smob
*bp_smob
= (breakpoint_smob
*) SCM_SMOB_DATA (self
);
120 bp_smob
->bp
->scm_bp_object
= NULL
;
122 /* Not necessary, done to catch bugs. */
124 bp_smob
->containing_scm
= SCM_UNDEFINED
;
125 bp_smob
->stop
= SCM_UNDEFINED
;
130 /* Return the name of TYPE.
131 This doesn't handle all types, just the ones we export. */
134 bpscm_type_to_string (enum bptype type
)
138 case bp_none
: return "BP_NONE";
139 case bp_breakpoint
: return "BP_BREAKPOINT";
140 case bp_watchpoint
: return "BP_WATCHPOINT";
141 case bp_hardware_watchpoint
: return "BP_HARDWARE_WATCHPOINT";
142 case bp_read_watchpoint
: return "BP_READ_WATCHPOINT";
143 case bp_access_watchpoint
: return "BP_ACCESS_WATCHPOINT";
144 case bp_catchpoint
: return "BP_CATCHPOINT";
145 default: return "internal/other";
149 /* Return the name of ENABLE_STATE. */
152 bpscm_enable_state_to_string (enum enable_state enable_state
)
154 switch (enable_state
)
156 case bp_disabled
: return "disabled";
157 case bp_enabled
: return "enabled";
158 case bp_call_disabled
: return "call_disabled";
159 default: return "unknown";
163 /* The smob "print" function for <gdb:breakpoint>. */
166 bpscm_print_breakpoint_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
168 breakpoint_smob
*bp_smob
= (breakpoint_smob
*) SCM_SMOB_DATA (self
);
169 struct breakpoint
*b
= bp_smob
->bp
;
171 gdbscm_printf (port
, "#<%s", breakpoint_smob_name
);
173 /* Only print what we export to the user.
174 The rest are possibly internal implementation details. */
176 gdbscm_printf (port
, " #%d", bp_smob
->number
);
178 /* Careful, the breakpoint may be invalid. */
181 gdbscm_printf (port
, " %s %s %s",
182 bpscm_type_to_string (b
->type
),
183 bpscm_enable_state_to_string (b
->enable_state
),
184 b
->silent
? "silent" : "noisy");
186 gdbscm_printf (port
, " hit:%d", b
->hit_count
);
187 gdbscm_printf (port
, " ignore:%d", b
->ignore_count
);
189 if (b
->locspec
!= nullptr)
191 const char *str
= b
->locspec
->to_string ();
193 gdbscm_printf (port
, " @%s", str
);
197 scm_puts (">", port
);
199 scm_remember_upto_here_1 (self
);
201 /* Non-zero means success. */
205 /* Low level routine to create a <gdb:breakpoint> object. */
208 bpscm_make_breakpoint_smob (void)
210 breakpoint_smob
*bp_smob
= (breakpoint_smob
*)
211 scm_gc_malloc (sizeof (breakpoint_smob
), breakpoint_smob_name
);
214 memset (bp_smob
, 0, sizeof (*bp_smob
));
215 bp_smob
->number
= -1;
216 bp_smob
->stop
= SCM_BOOL_F
;
217 bp_scm
= scm_new_smob (breakpoint_smob_tag
, (scm_t_bits
) bp_smob
);
218 bp_smob
->containing_scm
= bp_scm
;
219 gdbscm_init_gsmob (&bp_smob
->base
);
224 /* Return non-zero if we want a Scheme wrapper for breakpoint B.
225 If FROM_SCHEME is non-zero,this is called for a breakpoint created
226 by the user from Scheme. Otherwise it is zero. */
229 bpscm_want_scm_wrapper_p (struct breakpoint
*bp
, int from_scheme
)
231 /* Don't create <gdb:breakpoint> objects for internal GDB breakpoints. */
232 if (bp
->number
< 0 && !from_scheme
)
235 /* The others are not supported. */
236 if (bp
->type
!= bp_breakpoint
237 && bp
->type
!= bp_watchpoint
238 && bp
->type
!= bp_hardware_watchpoint
239 && bp
->type
!= bp_read_watchpoint
240 && bp
->type
!= bp_access_watchpoint
241 && bp
->type
!= bp_catchpoint
)
247 /* Install the Scheme side of a breakpoint, CONTAINING_SCM, in
251 bpscm_attach_scm_to_breakpoint (struct breakpoint
*bp
, SCM containing_scm
)
253 breakpoint_smob
*bp_smob
;
255 bp_smob
= (breakpoint_smob
*) SCM_SMOB_DATA (containing_scm
);
256 bp_smob
->number
= bp
->number
;
258 bp_smob
->containing_scm
= containing_scm
;
259 bp_smob
->bp
->scm_bp_object
= bp_smob
;
261 /* The owner of this breakpoint is not in GC-controlled memory, so we need
262 to protect it from GC until the breakpoint is deleted. */
263 scm_gc_protect_object (containing_scm
);
266 /* Return non-zero if SCM is a breakpoint smob. */
269 bpscm_is_breakpoint (SCM scm
)
271 return SCM_SMOB_PREDICATE (breakpoint_smob_tag
, scm
);
274 /* (breakpoint? scm) -> boolean */
277 gdbscm_breakpoint_p (SCM scm
)
279 return scm_from_bool (bpscm_is_breakpoint (scm
));
282 /* Returns the <gdb:breakpoint> object in SELF.
283 Throws an exception if SELF is not a <gdb:breakpoint> object. */
286 bpscm_get_breakpoint_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
288 SCM_ASSERT_TYPE (bpscm_is_breakpoint (self
), self
, arg_pos
, func_name
,
289 breakpoint_smob_name
);
294 /* Returns a pointer to the breakpoint smob of SELF.
295 Throws an exception if SELF is not a <gdb:breakpoint> object. */
297 static breakpoint_smob
*
298 bpscm_get_breakpoint_smob_arg_unsafe (SCM self
, int arg_pos
,
299 const char *func_name
)
301 SCM bp_scm
= bpscm_get_breakpoint_arg_unsafe (self
, arg_pos
, func_name
);
302 breakpoint_smob
*bp_smob
= (breakpoint_smob
*) SCM_SMOB_DATA (bp_scm
);
307 /* Return non-zero if breakpoint BP_SMOB is valid. */
310 bpscm_is_valid (breakpoint_smob
*bp_smob
)
312 return bp_smob
->bp
!= NULL
;
315 /* Returns the breakpoint smob in SELF, verifying it's valid.
316 Throws an exception if SELF is not a <gdb:breakpoint> object,
319 static breakpoint_smob
*
320 bpscm_get_valid_breakpoint_smob_arg_unsafe (SCM self
, int arg_pos
,
321 const char *func_name
)
323 breakpoint_smob
*bp_smob
324 = bpscm_get_breakpoint_smob_arg_unsafe (self
, arg_pos
, func_name
);
326 if (!bpscm_is_valid (bp_smob
))
328 gdbscm_invalid_object_error (func_name
, arg_pos
, self
,
329 _("<gdb:breakpoint>"));
335 /* Breakpoint methods. */
337 /* (make-breakpoint string [#:type integer] [#:wp-class integer]
338 [#:internal boolean] [#:temporary boolean]) -> <gdb:breakpoint>
340 The result is the <gdb:breakpoint> Scheme object.
341 The breakpoint is not available to be used yet, however.
342 It must still be added to gdb with register-breakpoint!. */
345 gdbscm_make_breakpoint (SCM location_scm
, SCM rest
)
347 const SCM keywords
[] = {
348 type_keyword
, wp_class_keyword
, internal_keyword
,
349 temporary_keyword
, SCM_BOOL_F
353 int type_arg_pos
= -1, access_type_arg_pos
= -1,
354 internal_arg_pos
= -1, temporary_arg_pos
= -1;
355 int type
= bp_breakpoint
;
356 int access_type
= hw_write
;
360 breakpoint_smob
*bp_smob
;
362 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, keywords
, "s#iitt",
363 location_scm
, &location
, rest
,
364 &type_arg_pos
, &type
,
365 &access_type_arg_pos
, &access_type
,
366 &internal_arg_pos
, &internal
,
367 &temporary_arg_pos
, &temporary
);
369 result
= bpscm_make_breakpoint_smob ();
370 bp_smob
= (breakpoint_smob
*) SCM_SMOB_DATA (result
);
373 location
= gdbscm_gc_xstrdup (s
);
379 if (access_type_arg_pos
> 0)
381 gdbscm_misc_error (FUNC_NAME
, access_type_arg_pos
,
382 scm_from_int (access_type
),
383 _("access type with breakpoint is not allowed"));
394 gdbscm_out_of_range_error (FUNC_NAME
, access_type_arg_pos
,
395 scm_from_int (access_type
),
396 _("invalid watchpoint class"));
400 case bp_hardware_watchpoint
:
401 case bp_read_watchpoint
:
402 case bp_access_watchpoint
:
405 const char *type_name
= bpscm_type_to_string ((enum bptype
) type
);
406 gdbscm_misc_error (FUNC_NAME
, type_arg_pos
,
407 gdbscm_scm_from_c_string (type_name
),
408 _("unsupported breakpoint type"));
412 gdbscm_out_of_range_error (FUNC_NAME
, type_arg_pos
,
414 _("invalid breakpoint type"));
417 bp_smob
->is_scheme_bkpt
= 1;
418 bp_smob
->spec
.location
= location
;
419 bp_smob
->spec
.type
= (enum bptype
) type
;
420 bp_smob
->spec
.access_type
= (enum target_hw_bp_type
) access_type
;
421 bp_smob
->spec
.is_internal
= internal
;
422 bp_smob
->spec
.is_temporary
= temporary
;
427 /* (register-breakpoint! <gdb:breakpoint>) -> unspecified
429 It is an error to register a breakpoint created outside of Guile,
430 or an already-registered breakpoint. */
433 gdbscm_register_breakpoint_x (SCM self
)
435 breakpoint_smob
*bp_smob
436 = bpscm_get_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
437 gdbscm_gdb_exception except
{};
438 const char *location
, *copy
;
440 /* We only support registering breakpoints created with make-breakpoint. */
441 if (!bp_smob
->is_scheme_bkpt
)
442 scm_misc_error (FUNC_NAME
, _("not a Scheme breakpoint"), SCM_EOL
);
444 if (bpscm_is_valid (bp_smob
))
445 scm_misc_error (FUNC_NAME
, _("breakpoint is already registered"), SCM_EOL
);
447 pending_breakpoint_scm
= self
;
448 location
= bp_smob
->spec
.location
;
449 copy
= skip_spaces (location
);
450 location_spec_up locspec
451 = string_to_location_spec_basic (©
,
453 symbol_name_match_type::WILD
);
457 int internal
= bp_smob
->spec
.is_internal
;
458 int temporary
= bp_smob
->spec
.is_temporary
;
460 switch (bp_smob
->spec
.type
)
464 const breakpoint_ops
*ops
=
465 breakpoint_ops_for_location_spec (locspec
.get (), false);
466 create_breakpoint (get_current_arch (),
467 locspec
.get (), NULL
, -1, -1, NULL
, false,
469 temporary
, bp_breakpoint
,
478 enum target_hw_bp_type access_type
= bp_smob
->spec
.access_type
;
480 if (access_type
== hw_write
)
481 watch_command_wrapper (location
, 0, internal
);
482 else if (access_type
== hw_access
)
483 awatch_command_wrapper (location
, 0, internal
);
484 else if (access_type
== hw_read
)
485 rwatch_command_wrapper (location
, 0, internal
);
487 gdb_assert_not_reached ("invalid access type");
491 gdb_assert_not_reached ("invalid breakpoint type");
494 catch (const gdb_exception
&ex
)
496 except
= unpack (ex
);
499 /* Ensure this gets reset, even if there's an error. */
500 pending_breakpoint_scm
= SCM_BOOL_F
;
501 GDBSCM_HANDLE_GDB_EXCEPTION (except
);
503 return SCM_UNSPECIFIED
;
506 /* (delete-breakpoint! <gdb:breakpoint>) -> unspecified
507 Scheme function which deletes (removes) the underlying GDB breakpoint
508 from GDB's list of breakpoints. This triggers the breakpoint_deleted
509 observer which will call gdbscm_breakpoint_deleted; that function cleans
510 up the Scheme bits. */
513 gdbscm_delete_breakpoint_x (SCM self
)
515 breakpoint_smob
*bp_smob
516 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
518 gdbscm_gdb_exception exc
{};
521 delete_breakpoint (bp_smob
->bp
);
523 catch (const gdb_exception
&except
)
525 exc
= unpack (except
);
528 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
529 return SCM_UNSPECIFIED
;
532 /* iterate_over_breakpoints function for gdbscm_breakpoints. */
535 bpscm_build_bp_list (struct breakpoint
*bp
, SCM
*list
)
537 breakpoint_smob
*bp_smob
= bp
->scm_bp_object
;
539 /* Lazily create wrappers for breakpoints created outside Scheme. */
543 if (bpscm_want_scm_wrapper_p (bp
, 0))
547 bp_scm
= bpscm_make_breakpoint_smob ();
548 bpscm_attach_scm_to_breakpoint (bp
, bp_scm
);
550 bp_smob
= bp
->scm_bp_object
;
554 /* Not all breakpoints will have a companion Scheme object.
555 Only breakpoints that trigger the created_breakpoint observer call,
556 and satisfy certain conditions (see bpscm_want_scm_wrapper_p),
557 get a companion object (this includes Scheme-created breakpoints). */
560 *list
= scm_cons (bp_smob
->containing_scm
, *list
);
563 /* (breakpoints) -> list
564 Return a list of all breakpoints. */
567 gdbscm_breakpoints (void)
571 for (breakpoint
&bp
: all_breakpoints ())
572 bpscm_build_bp_list (&bp
, &list
);
574 return scm_reverse_x (list
, SCM_EOL
);
577 /* (breakpoint-valid? <gdb:breakpoint>) -> boolean
578 Returns #t if SELF is still valid. */
581 gdbscm_breakpoint_valid_p (SCM self
)
583 breakpoint_smob
*bp_smob
584 = bpscm_get_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
586 return scm_from_bool (bpscm_is_valid (bp_smob
));
589 /* (breakpoint-enabled? <gdb:breakpoint>) -> boolean */
592 gdbscm_breakpoint_enabled_p (SCM self
)
594 breakpoint_smob
*bp_smob
595 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
597 return scm_from_bool (bp_smob
->bp
->enable_state
== bp_enabled
);
600 /* (set-breakpoint-enabled? <gdb:breakpoint> boolean) -> unspecified */
603 gdbscm_set_breakpoint_enabled_x (SCM self
, SCM newvalue
)
605 breakpoint_smob
*bp_smob
606 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
608 SCM_ASSERT_TYPE (gdbscm_is_bool (newvalue
), newvalue
, SCM_ARG2
, FUNC_NAME
,
611 gdbscm_gdb_exception exc
{};
614 if (gdbscm_is_true (newvalue
))
615 enable_breakpoint (bp_smob
->bp
);
617 disable_breakpoint (bp_smob
->bp
);
619 catch (const gdb_exception
&except
)
621 exc
= unpack (except
);
624 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
625 return SCM_UNSPECIFIED
;
628 /* (breakpoint-silent? <gdb:breakpoint>) -> boolean */
631 gdbscm_breakpoint_silent_p (SCM self
)
633 breakpoint_smob
*bp_smob
634 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
636 return scm_from_bool (bp_smob
->bp
->silent
);
639 /* (set-breakpoint-silent?! <gdb:breakpoint> boolean) -> unspecified */
642 gdbscm_set_breakpoint_silent_x (SCM self
, SCM newvalue
)
644 breakpoint_smob
*bp_smob
645 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
647 SCM_ASSERT_TYPE (gdbscm_is_bool (newvalue
), newvalue
, SCM_ARG2
, FUNC_NAME
,
650 gdbscm_gdb_exception exc
{};
653 breakpoint_set_silent (bp_smob
->bp
, gdbscm_is_true (newvalue
));
655 catch (const gdb_exception
&except
)
657 exc
= unpack (except
);
660 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
661 return SCM_UNSPECIFIED
;
664 /* (breakpoint-ignore-count <gdb:breakpoint>) -> integer */
667 gdbscm_breakpoint_ignore_count (SCM self
)
669 breakpoint_smob
*bp_smob
670 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
672 return scm_from_long (bp_smob
->bp
->ignore_count
);
675 /* (set-breakpoint-ignore-count! <gdb:breakpoint> integer)
679 gdbscm_set_breakpoint_ignore_count_x (SCM self
, SCM newvalue
)
681 breakpoint_smob
*bp_smob
682 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
685 SCM_ASSERT_TYPE (scm_is_signed_integer (newvalue
, LONG_MIN
, LONG_MAX
),
686 newvalue
, SCM_ARG2
, FUNC_NAME
, _("integer"));
688 value
= scm_to_long (newvalue
);
692 gdbscm_gdb_exception exc
{};
695 set_ignore_count (bp_smob
->number
, (int) value
, 0);
697 catch (const gdb_exception
&except
)
699 exc
= unpack (except
);
702 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
703 return SCM_UNSPECIFIED
;
706 /* (breakpoint-hit-count <gdb:breakpoint>) -> integer */
709 gdbscm_breakpoint_hit_count (SCM self
)
711 breakpoint_smob
*bp_smob
712 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
714 return scm_from_long (bp_smob
->bp
->hit_count
);
717 /* (set-breakpoint-hit-count! <gdb:breakpoint> integer) -> unspecified */
720 gdbscm_set_breakpoint_hit_count_x (SCM self
, SCM newvalue
)
722 breakpoint_smob
*bp_smob
723 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
726 SCM_ASSERT_TYPE (scm_is_signed_integer (newvalue
, LONG_MIN
, LONG_MAX
),
727 newvalue
, SCM_ARG2
, FUNC_NAME
, _("integer"));
729 value
= scm_to_long (newvalue
);
735 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG2
, newvalue
,
736 _("hit-count must be zero"));
739 bp_smob
->bp
->hit_count
= 0;
741 return SCM_UNSPECIFIED
;
744 /* (breakpoint-thread <gdb:breakpoint>) -> integer */
747 gdbscm_breakpoint_thread (SCM self
)
749 breakpoint_smob
*bp_smob
750 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
752 if (bp_smob
->bp
->thread
== -1)
755 return scm_from_long (bp_smob
->bp
->thread
);
758 /* (set-breakpoint-thread! <gdb:breakpoint> integer) -> unspecified */
761 gdbscm_set_breakpoint_thread_x (SCM self
, SCM newvalue
)
763 breakpoint_smob
*bp_smob
764 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
767 if (scm_is_signed_integer (newvalue
, LONG_MIN
, LONG_MAX
))
769 id
= scm_to_long (newvalue
);
770 if (!valid_global_thread_id (id
))
772 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG2
, newvalue
,
773 _("invalid thread id"));
776 if (bp_smob
->bp
->task
!= -1)
777 scm_misc_error (FUNC_NAME
,
778 _("cannot set both task and thread attributes"),
781 else if (gdbscm_is_false (newvalue
))
784 SCM_ASSERT_TYPE (0, newvalue
, SCM_ARG2
, FUNC_NAME
, _("integer or #f"));
786 if (bp_smob
->bp
->inferior
!= -1 && id
!= -1)
787 scm_misc_error (FUNC_NAME
,
788 _("Cannot have both 'thread' and 'inferior' "
789 "conditions on a breakpoint"), SCM_EOL
);
791 breakpoint_set_thread (bp_smob
->bp
, id
);
793 return SCM_UNSPECIFIED
;
796 /* (breakpoint-task <gdb:breakpoint>) -> integer */
799 gdbscm_breakpoint_task (SCM self
)
801 breakpoint_smob
*bp_smob
802 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
804 if (bp_smob
->bp
->task
== -1)
807 return scm_from_long (bp_smob
->bp
->task
);
810 /* (set-breakpoint-task! <gdb:breakpoint> integer) -> unspecified */
813 gdbscm_set_breakpoint_task_x (SCM self
, SCM newvalue
)
815 breakpoint_smob
*bp_smob
816 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
820 if (scm_is_signed_integer (newvalue
, LONG_MIN
, LONG_MAX
))
822 id
= scm_to_long (newvalue
);
824 gdbscm_gdb_exception exc
{};
827 valid_id
= valid_task_id (id
);
829 catch (const gdb_exception
&except
)
831 exc
= unpack (except
);
834 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
837 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG2
, newvalue
,
838 _("invalid task id"));
841 if (bp_smob
->bp
->thread
!= -1)
842 scm_misc_error (FUNC_NAME
,
843 _("cannot set both task and thread attributes"),
846 else if (gdbscm_is_false (newvalue
))
849 SCM_ASSERT_TYPE (0, newvalue
, SCM_ARG2
, FUNC_NAME
, _("integer or #f"));
851 gdbscm_gdb_exception exc
{};
854 breakpoint_set_task (bp_smob
->bp
, id
);
856 catch (const gdb_exception
&except
)
858 exc
= unpack (except
);
861 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
862 return SCM_UNSPECIFIED
;
865 /* (breakpoint-location <gdb:breakpoint>) -> string */
868 gdbscm_breakpoint_location (SCM self
)
870 breakpoint_smob
*bp_smob
871 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
873 if (bp_smob
->bp
->type
!= bp_breakpoint
)
876 const char *str
= bp_smob
->bp
->locspec
->to_string ();
880 return gdbscm_scm_from_c_string (str
);
883 /* (breakpoint-expression <gdb:breakpoint>) -> string
884 This is only valid for watchpoints.
885 Returns #f for non-watchpoints. */
888 gdbscm_breakpoint_expression (SCM self
)
890 breakpoint_smob
*bp_smob
891 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
893 if (!is_watchpoint (bp_smob
->bp
))
896 watchpoint
*wp
= gdb::checked_static_cast
<watchpoint
*> (bp_smob
->bp
);
898 const char *str
= wp
->exp_string
.get ();
902 return gdbscm_scm_from_c_string (str
);
905 /* (breakpoint-condition <gdb:breakpoint>) -> string */
908 gdbscm_breakpoint_condition (SCM self
)
910 breakpoint_smob
*bp_smob
911 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
914 str
= bp_smob
->bp
->cond_string
.get ();
918 return gdbscm_scm_from_c_string (str
);
921 /* (set-breakpoint-condition! <gdb:breakpoint> string|#f)
925 gdbscm_set_breakpoint_condition_x (SCM self
, SCM newvalue
)
927 breakpoint_smob
*bp_smob
928 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
930 SCM_ASSERT_TYPE (scm_is_string (newvalue
) || gdbscm_is_false (newvalue
),
931 newvalue
, SCM_ARG2
, FUNC_NAME
,
934 return gdbscm_wrap ([=]
936 gdb::unique_xmalloc_ptr
<char> exp
937 = (gdbscm_is_false (newvalue
)
939 : gdbscm_scm_to_c_string (newvalue
));
941 set_breakpoint_condition (bp_smob
->bp
, exp
? exp
.get () : "", 0, false);
943 return SCM_UNSPECIFIED
;
947 /* (breakpoint-stop <gdb:breakpoint>) -> procedure or #f */
950 gdbscm_breakpoint_stop (SCM self
)
952 breakpoint_smob
*bp_smob
953 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
955 return bp_smob
->stop
;
958 /* (set-breakpoint-stop! <gdb:breakpoint> procedure|#f)
962 gdbscm_set_breakpoint_stop_x (SCM self
, SCM newvalue
)
964 breakpoint_smob
*bp_smob
965 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
966 const struct extension_language_defn
*extlang
= NULL
;
968 SCM_ASSERT_TYPE (gdbscm_is_procedure (newvalue
)
969 || gdbscm_is_false (newvalue
),
970 newvalue
, SCM_ARG2
, FUNC_NAME
,
971 _("procedure or #f"));
973 if (bp_smob
->bp
->cond_string
!= NULL
)
974 extlang
= get_ext_lang_defn (EXT_LANG_GDB
);
976 extlang
= get_breakpoint_cond_ext_lang (bp_smob
->bp
, EXT_LANG_GUILE
);
980 = xstrprintf (_("Only one stop condition allowed. There is"
981 " currently a %s stop condition defined for"
982 " this breakpoint."),
983 ext_lang_capitalized_name (extlang
)).release ();
985 scm_dynwind_begin ((scm_t_dynwind_flags
) 0);
986 gdbscm_dynwind_xfree (error_text
);
987 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG1
, self
, error_text
);
988 /* The following line, while unnecessary, is present for completeness
993 bp_smob
->stop
= newvalue
;
995 return SCM_UNSPECIFIED
;
998 /* (breakpoint-commands <gdb:breakpoint>) -> string */
1001 gdbscm_breakpoint_commands (SCM self
)
1003 breakpoint_smob
*bp_smob
1004 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1005 struct breakpoint
*bp
;
1010 if (bp
->commands
== NULL
)
1015 gdbscm_gdb_exception exc
{};
1018 ui_out_redirect_pop
redir (current_uiout
, &buf
);
1019 print_command_lines (current_uiout
, breakpoint_commands (bp
), 0);
1021 catch (const gdb_exception
&except
)
1023 exc
= unpack (except
);
1026 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
1027 result
= gdbscm_scm_from_c_string (buf
.c_str ());
1032 /* (breakpoint-type <gdb:breakpoint>) -> integer */
1035 gdbscm_breakpoint_type (SCM self
)
1037 breakpoint_smob
*bp_smob
1038 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1040 return scm_from_long (bp_smob
->bp
->type
);
1043 /* (breakpoint-visible? <gdb:breakpoint>) -> boolean */
1046 gdbscm_breakpoint_visible (SCM self
)
1048 breakpoint_smob
*bp_smob
1049 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1051 return scm_from_bool (bp_smob
->bp
->number
>= 0);
1054 /* (breakpoint-number <gdb:breakpoint>) -> integer */
1057 gdbscm_breakpoint_number (SCM self
)
1059 breakpoint_smob
*bp_smob
1060 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1062 return scm_from_long (bp_smob
->number
);
1065 /* (breakpoint-temporary? <gdb:breakpoint>) -> boolean */
1068 gdbscm_breakpoint_temporary (SCM self
)
1070 breakpoint_smob
*bp_smob
1071 = bpscm_get_valid_breakpoint_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1073 return scm_from_bool (bp_smob
->bp
->disposition
== disp_del
1074 || bp_smob
->bp
->disposition
== disp_del_at_next_stop
);
1077 /* Return TRUE if "stop" has been set for this breakpoint.
1079 This is the extension_language_ops.breakpoint_has_cond "method". */
1082 gdbscm_breakpoint_has_cond (const struct extension_language_defn
*extlang
,
1083 struct breakpoint
*b
)
1085 breakpoint_smob
*bp_smob
= b
->scm_bp_object
;
1087 if (bp_smob
== NULL
)
1090 return gdbscm_is_procedure (bp_smob
->stop
);
1093 /* Call the "stop" method in the breakpoint class.
1094 This must only be called if gdbscm_breakpoint_has_cond returns true.
1095 If the stop method returns #t, the inferior will be stopped at the
1096 breakpoint. Otherwise the inferior will be allowed to continue
1097 (assuming other conditions don't indicate "stop").
1099 This is the extension_language_ops.breakpoint_cond_says_stop "method". */
1101 enum ext_lang_bp_stop
1102 gdbscm_breakpoint_cond_says_stop
1103 (const struct extension_language_defn
*extlang
, struct breakpoint
*b
)
1105 breakpoint_smob
*bp_smob
= b
->scm_bp_object
;
1106 SCM predicate_result
;
1109 if (bp_smob
== NULL
)
1110 return EXT_LANG_BP_STOP_UNSET
;
1111 if (!gdbscm_is_procedure (bp_smob
->stop
))
1112 return EXT_LANG_BP_STOP_UNSET
;
1117 = gdbscm_safe_call_1 (bp_smob
->stop
, bp_smob
->containing_scm
, NULL
);
1119 if (gdbscm_is_exception (predicate_result
))
1120 ; /* Exception already printed. */
1121 /* If the "stop" function returns #f that means
1122 the Scheme breakpoint wants GDB to continue. */
1123 else if (gdbscm_is_false (predicate_result
))
1126 return stop
? EXT_LANG_BP_STOP_YES
: EXT_LANG_BP_STOP_NO
;
1129 /* Event callback functions. */
1131 /* Callback that is used when a breakpoint is created.
1132 For breakpoints created by Scheme, i.e., gdbscm_create_breakpoint_x, finish
1133 object creation by connecting the Scheme wrapper to the gdb object.
1134 We ignore breakpoints created from gdb or python here, we create the
1135 Scheme wrapper for those when there's a need to, e.g.,
1136 gdbscm_breakpoints. */
1139 bpscm_breakpoint_created (struct breakpoint
*bp
)
1143 if (gdbscm_is_false (pending_breakpoint_scm
))
1146 /* Verify our caller error checked the user's request. */
1147 gdb_assert (bpscm_want_scm_wrapper_p (bp
, 1));
1149 bp_scm
= pending_breakpoint_scm
;
1150 pending_breakpoint_scm
= SCM_BOOL_F
;
1152 bpscm_attach_scm_to_breakpoint (bp
, bp_scm
);
1155 /* Callback that is used when a breakpoint is deleted. This will
1156 invalidate the corresponding Scheme object. */
1159 bpscm_breakpoint_deleted (struct breakpoint
*b
)
1161 int num
= b
->number
;
1162 struct breakpoint
*bp
;
1164 /* TODO: Why the lookup? We have B. */
1166 bp
= get_breakpoint (num
);
1169 breakpoint_smob
*bp_smob
= bp
->scm_bp_object
;
1174 bp_smob
->number
= -1;
1175 bp_smob
->stop
= SCM_BOOL_F
;
1176 scm_gc_unprotect_object (bp_smob
->containing_scm
);
1181 /* Initialize the Scheme breakpoint code. */
1183 static const scheme_integer_constant breakpoint_integer_constants
[] =
1185 { "BP_NONE", bp_none
},
1186 { "BP_BREAKPOINT", bp_breakpoint
},
1187 { "BP_WATCHPOINT", bp_watchpoint
},
1188 { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint
},
1189 { "BP_READ_WATCHPOINT", bp_read_watchpoint
},
1190 { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint
},
1191 { "BP_CATCHPOINT", bp_catchpoint
},
1193 { "WP_READ", hw_read
},
1194 { "WP_WRITE", hw_write
},
1195 { "WP_ACCESS", hw_access
},
1197 END_INTEGER_CONSTANTS
1200 static const scheme_function breakpoint_functions
[] =
1202 { "make-breakpoint", 1, 0, 1, as_a_scm_t_subr (gdbscm_make_breakpoint
),
1204 Create a GDB breakpoint object.\n\
1207 location [#:type <type>] [#:wp-class <wp-class>] [#:internal <bool>] [#:temporary <bool>]\n\
1209 <gdb:breakpoint> object" },
1211 { "register-breakpoint!", 1, 0, 0,
1212 as_a_scm_t_subr (gdbscm_register_breakpoint_x
),
1214 Register a <gdb:breakpoint> object with GDB." },
1216 { "delete-breakpoint!", 1, 0, 0, as_a_scm_t_subr (gdbscm_delete_breakpoint_x
),
1218 Delete the breakpoint from GDB." },
1220 { "breakpoints", 0, 0, 0, as_a_scm_t_subr (gdbscm_breakpoints
),
1222 Return a list of all GDB breakpoints.\n\
1226 { "breakpoint?", 1, 0, 0, as_a_scm_t_subr (gdbscm_breakpoint_p
),
1228 Return #t if the object is a <gdb:breakpoint> object." },
1230 { "breakpoint-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_breakpoint_valid_p
),
1232 Return #t if the breakpoint has not been deleted from GDB." },
1234 { "breakpoint-number", 1, 0, 0, as_a_scm_t_subr (gdbscm_breakpoint_number
),
1236 Return the breakpoint's number." },
1238 { "breakpoint-temporary?", 1, 0, 0, as_a_scm_t_subr (gdbscm_breakpoint_temporary
),
1240 Return #t if the breakpoint is a temporary breakpoint." },
1242 { "breakpoint-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_breakpoint_type
),
1244 Return the type of the breakpoint." },
1246 { "breakpoint-visible?", 1, 0, 0, as_a_scm_t_subr (gdbscm_breakpoint_visible
),
1248 Return #t if the breakpoint is visible to the user." },
1250 { "breakpoint-location", 1, 0, 0,
1251 as_a_scm_t_subr (gdbscm_breakpoint_location
),
1253 Return the location of the breakpoint as specified by the user." },
1255 { "breakpoint-expression", 1, 0, 0,
1256 as_a_scm_t_subr (gdbscm_breakpoint_expression
),
1258 Return the expression of the breakpoint as specified by the user.\n\
1259 Valid for watchpoints only, returns #f for non-watchpoints." },
1261 { "breakpoint-enabled?", 1, 0, 0,
1262 as_a_scm_t_subr (gdbscm_breakpoint_enabled_p
),
1264 Return #t if the breakpoint is enabled." },
1266 { "set-breakpoint-enabled!", 2, 0, 0,
1267 as_a_scm_t_subr (gdbscm_set_breakpoint_enabled_x
),
1269 Set the breakpoint's enabled state.\n\
1271 Arguments: <gdb:breakpoint> boolean" },
1273 { "breakpoint-silent?", 1, 0, 0, as_a_scm_t_subr (gdbscm_breakpoint_silent_p
),
1275 Return #t if the breakpoint is silent." },
1277 { "set-breakpoint-silent!", 2, 0, 0,
1278 as_a_scm_t_subr (gdbscm_set_breakpoint_silent_x
),
1280 Set the breakpoint's silent state.\n\
1282 Arguments: <gdb:breakpoint> boolean" },
1284 { "breakpoint-ignore-count", 1, 0, 0,
1285 as_a_scm_t_subr (gdbscm_breakpoint_ignore_count
),
1287 Return the breakpoint's \"ignore\" count." },
1289 { "set-breakpoint-ignore-count!", 2, 0, 0,
1290 as_a_scm_t_subr (gdbscm_set_breakpoint_ignore_count_x
),
1292 Set the breakpoint's \"ignore\" count.\n\
1294 Arguments: <gdb:breakpoint> count" },
1296 { "breakpoint-hit-count", 1, 0, 0,
1297 as_a_scm_t_subr (gdbscm_breakpoint_hit_count
),
1299 Return the breakpoint's \"hit\" count." },
1301 { "set-breakpoint-hit-count!", 2, 0, 0,
1302 as_a_scm_t_subr (gdbscm_set_breakpoint_hit_count_x
),
1304 Set the breakpoint's \"hit\" count. The value must be zero.\n\
1306 Arguments: <gdb:breakpoint> 0" },
1308 { "breakpoint-thread", 1, 0, 0, as_a_scm_t_subr (gdbscm_breakpoint_thread
),
1310 Return the breakpoint's global thread id or #f if there isn't one." },
1312 { "set-breakpoint-thread!", 2, 0, 0,
1313 as_a_scm_t_subr (gdbscm_set_breakpoint_thread_x
),
1315 Set the global thread id for this breakpoint.\n\
1317 Arguments: <gdb:breakpoint> global-thread-id" },
1319 { "breakpoint-task", 1, 0, 0, as_a_scm_t_subr (gdbscm_breakpoint_task
),
1321 Return the breakpoint's Ada task-id or #f if there isn't one." },
1323 { "set-breakpoint-task!", 2, 0, 0,
1324 as_a_scm_t_subr (gdbscm_set_breakpoint_task_x
),
1326 Set the breakpoint's Ada task-id.\n\
1328 Arguments: <gdb:breakpoint> task-id" },
1330 { "breakpoint-condition", 1, 0, 0,
1331 as_a_scm_t_subr (gdbscm_breakpoint_condition
),
1333 Return the breakpoint's condition as specified by the user.\n\
1334 Return #f if there isn't one." },
1336 { "set-breakpoint-condition!", 2, 0, 0,
1337 as_a_scm_t_subr (gdbscm_set_breakpoint_condition_x
),
1339 Set the breakpoint's condition.\n\
1341 Arguments: <gdb:breakpoint> condition\n\
1342 condition: a string" },
1344 { "breakpoint-stop", 1, 0, 0, as_a_scm_t_subr (gdbscm_breakpoint_stop
),
1346 Return the breakpoint's stop predicate.\n\
1347 Return #f if there isn't one." },
1349 { "set-breakpoint-stop!", 2, 0, 0,
1350 as_a_scm_t_subr (gdbscm_set_breakpoint_stop_x
),
1352 Set the breakpoint's stop predicate.\n\
1354 Arguments: <gdb:breakpoint> procedure\n\
1355 procedure: A procedure of one argument, the breakpoint.\n\
1356 Its result is true if program execution should stop." },
1358 { "breakpoint-commands", 1, 0, 0,
1359 as_a_scm_t_subr (gdbscm_breakpoint_commands
),
1361 Return the breakpoint's commands." },
1367 gdbscm_initialize_breakpoints (void)
1370 = gdbscm_make_smob_type (breakpoint_smob_name
, sizeof (breakpoint_smob
));
1371 scm_set_smob_free (breakpoint_smob_tag
, bpscm_free_breakpoint_smob
);
1372 scm_set_smob_print (breakpoint_smob_tag
, bpscm_print_breakpoint_smob
);
1374 gdb::observers::breakpoint_created
.attach (bpscm_breakpoint_created
,
1376 gdb::observers::breakpoint_deleted
.attach (bpscm_breakpoint_deleted
,
1379 gdbscm_define_integer_constants (breakpoint_integer_constants
, 1);
1380 gdbscm_define_functions (breakpoint_functions
, 1);
1382 type_keyword
= scm_from_latin1_keyword ("type");
1383 wp_class_keyword
= scm_from_latin1_keyword ("wp-class");
1384 internal_keyword
= scm_from_latin1_keyword ("internal");
1385 temporary_keyword
= scm_from_latin1_keyword ("temporary");