1 /* Python interface to breakpoints
3 Copyright (C) 2008-2021 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/>. */
22 #include "python-internal.h"
25 #include "breakpoint.h"
27 #include "gdbthread.h"
28 #include "observable.h"
29 #include "cli/cli-script.h"
31 #include "arch-utils.h"
37 /* Debugging of Python breakpoints. */
39 static bool pybp_debug
;
41 /* Implementation of "show debug py-breakpoint". */
44 show_pybp_debug (struct ui_file
*file
, int from_tty
,
45 struct cmd_list_element
*c
, const char *value
)
47 fprintf_filtered (file
, _("Python breakpoint debugging is %s.\n"), value
);
50 /* Print a "py-breakpoint" debug statement. */
52 #define pybp_debug_printf(fmt, ...) \
53 debug_prefixed_printf_cond (pybp_debug, "py-breakpoint", fmt, ##__VA_ARGS__)
55 /* Print a "py-breakpoint" enter/exit debug statements. */
57 #define PYBP_SCOPED_DEBUG_ENTER_EXIT \
58 scoped_debug_enter_exit (pybp_debug, "py-breakpoint")
60 /* Number of live breakpoints. */
63 /* Variables used to pass information between the Breakpoint
64 constructor and the breakpoint-created hook function. */
65 gdbpy_breakpoint_object
*bppy_pending_object
;
67 /* Function that is called when a Python condition is evaluated. */
68 static const char stop_func
[] = "stop";
70 /* This is used to initialize various gdb.bp_* constants. */
79 /* Entries related to the type of user set breakpoints. */
80 static struct pybp_code pybp_codes
[] =
82 { "BP_NONE", bp_none
},
83 { "BP_BREAKPOINT", bp_breakpoint
},
84 { "BP_HARDWARE_BREAKPOINT", bp_hardware_breakpoint
},
85 { "BP_WATCHPOINT", bp_watchpoint
},
86 { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint
},
87 { "BP_READ_WATCHPOINT", bp_read_watchpoint
},
88 { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint
},
89 {NULL
} /* Sentinel. */
92 /* Entries related to the type of watchpoint. */
93 static struct pybp_code pybp_watch_types
[] =
95 { "WP_READ", hw_read
},
96 { "WP_WRITE", hw_write
},
97 { "WP_ACCESS", hw_access
},
98 {NULL
} /* Sentinel. */
101 /* Python function which checks the validity of a breakpoint object. */
103 bppy_is_valid (PyObject
*self
, PyObject
*args
)
105 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
112 /* Python function to test whether or not the breakpoint is enabled. */
114 bppy_get_enabled (PyObject
*self
, void *closure
)
116 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
118 BPPY_REQUIRE_VALID (self_bp
);
121 if (self_bp
->bp
->enable_state
== bp_enabled
)
126 /* Python function to test whether or not the breakpoint is silent. */
128 bppy_get_silent (PyObject
*self
, void *closure
)
130 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
132 BPPY_REQUIRE_VALID (self_bp
);
133 if (self_bp
->bp
->silent
)
138 /* Python function to set the enabled state of a breakpoint. */
140 bppy_set_enabled (PyObject
*self
, PyObject
*newvalue
, void *closure
)
142 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
145 BPPY_SET_REQUIRE_VALID (self_bp
);
147 if (newvalue
== NULL
)
149 PyErr_SetString (PyExc_TypeError
,
150 _("Cannot delete `enabled' attribute."));
154 else if (! PyBool_Check (newvalue
))
156 PyErr_SetString (PyExc_TypeError
,
157 _("The value of `enabled' must be a boolean."));
161 cmp
= PyObject_IsTrue (newvalue
);
168 enable_breakpoint (self_bp
->bp
);
170 disable_breakpoint (self_bp
->bp
);
172 catch (const gdb_exception
&except
)
174 GDB_PY_SET_HANDLE_EXCEPTION (except
);
180 /* Python function to set the 'silent' state of a breakpoint. */
182 bppy_set_silent (PyObject
*self
, PyObject
*newvalue
, void *closure
)
184 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
187 BPPY_SET_REQUIRE_VALID (self_bp
);
189 if (newvalue
== NULL
)
191 PyErr_SetString (PyExc_TypeError
,
192 _("Cannot delete `silent' attribute."));
195 else if (! PyBool_Check (newvalue
))
197 PyErr_SetString (PyExc_TypeError
,
198 _("The value of `silent' must be a boolean."));
202 cmp
= PyObject_IsTrue (newvalue
);
206 breakpoint_set_silent (self_bp
->bp
, cmp
);
211 /* Python function to set the thread of a breakpoint. */
213 bppy_set_thread (PyObject
*self
, PyObject
*newvalue
, void *closure
)
215 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
218 BPPY_SET_REQUIRE_VALID (self_bp
);
220 if (newvalue
== NULL
)
222 PyErr_SetString (PyExc_TypeError
,
223 _("Cannot delete `thread' attribute."));
226 else if (PyInt_Check (newvalue
))
228 if (! gdb_py_int_as_long (newvalue
, &id
))
231 if (!valid_global_thread_id (id
))
233 PyErr_SetString (PyExc_RuntimeError
,
234 _("Invalid thread ID."));
238 else if (newvalue
== Py_None
)
242 PyErr_SetString (PyExc_TypeError
,
243 _("The value of `thread' must be an integer or None."));
247 breakpoint_set_thread (self_bp
->bp
, id
);
252 /* Python function to set the (Ada) task of a breakpoint. */
254 bppy_set_task (PyObject
*self
, PyObject
*newvalue
, void *closure
)
256 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
260 BPPY_SET_REQUIRE_VALID (self_bp
);
262 if (newvalue
== NULL
)
264 PyErr_SetString (PyExc_TypeError
,
265 _("Cannot delete `task' attribute."));
268 else if (PyInt_Check (newvalue
))
270 if (! gdb_py_int_as_long (newvalue
, &id
))
275 valid_id
= valid_task_id (id
);
277 catch (const gdb_exception
&except
)
279 GDB_PY_SET_HANDLE_EXCEPTION (except
);
284 PyErr_SetString (PyExc_RuntimeError
,
285 _("Invalid task ID."));
289 else if (newvalue
== Py_None
)
293 PyErr_SetString (PyExc_TypeError
,
294 _("The value of `task' must be an integer or None."));
298 breakpoint_set_task (self_bp
->bp
, id
);
303 /* Python function which deletes the underlying GDB breakpoint. This
304 triggers the breakpoint_deleted observer which will call
305 gdbpy_breakpoint_deleted; that function cleans up the Python
309 bppy_delete_breakpoint (PyObject
*self
, PyObject
*args
)
311 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
313 BPPY_REQUIRE_VALID (self_bp
);
317 delete_breakpoint (self_bp
->bp
);
319 catch (const gdb_exception
&except
)
321 GDB_PY_HANDLE_EXCEPTION (except
);
328 /* Python function to set the ignore count of a breakpoint. */
330 bppy_set_ignore_count (PyObject
*self
, PyObject
*newvalue
, void *closure
)
332 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
335 BPPY_SET_REQUIRE_VALID (self_bp
);
337 if (newvalue
== NULL
)
339 PyErr_SetString (PyExc_TypeError
,
340 _("Cannot delete `ignore_count' attribute."));
343 else if (! PyInt_Check (newvalue
))
345 PyErr_SetString (PyExc_TypeError
,
346 _("The value of `ignore_count' must be an integer."));
350 if (! gdb_py_int_as_long (newvalue
, &value
))
358 set_ignore_count (self_bp
->number
, (int) value
, 0);
360 catch (const gdb_exception
&except
)
362 GDB_PY_SET_HANDLE_EXCEPTION (except
);
368 /* Python function to set the hit count of a breakpoint. */
370 bppy_set_hit_count (PyObject
*self
, PyObject
*newvalue
, void *closure
)
372 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
374 BPPY_SET_REQUIRE_VALID (self_bp
);
376 if (newvalue
== NULL
)
378 PyErr_SetString (PyExc_TypeError
,
379 _("Cannot delete `hit_count' attribute."));
386 if (! gdb_py_int_as_long (newvalue
, &value
))
391 PyErr_SetString (PyExc_AttributeError
,
392 _("The value of `hit_count' must be zero."));
397 self_bp
->bp
->hit_count
= 0;
402 /* Python function to get the location of a breakpoint. */
404 bppy_get_location (PyObject
*self
, void *closure
)
406 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
408 BPPY_REQUIRE_VALID (obj
);
410 if (obj
->bp
->type
!= bp_breakpoint
411 && obj
->bp
->type
!= bp_hardware_breakpoint
)
414 const char *str
= event_location_to_string (obj
->bp
->location
.get ());
417 return host_string_to_python_string (str
).release ();
420 /* Python function to get the breakpoint expression. */
422 bppy_get_expression (PyObject
*self
, void *closure
)
425 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
426 struct watchpoint
*wp
;
428 BPPY_REQUIRE_VALID (obj
);
430 if (!is_watchpoint (obj
->bp
))
433 wp
= (struct watchpoint
*) obj
->bp
;
435 str
= wp
->exp_string
;
439 return host_string_to_python_string (str
).release ();
442 /* Python function to get the condition expression of a breakpoint. */
444 bppy_get_condition (PyObject
*self
, void *closure
)
447 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
449 BPPY_REQUIRE_VALID (obj
);
451 str
= obj
->bp
->cond_string
;
455 return host_string_to_python_string (str
).release ();
458 /* Returns 0 on success. Returns -1 on error, with a python exception set.
462 bppy_set_condition (PyObject
*self
, PyObject
*newvalue
, void *closure
)
464 gdb::unique_xmalloc_ptr
<char> exp_holder
;
465 const char *exp
= NULL
;
466 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
467 struct gdb_exception except
;
469 BPPY_SET_REQUIRE_VALID (self_bp
);
471 if (newvalue
== NULL
)
473 PyErr_SetString (PyExc_TypeError
,
474 _("Cannot delete `condition' attribute."));
477 else if (newvalue
== Py_None
)
481 exp_holder
= python_string_to_host_string (newvalue
);
482 if (exp_holder
== NULL
)
484 exp
= exp_holder
.get ();
489 set_breakpoint_condition (self_bp
->bp
, exp
, 0, false);
491 catch (gdb_exception
&ex
)
493 except
= std::move (ex
);
496 GDB_PY_SET_HANDLE_EXCEPTION (except
);
501 /* Python function to get the commands attached to a breakpoint. */
503 bppy_get_commands (PyObject
*self
, void *closure
)
505 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
506 struct breakpoint
*bp
= self_bp
->bp
;
508 BPPY_REQUIRE_VALID (self_bp
);
510 if (! self_bp
->bp
->commands
)
515 current_uiout
->redirect (&stb
);
518 print_command_lines (current_uiout
, breakpoint_commands (bp
), 0);
520 catch (const gdb_exception
&except
)
522 current_uiout
->redirect (NULL
);
523 gdbpy_convert_exception (except
);
527 current_uiout
->redirect (NULL
);
528 return host_string_to_python_string (stb
.c_str ()).release ();
531 /* Set the commands attached to a breakpoint. Returns 0 on success.
532 Returns -1 on error, with a python exception set. */
534 bppy_set_commands (PyObject
*self
, PyObject
*newvalue
, void *closure
)
536 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
537 struct gdb_exception except
;
539 BPPY_SET_REQUIRE_VALID (self_bp
);
541 gdb::unique_xmalloc_ptr
<char> commands
542 (python_string_to_host_string (newvalue
));
543 if (commands
== nullptr)
549 char *save_ptr
= nullptr;
553 const char *result
= strtok_r (first
? commands
.get () : nullptr,
559 counted_command_line lines
= read_command_lines_1 (reader
, 1, nullptr);
560 breakpoint_set_commands (self_bp
->bp
, std::move (lines
));
562 catch (gdb_exception
&ex
)
564 except
= std::move (ex
);
567 GDB_PY_SET_HANDLE_EXCEPTION (except
);
572 /* Python function to get the breakpoint type. */
574 bppy_get_type (PyObject
*self
, void *closure
)
576 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
578 BPPY_REQUIRE_VALID (self_bp
);
580 return gdb_py_object_from_longest (self_bp
->bp
->type
).release ();
583 /* Python function to get the visibility of the breakpoint. */
586 bppy_get_visibility (PyObject
*self
, void *closure
)
588 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
590 BPPY_REQUIRE_VALID (self_bp
);
592 if (user_breakpoint_p (self_bp
->bp
))
598 /* Python function to determine if the breakpoint is a temporary
602 bppy_get_temporary (PyObject
*self
, void *closure
)
604 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
606 BPPY_REQUIRE_VALID (self_bp
);
608 if (self_bp
->bp
->disposition
== disp_del
609 || self_bp
->bp
->disposition
== disp_del_at_next_stop
)
615 /* Python function to determine if the breakpoint is a pending
619 bppy_get_pending (PyObject
*self
, void *closure
)
621 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
623 BPPY_REQUIRE_VALID (self_bp
);
625 if (is_watchpoint (self_bp
->bp
))
627 if (pending_breakpoint_p (self_bp
->bp
))
633 /* Python function to get the breakpoint's number. */
635 bppy_get_number (PyObject
*self
, void *closure
)
637 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
639 BPPY_REQUIRE_VALID (self_bp
);
641 return gdb_py_object_from_longest (self_bp
->number
).release ();
644 /* Python function to get the breakpoint's thread ID. */
646 bppy_get_thread (PyObject
*self
, void *closure
)
648 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
650 BPPY_REQUIRE_VALID (self_bp
);
652 if (self_bp
->bp
->thread
== -1)
655 return gdb_py_object_from_longest (self_bp
->bp
->thread
).release ();
658 /* Python function to get the breakpoint's task ID (in Ada). */
660 bppy_get_task (PyObject
*self
, void *closure
)
662 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
664 BPPY_REQUIRE_VALID (self_bp
);
666 if (self_bp
->bp
->task
== 0)
669 return gdb_py_object_from_longest (self_bp
->bp
->task
).release ();
672 /* Python function to get the breakpoint's hit count. */
674 bppy_get_hit_count (PyObject
*self
, void *closure
)
676 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
678 BPPY_REQUIRE_VALID (self_bp
);
680 return gdb_py_object_from_longest (self_bp
->bp
->hit_count
).release ();
683 /* Python function to get the breakpoint's ignore count. */
685 bppy_get_ignore_count (PyObject
*self
, void *closure
)
687 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
689 BPPY_REQUIRE_VALID (self_bp
);
691 return gdb_py_object_from_longest (self_bp
->bp
->ignore_count
).release ();
694 /* Internal function to validate the Python parameters/keywords
695 provided to bppy_init. */
698 bppy_init_validate_args (const char *spec
, char *source
,
699 char *function
, char *label
,
700 char *line
, enum bptype type
)
702 /* If spec is defined, ensure that none of the explicit location
703 keywords are also defined. */
706 if (source
!= NULL
|| function
!= NULL
|| label
!= NULL
|| line
!= NULL
)
708 PyErr_SetString (PyExc_RuntimeError
,
709 _("Breakpoints specified with spec cannot "
710 "have source, function, label or line defined."));
716 /* If spec isn't defined, ensure that the user is not trying to
717 define a watchpoint with an explicit location. */
718 if (type
== bp_watchpoint
)
720 PyErr_SetString (PyExc_RuntimeError
,
721 _("Watchpoints cannot be set by explicit "
722 "location parameters."));
727 /* Otherwise, ensure some explicit locations are defined. */
728 if (source
== NULL
&& function
== NULL
&& label
== NULL
731 PyErr_SetString (PyExc_RuntimeError
,
732 _("Neither spec nor explicit location set."));
735 /* Finally, if source is specified, ensure that line, label
736 or function are specified too. */
737 if (source
!= NULL
&& function
== NULL
&& label
== NULL
740 PyErr_SetString (PyExc_RuntimeError
,
741 _("Specifying a source must also include a "
742 "line, label or function."));
750 /* Python function to create a new breakpoint. */
752 bppy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
754 static const char *keywords
[] = { "spec", "type", "wp_class", "internal",
755 "temporary","source", "function",
756 "label", "line", "qualified", NULL
};
757 const char *spec
= NULL
;
758 enum bptype type
= bp_breakpoint
;
759 int access_type
= hw_write
;
760 PyObject
*internal
= NULL
;
761 PyObject
*temporary
= NULL
;
762 PyObject
*lineobj
= NULL
;;
764 int temporary_bp
= 0;
765 gdb::unique_xmalloc_ptr
<char> line
;
768 char *function
= NULL
;
769 PyObject
* qualified
= NULL
;
771 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kwargs
, "|siiOOsssOO", keywords
,
772 &spec
, &type
, &access_type
,
775 &function
, &label
, &lineobj
,
782 if (PyInt_Check (lineobj
))
783 line
.reset (xstrprintf ("%ld", PyInt_AsLong (lineobj
)));
784 else if (PyString_Check (lineobj
))
785 line
= python_string_to_host_string (lineobj
);
788 PyErr_SetString (PyExc_RuntimeError
,
789 _("Line keyword should be an integer or a string. "));
796 internal_bp
= PyObject_IsTrue (internal
);
797 if (internal_bp
== -1)
801 if (temporary
!= NULL
)
803 temporary_bp
= PyObject_IsTrue (temporary
);
804 if (temporary_bp
== -1)
808 if (bppy_init_validate_args (spec
, source
, function
, label
, line
.get (),
812 bppy_pending_object
= (gdbpy_breakpoint_object
*) self
;
813 bppy_pending_object
->number
= -1;
814 bppy_pending_object
->bp
= NULL
;
821 case bp_hardware_breakpoint
:
823 event_location_up location
;
824 symbol_name_match_type func_name_match_type
825 = (qualified
!= NULL
&& PyObject_IsTrue (qualified
)
826 ? symbol_name_match_type::FULL
827 : symbol_name_match_type::WILD
);
831 gdb::unique_xmalloc_ptr
<char>
832 copy_holder (xstrdup (skip_spaces (spec
)));
833 const char *copy
= copy_holder
.get ();
835 location
= string_to_event_location (©
,
837 func_name_match_type
);
841 struct explicit_location explicit_loc
;
843 initialize_explicit_location (&explicit_loc
);
844 explicit_loc
.source_filename
= source
;
845 explicit_loc
.function_name
= function
;
846 explicit_loc
.label_name
= label
;
849 explicit_loc
.line_offset
=
850 linespec_parse_line_offset (line
.get ());
852 explicit_loc
.func_name_match_type
= func_name_match_type
;
854 location
= new_explicit_location (&explicit_loc
);
857 const struct breakpoint_ops
*ops
=
858 breakpoint_ops_for_event_location (location
.get (), false);
860 create_breakpoint (python_gdbarch
,
861 location
.get (), NULL
, -1, NULL
, false,
867 0, 1, internal_bp
, 0);
872 gdb::unique_xmalloc_ptr
<char>
873 copy_holder (xstrdup (skip_spaces (spec
)));
874 char *copy
= copy_holder
.get ();
876 if (access_type
== hw_write
)
877 watch_command_wrapper (copy
, 0, internal_bp
);
878 else if (access_type
== hw_access
)
879 awatch_command_wrapper (copy
, 0, internal_bp
);
880 else if (access_type
== hw_read
)
881 rwatch_command_wrapper (copy
, 0, internal_bp
);
883 error(_("Cannot understand watchpoint access type."));
887 error(_("Do not understand breakpoint type to set."));
890 catch (const gdb_exception
&except
)
892 bppy_pending_object
= NULL
;
893 gdbpy_convert_exception (except
);
897 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object
*) self
);
901 /* Append to LIST the breakpoint Python object associated to B.
903 Return true on success. Return false on failure, with the Python error
907 build_bp_list (struct breakpoint
*b
, PyObject
*list
)
909 PyObject
*bp
= (PyObject
*) b
->py_bp_object
;
911 /* Not all breakpoints will have a companion Python object.
912 Only breakpoints that were created via bppy_new, or
913 breakpoints that were created externally and are tracked by
914 the Python Scripting API. */
918 return PyList_Append (list
, bp
) == 0;
921 /* Static function to return a tuple holding all breakpoints. */
924 gdbpy_breakpoints (PyObject
*self
, PyObject
*args
)
927 return PyTuple_New (0);
929 gdbpy_ref
<> list (PyList_New (0));
933 /* If build_bp_list returns false, it signals an error condition. In that
934 case abandon building the list and return nullptr. */
935 for (breakpoint
*bp
: all_breakpoints ())
936 if (!build_bp_list (bp
, list
.get ()))
939 return PyList_AsTuple (list
.get ());
942 /* Call the "stop" method (if implemented) in the breakpoint
943 class. If the method returns True, the inferior will be
944 stopped at the breakpoint. Otherwise the inferior will be
945 allowed to continue. */
947 enum ext_lang_bp_stop
948 gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn
*extlang
,
949 struct breakpoint
*b
)
952 struct gdbpy_breakpoint_object
*bp_obj
= b
->py_bp_object
;
953 PyObject
*py_bp
= (PyObject
*) bp_obj
;
954 struct gdbarch
*garch
;
957 return EXT_LANG_BP_STOP_UNSET
;
960 garch
= b
->gdbarch
? b
->gdbarch
: get_current_arch ();
962 gdbpy_enter
enter_py (garch
, current_language
);
964 if (bp_obj
->is_finish_bp
)
965 bpfinishpy_pre_stop_hook (bp_obj
);
967 if (PyObject_HasAttrString (py_bp
, stop_func
))
969 gdbpy_ref
<> result (PyObject_CallMethod (py_bp
, stop_func
, NULL
));
974 int evaluate
= PyObject_IsTrue (result
.get ());
977 gdbpy_print_stack ();
979 /* If the "stop" function returns False that means
980 the Python breakpoint wants GDB to continue. */
985 gdbpy_print_stack ();
988 if (bp_obj
->is_finish_bp
)
989 bpfinishpy_post_stop_hook (bp_obj
);
992 return EXT_LANG_BP_STOP_UNSET
;
993 return stop
? EXT_LANG_BP_STOP_YES
: EXT_LANG_BP_STOP_NO
;
996 /* Checks if the "stop" method exists in this breakpoint.
997 Used by condition_command to ensure mutual exclusion of breakpoint
1001 gdbpy_breakpoint_has_cond (const struct extension_language_defn
*extlang
,
1002 struct breakpoint
*b
)
1005 struct gdbarch
*garch
;
1007 if (b
->py_bp_object
== NULL
)
1010 py_bp
= (PyObject
*) b
->py_bp_object
;
1011 garch
= b
->gdbarch
? b
->gdbarch
: get_current_arch ();
1013 gdbpy_enter
enter_py (garch
, current_language
);
1014 return PyObject_HasAttrString (py_bp
, stop_func
);
1019 /* Event callback functions. */
1021 /* Callback that is used when a breakpoint is created. This function
1022 will create a new Python breakpoint object. */
1024 gdbpy_breakpoint_created (struct breakpoint
*bp
)
1026 PYBP_SCOPED_DEBUG_ENTER_EXIT
;
1028 gdbpy_breakpoint_object
*newbp
;
1030 if (!user_breakpoint_p (bp
) && bppy_pending_object
== NULL
)
1032 pybp_debug_printf ("not attaching python object to this breakpoint");
1036 if (bp
->type
!= bp_breakpoint
1037 && bp
->type
!= bp_hardware_breakpoint
1038 && bp
->type
!= bp_watchpoint
1039 && bp
->type
!= bp_hardware_watchpoint
1040 && bp
->type
!= bp_read_watchpoint
1041 && bp
->type
!= bp_access_watchpoint
)
1043 pybp_debug_printf ("is not a breakpoint or watchpoint");
1047 struct gdbarch
*garch
= bp
->gdbarch
? bp
->gdbarch
: get_current_arch ();
1048 gdbpy_enter
enter_py (garch
, current_language
);
1050 if (bppy_pending_object
)
1052 newbp
= bppy_pending_object
;
1054 bppy_pending_object
= NULL
;
1055 pybp_debug_printf ("attaching existing breakpoint object");
1059 newbp
= PyObject_New (gdbpy_breakpoint_object
, &breakpoint_object_type
);
1060 pybp_debug_printf ("attaching new breakpoint object");
1064 newbp
->number
= bp
->number
;
1066 newbp
->bp
->py_bp_object
= newbp
;
1067 newbp
->is_finish_bp
= 0;
1072 PyErr_SetString (PyExc_RuntimeError
,
1073 _("Error while creating breakpoint from GDB."));
1074 gdbpy_print_stack ();
1077 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_created
))
1079 if (evpy_emit_event ((PyObject
*) newbp
,
1080 gdb_py_events
.breakpoint_created
) < 0)
1081 gdbpy_print_stack ();
1085 /* Callback that is used when a breakpoint is deleted. This will
1086 invalidate the corresponding Python object. */
1088 gdbpy_breakpoint_deleted (struct breakpoint
*b
)
1090 PYBP_SCOPED_DEBUG_ENTER_EXIT
;
1092 int num
= b
->number
;
1093 struct breakpoint
*bp
= NULL
;
1095 bp
= get_breakpoint (num
);
1098 struct gdbarch
*garch
= bp
->gdbarch
? bp
->gdbarch
: get_current_arch ();
1099 gdbpy_enter
enter_py (garch
, current_language
);
1101 gdbpy_ref
<gdbpy_breakpoint_object
> bp_obj (bp
->py_bp_object
);
1104 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_deleted
))
1106 if (evpy_emit_event ((PyObject
*) bp_obj
.get (),
1107 gdb_py_events
.breakpoint_deleted
) < 0)
1108 gdbpy_print_stack ();
1117 /* Callback that is used when a breakpoint is modified. */
1120 gdbpy_breakpoint_modified (struct breakpoint
*b
)
1122 PYBP_SCOPED_DEBUG_ENTER_EXIT
;
1124 int num
= b
->number
;
1125 struct breakpoint
*bp
= NULL
;
1127 bp
= get_breakpoint (num
);
1130 struct gdbarch
*garch
= bp
->gdbarch
? bp
->gdbarch
: get_current_arch ();
1131 gdbpy_enter
enter_py (garch
, current_language
);
1133 PyObject
*bp_obj
= (PyObject
*) bp
->py_bp_object
;
1136 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_modified
))
1138 if (evpy_emit_event (bp_obj
,
1139 gdb_py_events
.breakpoint_modified
) < 0)
1140 gdbpy_print_stack ();
1148 /* Initialize the Python breakpoint code. */
1150 gdbpy_initialize_breakpoints (void)
1154 breakpoint_object_type
.tp_new
= PyType_GenericNew
;
1155 if (PyType_Ready (&breakpoint_object_type
) < 0)
1158 if (gdb_pymodule_addobject (gdb_module
, "Breakpoint",
1159 (PyObject
*) &breakpoint_object_type
) < 0)
1162 gdb::observers::breakpoint_created
.attach (gdbpy_breakpoint_created
,
1164 gdb::observers::breakpoint_deleted
.attach (gdbpy_breakpoint_deleted
,
1166 gdb::observers::breakpoint_modified
.attach (gdbpy_breakpoint_modified
,
1169 /* Add breakpoint types constants. */
1170 for (i
= 0; pybp_codes
[i
].name
; ++i
)
1172 if (PyModule_AddIntConstant (gdb_module
, pybp_codes
[i
].name
,
1173 pybp_codes
[i
].code
) < 0)
1177 /* Add watchpoint types constants. */
1178 for (i
= 0; pybp_watch_types
[i
].name
; ++i
)
1180 if (PyModule_AddIntConstant (gdb_module
, pybp_watch_types
[i
].name
,
1181 pybp_watch_types
[i
].code
) < 0)
1190 /* Helper function that overrides this Python object's
1191 PyObject_GenericSetAttr to allow extra validation of the attribute
1195 local_setattro (PyObject
*self
, PyObject
*name
, PyObject
*v
)
1197 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
1198 gdb::unique_xmalloc_ptr
<char> attr (python_string_to_host_string (name
));
1203 /* If the attribute trying to be set is the "stop" method,
1204 but we already have a condition set in the CLI or other extension
1205 language, disallow this operation. */
1206 if (strcmp (attr
.get (), stop_func
) == 0)
1208 const struct extension_language_defn
*extlang
= NULL
;
1210 if (obj
->bp
->cond_string
!= NULL
)
1211 extlang
= get_ext_lang_defn (EXT_LANG_GDB
);
1212 if (extlang
== NULL
)
1213 extlang
= get_breakpoint_cond_ext_lang (obj
->bp
, EXT_LANG_PYTHON
);
1214 if (extlang
!= NULL
)
1216 std::string error_text
1217 = string_printf (_("Only one stop condition allowed. There is"
1218 " currently a %s stop condition defined for"
1219 " this breakpoint."),
1220 ext_lang_capitalized_name (extlang
));
1221 PyErr_SetString (PyExc_RuntimeError
, error_text
.c_str ());
1226 return PyObject_GenericSetAttr (self
, name
, v
);
1229 static gdb_PyGetSetDef breakpoint_object_getset
[] = {
1230 { "enabled", bppy_get_enabled
, bppy_set_enabled
,
1231 "Boolean telling whether the breakpoint is enabled.", NULL
},
1232 { "silent", bppy_get_silent
, bppy_set_silent
,
1233 "Boolean telling whether the breakpoint is silent.", NULL
},
1234 { "thread", bppy_get_thread
, bppy_set_thread
,
1235 "Thread ID for the breakpoint.\n\
1236 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1237 If the value is None, then this breakpoint is not thread-specific.\n\
1238 No other type of value can be used.", NULL
},
1239 { "task", bppy_get_task
, bppy_set_task
,
1240 "Thread ID for the breakpoint.\n\
1241 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1242 If the value is None, then this breakpoint is not task-specific.\n\
1243 No other type of value can be used.", NULL
},
1244 { "ignore_count", bppy_get_ignore_count
, bppy_set_ignore_count
,
1245 "Number of times this breakpoint should be automatically continued.",
1247 { "number", bppy_get_number
, NULL
,
1248 "Breakpoint's number assigned by GDB.", NULL
},
1249 { "hit_count", bppy_get_hit_count
, bppy_set_hit_count
,
1250 "Number of times the breakpoint has been hit.\n\
1251 Can be set to zero to clear the count. No other value is valid\n\
1252 when setting this property.", NULL
},
1253 { "location", bppy_get_location
, NULL
,
1254 "Location of the breakpoint, as specified by the user.", NULL
},
1255 { "expression", bppy_get_expression
, NULL
,
1256 "Expression of the breakpoint, as specified by the user.", NULL
},
1257 { "condition", bppy_get_condition
, bppy_set_condition
,
1258 "Condition of the breakpoint, as specified by the user,\
1259 or None if no condition set."},
1260 { "commands", bppy_get_commands
, bppy_set_commands
,
1261 "Commands of the breakpoint, as specified by the user."},
1262 { "type", bppy_get_type
, NULL
,
1263 "Type of breakpoint."},
1264 { "visible", bppy_get_visibility
, NULL
,
1265 "Whether the breakpoint is visible to the user."},
1266 { "temporary", bppy_get_temporary
, NULL
,
1267 "Whether this breakpoint is a temporary breakpoint."},
1268 { "pending", bppy_get_pending
, NULL
,
1269 "Whether this breakpoint is a pending breakpoint."},
1270 { NULL
} /* Sentinel. */
1273 static PyMethodDef breakpoint_object_methods
[] =
1275 { "is_valid", bppy_is_valid
, METH_NOARGS
,
1276 "Return true if this breakpoint is valid, false if not." },
1277 { "delete", bppy_delete_breakpoint
, METH_NOARGS
,
1278 "Delete the underlying GDB breakpoint." },
1279 { NULL
} /* Sentinel. */
1282 PyTypeObject breakpoint_object_type
=
1284 PyVarObject_HEAD_INIT (NULL
, 0)
1285 "gdb.Breakpoint", /*tp_name*/
1286 sizeof (gdbpy_breakpoint_object
), /*tp_basicsize*/
1295 0, /*tp_as_sequence*/
1296 0, /*tp_as_mapping*/
1301 (setattrofunc
)local_setattro
, /*tp_setattro */
1303 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1304 "GDB breakpoint object", /* tp_doc */
1305 0, /* tp_traverse */
1307 0, /* tp_richcompare */
1308 0, /* tp_weaklistoffset */
1310 0, /* tp_iternext */
1311 breakpoint_object_methods
, /* tp_methods */
1313 breakpoint_object_getset
, /* tp_getset */
1316 0, /* tp_descr_get */
1317 0, /* tp_descr_set */
1318 0, /* tp_dictoffset */
1319 bppy_init
, /* tp_init */
1323 void _initialize_py_breakpoint ();
1325 _initialize_py_breakpoint ()
1327 add_setshow_boolean_cmd
1328 ("py-breakpoint", class_maintenance
, &pybp_debug
,
1329 _("Set Python breakpoint debugging."),
1330 _("Show Python breakpoint debugging."),
1331 _("When on, Python breakpoint debugging is enabled."),
1334 &setdebuglist
, &showdebuglist
);