1 /* Python interface to breakpoints
3 Copyright (C) 2008-2022 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 { "BP_CATCHPOINT", bp_catchpoint
},
90 {NULL
} /* Sentinel. */
93 /* Entries related to the type of watchpoint. */
94 static struct pybp_code pybp_watch_types
[] =
96 { "WP_READ", hw_read
},
97 { "WP_WRITE", hw_write
},
98 { "WP_ACCESS", hw_access
},
99 {NULL
} /* Sentinel. */
102 /* Python function which checks the validity of a breakpoint object. */
104 bppy_is_valid (PyObject
*self
, PyObject
*args
)
106 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
113 /* Python function to test whether or not the breakpoint is enabled. */
115 bppy_get_enabled (PyObject
*self
, void *closure
)
117 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
119 BPPY_REQUIRE_VALID (self_bp
);
122 if (self_bp
->bp
->enable_state
== bp_enabled
)
127 /* Python function to test whether or not the breakpoint is silent. */
129 bppy_get_silent (PyObject
*self
, void *closure
)
131 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
133 BPPY_REQUIRE_VALID (self_bp
);
134 if (self_bp
->bp
->silent
)
139 /* Python function to set the enabled state of a breakpoint. */
141 bppy_set_enabled (PyObject
*self
, PyObject
*newvalue
, void *closure
)
143 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
146 BPPY_SET_REQUIRE_VALID (self_bp
);
148 if (newvalue
== NULL
)
150 PyErr_SetString (PyExc_TypeError
,
151 _("Cannot delete `enabled' attribute."));
155 else if (! PyBool_Check (newvalue
))
157 PyErr_SetString (PyExc_TypeError
,
158 _("The value of `enabled' must be a boolean."));
162 cmp
= PyObject_IsTrue (newvalue
);
169 enable_breakpoint (self_bp
->bp
);
171 disable_breakpoint (self_bp
->bp
);
173 catch (const gdb_exception
&except
)
175 GDB_PY_SET_HANDLE_EXCEPTION (except
);
181 /* Python function to set the 'silent' state of a breakpoint. */
183 bppy_set_silent (PyObject
*self
, PyObject
*newvalue
, void *closure
)
185 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
188 BPPY_SET_REQUIRE_VALID (self_bp
);
190 if (newvalue
== NULL
)
192 PyErr_SetString (PyExc_TypeError
,
193 _("Cannot delete `silent' attribute."));
196 else if (! PyBool_Check (newvalue
))
198 PyErr_SetString (PyExc_TypeError
,
199 _("The value of `silent' must be a boolean."));
203 cmp
= PyObject_IsTrue (newvalue
);
207 breakpoint_set_silent (self_bp
->bp
, cmp
);
212 /* Python function to set the thread of a breakpoint. */
214 bppy_set_thread (PyObject
*self
, PyObject
*newvalue
, void *closure
)
216 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
219 BPPY_SET_REQUIRE_VALID (self_bp
);
221 if (newvalue
== NULL
)
223 PyErr_SetString (PyExc_TypeError
,
224 _("Cannot delete `thread' attribute."));
227 else if (PyInt_Check (newvalue
))
229 if (! gdb_py_int_as_long (newvalue
, &id
))
232 if (!valid_global_thread_id (id
))
234 PyErr_SetString (PyExc_RuntimeError
,
235 _("Invalid thread ID."));
239 else if (newvalue
== Py_None
)
243 PyErr_SetString (PyExc_TypeError
,
244 _("The value of `thread' must be an integer or None."));
248 breakpoint_set_thread (self_bp
->bp
, id
);
253 /* Python function to set the (Ada) task of a breakpoint. */
255 bppy_set_task (PyObject
*self
, PyObject
*newvalue
, void *closure
)
257 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
261 BPPY_SET_REQUIRE_VALID (self_bp
);
263 if (newvalue
== NULL
)
265 PyErr_SetString (PyExc_TypeError
,
266 _("Cannot delete `task' attribute."));
269 else if (PyInt_Check (newvalue
))
271 if (! gdb_py_int_as_long (newvalue
, &id
))
276 valid_id
= valid_task_id (id
);
278 catch (const gdb_exception
&except
)
280 GDB_PY_SET_HANDLE_EXCEPTION (except
);
285 PyErr_SetString (PyExc_RuntimeError
,
286 _("Invalid task ID."));
290 else if (newvalue
== Py_None
)
294 PyErr_SetString (PyExc_TypeError
,
295 _("The value of `task' must be an integer or None."));
299 breakpoint_set_task (self_bp
->bp
, id
);
304 /* Python function which deletes the underlying GDB breakpoint. This
305 triggers the breakpoint_deleted observer which will call
306 gdbpy_breakpoint_deleted; that function cleans up the Python
310 bppy_delete_breakpoint (PyObject
*self
, PyObject
*args
)
312 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
314 BPPY_REQUIRE_VALID (self_bp
);
318 delete_breakpoint (self_bp
->bp
);
320 catch (const gdb_exception
&except
)
322 GDB_PY_HANDLE_EXCEPTION (except
);
329 /* Python function to set the ignore count of a breakpoint. */
331 bppy_set_ignore_count (PyObject
*self
, PyObject
*newvalue
, void *closure
)
333 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
336 BPPY_SET_REQUIRE_VALID (self_bp
);
338 if (newvalue
== NULL
)
340 PyErr_SetString (PyExc_TypeError
,
341 _("Cannot delete `ignore_count' attribute."));
344 else if (! PyInt_Check (newvalue
))
346 PyErr_SetString (PyExc_TypeError
,
347 _("The value of `ignore_count' must be an integer."));
351 if (! gdb_py_int_as_long (newvalue
, &value
))
359 set_ignore_count (self_bp
->number
, (int) value
, 0);
361 catch (const gdb_exception
&except
)
363 GDB_PY_SET_HANDLE_EXCEPTION (except
);
369 /* Python function to set the hit count of a breakpoint. */
371 bppy_set_hit_count (PyObject
*self
, PyObject
*newvalue
, void *closure
)
373 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
375 BPPY_SET_REQUIRE_VALID (self_bp
);
377 if (newvalue
== NULL
)
379 PyErr_SetString (PyExc_TypeError
,
380 _("Cannot delete `hit_count' attribute."));
387 if (! gdb_py_int_as_long (newvalue
, &value
))
392 PyErr_SetString (PyExc_AttributeError
,
393 _("The value of `hit_count' must be zero."));
398 self_bp
->bp
->hit_count
= 0;
403 /* Python function to get the location of a breakpoint. */
405 bppy_get_location (PyObject
*self
, void *closure
)
407 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
409 BPPY_REQUIRE_VALID (obj
);
411 if (obj
->bp
->type
!= bp_breakpoint
412 && obj
->bp
->type
!= bp_hardware_breakpoint
)
415 const char *str
= event_location_to_string (obj
->bp
->location
.get ());
418 return host_string_to_python_string (str
).release ();
421 /* Python function to get the breakpoint expression. */
423 bppy_get_expression (PyObject
*self
, void *closure
)
426 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
427 struct watchpoint
*wp
;
429 BPPY_REQUIRE_VALID (obj
);
431 if (!is_watchpoint (obj
->bp
))
434 wp
= (struct watchpoint
*) obj
->bp
;
436 str
= wp
->exp_string
.get ();
440 return host_string_to_python_string (str
).release ();
443 /* Python function to get the condition expression of a breakpoint. */
445 bppy_get_condition (PyObject
*self
, void *closure
)
448 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
450 BPPY_REQUIRE_VALID (obj
);
452 str
= obj
->bp
->cond_string
.get ();
456 return host_string_to_python_string (str
).release ();
459 /* Returns 0 on success. Returns -1 on error, with a python exception set.
463 bppy_set_condition (PyObject
*self
, PyObject
*newvalue
, void *closure
)
465 gdb::unique_xmalloc_ptr
<char> exp_holder
;
466 const char *exp
= NULL
;
467 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
468 struct gdb_exception except
;
470 BPPY_SET_REQUIRE_VALID (self_bp
);
472 if (newvalue
== NULL
)
474 PyErr_SetString (PyExc_TypeError
,
475 _("Cannot delete `condition' attribute."));
478 else if (newvalue
== Py_None
)
482 exp_holder
= python_string_to_host_string (newvalue
);
483 if (exp_holder
== NULL
)
485 exp
= exp_holder
.get ();
490 set_breakpoint_condition (self_bp
->bp
, exp
, 0, false);
492 catch (gdb_exception
&ex
)
494 except
= std::move (ex
);
497 GDB_PY_SET_HANDLE_EXCEPTION (except
);
502 /* Python function to get the commands attached to a breakpoint. */
504 bppy_get_commands (PyObject
*self
, void *closure
)
506 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
507 struct breakpoint
*bp
= self_bp
->bp
;
509 BPPY_REQUIRE_VALID (self_bp
);
511 if (! self_bp
->bp
->commands
)
516 current_uiout
->redirect (&stb
);
519 print_command_lines (current_uiout
, breakpoint_commands (bp
), 0);
521 catch (const gdb_exception
&except
)
523 current_uiout
->redirect (NULL
);
524 gdbpy_convert_exception (except
);
528 current_uiout
->redirect (NULL
);
529 return host_string_to_python_string (stb
.c_str ()).release ();
532 /* Set the commands attached to a breakpoint. Returns 0 on success.
533 Returns -1 on error, with a python exception set. */
535 bppy_set_commands (PyObject
*self
, PyObject
*newvalue
, void *closure
)
537 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
538 struct gdb_exception except
;
540 BPPY_SET_REQUIRE_VALID (self_bp
);
542 gdb::unique_xmalloc_ptr
<char> commands
543 (python_string_to_host_string (newvalue
));
544 if (commands
== nullptr)
550 char *save_ptr
= nullptr;
554 const char *result
= strtok_r (first
? commands
.get () : nullptr,
560 counted_command_line lines
= read_command_lines_1 (reader
, 1, nullptr);
561 breakpoint_set_commands (self_bp
->bp
, std::move (lines
));
563 catch (gdb_exception
&ex
)
565 except
= std::move (ex
);
568 GDB_PY_SET_HANDLE_EXCEPTION (except
);
573 /* Python function to get the breakpoint type. */
575 bppy_get_type (PyObject
*self
, void *closure
)
577 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
579 BPPY_REQUIRE_VALID (self_bp
);
581 return gdb_py_object_from_longest (self_bp
->bp
->type
).release ();
584 /* Python function to get the visibility of the breakpoint. */
587 bppy_get_visibility (PyObject
*self
, void *closure
)
589 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
591 BPPY_REQUIRE_VALID (self_bp
);
593 if (user_breakpoint_p (self_bp
->bp
))
599 /* Python function to determine if the breakpoint is a temporary
603 bppy_get_temporary (PyObject
*self
, void *closure
)
605 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
607 BPPY_REQUIRE_VALID (self_bp
);
609 if (self_bp
->bp
->disposition
== disp_del
610 || self_bp
->bp
->disposition
== disp_del_at_next_stop
)
616 /* Python function to determine if the breakpoint is a pending
620 bppy_get_pending (PyObject
*self
, void *closure
)
622 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
624 BPPY_REQUIRE_VALID (self_bp
);
626 if (is_watchpoint (self_bp
->bp
))
628 if (pending_breakpoint_p (self_bp
->bp
))
634 /* Python function to get the breakpoint's number. */
636 bppy_get_number (PyObject
*self
, void *closure
)
638 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
640 BPPY_REQUIRE_VALID (self_bp
);
642 return gdb_py_object_from_longest (self_bp
->number
).release ();
645 /* Python function to get the breakpoint's thread ID. */
647 bppy_get_thread (PyObject
*self
, void *closure
)
649 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
651 BPPY_REQUIRE_VALID (self_bp
);
653 if (self_bp
->bp
->thread
== -1)
656 return gdb_py_object_from_longest (self_bp
->bp
->thread
).release ();
659 /* Python function to get the breakpoint's task ID (in Ada). */
661 bppy_get_task (PyObject
*self
, void *closure
)
663 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
665 BPPY_REQUIRE_VALID (self_bp
);
667 if (self_bp
->bp
->task
== 0)
670 return gdb_py_object_from_longest (self_bp
->bp
->task
).release ();
673 /* Python function to get the breakpoint's hit count. */
675 bppy_get_hit_count (PyObject
*self
, void *closure
)
677 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
679 BPPY_REQUIRE_VALID (self_bp
);
681 return gdb_py_object_from_longest (self_bp
->bp
->hit_count
).release ();
684 /* Python function to get the breakpoint's ignore count. */
686 bppy_get_ignore_count (PyObject
*self
, void *closure
)
688 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
690 BPPY_REQUIRE_VALID (self_bp
);
692 return gdb_py_object_from_longest (self_bp
->bp
->ignore_count
).release ();
695 /* Internal function to validate the Python parameters/keywords
696 provided to bppy_init. */
699 bppy_init_validate_args (const char *spec
, char *source
,
700 char *function
, char *label
,
701 char *line
, enum bptype type
)
703 /* If spec is defined, ensure that none of the explicit location
704 keywords are also defined. */
707 if (source
!= NULL
|| function
!= NULL
|| label
!= NULL
|| line
!= NULL
)
709 PyErr_SetString (PyExc_RuntimeError
,
710 _("Breakpoints specified with spec cannot "
711 "have source, function, label or line defined."));
717 /* If spec isn't defined, ensure that the user is not trying to
718 define a watchpoint with an explicit location. */
719 if (type
== bp_watchpoint
)
721 PyErr_SetString (PyExc_RuntimeError
,
722 _("Watchpoints cannot be set by explicit "
723 "location parameters."));
728 /* Otherwise, ensure some explicit locations are defined. */
729 if (source
== NULL
&& function
== NULL
&& label
== NULL
732 PyErr_SetString (PyExc_RuntimeError
,
733 _("Neither spec nor explicit location set."));
736 /* Finally, if source is specified, ensure that line, label
737 or function are specified too. */
738 if (source
!= NULL
&& function
== NULL
&& label
== NULL
741 PyErr_SetString (PyExc_RuntimeError
,
742 _("Specifying a source must also include a "
743 "line, label or function."));
751 /* Python function to create a new breakpoint. */
753 bppy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
755 static const char *keywords
[] = { "spec", "type", "wp_class", "internal",
756 "temporary","source", "function",
757 "label", "line", "qualified", NULL
};
758 const char *spec
= NULL
;
759 enum bptype type
= bp_breakpoint
;
760 int access_type
= hw_write
;
761 PyObject
*internal
= NULL
;
762 PyObject
*temporary
= NULL
;
763 PyObject
*lineobj
= NULL
;;
765 int temporary_bp
= 0;
766 gdb::unique_xmalloc_ptr
<char> line
;
769 char *function
= NULL
;
770 PyObject
* qualified
= NULL
;
772 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kwargs
, "|siiOOsssOO", keywords
,
773 &spec
, &type
, &access_type
,
776 &function
, &label
, &lineobj
,
783 if (PyInt_Check (lineobj
))
784 line
= xstrprintf ("%ld", PyInt_AsLong (lineobj
));
785 else if (PyString_Check (lineobj
))
786 line
= python_string_to_host_string (lineobj
);
789 PyErr_SetString (PyExc_RuntimeError
,
790 _("Line keyword should be an integer or a string. "));
797 internal_bp
= PyObject_IsTrue (internal
);
798 if (internal_bp
== -1)
802 if (temporary
!= NULL
)
804 temporary_bp
= PyObject_IsTrue (temporary
);
805 if (temporary_bp
== -1)
809 if (bppy_init_validate_args (spec
, source
, function
, label
, line
.get (),
813 bppy_pending_object
= (gdbpy_breakpoint_object
*) self
;
814 bppy_pending_object
->number
= -1;
815 bppy_pending_object
->bp
= NULL
;
822 case bp_hardware_breakpoint
:
824 event_location_up location
;
825 symbol_name_match_type func_name_match_type
826 = (qualified
!= NULL
&& PyObject_IsTrue (qualified
)
827 ? symbol_name_match_type::FULL
828 : symbol_name_match_type::WILD
);
832 gdb::unique_xmalloc_ptr
<char>
833 copy_holder (xstrdup (skip_spaces (spec
)));
834 const char *copy
= copy_holder
.get ();
836 location
= string_to_event_location (©
,
838 func_name_match_type
);
842 struct explicit_location explicit_loc
;
844 initialize_explicit_location (&explicit_loc
);
845 explicit_loc
.source_filename
= source
;
846 explicit_loc
.function_name
= function
;
847 explicit_loc
.label_name
= label
;
850 explicit_loc
.line_offset
=
851 linespec_parse_line_offset (line
.get ());
853 explicit_loc
.func_name_match_type
= func_name_match_type
;
855 location
= new_explicit_location (&explicit_loc
);
858 const struct breakpoint_ops
*ops
=
859 breakpoint_ops_for_event_location (location
.get (), false);
861 create_breakpoint (python_gdbarch
,
862 location
.get (), NULL
, -1, NULL
, false,
868 0, 1, internal_bp
, 0);
873 gdb::unique_xmalloc_ptr
<char>
874 copy_holder (xstrdup (skip_spaces (spec
)));
875 char *copy
= copy_holder
.get ();
877 if (access_type
== hw_write
)
878 watch_command_wrapper (copy
, 0, internal_bp
);
879 else if (access_type
== hw_access
)
880 awatch_command_wrapper (copy
, 0, internal_bp
);
881 else if (access_type
== hw_read
)
882 rwatch_command_wrapper (copy
, 0, internal_bp
);
884 error(_("Cannot understand watchpoint access type."));
888 error (_("BP_CATCHPOINT not supported"));
890 error(_("Do not understand breakpoint type to set."));
893 catch (const gdb_exception
&except
)
895 bppy_pending_object
= NULL
;
896 gdbpy_convert_exception (except
);
900 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object
*) self
);
904 /* Append to LIST the breakpoint Python object associated to B.
906 Return true on success. Return false on failure, with the Python error
910 build_bp_list (struct breakpoint
*b
, PyObject
*list
)
912 PyObject
*bp
= (PyObject
*) b
->py_bp_object
;
914 /* Not all breakpoints will have a companion Python object.
915 Only breakpoints that were created via bppy_new, or
916 breakpoints that were created externally and are tracked by
917 the Python Scripting API. */
921 return PyList_Append (list
, bp
) == 0;
924 /* Static function to return a tuple holding all breakpoints. */
927 gdbpy_breakpoints (PyObject
*self
, PyObject
*args
)
930 return PyTuple_New (0);
932 gdbpy_ref
<> list (PyList_New (0));
936 /* If build_bp_list returns false, it signals an error condition. In that
937 case abandon building the list and return nullptr. */
938 for (breakpoint
*bp
: all_breakpoints ())
939 if (!build_bp_list (bp
, list
.get ()))
942 return PyList_AsTuple (list
.get ());
945 /* Call the "stop" method (if implemented) in the breakpoint
946 class. If the method returns True, the inferior will be
947 stopped at the breakpoint. Otherwise the inferior will be
948 allowed to continue. */
950 enum ext_lang_bp_stop
951 gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn
*extlang
,
952 struct breakpoint
*b
)
955 struct gdbpy_breakpoint_object
*bp_obj
= b
->py_bp_object
;
956 PyObject
*py_bp
= (PyObject
*) bp_obj
;
957 struct gdbarch
*garch
;
960 return EXT_LANG_BP_STOP_UNSET
;
963 garch
= b
->gdbarch
? b
->gdbarch
: get_current_arch ();
965 gdbpy_enter
enter_py (garch
, current_language
);
967 if (bp_obj
->is_finish_bp
)
968 bpfinishpy_pre_stop_hook (bp_obj
);
970 if (PyObject_HasAttrString (py_bp
, stop_func
))
972 gdbpy_ref
<> result (PyObject_CallMethod (py_bp
, stop_func
, NULL
));
977 int evaluate
= PyObject_IsTrue (result
.get ());
980 gdbpy_print_stack ();
982 /* If the "stop" function returns False that means
983 the Python breakpoint wants GDB to continue. */
988 gdbpy_print_stack ();
991 if (bp_obj
->is_finish_bp
)
992 bpfinishpy_post_stop_hook (bp_obj
);
995 return EXT_LANG_BP_STOP_UNSET
;
996 return stop
? EXT_LANG_BP_STOP_YES
: EXT_LANG_BP_STOP_NO
;
999 /* Checks if the "stop" method exists in this breakpoint.
1000 Used by condition_command to ensure mutual exclusion of breakpoint
1004 gdbpy_breakpoint_has_cond (const struct extension_language_defn
*extlang
,
1005 struct breakpoint
*b
)
1008 struct gdbarch
*garch
;
1010 if (b
->py_bp_object
== NULL
)
1013 py_bp
= (PyObject
*) b
->py_bp_object
;
1014 garch
= b
->gdbarch
? b
->gdbarch
: get_current_arch ();
1016 gdbpy_enter
enter_py (garch
, current_language
);
1017 return PyObject_HasAttrString (py_bp
, stop_func
);
1022 /* Event callback functions. */
1024 /* Callback that is used when a breakpoint is created. This function
1025 will create a new Python breakpoint object. */
1027 gdbpy_breakpoint_created (struct breakpoint
*bp
)
1029 PYBP_SCOPED_DEBUG_ENTER_EXIT
;
1031 gdbpy_breakpoint_object
*newbp
;
1033 if (!user_breakpoint_p (bp
) && bppy_pending_object
== NULL
)
1035 pybp_debug_printf ("not attaching python object to this breakpoint");
1039 if (bp
->type
!= bp_breakpoint
1040 && bp
->type
!= bp_hardware_breakpoint
1041 && bp
->type
!= bp_watchpoint
1042 && bp
->type
!= bp_hardware_watchpoint
1043 && bp
->type
!= bp_read_watchpoint
1044 && bp
->type
!= bp_access_watchpoint
1045 && bp
->type
!= bp_catchpoint
)
1047 pybp_debug_printf ("is not a breakpoint or watchpoint");
1051 struct gdbarch
*garch
= bp
->gdbarch
? bp
->gdbarch
: get_current_arch ();
1052 gdbpy_enter
enter_py (garch
, current_language
);
1054 if (bppy_pending_object
)
1056 newbp
= bppy_pending_object
;
1058 bppy_pending_object
= NULL
;
1059 pybp_debug_printf ("attaching existing breakpoint object");
1063 newbp
= PyObject_New (gdbpy_breakpoint_object
, &breakpoint_object_type
);
1064 pybp_debug_printf ("attaching new breakpoint object");
1068 newbp
->number
= bp
->number
;
1070 newbp
->bp
->py_bp_object
= newbp
;
1071 newbp
->is_finish_bp
= 0;
1076 PyErr_SetString (PyExc_RuntimeError
,
1077 _("Error while creating breakpoint from GDB."));
1078 gdbpy_print_stack ();
1081 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_created
))
1083 if (evpy_emit_event ((PyObject
*) newbp
,
1084 gdb_py_events
.breakpoint_created
) < 0)
1085 gdbpy_print_stack ();
1089 /* Callback that is used when a breakpoint is deleted. This will
1090 invalidate the corresponding Python object. */
1092 gdbpy_breakpoint_deleted (struct breakpoint
*b
)
1094 PYBP_SCOPED_DEBUG_ENTER_EXIT
;
1096 int num
= b
->number
;
1097 struct breakpoint
*bp
= NULL
;
1099 bp
= get_breakpoint (num
);
1102 struct gdbarch
*garch
= bp
->gdbarch
? bp
->gdbarch
: get_current_arch ();
1103 gdbpy_enter
enter_py (garch
, current_language
);
1105 gdbpy_ref
<gdbpy_breakpoint_object
> bp_obj (bp
->py_bp_object
);
1108 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_deleted
))
1110 if (evpy_emit_event ((PyObject
*) bp_obj
.get (),
1111 gdb_py_events
.breakpoint_deleted
) < 0)
1112 gdbpy_print_stack ();
1121 /* Callback that is used when a breakpoint is modified. */
1124 gdbpy_breakpoint_modified (struct breakpoint
*b
)
1126 PYBP_SCOPED_DEBUG_ENTER_EXIT
;
1128 int num
= b
->number
;
1129 struct breakpoint
*bp
= NULL
;
1131 bp
= get_breakpoint (num
);
1134 struct gdbarch
*garch
= bp
->gdbarch
? bp
->gdbarch
: get_current_arch ();
1135 gdbpy_enter
enter_py (garch
, current_language
);
1137 PyObject
*bp_obj
= (PyObject
*) bp
->py_bp_object
;
1140 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_modified
))
1142 if (evpy_emit_event (bp_obj
,
1143 gdb_py_events
.breakpoint_modified
) < 0)
1144 gdbpy_print_stack ();
1152 /* Initialize the Python breakpoint code. */
1154 gdbpy_initialize_breakpoints (void)
1158 breakpoint_object_type
.tp_new
= PyType_GenericNew
;
1159 if (PyType_Ready (&breakpoint_object_type
) < 0)
1162 if (gdb_pymodule_addobject (gdb_module
, "Breakpoint",
1163 (PyObject
*) &breakpoint_object_type
) < 0)
1166 gdb::observers::breakpoint_created
.attach (gdbpy_breakpoint_created
,
1168 gdb::observers::breakpoint_deleted
.attach (gdbpy_breakpoint_deleted
,
1170 gdb::observers::breakpoint_modified
.attach (gdbpy_breakpoint_modified
,
1173 /* Add breakpoint types constants. */
1174 for (i
= 0; pybp_codes
[i
].name
; ++i
)
1176 if (PyModule_AddIntConstant (gdb_module
, pybp_codes
[i
].name
,
1177 pybp_codes
[i
].code
) < 0)
1181 /* Add watchpoint types constants. */
1182 for (i
= 0; pybp_watch_types
[i
].name
; ++i
)
1184 if (PyModule_AddIntConstant (gdb_module
, pybp_watch_types
[i
].name
,
1185 pybp_watch_types
[i
].code
) < 0)
1194 /* Helper function that overrides this Python object's
1195 PyObject_GenericSetAttr to allow extra validation of the attribute
1199 local_setattro (PyObject
*self
, PyObject
*name
, PyObject
*v
)
1201 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
1202 gdb::unique_xmalloc_ptr
<char> attr (python_string_to_host_string (name
));
1207 /* If the attribute trying to be set is the "stop" method,
1208 but we already have a condition set in the CLI or other extension
1209 language, disallow this operation. */
1210 if (strcmp (attr
.get (), stop_func
) == 0)
1212 const struct extension_language_defn
*extlang
= NULL
;
1214 if (obj
->bp
->cond_string
!= NULL
)
1215 extlang
= get_ext_lang_defn (EXT_LANG_GDB
);
1216 if (extlang
== NULL
)
1217 extlang
= get_breakpoint_cond_ext_lang (obj
->bp
, EXT_LANG_PYTHON
);
1218 if (extlang
!= NULL
)
1220 std::string error_text
1221 = string_printf (_("Only one stop condition allowed. There is"
1222 " currently a %s stop condition defined for"
1223 " this breakpoint."),
1224 ext_lang_capitalized_name (extlang
));
1225 PyErr_SetString (PyExc_RuntimeError
, error_text
.c_str ());
1230 return PyObject_GenericSetAttr (self
, name
, v
);
1233 static gdb_PyGetSetDef breakpoint_object_getset
[] = {
1234 { "enabled", bppy_get_enabled
, bppy_set_enabled
,
1235 "Boolean telling whether the breakpoint is enabled.", NULL
},
1236 { "silent", bppy_get_silent
, bppy_set_silent
,
1237 "Boolean telling whether the breakpoint is silent.", NULL
},
1238 { "thread", bppy_get_thread
, bppy_set_thread
,
1239 "Thread ID for the breakpoint.\n\
1240 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1241 If the value is None, then this breakpoint is not thread-specific.\n\
1242 No other type of value can be used.", NULL
},
1243 { "task", bppy_get_task
, bppy_set_task
,
1244 "Thread ID for the breakpoint.\n\
1245 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1246 If the value is None, then this breakpoint is not task-specific.\n\
1247 No other type of value can be used.", NULL
},
1248 { "ignore_count", bppy_get_ignore_count
, bppy_set_ignore_count
,
1249 "Number of times this breakpoint should be automatically continued.",
1251 { "number", bppy_get_number
, NULL
,
1252 "Breakpoint's number assigned by GDB.", NULL
},
1253 { "hit_count", bppy_get_hit_count
, bppy_set_hit_count
,
1254 "Number of times the breakpoint has been hit.\n\
1255 Can be set to zero to clear the count. No other value is valid\n\
1256 when setting this property.", NULL
},
1257 { "location", bppy_get_location
, NULL
,
1258 "Location of the breakpoint, as specified by the user.", NULL
},
1259 { "expression", bppy_get_expression
, NULL
,
1260 "Expression of the breakpoint, as specified by the user.", NULL
},
1261 { "condition", bppy_get_condition
, bppy_set_condition
,
1262 "Condition of the breakpoint, as specified by the user,\
1263 or None if no condition set."},
1264 { "commands", bppy_get_commands
, bppy_set_commands
,
1265 "Commands of the breakpoint, as specified by the user."},
1266 { "type", bppy_get_type
, NULL
,
1267 "Type of breakpoint."},
1268 { "visible", bppy_get_visibility
, NULL
,
1269 "Whether the breakpoint is visible to the user."},
1270 { "temporary", bppy_get_temporary
, NULL
,
1271 "Whether this breakpoint is a temporary breakpoint."},
1272 { "pending", bppy_get_pending
, NULL
,
1273 "Whether this breakpoint is a pending breakpoint."},
1274 { NULL
} /* Sentinel. */
1277 static PyMethodDef breakpoint_object_methods
[] =
1279 { "is_valid", bppy_is_valid
, METH_NOARGS
,
1280 "Return true if this breakpoint is valid, false if not." },
1281 { "delete", bppy_delete_breakpoint
, METH_NOARGS
,
1282 "Delete the underlying GDB breakpoint." },
1283 { NULL
} /* Sentinel. */
1286 PyTypeObject breakpoint_object_type
=
1288 PyVarObject_HEAD_INIT (NULL
, 0)
1289 "gdb.Breakpoint", /*tp_name*/
1290 sizeof (gdbpy_breakpoint_object
), /*tp_basicsize*/
1299 0, /*tp_as_sequence*/
1300 0, /*tp_as_mapping*/
1305 (setattrofunc
)local_setattro
, /*tp_setattro */
1307 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1308 "GDB breakpoint object", /* tp_doc */
1309 0, /* tp_traverse */
1311 0, /* tp_richcompare */
1312 0, /* tp_weaklistoffset */
1314 0, /* tp_iternext */
1315 breakpoint_object_methods
, /* tp_methods */
1317 breakpoint_object_getset
, /* tp_getset */
1320 0, /* tp_descr_get */
1321 0, /* tp_descr_set */
1322 0, /* tp_dictoffset */
1323 bppy_init
, /* tp_init */
1327 void _initialize_py_breakpoint ();
1329 _initialize_py_breakpoint ()
1331 add_setshow_boolean_cmd
1332 ("py-breakpoint", class_maintenance
, &pybp_debug
,
1333 _("Set Python breakpoint debugging."),
1334 _("Show Python breakpoint debugging."),
1335 _("When on, Python breakpoint debugging is enabled."),
1338 &setdebuglist
, &showdebuglist
);