1 /* Python interface to breakpoints
3 Copyright (C) 2008-2019 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 /* Number of live breakpoints. */
40 /* Variables used to pass information between the Breakpoint
41 constructor and the breakpoint-created hook function. */
42 gdbpy_breakpoint_object
*bppy_pending_object
;
44 /* Function that is called when a Python condition is evaluated. */
45 static const char stop_func
[] = "stop";
47 /* This is used to initialize various gdb.bp_* constants. */
56 /* Entries related to the type of user set breakpoints. */
57 static struct pybp_code pybp_codes
[] =
59 { "BP_NONE", bp_none
},
60 { "BP_BREAKPOINT", bp_breakpoint
},
61 { "BP_WATCHPOINT", bp_watchpoint
},
62 { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint
},
63 { "BP_READ_WATCHPOINT", bp_read_watchpoint
},
64 { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint
},
65 {NULL
} /* Sentinel. */
68 /* Entries related to the type of watchpoint. */
69 static struct pybp_code pybp_watch_types
[] =
71 { "WP_READ", hw_read
},
72 { "WP_WRITE", hw_write
},
73 { "WP_ACCESS", hw_access
},
74 {NULL
} /* Sentinel. */
77 /* Python function which checks the validity of a breakpoint object. */
79 bppy_is_valid (PyObject
*self
, PyObject
*args
)
81 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
88 /* Python function to test whether or not the breakpoint is enabled. */
90 bppy_get_enabled (PyObject
*self
, void *closure
)
92 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
94 BPPY_REQUIRE_VALID (self_bp
);
97 if (self_bp
->bp
->enable_state
== bp_enabled
)
102 /* Python function to test whether or not the breakpoint is silent. */
104 bppy_get_silent (PyObject
*self
, void *closure
)
106 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
108 BPPY_REQUIRE_VALID (self_bp
);
109 if (self_bp
->bp
->silent
)
114 /* Python function to set the enabled state of a breakpoint. */
116 bppy_set_enabled (PyObject
*self
, PyObject
*newvalue
, void *closure
)
118 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
121 BPPY_SET_REQUIRE_VALID (self_bp
);
123 if (newvalue
== NULL
)
125 PyErr_SetString (PyExc_TypeError
,
126 _("Cannot delete `enabled' attribute."));
130 else if (! PyBool_Check (newvalue
))
132 PyErr_SetString (PyExc_TypeError
,
133 _("The value of `enabled' must be a boolean."));
137 cmp
= PyObject_IsTrue (newvalue
);
144 enable_breakpoint (self_bp
->bp
);
146 disable_breakpoint (self_bp
->bp
);
148 CATCH (except
, RETURN_MASK_ALL
)
150 GDB_PY_SET_HANDLE_EXCEPTION (except
);
157 /* Python function to set the 'silent' state of a breakpoint. */
159 bppy_set_silent (PyObject
*self
, PyObject
*newvalue
, void *closure
)
161 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
164 BPPY_SET_REQUIRE_VALID (self_bp
);
166 if (newvalue
== NULL
)
168 PyErr_SetString (PyExc_TypeError
,
169 _("Cannot delete `silent' attribute."));
172 else if (! PyBool_Check (newvalue
))
174 PyErr_SetString (PyExc_TypeError
,
175 _("The value of `silent' must be a boolean."));
179 cmp
= PyObject_IsTrue (newvalue
);
183 breakpoint_set_silent (self_bp
->bp
, cmp
);
188 /* Python function to set the thread of a breakpoint. */
190 bppy_set_thread (PyObject
*self
, PyObject
*newvalue
, void *closure
)
192 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
195 BPPY_SET_REQUIRE_VALID (self_bp
);
197 if (newvalue
== NULL
)
199 PyErr_SetString (PyExc_TypeError
,
200 _("Cannot delete `thread' attribute."));
203 else if (PyInt_Check (newvalue
))
205 if (! gdb_py_int_as_long (newvalue
, &id
))
208 if (!valid_global_thread_id (id
))
210 PyErr_SetString (PyExc_RuntimeError
,
211 _("Invalid thread ID."));
215 else if (newvalue
== Py_None
)
219 PyErr_SetString (PyExc_TypeError
,
220 _("The value of `thread' must be an integer or None."));
224 breakpoint_set_thread (self_bp
->bp
, id
);
229 /* Python function to set the (Ada) task of a breakpoint. */
231 bppy_set_task (PyObject
*self
, PyObject
*newvalue
, void *closure
)
233 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
237 BPPY_SET_REQUIRE_VALID (self_bp
);
239 if (newvalue
== NULL
)
241 PyErr_SetString (PyExc_TypeError
,
242 _("Cannot delete `task' attribute."));
245 else if (PyInt_Check (newvalue
))
247 if (! gdb_py_int_as_long (newvalue
, &id
))
252 valid_id
= valid_task_id (id
);
254 CATCH (except
, RETURN_MASK_ALL
)
256 GDB_PY_SET_HANDLE_EXCEPTION (except
);
262 PyErr_SetString (PyExc_RuntimeError
,
263 _("Invalid task ID."));
267 else if (newvalue
== Py_None
)
271 PyErr_SetString (PyExc_TypeError
,
272 _("The value of `task' must be an integer or None."));
276 breakpoint_set_task (self_bp
->bp
, id
);
281 /* Python function which deletes the underlying GDB breakpoint. This
282 triggers the breakpoint_deleted observer which will call
283 gdbpy_breakpoint_deleted; that function cleans up the Python
287 bppy_delete_breakpoint (PyObject
*self
, PyObject
*args
)
289 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
291 BPPY_REQUIRE_VALID (self_bp
);
295 delete_breakpoint (self_bp
->bp
);
297 CATCH (except
, RETURN_MASK_ALL
)
299 GDB_PY_HANDLE_EXCEPTION (except
);
307 /* Python function to set the ignore count of a breakpoint. */
309 bppy_set_ignore_count (PyObject
*self
, PyObject
*newvalue
, void *closure
)
311 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
314 BPPY_SET_REQUIRE_VALID (self_bp
);
316 if (newvalue
== NULL
)
318 PyErr_SetString (PyExc_TypeError
,
319 _("Cannot delete `ignore_count' attribute."));
322 else if (! PyInt_Check (newvalue
))
324 PyErr_SetString (PyExc_TypeError
,
325 _("The value of `ignore_count' must be an integer."));
329 if (! gdb_py_int_as_long (newvalue
, &value
))
337 set_ignore_count (self_bp
->number
, (int) value
, 0);
339 CATCH (except
, RETURN_MASK_ALL
)
341 GDB_PY_SET_HANDLE_EXCEPTION (except
);
348 /* Python function to set the hit count of a breakpoint. */
350 bppy_set_hit_count (PyObject
*self
, PyObject
*newvalue
, void *closure
)
352 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
354 BPPY_SET_REQUIRE_VALID (self_bp
);
356 if (newvalue
== NULL
)
358 PyErr_SetString (PyExc_TypeError
,
359 _("Cannot delete `hit_count' attribute."));
366 if (! gdb_py_int_as_long (newvalue
, &value
))
371 PyErr_SetString (PyExc_AttributeError
,
372 _("The value of `hit_count' must be zero."));
377 self_bp
->bp
->hit_count
= 0;
382 /* Python function to get the location of a breakpoint. */
384 bppy_get_location (PyObject
*self
, void *closure
)
387 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
389 BPPY_REQUIRE_VALID (obj
);
391 if (obj
->bp
->type
!= bp_breakpoint
)
394 struct event_location
*location
= obj
->bp
->location
.get ();
395 /* "catch throw" makes a breakpoint of type bp_breakpoint that does
396 not have a location. */
397 if (location
== nullptr)
399 str
= event_location_to_string (location
);
402 return host_string_to_python_string (str
).release ();
405 /* Python function to get the breakpoint expression. */
407 bppy_get_expression (PyObject
*self
, void *closure
)
410 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
411 struct watchpoint
*wp
;
413 BPPY_REQUIRE_VALID (obj
);
415 if (!is_watchpoint (obj
->bp
))
418 wp
= (struct watchpoint
*) obj
->bp
;
420 str
= wp
->exp_string
;
424 return host_string_to_python_string (str
).release ();
427 /* Python function to get the condition expression of a breakpoint. */
429 bppy_get_condition (PyObject
*self
, void *closure
)
432 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
434 BPPY_REQUIRE_VALID (obj
);
436 str
= obj
->bp
->cond_string
;
440 return host_string_to_python_string (str
).release ();
443 /* Returns 0 on success. Returns -1 on error, with a python exception set.
447 bppy_set_condition (PyObject
*self
, PyObject
*newvalue
, void *closure
)
449 gdb::unique_xmalloc_ptr
<char> exp_holder
;
450 const char *exp
= NULL
;
451 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
452 struct gdb_exception except
= exception_none
;
454 BPPY_SET_REQUIRE_VALID (self_bp
);
456 if (newvalue
== NULL
)
458 PyErr_SetString (PyExc_TypeError
,
459 _("Cannot delete `condition' attribute."));
462 else if (newvalue
== Py_None
)
466 exp_holder
= python_string_to_host_string (newvalue
);
467 if (exp_holder
== NULL
)
469 exp
= exp_holder
.get ();
474 set_breakpoint_condition (self_bp
->bp
, exp
, 0);
476 CATCH (ex
, RETURN_MASK_ALL
)
482 GDB_PY_SET_HANDLE_EXCEPTION (except
);
487 /* Python function to get the commands attached to a breakpoint. */
489 bppy_get_commands (PyObject
*self
, void *closure
)
491 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
492 struct breakpoint
*bp
= self_bp
->bp
;
494 BPPY_REQUIRE_VALID (self_bp
);
496 if (! self_bp
->bp
->commands
)
501 current_uiout
->redirect (&stb
);
504 print_command_lines (current_uiout
, breakpoint_commands (bp
), 0);
506 CATCH (except
, RETURN_MASK_ALL
)
508 current_uiout
->redirect (NULL
);
509 gdbpy_convert_exception (except
);
514 current_uiout
->redirect (NULL
);
515 return host_string_to_python_string (stb
.c_str ()).release ();
518 /* Set the commands attached to a breakpoint. Returns 0 on success.
519 Returns -1 on error, with a python exception set. */
521 bppy_set_commands (PyObject
*self
, PyObject
*newvalue
, void *closure
)
523 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
524 struct gdb_exception except
= exception_none
;
526 BPPY_SET_REQUIRE_VALID (self_bp
);
528 gdb::unique_xmalloc_ptr
<char> commands
529 (python_string_to_host_string (newvalue
));
530 if (commands
== nullptr)
536 char *save_ptr
= nullptr;
540 const char *result
= strtok_r (first
? commands
.get () : nullptr,
546 counted_command_line lines
= read_command_lines_1 (reader
, 1, nullptr);
547 breakpoint_set_commands (self_bp
->bp
, std::move (lines
));
549 CATCH (ex
, RETURN_MASK_ALL
)
555 GDB_PY_SET_HANDLE_EXCEPTION (except
);
560 /* Python function to get the breakpoint type. */
562 bppy_get_type (PyObject
*self
, void *closure
)
564 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
566 BPPY_REQUIRE_VALID (self_bp
);
568 return PyInt_FromLong (self_bp
->bp
->type
);
571 /* Python function to get the visibility of the breakpoint. */
574 bppy_get_visibility (PyObject
*self
, void *closure
)
576 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
578 BPPY_REQUIRE_VALID (self_bp
);
580 if (user_breakpoint_p (self_bp
->bp
))
586 /* Python function to determine if the breakpoint is a temporary
590 bppy_get_temporary (PyObject
*self
, void *closure
)
592 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
594 BPPY_REQUIRE_VALID (self_bp
);
596 if (self_bp
->bp
->disposition
== disp_del
597 || self_bp
->bp
->disposition
== disp_del_at_next_stop
)
603 /* Python function to determine if the breakpoint is a pending
607 bppy_get_pending (PyObject
*self
, void *closure
)
609 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
611 BPPY_REQUIRE_VALID (self_bp
);
613 if (is_watchpoint (self_bp
->bp
))
615 if (pending_breakpoint_p (self_bp
->bp
))
621 /* Python function to get the breakpoint's number. */
623 bppy_get_number (PyObject
*self
, void *closure
)
625 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
627 BPPY_REQUIRE_VALID (self_bp
);
629 return PyInt_FromLong (self_bp
->number
);
632 /* Python function to get the breakpoint's thread ID. */
634 bppy_get_thread (PyObject
*self
, void *closure
)
636 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
638 BPPY_REQUIRE_VALID (self_bp
);
640 if (self_bp
->bp
->thread
== -1)
643 return PyInt_FromLong (self_bp
->bp
->thread
);
646 /* Python function to get the breakpoint's task ID (in Ada). */
648 bppy_get_task (PyObject
*self
, void *closure
)
650 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
652 BPPY_REQUIRE_VALID (self_bp
);
654 if (self_bp
->bp
->task
== 0)
657 return PyInt_FromLong (self_bp
->bp
->task
);
660 /* Python function to get the breakpoint's hit count. */
662 bppy_get_hit_count (PyObject
*self
, void *closure
)
664 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
666 BPPY_REQUIRE_VALID (self_bp
);
668 return PyInt_FromLong (self_bp
->bp
->hit_count
);
671 /* Python function to get the breakpoint's ignore count. */
673 bppy_get_ignore_count (PyObject
*self
, void *closure
)
675 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
677 BPPY_REQUIRE_VALID (self_bp
);
679 return PyInt_FromLong (self_bp
->bp
->ignore_count
);
682 /* Internal function to validate the Python parameters/keywords
683 provided to bppy_init. */
686 bppy_init_validate_args (const char *spec
, char *source
,
687 char *function
, char *label
,
688 char *line
, enum bptype type
)
690 /* If spec is defined, ensure that none of the explicit location
691 keywords are also defined. */
694 if (source
!= NULL
|| function
!= NULL
|| label
!= NULL
|| line
!= NULL
)
696 PyErr_SetString (PyExc_RuntimeError
,
697 _("Breakpoints specified with spec cannot "
698 "have source, function, label or line defined."));
704 /* If spec isn't defined, ensure that the user is not trying to
705 define a watchpoint with an explicit location. */
706 if (type
== bp_watchpoint
)
708 PyErr_SetString (PyExc_RuntimeError
,
709 _("Watchpoints cannot be set by explicit "
710 "location parameters."));
715 /* Otherwise, ensure some explicit locations are defined. */
716 if (source
== NULL
&& function
== NULL
&& label
== NULL
719 PyErr_SetString (PyExc_RuntimeError
,
720 _("Neither spec nor explicit location set."));
723 /* Finally, if source is specified, ensure that line, label
724 or function are specified too. */
725 if (source
!= NULL
&& function
== NULL
&& label
== NULL
728 PyErr_SetString (PyExc_RuntimeError
,
729 _("Specifying a source must also include a "
730 "line, label or function."));
738 /* Python function to create a new breakpoint. */
740 bppy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
742 static const char *keywords
[] = { "spec", "type", "wp_class", "internal",
743 "temporary","source", "function",
744 "label", "line", "qualified", NULL
};
745 const char *spec
= NULL
;
746 enum bptype type
= bp_breakpoint
;
747 int access_type
= hw_write
;
748 PyObject
*internal
= NULL
;
749 PyObject
*temporary
= NULL
;
750 PyObject
*lineobj
= NULL
;;
752 int temporary_bp
= 0;
753 gdb::unique_xmalloc_ptr
<char> line
;
756 char *function
= NULL
;
757 PyObject
* qualified
= NULL
;
759 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kwargs
, "|siiOOsssOO", keywords
,
760 &spec
, &type
, &access_type
,
763 &function
, &label
, &lineobj
,
770 if (PyInt_Check (lineobj
))
771 line
.reset (xstrprintf ("%ld", PyInt_AsLong (lineobj
)));
772 else if (PyString_Check (lineobj
))
773 line
= python_string_to_host_string (lineobj
);
776 PyErr_SetString (PyExc_RuntimeError
,
777 _("Line keyword should be an integer or a string. "));
784 internal_bp
= PyObject_IsTrue (internal
);
785 if (internal_bp
== -1)
789 if (temporary
!= NULL
)
791 temporary_bp
= PyObject_IsTrue (temporary
);
792 if (temporary_bp
== -1)
796 if (bppy_init_validate_args (spec
, source
, function
, label
, line
.get (),
800 bppy_pending_object
= (gdbpy_breakpoint_object
*) self
;
801 bppy_pending_object
->number
= -1;
802 bppy_pending_object
->bp
= NULL
;
810 event_location_up location
;
811 symbol_name_match_type func_name_match_type
812 = (qualified
!= NULL
&& PyObject_IsTrue (qualified
)
813 ? symbol_name_match_type::FULL
814 : symbol_name_match_type::WILD
);
818 gdb::unique_xmalloc_ptr
<char>
819 copy_holder (xstrdup (skip_spaces (spec
)));
820 const char *copy
= copy_holder
.get ();
822 location
= string_to_event_location (©
,
824 func_name_match_type
);
828 struct explicit_location explicit_loc
;
830 initialize_explicit_location (&explicit_loc
);
831 explicit_loc
.source_filename
= source
;
832 explicit_loc
.function_name
= function
;
833 explicit_loc
.label_name
= label
;
836 explicit_loc
.line_offset
=
837 linespec_parse_line_offset (line
.get ());
839 explicit_loc
.func_name_match_type
= func_name_match_type
;
841 location
= new_explicit_location (&explicit_loc
);
844 create_breakpoint (python_gdbarch
,
845 location
.get (), NULL
, -1, NULL
,
847 temporary_bp
, bp_breakpoint
,
850 &bkpt_breakpoint_ops
,
851 0, 1, internal_bp
, 0);
856 gdb::unique_xmalloc_ptr
<char>
857 copy_holder (xstrdup (skip_spaces (spec
)));
858 char *copy
= copy_holder
.get ();
860 if (access_type
== hw_write
)
861 watch_command_wrapper (copy
, 0, internal_bp
);
862 else if (access_type
== hw_access
)
863 awatch_command_wrapper (copy
, 0, internal_bp
);
864 else if (access_type
== hw_read
)
865 rwatch_command_wrapper (copy
, 0, internal_bp
);
867 error(_("Cannot understand watchpoint access type."));
871 error(_("Do not understand breakpoint type to set."));
874 CATCH (except
, RETURN_MASK_ALL
)
876 bppy_pending_object
= NULL
;
877 gdbpy_convert_exception (except
);
882 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object
*) self
);
889 build_bp_list (struct breakpoint
*b
, void *arg
)
891 PyObject
*list
= (PyObject
*) arg
;
892 PyObject
*bp
= (PyObject
*) b
->py_bp_object
;
895 /* Not all breakpoints will have a companion Python object.
896 Only breakpoints that were created via bppy_new, or
897 breakpoints that were created externally and are tracked by
898 the Python Scripting API. */
900 iserr
= PyList_Append (list
, bp
);
908 /* Static function to return a tuple holding all breakpoints. */
911 gdbpy_breakpoints (PyObject
*self
, PyObject
*args
)
914 return PyTuple_New (0);
916 gdbpy_ref
<> list (PyList_New (0));
920 /* If iterate_over_breakpoints returns non NULL it signals an error
921 condition. In that case abandon building the list and return
923 if (iterate_over_breakpoints (build_bp_list
, list
.get ()) != NULL
)
926 return PyList_AsTuple (list
.get ());
929 /* Call the "stop" method (if implemented) in the breakpoint
930 class. If the method returns True, the inferior will be
931 stopped at the breakpoint. Otherwise the inferior will be
932 allowed to continue. */
934 enum ext_lang_bp_stop
935 gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn
*extlang
,
936 struct breakpoint
*b
)
939 struct gdbpy_breakpoint_object
*bp_obj
= b
->py_bp_object
;
940 PyObject
*py_bp
= (PyObject
*) bp_obj
;
941 struct gdbarch
*garch
;
944 return EXT_LANG_BP_STOP_UNSET
;
947 garch
= b
->gdbarch
? b
->gdbarch
: get_current_arch ();
949 gdbpy_enter
enter_py (garch
, current_language
);
951 if (bp_obj
->is_finish_bp
)
952 bpfinishpy_pre_stop_hook (bp_obj
);
954 if (PyObject_HasAttrString (py_bp
, stop_func
))
956 gdbpy_ref
<> result (PyObject_CallMethod (py_bp
, stop_func
, NULL
));
961 int evaluate
= PyObject_IsTrue (result
.get ());
964 gdbpy_print_stack ();
966 /* If the "stop" function returns False that means
967 the Python breakpoint wants GDB to continue. */
972 gdbpy_print_stack ();
975 if (bp_obj
->is_finish_bp
)
976 bpfinishpy_post_stop_hook (bp_obj
);
979 return EXT_LANG_BP_STOP_UNSET
;
980 return stop
? EXT_LANG_BP_STOP_YES
: EXT_LANG_BP_STOP_NO
;
983 /* Checks if the "stop" method exists in this breakpoint.
984 Used by condition_command to ensure mutual exclusion of breakpoint
988 gdbpy_breakpoint_has_cond (const struct extension_language_defn
*extlang
,
989 struct breakpoint
*b
)
992 struct gdbarch
*garch
;
994 if (b
->py_bp_object
== NULL
)
997 py_bp
= (PyObject
*) b
->py_bp_object
;
998 garch
= b
->gdbarch
? b
->gdbarch
: get_current_arch ();
1000 gdbpy_enter
enter_py (garch
, current_language
);
1001 return PyObject_HasAttrString (py_bp
, stop_func
);
1006 /* Event callback functions. */
1008 /* Callback that is used when a breakpoint is created. This function
1009 will create a new Python breakpoint object. */
1011 gdbpy_breakpoint_created (struct breakpoint
*bp
)
1013 gdbpy_breakpoint_object
*newbp
;
1014 PyGILState_STATE state
;
1016 if (!user_breakpoint_p (bp
) && bppy_pending_object
== NULL
)
1019 if (bp
->type
!= bp_breakpoint
1020 && bp
->type
!= bp_watchpoint
1021 && bp
->type
!= bp_hardware_watchpoint
1022 && bp
->type
!= bp_read_watchpoint
1023 && bp
->type
!= bp_access_watchpoint
)
1026 state
= PyGILState_Ensure ();
1028 if (bppy_pending_object
)
1030 newbp
= bppy_pending_object
;
1031 bppy_pending_object
= NULL
;
1034 newbp
= PyObject_New (gdbpy_breakpoint_object
, &breakpoint_object_type
);
1037 newbp
->number
= bp
->number
;
1039 newbp
->bp
->py_bp_object
= newbp
;
1040 newbp
->is_finish_bp
= 0;
1046 PyErr_SetString (PyExc_RuntimeError
,
1047 _("Error while creating breakpoint from GDB."));
1048 gdbpy_print_stack ();
1051 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_created
))
1053 if (evpy_emit_event ((PyObject
*) newbp
,
1054 gdb_py_events
.breakpoint_created
) < 0)
1055 gdbpy_print_stack ();
1058 PyGILState_Release (state
);
1061 /* Callback that is used when a breakpoint is deleted. This will
1062 invalidate the corresponding Python object. */
1064 gdbpy_breakpoint_deleted (struct breakpoint
*b
)
1066 int num
= b
->number
;
1067 PyGILState_STATE state
;
1068 struct breakpoint
*bp
= NULL
;
1070 state
= PyGILState_Ensure ();
1071 bp
= get_breakpoint (num
);
1074 gdbpy_ref
<gdbpy_breakpoint_object
> bp_obj (bp
->py_bp_object
);
1077 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_deleted
))
1079 if (evpy_emit_event ((PyObject
*) bp_obj
.get (),
1080 gdb_py_events
.breakpoint_deleted
) < 0)
1081 gdbpy_print_stack ();
1088 PyGILState_Release (state
);
1091 /* Callback that is used when a breakpoint is modified. */
1094 gdbpy_breakpoint_modified (struct breakpoint
*b
)
1096 int num
= b
->number
;
1097 PyGILState_STATE state
;
1098 struct breakpoint
*bp
= NULL
;
1100 state
= PyGILState_Ensure ();
1101 bp
= get_breakpoint (num
);
1104 PyObject
*bp_obj
= (PyObject
*) bp
->py_bp_object
;
1107 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_modified
))
1109 if (evpy_emit_event (bp_obj
,
1110 gdb_py_events
.breakpoint_modified
) < 0)
1111 gdbpy_print_stack ();
1115 PyGILState_Release (state
);
1120 /* Initialize the Python breakpoint code. */
1122 gdbpy_initialize_breakpoints (void)
1126 breakpoint_object_type
.tp_new
= PyType_GenericNew
;
1127 if (PyType_Ready (&breakpoint_object_type
) < 0)
1130 if (gdb_pymodule_addobject (gdb_module
, "Breakpoint",
1131 (PyObject
*) &breakpoint_object_type
) < 0)
1134 gdb::observers::breakpoint_created
.attach (gdbpy_breakpoint_created
);
1135 gdb::observers::breakpoint_deleted
.attach (gdbpy_breakpoint_deleted
);
1136 gdb::observers::breakpoint_modified
.attach (gdbpy_breakpoint_modified
);
1138 /* Add breakpoint types constants. */
1139 for (i
= 0; pybp_codes
[i
].name
; ++i
)
1141 if (PyModule_AddIntConstant (gdb_module
, pybp_codes
[i
].name
,
1142 pybp_codes
[i
].code
) < 0)
1146 /* Add watchpoint types constants. */
1147 for (i
= 0; pybp_watch_types
[i
].name
; ++i
)
1149 if (PyModule_AddIntConstant (gdb_module
, pybp_watch_types
[i
].name
,
1150 pybp_watch_types
[i
].code
) < 0)
1159 /* Helper function that overrides this Python object's
1160 PyObject_GenericSetAttr to allow extra validation of the attribute
1164 local_setattro (PyObject
*self
, PyObject
*name
, PyObject
*v
)
1166 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
1167 gdb::unique_xmalloc_ptr
<char> attr (python_string_to_host_string (name
));
1172 /* If the attribute trying to be set is the "stop" method,
1173 but we already have a condition set in the CLI or other extension
1174 language, disallow this operation. */
1175 if (strcmp (attr
.get (), stop_func
) == 0)
1177 const struct extension_language_defn
*extlang
= NULL
;
1179 if (obj
->bp
->cond_string
!= NULL
)
1180 extlang
= get_ext_lang_defn (EXT_LANG_GDB
);
1181 if (extlang
== NULL
)
1182 extlang
= get_breakpoint_cond_ext_lang (obj
->bp
, EXT_LANG_PYTHON
);
1183 if (extlang
!= NULL
)
1185 std::string error_text
1186 = string_printf (_("Only one stop condition allowed. There is"
1187 " currently a %s stop condition defined for"
1188 " this breakpoint."),
1189 ext_lang_capitalized_name (extlang
));
1190 PyErr_SetString (PyExc_RuntimeError
, error_text
.c_str ());
1195 return PyObject_GenericSetAttr (self
, name
, v
);
1198 static gdb_PyGetSetDef breakpoint_object_getset
[] = {
1199 { "enabled", bppy_get_enabled
, bppy_set_enabled
,
1200 "Boolean telling whether the breakpoint is enabled.", NULL
},
1201 { "silent", bppy_get_silent
, bppy_set_silent
,
1202 "Boolean telling whether the breakpoint is silent.", NULL
},
1203 { "thread", bppy_get_thread
, bppy_set_thread
,
1204 "Thread ID for the breakpoint.\n\
1205 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1206 If the value is None, then this breakpoint is not thread-specific.\n\
1207 No other type of value can be used.", NULL
},
1208 { "task", bppy_get_task
, bppy_set_task
,
1209 "Thread ID for the breakpoint.\n\
1210 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1211 If the value is None, then this breakpoint is not task-specific.\n\
1212 No other type of value can be used.", NULL
},
1213 { "ignore_count", bppy_get_ignore_count
, bppy_set_ignore_count
,
1214 "Number of times this breakpoint should be automatically continued.",
1216 { "number", bppy_get_number
, NULL
,
1217 "Breakpoint's number assigned by GDB.", NULL
},
1218 { "hit_count", bppy_get_hit_count
, bppy_set_hit_count
,
1219 "Number of times the breakpoint has been hit.\n\
1220 Can be set to zero to clear the count. No other value is valid\n\
1221 when setting this property.", NULL
},
1222 { "location", bppy_get_location
, NULL
,
1223 "Location of the breakpoint, as specified by the user.", NULL
},
1224 { "expression", bppy_get_expression
, NULL
,
1225 "Expression of the breakpoint, as specified by the user.", NULL
},
1226 { "condition", bppy_get_condition
, bppy_set_condition
,
1227 "Condition of the breakpoint, as specified by the user,\
1228 or None if no condition set."},
1229 { "commands", bppy_get_commands
, bppy_set_commands
,
1230 "Commands of the breakpoint, as specified by the user."},
1231 { "type", bppy_get_type
, NULL
,
1232 "Type of breakpoint."},
1233 { "visible", bppy_get_visibility
, NULL
,
1234 "Whether the breakpoint is visible to the user."},
1235 { "temporary", bppy_get_temporary
, NULL
,
1236 "Whether this breakpoint is a temporary breakpoint."},
1237 { "pending", bppy_get_pending
, NULL
,
1238 "Whether this breakpoint is a pending breakpoint."},
1239 { NULL
} /* Sentinel. */
1242 static PyMethodDef breakpoint_object_methods
[] =
1244 { "is_valid", bppy_is_valid
, METH_NOARGS
,
1245 "Return true if this breakpoint is valid, false if not." },
1246 { "delete", bppy_delete_breakpoint
, METH_NOARGS
,
1247 "Delete the underlying GDB breakpoint." },
1248 { NULL
} /* Sentinel. */
1251 PyTypeObject breakpoint_object_type
=
1253 PyVarObject_HEAD_INIT (NULL
, 0)
1254 "gdb.Breakpoint", /*tp_name*/
1255 sizeof (gdbpy_breakpoint_object
), /*tp_basicsize*/
1264 0, /*tp_as_sequence*/
1265 0, /*tp_as_mapping*/
1270 (setattrofunc
)local_setattro
, /*tp_setattro */
1272 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1273 "GDB breakpoint object", /* tp_doc */
1274 0, /* tp_traverse */
1276 0, /* tp_richcompare */
1277 0, /* tp_weaklistoffset */
1279 0, /* tp_iternext */
1280 breakpoint_object_methods
, /* tp_methods */
1282 breakpoint_object_getset
, /* tp_getset */
1285 0, /* tp_descr_get */
1286 0, /* tp_descr_set */
1287 0, /* tp_dictoffset */
1288 bppy_init
, /* tp_init */