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);
904 build_bp_list (struct breakpoint *b, PyObject *list)
906 PyObject *bp = (PyObject *) b->py_bp_object;
909 /* Not all breakpoints will have a companion Python object.
910 Only breakpoints that were created via bppy_new, or
911 breakpoints that were created externally and are tracked by
912 the Python Scripting API. */
914 iserr = PyList_Append (list, bp);
922 /* Static function to return a tuple holding all breakpoints. */
925 gdbpy_breakpoints (PyObject *self, PyObject *args)
928 return PyTuple_New (0);
930 gdbpy_ref<> list (PyList_New (0));
934 /* If iterate_over_breakpoints returns non NULL it signals an error
935 condition. In that case abandon building the list and return
937 auto callback = [&] (breakpoint *bp)
939 return build_bp_list(bp, list.get ());
941 if (iterate_over_breakpoints (callback) != NULL)
944 return PyList_AsTuple (list.get ());
947 /* Call the "stop" method (if implemented) in the breakpoint
948 class. If the method returns True, the inferior will be
949 stopped at the breakpoint. Otherwise the inferior will be
950 allowed to continue. */
952 enum ext_lang_bp_stop
953 gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
954 struct breakpoint *b)
957 struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
958 PyObject *py_bp = (PyObject *) bp_obj;
959 struct gdbarch *garch;
962 return EXT_LANG_BP_STOP_UNSET;
965 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
967 gdbpy_enter enter_py (garch, current_language);
969 if (bp_obj->is_finish_bp)
970 bpfinishpy_pre_stop_hook (bp_obj);
972 if (PyObject_HasAttrString (py_bp, stop_func))
974 gdbpy_ref<> result (PyObject_CallMethod (py_bp, stop_func, NULL));
979 int evaluate = PyObject_IsTrue (result.get ());
982 gdbpy_print_stack ();
984 /* If the "stop" function returns False that means
985 the Python breakpoint wants GDB to continue. */
990 gdbpy_print_stack ();
993 if (bp_obj->is_finish_bp)
994 bpfinishpy_post_stop_hook (bp_obj);
997 return EXT_LANG_BP_STOP_UNSET;
998 return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
1001 /* Checks if the "stop" method exists in this breakpoint.
1002 Used by condition_command to ensure mutual exclusion of breakpoint
1006 gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
1007 struct breakpoint *b)
1010 struct gdbarch *garch;
1012 if (b->py_bp_object == NULL)
1015 py_bp = (PyObject *) b->py_bp_object;
1016 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
1018 gdbpy_enter enter_py (garch, current_language);
1019 return PyObject_HasAttrString (py_bp, stop_func);
1024 /* Event callback functions. */
1026 /* Callback that is used when a breakpoint is created. This function
1027 will create a new Python breakpoint object. */
1029 gdbpy_breakpoint_created (struct breakpoint *bp)
1031 PYBP_SCOPED_DEBUG_ENTER_EXIT;
1033 gdbpy_breakpoint_object *newbp;
1035 if (!user_breakpoint_p (bp) && bppy_pending_object == NULL)
1037 pybp_debug_printf ("not attaching python object to this breakpoint");
1041 if (bp->type != bp_breakpoint
1042 && bp->type != bp_hardware_breakpoint
1043 && bp->type != bp_watchpoint
1044 && bp->type != bp_hardware_watchpoint
1045 && bp->type != bp_read_watchpoint
1046 && bp->type != bp_access_watchpoint)
1048 pybp_debug_printf ("is not a breakpoint or watchpoint");
1052 struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1053 gdbpy_enter enter_py (garch, current_language);
1055 if (bppy_pending_object)
1057 newbp = bppy_pending_object;
1059 bppy_pending_object = NULL;
1060 pybp_debug_printf ("attaching existing breakpoint object");
1064 newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type);
1065 pybp_debug_printf ("attaching new breakpoint object");
1069 newbp->number = bp->number;
1071 newbp->bp->py_bp_object = newbp;
1072 newbp->is_finish_bp = 0;
1077 PyErr_SetString (PyExc_RuntimeError,
1078 _("Error while creating breakpoint from GDB."));
1079 gdbpy_print_stack ();
1082 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
1084 if (evpy_emit_event ((PyObject *) newbp,
1085 gdb_py_events.breakpoint_created) < 0)
1086 gdbpy_print_stack ();
1090 /* Callback that is used when a breakpoint is deleted. This will
1091 invalidate the corresponding Python object. */
1093 gdbpy_breakpoint_deleted (struct breakpoint *b)
1095 PYBP_SCOPED_DEBUG_ENTER_EXIT;
1097 int num = b->number;
1098 struct breakpoint *bp = NULL;
1100 bp = get_breakpoint (num);
1103 struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1104 gdbpy_enter enter_py (garch, current_language);
1106 gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object);
1109 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
1111 if (evpy_emit_event ((PyObject *) bp_obj.get (),
1112 gdb_py_events.breakpoint_deleted) < 0)
1113 gdbpy_print_stack ();
1122 /* Callback that is used when a breakpoint is modified. */
1125 gdbpy_breakpoint_modified (struct breakpoint *b)
1127 PYBP_SCOPED_DEBUG_ENTER_EXIT;
1129 int num = b->number;
1130 struct breakpoint *bp = NULL;
1132 bp = get_breakpoint (num);
1135 struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1136 gdbpy_enter enter_py (garch, current_language);
1138 PyObject *bp_obj = (PyObject *) bp->py_bp_object;
1141 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
1143 if (evpy_emit_event (bp_obj,
1144 gdb_py_events.breakpoint_modified) < 0)
1145 gdbpy_print_stack ();
1153 /* Initialize the Python breakpoint code. */
1155 gdbpy_initialize_breakpoints (void)
1159 breakpoint_object_type.tp_new = PyType_GenericNew;
1160 if (PyType_Ready (&breakpoint_object_type) < 0)
1163 if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
1164 (PyObject *) &breakpoint_object_type) < 0)
1167 gdb::observers::breakpoint_created.attach (gdbpy_breakpoint_created,
1169 gdb::observers::breakpoint_deleted.attach (gdbpy_breakpoint_deleted,
1171 gdb::observers::breakpoint_modified.attach (gdbpy_breakpoint_modified,
1174 /* Add breakpoint types constants. */
1175 for (i = 0; pybp_codes[i].name; ++i)
1177 if (PyModule_AddIntConstant (gdb_module, pybp_codes[i].name,
1178 pybp_codes[i].code) < 0)
1182 /* Add watchpoint types constants. */
1183 for (i = 0; pybp_watch_types[i].name; ++i)
1185 if (PyModule_AddIntConstant (gdb_module, pybp_watch_types[i].name,
1186 pybp_watch_types[i].code) < 0)
1195 /* Helper function that overrides this Python object's
1196 PyObject_GenericSetAttr to allow extra validation of the attribute
1200 local_setattro (PyObject *self, PyObject *name, PyObject *v)
1202 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
1203 gdb::unique_xmalloc_ptr<char> attr (python_string_to_host_string (name));
1208 /* If the attribute trying to be set is the "stop" method,
1209 but we already have a condition set in the CLI or other extension
1210 language, disallow this operation. */
1211 if (strcmp (attr.get (), stop_func) == 0)
1213 const struct extension_language_defn *extlang = NULL;
1215 if (obj->bp->cond_string != NULL)
1216 extlang = get_ext_lang_defn (EXT_LANG_GDB);
1217 if (extlang == NULL)
1218 extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
1219 if (extlang != NULL)
1221 std::string error_text
1222 = string_printf (_("Only one stop condition allowed. There is"
1223 " currently a %s stop condition defined for"
1224 " this breakpoint."),
1225 ext_lang_capitalized_name (extlang));
1226 PyErr_SetString (PyExc_RuntimeError, error_text.c_str ());
1231 return PyObject_GenericSetAttr (self, name, v);
1234 static gdb_PyGetSetDef breakpoint_object_getset[] = {
1235 { "enabled", bppy_get_enabled, bppy_set_enabled,
1236 "Boolean telling whether the breakpoint is enabled.", NULL },
1237 { "silent", bppy_get_silent, bppy_set_silent,
1238 "Boolean telling whether the breakpoint is silent.", NULL },
1239 { "thread", bppy_get_thread, bppy_set_thread,
1240 "Thread ID for the breakpoint.\n\
1241 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1242 If the value is None, then this breakpoint is not thread-specific.\n\
1243 No other type of value can be used.", NULL },
1244 { "task", bppy_get_task, bppy_set_task,
1245 "Thread ID for the breakpoint.\n\
1246 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1247 If the value is None, then this breakpoint is not task-specific.\n\
1248 No other type of value can be used.", NULL },
1249 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
1250 "Number of times this breakpoint should be automatically continued.",
1252 { "number", bppy_get_number, NULL,
1253 "Breakpoint's number assigned by GDB.", NULL },
1254 { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
1255 "Number of times the breakpoint has been hit.\n\
1256 Can be set to zero to clear the count. No other value is valid\n\
1257 when setting this property.", NULL },
1258 { "location", bppy_get_location, NULL,
1259 "Location of the breakpoint, as specified by the user.", NULL},
1260 { "expression", bppy_get_expression, NULL,
1261 "Expression of the breakpoint, as specified by the user.", NULL},
1262 { "condition", bppy_get_condition, bppy_set_condition,
1263 "Condition of the breakpoint, as specified by the user,\
1264 or None if no condition set."},
1265 { "commands", bppy_get_commands, bppy_set_commands,
1266 "Commands of the breakpoint, as specified by the user."},
1267 { "type", bppy_get_type, NULL,
1268 "Type of breakpoint."},
1269 { "visible", bppy_get_visibility, NULL,
1270 "Whether the breakpoint is visible to the user."},
1271 { "temporary", bppy_get_temporary, NULL,
1272 "Whether this breakpoint is a temporary breakpoint."},
1273 { "pending", bppy_get_pending, NULL,
1274 "Whether this breakpoint is a pending breakpoint."},
1275 { NULL } /* Sentinel. */
1278 static PyMethodDef breakpoint_object_methods[] =
1280 { "is_valid", bppy_is_valid, METH_NOARGS,
1281 "Return true if this breakpoint is valid, false if not." },
1282 { "delete", bppy_delete_breakpoint, METH_NOARGS,
1283 "Delete the underlying GDB breakpoint." },
1284 { NULL } /* Sentinel. */
1287 PyTypeObject breakpoint_object_type =
1289 PyVarObject_HEAD_INIT (NULL, 0)
1290 "gdb.Breakpoint", /*tp_name*/
1291 sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/
1300 0, /*tp_as_sequence*/
1301 0, /*tp_as_mapping*/
1306 (setattrofunc)local_setattro, /*tp_setattro */
1308 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1309 "GDB breakpoint object", /* tp_doc */
1310 0, /* tp_traverse */
1312 0, /* tp_richcompare */
1313 0, /* tp_weaklistoffset */
1315 0, /* tp_iternext */
1316 breakpoint_object_methods, /* tp_methods */
1318 breakpoint_object_getset, /* tp_getset */
1321 0, /* tp_descr_get */
1322 0, /* tp_descr_set */
1323 0, /* tp_dictoffset */
1324 bppy_init, /* tp_init */
1328 void _initialize_py_breakpoint ();
1330 _initialize_py_breakpoint ()
1332 add_setshow_boolean_cmd
1333 ("py-breakpoint", class_maintenance, &pybp_debug,
1334 _("Set Python breakpoint debugging."),
1335 _("Show Python breakpoint debugging."),
1336 _("When on, Python breakpoint debugging is enabled."),
1339 &setdebuglist, &showdebuglist);