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 (const gdb_exception
&except
)
150 GDB_PY_SET_HANDLE_EXCEPTION (except
);
156 /* Python function to set the 'silent' state of a breakpoint. */
158 bppy_set_silent (PyObject
*self
, PyObject
*newvalue
, void *closure
)
160 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
163 BPPY_SET_REQUIRE_VALID (self_bp
);
165 if (newvalue
== NULL
)
167 PyErr_SetString (PyExc_TypeError
,
168 _("Cannot delete `silent' attribute."));
171 else if (! PyBool_Check (newvalue
))
173 PyErr_SetString (PyExc_TypeError
,
174 _("The value of `silent' must be a boolean."));
178 cmp
= PyObject_IsTrue (newvalue
);
182 breakpoint_set_silent (self_bp
->bp
, cmp
);
187 /* Python function to set the thread of a breakpoint. */
189 bppy_set_thread (PyObject
*self
, PyObject
*newvalue
, void *closure
)
191 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
194 BPPY_SET_REQUIRE_VALID (self_bp
);
196 if (newvalue
== NULL
)
198 PyErr_SetString (PyExc_TypeError
,
199 _("Cannot delete `thread' attribute."));
202 else if (PyInt_Check (newvalue
))
204 if (! gdb_py_int_as_long (newvalue
, &id
))
207 if (!valid_global_thread_id (id
))
209 PyErr_SetString (PyExc_RuntimeError
,
210 _("Invalid thread ID."));
214 else if (newvalue
== Py_None
)
218 PyErr_SetString (PyExc_TypeError
,
219 _("The value of `thread' must be an integer or None."));
223 breakpoint_set_thread (self_bp
->bp
, id
);
228 /* Python function to set the (Ada) task of a breakpoint. */
230 bppy_set_task (PyObject
*self
, PyObject
*newvalue
, void *closure
)
232 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
236 BPPY_SET_REQUIRE_VALID (self_bp
);
238 if (newvalue
== NULL
)
240 PyErr_SetString (PyExc_TypeError
,
241 _("Cannot delete `task' attribute."));
244 else if (PyInt_Check (newvalue
))
246 if (! gdb_py_int_as_long (newvalue
, &id
))
251 valid_id
= valid_task_id (id
);
253 catch (const gdb_exception
&except
)
255 GDB_PY_SET_HANDLE_EXCEPTION (except
);
260 PyErr_SetString (PyExc_RuntimeError
,
261 _("Invalid task ID."));
265 else if (newvalue
== Py_None
)
269 PyErr_SetString (PyExc_TypeError
,
270 _("The value of `task' must be an integer or None."));
274 breakpoint_set_task (self_bp
->bp
, id
);
279 /* Python function which deletes the underlying GDB breakpoint. This
280 triggers the breakpoint_deleted observer which will call
281 gdbpy_breakpoint_deleted; that function cleans up the Python
285 bppy_delete_breakpoint (PyObject
*self
, PyObject
*args
)
287 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
289 BPPY_REQUIRE_VALID (self_bp
);
293 delete_breakpoint (self_bp
->bp
);
295 catch (const gdb_exception
&except
)
297 GDB_PY_HANDLE_EXCEPTION (except
);
304 /* Python function to set the ignore count of a breakpoint. */
306 bppy_set_ignore_count (PyObject
*self
, PyObject
*newvalue
, void *closure
)
308 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
311 BPPY_SET_REQUIRE_VALID (self_bp
);
313 if (newvalue
== NULL
)
315 PyErr_SetString (PyExc_TypeError
,
316 _("Cannot delete `ignore_count' attribute."));
319 else if (! PyInt_Check (newvalue
))
321 PyErr_SetString (PyExc_TypeError
,
322 _("The value of `ignore_count' must be an integer."));
326 if (! gdb_py_int_as_long (newvalue
, &value
))
334 set_ignore_count (self_bp
->number
, (int) value
, 0);
336 catch (const gdb_exception
&except
)
338 GDB_PY_SET_HANDLE_EXCEPTION (except
);
344 /* Python function to set the hit count of a breakpoint. */
346 bppy_set_hit_count (PyObject
*self
, PyObject
*newvalue
, void *closure
)
348 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
350 BPPY_SET_REQUIRE_VALID (self_bp
);
352 if (newvalue
== NULL
)
354 PyErr_SetString (PyExc_TypeError
,
355 _("Cannot delete `hit_count' attribute."));
362 if (! gdb_py_int_as_long (newvalue
, &value
))
367 PyErr_SetString (PyExc_AttributeError
,
368 _("The value of `hit_count' must be zero."));
373 self_bp
->bp
->hit_count
= 0;
378 /* Python function to get the location of a breakpoint. */
380 bppy_get_location (PyObject
*self
, void *closure
)
383 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
385 BPPY_REQUIRE_VALID (obj
);
387 if (obj
->bp
->type
!= bp_breakpoint
)
390 struct event_location
*location
= obj
->bp
->location
.get ();
391 /* "catch throw" makes a breakpoint of type bp_breakpoint that does
392 not have a location. */
393 if (location
== nullptr)
395 str
= event_location_to_string (location
);
398 return host_string_to_python_string (str
).release ();
401 /* Python function to get the breakpoint expression. */
403 bppy_get_expression (PyObject
*self
, void *closure
)
406 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
407 struct watchpoint
*wp
;
409 BPPY_REQUIRE_VALID (obj
);
411 if (!is_watchpoint (obj
->bp
))
414 wp
= (struct watchpoint
*) obj
->bp
;
416 str
= wp
->exp_string
;
420 return host_string_to_python_string (str
).release ();
423 /* Python function to get the condition expression of a breakpoint. */
425 bppy_get_condition (PyObject
*self
, void *closure
)
428 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
430 BPPY_REQUIRE_VALID (obj
);
432 str
= obj
->bp
->cond_string
;
436 return host_string_to_python_string (str
).release ();
439 /* Returns 0 on success. Returns -1 on error, with a python exception set.
443 bppy_set_condition (PyObject
*self
, PyObject
*newvalue
, void *closure
)
445 gdb::unique_xmalloc_ptr
<char> exp_holder
;
446 const char *exp
= NULL
;
447 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
448 struct gdb_exception except
= exception_none
;
450 BPPY_SET_REQUIRE_VALID (self_bp
);
452 if (newvalue
== NULL
)
454 PyErr_SetString (PyExc_TypeError
,
455 _("Cannot delete `condition' attribute."));
458 else if (newvalue
== Py_None
)
462 exp_holder
= python_string_to_host_string (newvalue
);
463 if (exp_holder
== NULL
)
465 exp
= exp_holder
.get ();
470 set_breakpoint_condition (self_bp
->bp
, exp
, 0);
472 catch (const gdb_exception
&ex
)
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 (const gdb_exception
&except
)
503 current_uiout
->redirect (NULL
);
504 gdbpy_convert_exception (except
);
508 current_uiout
->redirect (NULL
);
509 return host_string_to_python_string (stb
.c_str ()).release ();
512 /* Set the commands attached to a breakpoint. Returns 0 on success.
513 Returns -1 on error, with a python exception set. */
515 bppy_set_commands (PyObject
*self
, PyObject
*newvalue
, void *closure
)
517 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
518 struct gdb_exception except
= exception_none
;
520 BPPY_SET_REQUIRE_VALID (self_bp
);
522 gdb::unique_xmalloc_ptr
<char> commands
523 (python_string_to_host_string (newvalue
));
524 if (commands
== nullptr)
530 char *save_ptr
= nullptr;
534 const char *result
= strtok_r (first
? commands
.get () : nullptr,
540 counted_command_line lines
= read_command_lines_1 (reader
, 1, nullptr);
541 breakpoint_set_commands (self_bp
->bp
, std::move (lines
));
543 catch (const gdb_exception
&ex
)
548 GDB_PY_SET_HANDLE_EXCEPTION (except
);
553 /* Python function to get the breakpoint type. */
555 bppy_get_type (PyObject
*self
, void *closure
)
557 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
559 BPPY_REQUIRE_VALID (self_bp
);
561 return PyInt_FromLong (self_bp
->bp
->type
);
564 /* Python function to get the visibility of the breakpoint. */
567 bppy_get_visibility (PyObject
*self
, void *closure
)
569 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
571 BPPY_REQUIRE_VALID (self_bp
);
573 if (user_breakpoint_p (self_bp
->bp
))
579 /* Python function to determine if the breakpoint is a temporary
583 bppy_get_temporary (PyObject
*self
, void *closure
)
585 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
587 BPPY_REQUIRE_VALID (self_bp
);
589 if (self_bp
->bp
->disposition
== disp_del
590 || self_bp
->bp
->disposition
== disp_del_at_next_stop
)
596 /* Python function to determine if the breakpoint is a pending
600 bppy_get_pending (PyObject
*self
, void *closure
)
602 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
604 BPPY_REQUIRE_VALID (self_bp
);
606 if (is_watchpoint (self_bp
->bp
))
608 if (pending_breakpoint_p (self_bp
->bp
))
614 /* Python function to get the breakpoint's number. */
616 bppy_get_number (PyObject
*self
, void *closure
)
618 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
620 BPPY_REQUIRE_VALID (self_bp
);
622 return PyInt_FromLong (self_bp
->number
);
625 /* Python function to get the breakpoint's thread ID. */
627 bppy_get_thread (PyObject
*self
, void *closure
)
629 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
631 BPPY_REQUIRE_VALID (self_bp
);
633 if (self_bp
->bp
->thread
== -1)
636 return PyInt_FromLong (self_bp
->bp
->thread
);
639 /* Python function to get the breakpoint's task ID (in Ada). */
641 bppy_get_task (PyObject
*self
, void *closure
)
643 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
645 BPPY_REQUIRE_VALID (self_bp
);
647 if (self_bp
->bp
->task
== 0)
650 return PyInt_FromLong (self_bp
->bp
->task
);
653 /* Python function to get the breakpoint's hit count. */
655 bppy_get_hit_count (PyObject
*self
, void *closure
)
657 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
659 BPPY_REQUIRE_VALID (self_bp
);
661 return PyInt_FromLong (self_bp
->bp
->hit_count
);
664 /* Python function to get the breakpoint's ignore count. */
666 bppy_get_ignore_count (PyObject
*self
, void *closure
)
668 gdbpy_breakpoint_object
*self_bp
= (gdbpy_breakpoint_object
*) self
;
670 BPPY_REQUIRE_VALID (self_bp
);
672 return PyInt_FromLong (self_bp
->bp
->ignore_count
);
675 /* Internal function to validate the Python parameters/keywords
676 provided to bppy_init. */
679 bppy_init_validate_args (const char *spec
, char *source
,
680 char *function
, char *label
,
681 char *line
, enum bptype type
)
683 /* If spec is defined, ensure that none of the explicit location
684 keywords are also defined. */
687 if (source
!= NULL
|| function
!= NULL
|| label
!= NULL
|| line
!= NULL
)
689 PyErr_SetString (PyExc_RuntimeError
,
690 _("Breakpoints specified with spec cannot "
691 "have source, function, label or line defined."));
697 /* If spec isn't defined, ensure that the user is not trying to
698 define a watchpoint with an explicit location. */
699 if (type
== bp_watchpoint
)
701 PyErr_SetString (PyExc_RuntimeError
,
702 _("Watchpoints cannot be set by explicit "
703 "location parameters."));
708 /* Otherwise, ensure some explicit locations are defined. */
709 if (source
== NULL
&& function
== NULL
&& label
== NULL
712 PyErr_SetString (PyExc_RuntimeError
,
713 _("Neither spec nor explicit location set."));
716 /* Finally, if source is specified, ensure that line, label
717 or function are specified too. */
718 if (source
!= NULL
&& function
== NULL
&& label
== NULL
721 PyErr_SetString (PyExc_RuntimeError
,
722 _("Specifying a source must also include a "
723 "line, label or function."));
731 /* Python function to create a new breakpoint. */
733 bppy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
735 static const char *keywords
[] = { "spec", "type", "wp_class", "internal",
736 "temporary","source", "function",
737 "label", "line", "qualified", NULL
};
738 const char *spec
= NULL
;
739 enum bptype type
= bp_breakpoint
;
740 int access_type
= hw_write
;
741 PyObject
*internal
= NULL
;
742 PyObject
*temporary
= NULL
;
743 PyObject
*lineobj
= NULL
;;
745 int temporary_bp
= 0;
746 gdb::unique_xmalloc_ptr
<char> line
;
749 char *function
= NULL
;
750 PyObject
* qualified
= NULL
;
752 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kwargs
, "|siiOOsssOO", keywords
,
753 &spec
, &type
, &access_type
,
756 &function
, &label
, &lineobj
,
763 if (PyInt_Check (lineobj
))
764 line
.reset (xstrprintf ("%ld", PyInt_AsLong (lineobj
)));
765 else if (PyString_Check (lineobj
))
766 line
= python_string_to_host_string (lineobj
);
769 PyErr_SetString (PyExc_RuntimeError
,
770 _("Line keyword should be an integer or a string. "));
777 internal_bp
= PyObject_IsTrue (internal
);
778 if (internal_bp
== -1)
782 if (temporary
!= NULL
)
784 temporary_bp
= PyObject_IsTrue (temporary
);
785 if (temporary_bp
== -1)
789 if (bppy_init_validate_args (spec
, source
, function
, label
, line
.get (),
793 bppy_pending_object
= (gdbpy_breakpoint_object
*) self
;
794 bppy_pending_object
->number
= -1;
795 bppy_pending_object
->bp
= NULL
;
803 event_location_up location
;
804 symbol_name_match_type func_name_match_type
805 = (qualified
!= NULL
&& PyObject_IsTrue (qualified
)
806 ? symbol_name_match_type::FULL
807 : symbol_name_match_type::WILD
);
811 gdb::unique_xmalloc_ptr
<char>
812 copy_holder (xstrdup (skip_spaces (spec
)));
813 const char *copy
= copy_holder
.get ();
815 location
= string_to_event_location (©
,
817 func_name_match_type
);
821 struct explicit_location explicit_loc
;
823 initialize_explicit_location (&explicit_loc
);
824 explicit_loc
.source_filename
= source
;
825 explicit_loc
.function_name
= function
;
826 explicit_loc
.label_name
= label
;
829 explicit_loc
.line_offset
=
830 linespec_parse_line_offset (line
.get ());
832 explicit_loc
.func_name_match_type
= func_name_match_type
;
834 location
= new_explicit_location (&explicit_loc
);
837 create_breakpoint (python_gdbarch
,
838 location
.get (), NULL
, -1, NULL
,
840 temporary_bp
, bp_breakpoint
,
843 &bkpt_breakpoint_ops
,
844 0, 1, internal_bp
, 0);
849 gdb::unique_xmalloc_ptr
<char>
850 copy_holder (xstrdup (skip_spaces (spec
)));
851 char *copy
= copy_holder
.get ();
853 if (access_type
== hw_write
)
854 watch_command_wrapper (copy
, 0, internal_bp
);
855 else if (access_type
== hw_access
)
856 awatch_command_wrapper (copy
, 0, internal_bp
);
857 else if (access_type
== hw_read
)
858 rwatch_command_wrapper (copy
, 0, internal_bp
);
860 error(_("Cannot understand watchpoint access type."));
864 error(_("Do not understand breakpoint type to set."));
867 catch (const gdb_exception
&except
)
869 bppy_pending_object
= NULL
;
870 gdbpy_convert_exception (except
);
874 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object
*) self
);
881 build_bp_list (struct breakpoint
*b
, void *arg
)
883 PyObject
*list
= (PyObject
*) arg
;
884 PyObject
*bp
= (PyObject
*) b
->py_bp_object
;
887 /* Not all breakpoints will have a companion Python object.
888 Only breakpoints that were created via bppy_new, or
889 breakpoints that were created externally and are tracked by
890 the Python Scripting API. */
892 iserr
= PyList_Append (list
, bp
);
900 /* Static function to return a tuple holding all breakpoints. */
903 gdbpy_breakpoints (PyObject
*self
, PyObject
*args
)
906 return PyTuple_New (0);
908 gdbpy_ref
<> list (PyList_New (0));
912 /* If iterate_over_breakpoints returns non NULL it signals an error
913 condition. In that case abandon building the list and return
915 if (iterate_over_breakpoints (build_bp_list
, list
.get ()) != NULL
)
918 return PyList_AsTuple (list
.get ());
921 /* Call the "stop" method (if implemented) in the breakpoint
922 class. If the method returns True, the inferior will be
923 stopped at the breakpoint. Otherwise the inferior will be
924 allowed to continue. */
926 enum ext_lang_bp_stop
927 gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn
*extlang
,
928 struct breakpoint
*b
)
931 struct gdbpy_breakpoint_object
*bp_obj
= b
->py_bp_object
;
932 PyObject
*py_bp
= (PyObject
*) bp_obj
;
933 struct gdbarch
*garch
;
936 return EXT_LANG_BP_STOP_UNSET
;
939 garch
= b
->gdbarch
? b
->gdbarch
: get_current_arch ();
941 gdbpy_enter
enter_py (garch
, current_language
);
943 if (bp_obj
->is_finish_bp
)
944 bpfinishpy_pre_stop_hook (bp_obj
);
946 if (PyObject_HasAttrString (py_bp
, stop_func
))
948 gdbpy_ref
<> result (PyObject_CallMethod (py_bp
, stop_func
, NULL
));
953 int evaluate
= PyObject_IsTrue (result
.get ());
956 gdbpy_print_stack ();
958 /* If the "stop" function returns False that means
959 the Python breakpoint wants GDB to continue. */
964 gdbpy_print_stack ();
967 if (bp_obj
->is_finish_bp
)
968 bpfinishpy_post_stop_hook (bp_obj
);
971 return EXT_LANG_BP_STOP_UNSET
;
972 return stop
? EXT_LANG_BP_STOP_YES
: EXT_LANG_BP_STOP_NO
;
975 /* Checks if the "stop" method exists in this breakpoint.
976 Used by condition_command to ensure mutual exclusion of breakpoint
980 gdbpy_breakpoint_has_cond (const struct extension_language_defn
*extlang
,
981 struct breakpoint
*b
)
984 struct gdbarch
*garch
;
986 if (b
->py_bp_object
== NULL
)
989 py_bp
= (PyObject
*) b
->py_bp_object
;
990 garch
= b
->gdbarch
? b
->gdbarch
: get_current_arch ();
992 gdbpy_enter
enter_py (garch
, current_language
);
993 return PyObject_HasAttrString (py_bp
, stop_func
);
998 /* Event callback functions. */
1000 /* Callback that is used when a breakpoint is created. This function
1001 will create a new Python breakpoint object. */
1003 gdbpy_breakpoint_created (struct breakpoint
*bp
)
1005 gdbpy_breakpoint_object
*newbp
;
1006 PyGILState_STATE state
;
1008 if (!user_breakpoint_p (bp
) && bppy_pending_object
== NULL
)
1011 if (bp
->type
!= bp_breakpoint
1012 && bp
->type
!= bp_watchpoint
1013 && bp
->type
!= bp_hardware_watchpoint
1014 && bp
->type
!= bp_read_watchpoint
1015 && bp
->type
!= bp_access_watchpoint
)
1018 state
= PyGILState_Ensure ();
1020 if (bppy_pending_object
)
1022 newbp
= bppy_pending_object
;
1023 bppy_pending_object
= NULL
;
1026 newbp
= PyObject_New (gdbpy_breakpoint_object
, &breakpoint_object_type
);
1029 newbp
->number
= bp
->number
;
1031 newbp
->bp
->py_bp_object
= newbp
;
1032 newbp
->is_finish_bp
= 0;
1038 PyErr_SetString (PyExc_RuntimeError
,
1039 _("Error while creating breakpoint from GDB."));
1040 gdbpy_print_stack ();
1043 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_created
))
1045 if (evpy_emit_event ((PyObject
*) newbp
,
1046 gdb_py_events
.breakpoint_created
) < 0)
1047 gdbpy_print_stack ();
1050 PyGILState_Release (state
);
1053 /* Callback that is used when a breakpoint is deleted. This will
1054 invalidate the corresponding Python object. */
1056 gdbpy_breakpoint_deleted (struct breakpoint
*b
)
1058 int num
= b
->number
;
1059 PyGILState_STATE state
;
1060 struct breakpoint
*bp
= NULL
;
1062 state
= PyGILState_Ensure ();
1063 bp
= get_breakpoint (num
);
1066 gdbpy_ref
<gdbpy_breakpoint_object
> bp_obj (bp
->py_bp_object
);
1069 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_deleted
))
1071 if (evpy_emit_event ((PyObject
*) bp_obj
.get (),
1072 gdb_py_events
.breakpoint_deleted
) < 0)
1073 gdbpy_print_stack ();
1080 PyGILState_Release (state
);
1083 /* Callback that is used when a breakpoint is modified. */
1086 gdbpy_breakpoint_modified (struct breakpoint
*b
)
1088 int num
= b
->number
;
1089 PyGILState_STATE state
;
1090 struct breakpoint
*bp
= NULL
;
1092 state
= PyGILState_Ensure ();
1093 bp
= get_breakpoint (num
);
1096 PyObject
*bp_obj
= (PyObject
*) bp
->py_bp_object
;
1099 if (!evregpy_no_listeners_p (gdb_py_events
.breakpoint_modified
))
1101 if (evpy_emit_event (bp_obj
,
1102 gdb_py_events
.breakpoint_modified
) < 0)
1103 gdbpy_print_stack ();
1107 PyGILState_Release (state
);
1112 /* Initialize the Python breakpoint code. */
1114 gdbpy_initialize_breakpoints (void)
1118 breakpoint_object_type
.tp_new
= PyType_GenericNew
;
1119 if (PyType_Ready (&breakpoint_object_type
) < 0)
1122 if (gdb_pymodule_addobject (gdb_module
, "Breakpoint",
1123 (PyObject
*) &breakpoint_object_type
) < 0)
1126 gdb::observers::breakpoint_created
.attach (gdbpy_breakpoint_created
);
1127 gdb::observers::breakpoint_deleted
.attach (gdbpy_breakpoint_deleted
);
1128 gdb::observers::breakpoint_modified
.attach (gdbpy_breakpoint_modified
);
1130 /* Add breakpoint types constants. */
1131 for (i
= 0; pybp_codes
[i
].name
; ++i
)
1133 if (PyModule_AddIntConstant (gdb_module
, pybp_codes
[i
].name
,
1134 pybp_codes
[i
].code
) < 0)
1138 /* Add watchpoint types constants. */
1139 for (i
= 0; pybp_watch_types
[i
].name
; ++i
)
1141 if (PyModule_AddIntConstant (gdb_module
, pybp_watch_types
[i
].name
,
1142 pybp_watch_types
[i
].code
) < 0)
1151 /* Helper function that overrides this Python object's
1152 PyObject_GenericSetAttr to allow extra validation of the attribute
1156 local_setattro (PyObject
*self
, PyObject
*name
, PyObject
*v
)
1158 gdbpy_breakpoint_object
*obj
= (gdbpy_breakpoint_object
*) self
;
1159 gdb::unique_xmalloc_ptr
<char> attr (python_string_to_host_string (name
));
1164 /* If the attribute trying to be set is the "stop" method,
1165 but we already have a condition set in the CLI or other extension
1166 language, disallow this operation. */
1167 if (strcmp (attr
.get (), stop_func
) == 0)
1169 const struct extension_language_defn
*extlang
= NULL
;
1171 if (obj
->bp
->cond_string
!= NULL
)
1172 extlang
= get_ext_lang_defn (EXT_LANG_GDB
);
1173 if (extlang
== NULL
)
1174 extlang
= get_breakpoint_cond_ext_lang (obj
->bp
, EXT_LANG_PYTHON
);
1175 if (extlang
!= NULL
)
1177 std::string error_text
1178 = string_printf (_("Only one stop condition allowed. There is"
1179 " currently a %s stop condition defined for"
1180 " this breakpoint."),
1181 ext_lang_capitalized_name (extlang
));
1182 PyErr_SetString (PyExc_RuntimeError
, error_text
.c_str ());
1187 return PyObject_GenericSetAttr (self
, name
, v
);
1190 static gdb_PyGetSetDef breakpoint_object_getset
[] = {
1191 { "enabled", bppy_get_enabled
, bppy_set_enabled
,
1192 "Boolean telling whether the breakpoint is enabled.", NULL
},
1193 { "silent", bppy_get_silent
, bppy_set_silent
,
1194 "Boolean telling whether the breakpoint is silent.", NULL
},
1195 { "thread", bppy_get_thread
, bppy_set_thread
,
1196 "Thread ID for the breakpoint.\n\
1197 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1198 If the value is None, then this breakpoint is not thread-specific.\n\
1199 No other type of value can be used.", NULL
},
1200 { "task", bppy_get_task
, bppy_set_task
,
1201 "Thread ID for the breakpoint.\n\
1202 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1203 If the value is None, then this breakpoint is not task-specific.\n\
1204 No other type of value can be used.", NULL
},
1205 { "ignore_count", bppy_get_ignore_count
, bppy_set_ignore_count
,
1206 "Number of times this breakpoint should be automatically continued.",
1208 { "number", bppy_get_number
, NULL
,
1209 "Breakpoint's number assigned by GDB.", NULL
},
1210 { "hit_count", bppy_get_hit_count
, bppy_set_hit_count
,
1211 "Number of times the breakpoint has been hit.\n\
1212 Can be set to zero to clear the count. No other value is valid\n\
1213 when setting this property.", NULL
},
1214 { "location", bppy_get_location
, NULL
,
1215 "Location of the breakpoint, as specified by the user.", NULL
},
1216 { "expression", bppy_get_expression
, NULL
,
1217 "Expression of the breakpoint, as specified by the user.", NULL
},
1218 { "condition", bppy_get_condition
, bppy_set_condition
,
1219 "Condition of the breakpoint, as specified by the user,\
1220 or None if no condition set."},
1221 { "commands", bppy_get_commands
, bppy_set_commands
,
1222 "Commands of the breakpoint, as specified by the user."},
1223 { "type", bppy_get_type
, NULL
,
1224 "Type of breakpoint."},
1225 { "visible", bppy_get_visibility
, NULL
,
1226 "Whether the breakpoint is visible to the user."},
1227 { "temporary", bppy_get_temporary
, NULL
,
1228 "Whether this breakpoint is a temporary breakpoint."},
1229 { "pending", bppy_get_pending
, NULL
,
1230 "Whether this breakpoint is a pending breakpoint."},
1231 { NULL
} /* Sentinel. */
1234 static PyMethodDef breakpoint_object_methods
[] =
1236 { "is_valid", bppy_is_valid
, METH_NOARGS
,
1237 "Return true if this breakpoint is valid, false if not." },
1238 { "delete", bppy_delete_breakpoint
, METH_NOARGS
,
1239 "Delete the underlying GDB breakpoint." },
1240 { NULL
} /* Sentinel. */
1243 PyTypeObject breakpoint_object_type
=
1245 PyVarObject_HEAD_INIT (NULL
, 0)
1246 "gdb.Breakpoint", /*tp_name*/
1247 sizeof (gdbpy_breakpoint_object
), /*tp_basicsize*/
1256 0, /*tp_as_sequence*/
1257 0, /*tp_as_mapping*/
1262 (setattrofunc
)local_setattro
, /*tp_setattro */
1264 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1265 "GDB breakpoint object", /* tp_doc */
1266 0, /* tp_traverse */
1268 0, /* tp_richcompare */
1269 0, /* tp_weaklistoffset */
1271 0, /* tp_iternext */
1272 breakpoint_object_methods
, /* tp_methods */
1274 breakpoint_object_getset
, /* tp_getset */
1277 0, /* tp_descr_get */
1278 0, /* tp_descr_set */
1279 0, /* tp_dictoffset */
1280 bppy_init
, /* tp_init */