1 /* Python interface to breakpoints
3 Copyright (C) 2008-2018 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 str
= event_location_to_string (obj
->bp
->location
.get ());
397 return host_string_to_python_string (str
);
400 /* Python function to get the breakpoint expression. */
402 bppy_get_expression (PyObject
*self
, void *closure
)
405 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
406 struct watchpoint
*wp
;
408 BPPY_REQUIRE_VALID (obj
);
410 if (!is_watchpoint (obj
->bp
))
413 wp
= (struct watchpoint
*) obj
->bp
;
415 str
= wp
->exp_string
;
419 return host_string_to_python_string (str
);
422 /* Python function to get the condition expression of a breakpoint. */
424 bppy_get_condition (PyObject
*self
, void *closure
)
427 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
429 BPPY_REQUIRE_VALID (obj
);
431 str
= obj
->bp
->cond_string
;
435 return host_string_to_python_string (str
);
438 /* Returns 0 on success. Returns -1 on error, with a python exception set.
442 bppy_set_condition (PyObject
*self
, PyObject
*newvalue
, void *closure
)
444 gdb::unique_xmalloc_ptr
<char> exp_holder
;
445 const char *exp
= NULL
;
446 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
447 struct gdb_exception except
= exception_none
;
449 BPPY_SET_REQUIRE_VALID (self_bp
);
451 if (newvalue
== NULL
)
453 PyErr_SetString (PyExc_TypeError
,
454 _("Cannot delete `condition' attribute."));
457 else if (newvalue
== Py_None
)
461 exp_holder
= python_string_to_host_string (newvalue
);
462 if (exp_holder
== NULL
)
464 exp
= exp_holder
.get ();
469 set_breakpoint_condition (self_bp
->bp
, exp
, 0);
471 CATCH (ex
, RETURN_MASK_ALL
)
477 GDB_PY_SET_HANDLE_EXCEPTION (except
);
482 /* Python function to get the commands attached to a breakpoint. */
484 bppy_get_commands (PyObject
*self
, void *closure
)
486 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
487 struct breakpoint
*bp
= self_bp
->bp
;
489 BPPY_REQUIRE_VALID (self_bp
);
491 if (! self_bp
->bp
->commands
)
496 current_uiout
->redirect (&stb
);
499 print_command_lines (current_uiout
, breakpoint_commands (bp
), 0);
501 CATCH (except
, RETURN_MASK_ALL
)
503 current_uiout
->redirect (NULL
);
504 gdbpy_convert_exception (except
);
509 current_uiout
->redirect (NULL
);
510 return host_string_to_python_string (stb
.c_str ());
513 /* Set the commands attached to a breakpoint. Returns 0 on success.
514 Returns -1 on error, with a python exception set. */
516 bppy_set_commands (PyObject
*self
, PyObject
*newvalue
, void *closure
)
518 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
519 struct gdb_exception except
= exception_none
;
521 BPPY_SET_REQUIRE_VALID (self_bp
);
523 gdb::unique_xmalloc_ptr
<char> commands
524 (python_string_to_host_string (newvalue
));
525 if (commands
== nullptr)
531 char *save_ptr
= nullptr;
535 const char *result
= strtok_r (first
? commands
.get () : nullptr,
541 counted_command_line lines
= read_command_lines_1 (reader
, 1, nullptr);
542 breakpoint_set_commands (self_bp
->bp
, std::move (lines
));
544 CATCH (ex
, RETURN_MASK_ALL
)
550 GDB_PY_SET_HANDLE_EXCEPTION (except
);
555 /* Python function to get the breakpoint type. */
557 bppy_get_type (PyObject
*self
, void *closure
)
559 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
561 BPPY_REQUIRE_VALID (self_bp
);
563 return PyInt_FromLong (self_bp
->bp
->type
);
566 /* Python function to get the visibility of the breakpoint. */
569 bppy_get_visibility (PyObject
*self
, void *closure
)
571 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
573 BPPY_REQUIRE_VALID (self_bp
);
575 if (user_breakpoint_p (self_bp
->bp
))
581 /* Python function to determine if the breakpoint is a temporary
585 bppy_get_temporary (PyObject
*self
, void *closure
)
587 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
589 BPPY_REQUIRE_VALID (self_bp
);
591 if (self_bp
->bp
->disposition
== disp_del
592 || self_bp
->bp
->disposition
== disp_del_at_next_stop
)
598 /* Python function to determine if the breakpoint is a pending
602 bppy_get_pending (PyObject
*self
, void *closure
)
604 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
606 BPPY_REQUIRE_VALID (self_bp
);
608 if (is_watchpoint (self_bp
->bp
))
610 if (pending_breakpoint_p (self_bp
->bp
))
616 /* Python function to get the breakpoint's number. */
618 bppy_get_number (PyObject
*self
, void *closure
)
620 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
622 BPPY_REQUIRE_VALID (self_bp
);
624 return PyInt_FromLong (self_bp
->number
);
627 /* Python function to get the breakpoint's thread ID. */
629 bppy_get_thread (PyObject
*self
, void *closure
)
631 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
633 BPPY_REQUIRE_VALID (self_bp
);
635 if (self_bp
->bp
->thread
== -1)
638 return PyInt_FromLong (self_bp
->bp
->thread
);
641 /* Python function to get the breakpoint's task ID (in Ada). */
643 bppy_get_task (PyObject
*self
, void *closure
)
645 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
647 BPPY_REQUIRE_VALID (self_bp
);
649 if (self_bp
->bp
->task
== 0)
652 return PyInt_FromLong (self_bp
->bp
->task
);
655 /* Python function to get the breakpoint's hit count. */
657 bppy_get_hit_count (PyObject
*self
, void *closure
)
659 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
661 BPPY_REQUIRE_VALID (self_bp
);
663 return PyInt_FromLong (self_bp
->bp
->hit_count
);
666 /* Python function to get the breakpoint's ignore count. */
668 bppy_get_ignore_count (PyObject
*self
, void *closure
)
670 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
672 BPPY_REQUIRE_VALID (self_bp
);
674 return PyInt_FromLong (self_bp
->bp
->ignore_count
);
677 /* Internal function to validate the Python parameters/keywords
678 provided to bppy_init. */
681 bppy_init_validate_args (const char *spec
, char *source
,
682 char *function
, char *label
,
683 char *line
, enum bptype type
)
685 /* If spec is defined, ensure that none of the explicit location
686 keywords are also defined. */
689 if (source
!= NULL
|| function
!= NULL
|| label
!= NULL
|| line
!= NULL
)
691 PyErr_SetString (PyExc_RuntimeError
,
692 _("Breakpoints specified with spec cannot "
693 "have source, function, label or line defined."));
699 /* If spec isn't defined, ensure that the user is not trying to
700 define a watchpoint with an explicit location. */
701 if (type
== bp_watchpoint
)
703 PyErr_SetString (PyExc_RuntimeError
,
704 _("Watchpoints cannot be set by explicit "
705 "location parameters."));
710 /* Otherwise, ensure some explicit locations are defined. */
711 if (source
== NULL
&& function
== NULL
&& label
== NULL
714 PyErr_SetString (PyExc_RuntimeError
,
715 _("Neither spec nor explicit location set."));
718 /* Finally, if source is specified, ensure that line, label
719 or function are specified too. */
720 if (source
!= NULL
&& function
== NULL
&& label
== NULL
723 PyErr_SetString (PyExc_RuntimeError
,
724 _("Specifying a source must also include a "
725 "line, label or function."));
733 /* Python function to create a new breakpoint. */
735 bppy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
737 static const char *keywords
[] = { "spec", "type", "wp_class", "internal",
738 "temporary","source", "function",
739 "label", "line", "qualified", NULL
};
740 const char *spec
= NULL
;
741 enum bptype type
= bp_breakpoint
;
742 int access_type
= hw_write
;
743 PyObject
*internal
= NULL
;
744 PyObject
*temporary
= NULL
;
745 PyObject
*lineobj
= NULL
;;
747 int temporary_bp
= 0;
748 gdb::unique_xmalloc_ptr
<char> line
;
751 char *function
= NULL
;
752 PyObject
* qualified
= NULL
;
754 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kwargs
, "|siiOOsssOO", keywords
,
755 &spec
, &type
, &access_type
,
758 &function
, &label
, &lineobj
,
765 if (PyInt_Check (lineobj
))
766 line
.reset (xstrprintf ("%ld", PyInt_AsLong (lineobj
)));
767 else if (PyString_Check (lineobj
))
768 line
= python_string_to_host_string (lineobj
);
771 PyErr_SetString (PyExc_RuntimeError
,
772 _("Line keyword should be an integer or a string. "));
779 internal_bp
= PyObject_IsTrue (internal
);
780 if (internal_bp
== -1)
784 if (temporary
!= NULL
)
786 temporary_bp
= PyObject_IsTrue (temporary
);
787 if (temporary_bp
== -1)
791 if (bppy_init_validate_args (spec
, source
, function
, label
, line
.get (),
795 bppy_pending_object
= (gdbpy_breakpoint_object
*) self
;
796 bppy_pending_object
->number
= -1;
797 bppy_pending_object
->bp
= NULL
;
805 event_location_up location
;
806 symbol_name_match_type func_name_match_type
807 = (qualified
!= NULL
&& PyObject_IsTrue (qualified
)
808 ? symbol_name_match_type::FULL
809 : symbol_name_match_type::WILD
);
813 gdb::unique_xmalloc_ptr
<char>
814 copy_holder (xstrdup (skip_spaces (spec
)));
815 const char *copy
= copy_holder
.get ();
817 location
= string_to_event_location (©
,
819 func_name_match_type
);
823 struct explicit_location explicit_loc
;
825 initialize_explicit_location (&explicit_loc
);
826 explicit_loc
.source_filename
= source
;
827 explicit_loc
.function_name
= function
;
828 explicit_loc
.label_name
= label
;
831 explicit_loc
.line_offset
=
832 linespec_parse_line_offset (line
.get ());
834 explicit_loc
.func_name_match_type
= func_name_match_type
;
836 location
= new_explicit_location (&explicit_loc
);
839 create_breakpoint (python_gdbarch
,
840 location
.get (), NULL
, -1, NULL
,
842 temporary_bp
, bp_breakpoint
,
845 &bkpt_breakpoint_ops
,
846 0, 1, internal_bp
, 0);
851 gdb::unique_xmalloc_ptr
<char>
852 copy_holder (xstrdup (skip_spaces (spec
)));
853 char *copy
= copy_holder
.get ();
855 if (access_type
== hw_write
)
856 watch_command_wrapper (copy
, 0, internal_bp
);
857 else if (access_type
== hw_access
)
858 awatch_command_wrapper (copy
, 0, internal_bp
);
859 else if (access_type
== hw_read
)
860 rwatch_command_wrapper (copy
, 0, internal_bp
);
862 error(_("Cannot understand watchpoint access type."));
866 error(_("Do not understand breakpoint type to set."));
869 CATCH (except
, RETURN_MASK_ALL
)
871 bppy_pending_object
= NULL
;
872 PyErr_Format (except
.reason
== RETURN_QUIT
873 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
874 "%s", except
.message
);
879 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object
*) self
);
886 build_bp_list (struct breakpoint
*b
, void *arg
)
888 PyObject
*list
= (PyObject
*) arg
;
889 PyObject
*bp
= (PyObject
*) b
->py_bp_object
;
892 /* Not all breakpoints will have a companion Python object.
893 Only breakpoints that were created via bppy_new, or
894 breakpoints that were created externally and are tracked by
895 the Python Scripting API. */
897 iserr
= PyList_Append (list
, bp
);
905 /* Static function to return a tuple holding all breakpoints. */
908 gdbpy_breakpoints (PyObject
*self
, PyObject
*args
)
911 return PyTuple_New (0);
913 gdbpy_ref
<> list (PyList_New (0));
917 /* If iterate_over_breakpoints returns non NULL it signals an error
918 condition. In that case abandon building the list and return
920 if (iterate_over_breakpoints (build_bp_list
, list
.get ()) != NULL
)
923 return PyList_AsTuple (list
.get ());
926 /* Call the "stop" method (if implemented) in the breakpoint
927 class. If the method returns True, the inferior will be
928 stopped at the breakpoint. Otherwise the inferior will be
929 allowed to continue. */
931 enum ext_lang_bp_stop
932 gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn
*extlang
,
933 struct breakpoint
*b
)
936 struct gdbpy_breakpoint_object
*bp_obj
= b
->py_bp_object
;
937 PyObject
*py_bp
= (PyObject
*) bp_obj
;
938 struct gdbarch
*garch
;
941 return EXT_LANG_BP_STOP_UNSET
;
944 garch
= b
->gdbarch
? b
->gdbarch
: get_current_arch ();
946 gdbpy_enter
enter_py (garch
, current_language
);
948 if (bp_obj
->is_finish_bp
)
949 bpfinishpy_pre_stop_hook (bp_obj
);
951 if (PyObject_HasAttrString (py_bp
, stop_func
))
953 gdbpy_ref
<> result (PyObject_CallMethod (py_bp
, stop_func
, NULL
));
958 int evaluate
= PyObject_IsTrue (result
.get ());
961 gdbpy_print_stack ();
963 /* If the "stop" function returns False that means
964 the Python breakpoint wants GDB to continue. */
969 gdbpy_print_stack ();
972 if (bp_obj
->is_finish_bp
)
973 bpfinishpy_post_stop_hook (bp_obj
);
976 return EXT_LANG_BP_STOP_UNSET
;
977 return stop
? EXT_LANG_BP_STOP_YES
: EXT_LANG_BP_STOP_NO
;
980 /* Checks if the "stop" method exists in this breakpoint.
981 Used by condition_command to ensure mutual exclusion of breakpoint
985 gdbpy_breakpoint_has_cond (const struct extension_language_defn
*extlang
,
986 struct breakpoint
*b
)
989 struct gdbarch
*garch
;
991 if (b
->py_bp_object
== NULL
)
994 py_bp
= (PyObject
*) b
->py_bp_object
;
995 garch
= b
->gdbarch
? b
->gdbarch
: get_current_arch ();
997 gdbpy_enter
enter_py (garch
, current_language
);
998 return PyObject_HasAttrString (py_bp
, stop_func
);
1003 /* Event callback functions. */
1005 /* Callback that is used when a breakpoint is created. This function
1006 will create a new Python breakpoint object. */
1008 gdbpy_breakpoint_created (struct breakpoint
*bp
)
1010 gdbpy_breakpoint_object
*newbp
;
1011 PyGILState_STATE state
;
1013 if (!user_breakpoint_p (bp
) && bppy_pending_object
== NULL
)
1016 if (bp
->type
!= bp_breakpoint
1017 && bp
->type
!= bp_watchpoint
1018 && bp
->type
!= bp_hardware_watchpoint
1019 && bp
->type
!= bp_read_watchpoint
1020 && bp
->type
!= bp_access_watchpoint
)
1023 state
= PyGILState_Ensure ();
1025 if (bppy_pending_object
)
1027 newbp
= bppy_pending_object
;
1028 bppy_pending_object
= NULL
;
1031 newbp
= PyObject_New (gdbpy_breakpoint_object
, &breakpoint_object_type
);
1034 newbp
->number
= bp
->number
;
1036 newbp
->bp
->py_bp_object
= newbp
;
1037 newbp
->is_finish_bp
= 0;
1043 PyErr_SetString (PyExc_RuntimeError
,
1044 _("Error while creating breakpoint from GDB."));
1045 gdbpy_print_stack ();
1048 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_created
))
1050 if (evpy_emit_event ((PyObject
*) newbp
,
1051 gdb_py_events
.breakpoint_created
) < 0)
1052 gdbpy_print_stack ();
1055 PyGILState_Release (state
);
1058 /* Callback that is used when a breakpoint is deleted. This will
1059 invalidate the corresponding Python object. */
1061 gdbpy_breakpoint_deleted (struct breakpoint
*b
)
1063 int num
= b
->number
;
1064 PyGILState_STATE state
;
1065 struct breakpoint
*bp
= NULL
;
1067 state
= PyGILState_Ensure ();
1068 bp
= get_breakpoint (num
);
1071 gdbpy_ref
<gdbpy_breakpoint_object
> bp_obj (bp
->py_bp_object
);
1074 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_deleted
))
1076 if (evpy_emit_event ((PyObject
*) bp_obj
.get (),
1077 gdb_py_events
.breakpoint_deleted
) < 0)
1078 gdbpy_print_stack ();
1085 PyGILState_Release (state
);
1088 /* Callback that is used when a breakpoint is modified. */
1091 gdbpy_breakpoint_modified (struct breakpoint
*b
)
1093 int num
= b
->number
;
1094 PyGILState_STATE state
;
1095 struct breakpoint
*bp
= NULL
;
1097 state
= PyGILState_Ensure ();
1098 bp
= get_breakpoint (num
);
1101 PyObject
*bp_obj
= (PyObject
*) bp
->py_bp_object
;
1104 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_modified
))
1106 if (evpy_emit_event (bp_obj
,
1107 gdb_py_events
.breakpoint_modified
) < 0)
1108 gdbpy_print_stack ();
1112 PyGILState_Release (state
);
1117 /* Initialize the Python breakpoint code. */
1119 gdbpy_initialize_breakpoints (void)
1123 breakpoint_object_type
.tp_new
= PyType_GenericNew
;
1124 if (PyType_Ready (&breakpoint_object_type
) < 0)
1127 if (gdb_pymodule_addobject (gdb_module
, "Breakpoint",
1128 (PyObject
*) &breakpoint_object_type
) < 0)
1131 gdb::observers::breakpoint_created
.attach (gdbpy_breakpoint_created
);
1132 gdb::observers::breakpoint_deleted
.attach (gdbpy_breakpoint_deleted
);
1133 gdb::observers::breakpoint_modified
.attach (gdbpy_breakpoint_modified
);
1135 /* Add breakpoint types constants. */
1136 for (i
= 0; pybp_codes
[i
].name
; ++i
)
1138 if (PyModule_AddIntConstant (gdb_module
,
1139 /* Cast needed for Python 2.4. */
1140 (char *) pybp_codes
[i
].name
,
1141 pybp_codes
[i
].code
) < 0)
1145 /* Add watchpoint types constants. */
1146 for (i
= 0; pybp_watch_types
[i
].name
; ++i
)
1148 if (PyModule_AddIntConstant (gdb_module
,
1149 /* Cast needed for Python 2.4. */
1150 (char *) pybp_watch_types
[i
].name
,
1151 pybp_watch_types
[i
].code
) < 0)
1160 /* Helper function that overrides this Python object's
1161 PyObject_GenericSetAttr to allow extra validation of the attribute
1165 local_setattro (PyObject
*self
, PyObject
*name
, PyObject
*v
)
1167 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
1168 gdb::unique_xmalloc_ptr
<char> attr (python_string_to_host_string (name
));
1173 /* If the attribute trying to be set is the "stop" method,
1174 but we already have a condition set in the CLI or other extension
1175 language, disallow this operation. */
1176 if (strcmp (attr
.get (), stop_func
) == 0)
1178 const struct extension_language_defn
*extlang
= NULL
;
1180 if (obj
->bp
->cond_string
!= NULL
)
1181 extlang
= get_ext_lang_defn (EXT_LANG_GDB
);
1182 if (extlang
== NULL
)
1183 extlang
= get_breakpoint_cond_ext_lang (obj
->bp
, EXT_LANG_PYTHON
);
1184 if (extlang
!= NULL
)
1186 std::string error_text
1187 = string_printf (_("Only one stop condition allowed. There is"
1188 " currently a %s stop condition defined for"
1189 " this breakpoint."),
1190 ext_lang_capitalized_name (extlang
));
1191 PyErr_SetString (PyExc_RuntimeError
, error_text
.c_str ());
1196 return PyObject_GenericSetAttr ((PyObject
*)self
, name
, v
);
1199 static gdb_PyGetSetDef breakpoint_object_getset
[] = {
1200 { "enabled", bppy_get_enabled
, bppy_set_enabled
,
1201 "Boolean telling whether the breakpoint is enabled.", NULL
},
1202 { "silent", bppy_get_silent
, bppy_set_silent
,
1203 "Boolean telling whether the breakpoint is silent.", NULL
},
1204 { "thread", bppy_get_thread
, bppy_set_thread
,
1205 "Thread ID for the breakpoint.\n\
1206 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1207 If the value is None, then this breakpoint is not thread-specific.\n\
1208 No other type of value can be used.", NULL
},
1209 { "task", bppy_get_task
, bppy_set_task
,
1210 "Thread ID for the breakpoint.\n\
1211 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1212 If the value is None, then this breakpoint is not task-specific.\n\
1213 No other type of value can be used.", NULL
},
1214 { "ignore_count", bppy_get_ignore_count
, bppy_set_ignore_count
,
1215 "Number of times this breakpoint should be automatically continued.",
1217 { "number", bppy_get_number
, NULL
,
1218 "Breakpoint's number assigned by GDB.", NULL
},
1219 { "hit_count", bppy_get_hit_count
, bppy_set_hit_count
,
1220 "Number of times the breakpoint has been hit.\n\
1221 Can be set to zero to clear the count. No other value is valid\n\
1222 when setting this property.", NULL
},
1223 { "location", bppy_get_location
, NULL
,
1224 "Location of the breakpoint, as specified by the user.", NULL
},
1225 { "expression", bppy_get_expression
, NULL
,
1226 "Expression of the breakpoint, as specified by the user.", NULL
},
1227 { "condition", bppy_get_condition
, bppy_set_condition
,
1228 "Condition of the breakpoint, as specified by the user,\
1229 or None if no condition set."},
1230 { "commands", bppy_get_commands
, bppy_set_commands
,
1231 "Commands of the breakpoint, as specified by the user."},
1232 { "type", bppy_get_type
, NULL
,
1233 "Type of breakpoint."},
1234 { "visible", bppy_get_visibility
, NULL
,
1235 "Whether the breakpoint is visible to the user."},
1236 { "temporary", bppy_get_temporary
, NULL
,
1237 "Whether this breakpoint is a temporary breakpoint."},
1238 { "pending", bppy_get_pending
, NULL
,
1239 "Whether this breakpoint is a pending breakpoint."},
1240 { NULL
} /* Sentinel. */
1243 static PyMethodDef breakpoint_object_methods
[] =
1245 { "is_valid", bppy_is_valid
, METH_NOARGS
,
1246 "Return true if this breakpoint is valid, false if not." },
1247 { "delete", bppy_delete_breakpoint
, METH_NOARGS
,
1248 "Delete the underlying GDB breakpoint." },
1249 { NULL
} /* Sentinel. */
1252 PyTypeObject breakpoint_object_type
=
1254 PyVarObject_HEAD_INIT (NULL
, 0)
1255 "gdb.Breakpoint", /*tp_name*/
1256 sizeof (gdbpy_breakpoint_object
), /*tp_basicsize*/
1265 0, /*tp_as_sequence*/
1266 0, /*tp_as_mapping*/
1271 (setattrofunc
)local_setattro
, /*tp_setattro */
1273 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1274 "GDB breakpoint object", /* tp_doc */
1275 0, /* tp_traverse */
1277 0, /* tp_richcompare */
1278 0, /* tp_weaklistoffset */
1280 0, /* tp_iternext */
1281 breakpoint_object_methods
, /* tp_methods */
1283 breakpoint_object_getset
, /* tp_getset */
1286 0, /* tp_descr_get */
1287 0, /* tp_descr_set */
1288 0, /* tp_dictoffset */
1289 bppy_init
, /* tp_init */