1 /* Python interface to breakpoints
3 Copyright (C) 2008, 2009, 2010, 2011 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 "exceptions.h"
23 #include "python-internal.h"
25 #include "breakpoint.h"
27 #include "gdbthread.h"
29 #include "cli/cli-script.h"
32 static PyTypeObject breakpoint_object_type
;
34 /* Number of live breakpoints. */
37 /* Variables used to pass information between the Breakpoint
38 constructor and the breakpoint-created hook function. */
39 static breakpoint_object
*bppy_pending_object
;
41 struct breakpoint_object
45 /* The breakpoint number according to gdb. */
48 /* The gdb breakpoint object, or NULL if the breakpoint has been
50 struct breakpoint
*bp
;
53 /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
54 exception if it is invalid. */
55 #define BPPY_REQUIRE_VALID(Breakpoint) \
57 if ((Breakpoint)->bp == NULL) \
58 return PyErr_Format (PyExc_RuntimeError, \
59 _("Breakpoint %d is invalid."), \
60 (Breakpoint)->number); \
63 /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
64 exception if it is invalid. This macro is for use in setter functions. */
65 #define BPPY_SET_REQUIRE_VALID(Breakpoint) \
67 if ((Breakpoint)->bp == NULL) \
69 PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \
70 (Breakpoint)->number); \
75 /* This is used to initialize various gdb.bp_* constants. */
84 /* Entries related to the type of user set breakpoints. */
85 static struct pybp_code pybp_codes
[] =
87 { "BP_NONE", bp_none
},
88 { "BP_BREAKPOINT", bp_breakpoint
},
89 { "BP_WATCHPOINT", bp_watchpoint
},
90 { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint
},
91 { "BP_READ_WATCHPOINT", bp_read_watchpoint
},
92 { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint
},
93 {NULL
} /* Sentinel. */
96 /* Entries related to the type of watchpoint. */
97 static struct pybp_code pybp_watch_types
[] =
99 { "WP_READ", hw_read
},
100 { "WP_WRITE", hw_write
},
101 { "WP_ACCESS", hw_access
},
102 {NULL
} /* Sentinel. */
105 /* Python function which checks the validity of a breakpoint object. */
107 bppy_is_valid (PyObject
*self
, PyObject
*args
)
109 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
116 /* Python function to test whether or not the breakpoint is enabled. */
118 bppy_get_enabled (PyObject
*self
, void *closure
)
120 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
122 BPPY_REQUIRE_VALID (self_bp
);
125 if (self_bp
->bp
->enable_state
== bp_enabled
)
130 /* Python function to test whether or not the breakpoint is silent. */
132 bppy_get_silent (PyObject
*self
, void *closure
)
134 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
136 BPPY_REQUIRE_VALID (self_bp
);
137 if (self_bp
->bp
->silent
)
142 /* Python function to set the enabled state of a breakpoint. */
144 bppy_set_enabled (PyObject
*self
, PyObject
*newvalue
, void *closure
)
146 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
149 BPPY_SET_REQUIRE_VALID (self_bp
);
151 if (newvalue
== NULL
)
153 PyErr_SetString (PyExc_TypeError
,
154 _("Cannot delete `enabled' attribute."));
158 else if (! PyBool_Check (newvalue
))
160 PyErr_SetString (PyExc_TypeError
,
161 _("The value of `enabled' must be a boolean."));
165 cmp
= PyObject_IsTrue (newvalue
);
169 enable_breakpoint (self_bp
->bp
);
171 disable_breakpoint (self_bp
->bp
);
175 /* Python function to set the 'silent' state of a breakpoint. */
177 bppy_set_silent (PyObject
*self
, PyObject
*newvalue
, void *closure
)
179 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
182 BPPY_SET_REQUIRE_VALID (self_bp
);
184 if (newvalue
== NULL
)
186 PyErr_SetString (PyExc_TypeError
,
187 _("Cannot delete `silent' attribute."));
190 else if (! PyBool_Check (newvalue
))
192 PyErr_SetString (PyExc_TypeError
,
193 _("The value of `silent' must be a boolean."));
197 cmp
= PyObject_IsTrue (newvalue
);
201 breakpoint_set_silent (self_bp
->bp
, cmp
);
206 /* Python function to set the thread of a breakpoint. */
208 bppy_set_thread (PyObject
*self
, PyObject
*newvalue
, void *closure
)
210 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
213 BPPY_SET_REQUIRE_VALID (self_bp
);
215 if (newvalue
== NULL
)
217 PyErr_SetString (PyExc_TypeError
,
218 _("Cannot delete `thread' attribute."));
221 else if (PyInt_Check (newvalue
))
223 if (! gdb_py_int_as_long (newvalue
, &id
))
226 if (! valid_thread_id (id
))
228 PyErr_SetString (PyExc_RuntimeError
,
229 _("Invalid thread ID."));
233 else if (newvalue
== Py_None
)
237 PyErr_SetString (PyExc_TypeError
,
238 _("The value of `thread' must be an integer or None."));
242 breakpoint_set_thread (self_bp
->bp
, id
);
247 /* Python function to set the (Ada) task of a breakpoint. */
249 bppy_set_task (PyObject
*self
, PyObject
*newvalue
, void *closure
)
251 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
254 BPPY_SET_REQUIRE_VALID (self_bp
);
256 if (newvalue
== NULL
)
258 PyErr_SetString (PyExc_TypeError
,
259 _("Cannot delete `task' attribute."));
262 else if (PyInt_Check (newvalue
))
264 if (! gdb_py_int_as_long (newvalue
, &id
))
267 if (! valid_task_id (id
))
269 PyErr_SetString (PyExc_RuntimeError
,
270 _("Invalid task ID."));
274 else if (newvalue
== Py_None
)
278 PyErr_SetString (PyExc_TypeError
,
279 _("The value of `task' must be an integer or None."));
283 breakpoint_set_task (self_bp
->bp
, id
);
288 /* Python function which deletes the underlying GDB breakpoint. This
289 triggers the breakpoint_deleted observer which will call
290 gdbpy_breakpoint_deleted; that function cleans up the Python
294 bppy_delete_breakpoint (PyObject
*self
, PyObject
*args
)
296 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
298 BPPY_REQUIRE_VALID (self_bp
);
300 delete_breakpoint (self_bp
->bp
);
306 /* Python function to set the ignore count of a breakpoint. */
308 bppy_set_ignore_count (PyObject
*self
, PyObject
*newvalue
, void *closure
)
310 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
313 BPPY_SET_REQUIRE_VALID (self_bp
);
315 if (newvalue
== NULL
)
317 PyErr_SetString (PyExc_TypeError
,
318 _("Cannot delete `ignore_count' attribute."));
321 else if (! PyInt_Check (newvalue
))
323 PyErr_SetString (PyExc_TypeError
,
324 _("The value of `ignore_count' must be an integer."));
328 if (! gdb_py_int_as_long (newvalue
, &value
))
333 set_ignore_count (self_bp
->number
, (int) value
, 0);
338 /* Python function to set the hit count of a breakpoint. */
340 bppy_set_hit_count (PyObject
*self
, PyObject
*newvalue
, void *closure
)
342 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
344 BPPY_SET_REQUIRE_VALID (self_bp
);
346 if (newvalue
== NULL
)
348 PyErr_SetString (PyExc_TypeError
,
349 _("Cannot delete `hit_count' attribute."));
356 if (! gdb_py_int_as_long (newvalue
, &value
))
361 PyErr_SetString (PyExc_AttributeError
,
362 _("The value of `hit_count' must be zero."));
367 self_bp
->bp
->hit_count
= 0;
372 /* Python function to get the location of a breakpoint. */
374 bppy_get_location (PyObject
*self
, void *closure
)
377 breakpoint_object
*obj
= (breakpoint_object
*) self
;
379 BPPY_REQUIRE_VALID (obj
);
381 if (obj
->bp
->type
!= bp_breakpoint
)
384 str
= obj
->bp
->addr_string
;
388 return PyString_Decode (str
, strlen (str
), host_charset (), NULL
);
391 /* Python function to get the breakpoint expression. */
393 bppy_get_expression (PyObject
*self
, void *closure
)
396 breakpoint_object
*obj
= (breakpoint_object
*) self
;
398 BPPY_REQUIRE_VALID (obj
);
400 if (obj
->bp
->type
!= bp_watchpoint
401 && obj
->bp
->type
!= bp_hardware_watchpoint
402 && obj
->bp
->type
!= bp_read_watchpoint
403 && obj
->bp
->type
!= bp_access_watchpoint
)
406 str
= obj
->bp
->exp_string
;
410 return PyString_Decode (str
, strlen (str
), host_charset (), NULL
);
413 /* Python function to get the condition expression of a breakpoint. */
415 bppy_get_condition (PyObject
*self
, void *closure
)
418 breakpoint_object
*obj
= (breakpoint_object
*) self
;
420 BPPY_REQUIRE_VALID (obj
);
422 str
= obj
->bp
->cond_string
;
426 return PyString_Decode (str
, strlen (str
), host_charset (), NULL
);
429 /* Returns 0 on success. Returns -1 on error, with a python exception set.
433 bppy_set_condition (PyObject
*self
, PyObject
*newvalue
, void *closure
)
436 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
437 volatile struct gdb_exception except
;
439 BPPY_SET_REQUIRE_VALID (self_bp
);
441 if (newvalue
== NULL
)
443 PyErr_SetString (PyExc_TypeError
,
444 _("Cannot delete `condition' attribute."));
447 else if (newvalue
== Py_None
)
451 exp
= python_string_to_host_string (newvalue
);
456 TRY_CATCH (except
, RETURN_MASK_ALL
)
458 set_breakpoint_condition (self_bp
->bp
, exp
, 0);
460 GDB_PY_SET_HANDLE_EXCEPTION (except
);
465 /* Python function to get the commands attached to a breakpoint. */
467 bppy_get_commands (PyObject
*self
, void *closure
)
469 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
470 struct breakpoint
*bp
= self_bp
->bp
;
472 volatile struct gdb_exception except
;
473 struct ui_file
*string_file
;
474 struct cleanup
*chain
;
478 BPPY_REQUIRE_VALID (self_bp
);
480 if (! self_bp
->bp
->commands
)
483 string_file
= mem_fileopen ();
484 chain
= make_cleanup_ui_file_delete (string_file
);
486 ui_out_redirect (uiout
, string_file
);
487 TRY_CATCH (except
, RETURN_MASK_ALL
)
489 print_command_lines (uiout
, breakpoint_commands (bp
), 0);
491 ui_out_redirect (uiout
, NULL
);
492 cmdstr
= ui_file_xstrdup (string_file
, &length
);
493 GDB_PY_HANDLE_EXCEPTION (except
);
495 result
= PyString_Decode (cmdstr
, strlen (cmdstr
), host_charset (), NULL
);
501 /* Python function to get the breakpoint type. */
503 bppy_get_type (PyObject
*self
, void *closure
)
505 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
507 BPPY_REQUIRE_VALID (self_bp
);
509 return PyInt_FromLong (self_bp
->bp
->type
);
512 /* Python function to get the visibility of the breakpoint. */
515 bppy_get_visibility (PyObject
*self
, void *closure
)
517 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
519 BPPY_REQUIRE_VALID (self_bp
);
521 if (self_bp
->bp
->number
< 0)
527 /* Python function to get the breakpoint's number. */
529 bppy_get_number (PyObject
*self
, void *closure
)
531 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
533 BPPY_REQUIRE_VALID (self_bp
);
535 return PyInt_FromLong (self_bp
->number
);
538 /* Python function to get the breakpoint's thread ID. */
540 bppy_get_thread (PyObject
*self
, void *closure
)
542 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
544 BPPY_REQUIRE_VALID (self_bp
);
546 if (self_bp
->bp
->thread
== -1)
549 return PyInt_FromLong (self_bp
->bp
->thread
);
552 /* Python function to get the breakpoint's task ID (in Ada). */
554 bppy_get_task (PyObject
*self
, void *closure
)
556 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
558 BPPY_REQUIRE_VALID (self_bp
);
560 if (self_bp
->bp
->task
== 0)
563 return PyInt_FromLong (self_bp
->bp
->task
);
566 /* Python function to get the breakpoint's hit count. */
568 bppy_get_hit_count (PyObject
*self
, void *closure
)
570 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
572 BPPY_REQUIRE_VALID (self_bp
);
574 return PyInt_FromLong (self_bp
->bp
->hit_count
);
577 /* Python function to get the breakpoint's ignore count. */
579 bppy_get_ignore_count (PyObject
*self
, void *closure
)
581 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
583 BPPY_REQUIRE_VALID (self_bp
);
585 return PyInt_FromLong (self_bp
->bp
->ignore_count
);
588 /* Python function to create a new breakpoint. */
590 bppy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*kwargs
)
593 static char *keywords
[] = { "spec", "type", "wp_class", "internal", NULL
};
595 int type
= bp_breakpoint
;
596 int access_type
= hw_write
;
597 PyObject
*internal
= NULL
;
599 volatile struct gdb_exception except
;
601 if (! PyArg_ParseTupleAndKeywords (args
, kwargs
, "s|iiO", keywords
,
602 &spec
, &type
, &access_type
, &internal
))
607 internal_bp
= PyObject_IsTrue (internal
);
608 if (internal_bp
== -1)
612 result
= subtype
->tp_alloc (subtype
, 0);
615 bppy_pending_object
= (breakpoint_object
*) result
;
616 bppy_pending_object
->number
= -1;
617 bppy_pending_object
->bp
= NULL
;
619 TRY_CATCH (except
, RETURN_MASK_ALL
)
625 create_breakpoint (python_gdbarch
,
631 NULL
, 0, 1, internal_bp
);
636 if (access_type
== hw_write
)
637 watch_command_wrapper (spec
, 0, internal_bp
);
638 else if (access_type
== hw_access
)
639 awatch_command_wrapper (spec
, 0, internal_bp
);
640 else if (access_type
== hw_read
)
641 rwatch_command_wrapper (spec
, 0, internal_bp
);
643 error(_("Cannot understand watchpoint access type."));
647 error(_("Do not understand breakpoint type to set."));
650 if (except
.reason
< 0)
652 subtype
->tp_free (result
);
653 return PyErr_Format (except
.reason
== RETURN_QUIT
654 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
655 "%s", except
.message
);
658 BPPY_REQUIRE_VALID ((breakpoint_object
*) result
);
665 build_bp_list (struct breakpoint
*b
, void *arg
)
667 PyObject
*list
= arg
;
668 PyObject
*bp
= (PyObject
*) b
->py_bp_object
;
671 /* Not all breakpoints will have a companion Python object.
672 Only breakpoints that were created via bppy_new, or
673 breakpoints that were created externally and are tracked by
674 the Python Scripting API. */
676 iserr
= PyList_Append (list
, bp
);
684 /* Static function to return a tuple holding all breakpoints. */
687 gdbpy_breakpoints (PyObject
*self
, PyObject
*args
)
694 list
= PyList_New (0);
698 /* If iteratre_over_breakpoints returns non NULL it signals an error
699 condition. In that case abandon building the list and return
701 if (iterate_over_breakpoints (build_bp_list
, list
) != NULL
)
707 return PyList_AsTuple (list
);
712 /* Event callback functions. */
714 /* Callback that is used when a breakpoint is created. This function
715 will create a new Python breakpoint object. */
717 gdbpy_breakpoint_created (int num
)
719 breakpoint_object
*newbp
;
720 struct breakpoint
*bp
= NULL
;
721 PyGILState_STATE state
;
723 bp
= get_breakpoint (num
);
727 if (num
< 0 && bppy_pending_object
== NULL
)
730 if (bp
->type
!= bp_breakpoint
731 && bp
->type
!= bp_watchpoint
732 && bp
->type
!= bp_hardware_watchpoint
733 && bp
->type
!= bp_read_watchpoint
734 && bp
->type
!= bp_access_watchpoint
)
737 state
= PyGILState_Ensure ();
739 if (bppy_pending_object
)
741 newbp
= bppy_pending_object
;
742 bppy_pending_object
= NULL
;
745 newbp
= PyObject_New (breakpoint_object
, &breakpoint_object_type
);
750 newbp
->bp
->py_bp_object
= newbp
;
756 PyErr_SetString (PyExc_RuntimeError
,
757 _("Error while creating breakpoint from GDB."));
758 gdbpy_print_stack ();
761 PyGILState_Release (state
);
764 /* Callback that is used when a breakpoint is deleted. This will
765 invalidate the corresponding Python object. */
767 gdbpy_breakpoint_deleted (int num
)
769 PyGILState_STATE state
;
770 struct breakpoint
*bp
= NULL
;
771 breakpoint_object
*bp_obj
;
773 state
= PyGILState_Ensure ();
774 bp
= get_breakpoint (num
);
778 bp_obj
= bp
->py_bp_object
;
785 PyGILState_Release (state
);
790 /* Initialize the Python breakpoint code. */
792 gdbpy_initialize_breakpoints (void)
796 breakpoint_object_type
.tp_new
= bppy_new
;
797 if (PyType_Ready (&breakpoint_object_type
) < 0)
800 Py_INCREF (&breakpoint_object_type
);
801 PyModule_AddObject (gdb_module
, "Breakpoint",
802 (PyObject
*) &breakpoint_object_type
);
804 observer_attach_breakpoint_created (gdbpy_breakpoint_created
);
805 observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted
);
807 /* Add breakpoint types constants. */
808 for (i
= 0; pybp_codes
[i
].name
; ++i
)
810 if (PyModule_AddIntConstant (gdb_module
,
811 /* Cast needed for Python 2.4. */
812 (char *) pybp_codes
[i
].name
,
813 pybp_codes
[i
].code
) < 0)
817 /* Add watchpoint types constants. */
818 for (i
= 0; pybp_watch_types
[i
].name
; ++i
)
820 if (PyModule_AddIntConstant (gdb_module
,
821 /* Cast needed for Python 2.4. */
822 (char *) pybp_watch_types
[i
].name
,
823 pybp_watch_types
[i
].code
) < 0)
831 static PyGetSetDef breakpoint_object_getset
[] = {
832 { "enabled", bppy_get_enabled
, bppy_set_enabled
,
833 "Boolean telling whether the breakpoint is enabled.", NULL
},
834 { "silent", bppy_get_silent
, bppy_set_silent
,
835 "Boolean telling whether the breakpoint is silent.", NULL
},
836 { "thread", bppy_get_thread
, bppy_set_thread
,
837 "Thread ID for the breakpoint.\n\
838 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
839 If the value is None, then this breakpoint is not thread-specific.\n\
840 No other type of value can be used.", NULL
},
841 { "task", bppy_get_task
, bppy_set_task
,
842 "Thread ID for the breakpoint.\n\
843 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
844 If the value is None, then this breakpoint is not task-specific.\n\
845 No other type of value can be used.", NULL
},
846 { "ignore_count", bppy_get_ignore_count
, bppy_set_ignore_count
,
847 "Number of times this breakpoint should be automatically continued.",
849 { "number", bppy_get_number
, NULL
,
850 "Breakpoint's number assigned by GDB.", NULL
},
851 { "hit_count", bppy_get_hit_count
, bppy_set_hit_count
,
852 "Number of times the breakpoint has been hit.\n\
853 Can be set to zero to clear the count. No other value is valid\n\
854 when setting this property.", NULL
},
855 { "location", bppy_get_location
, NULL
,
856 "Location of the breakpoint, as specified by the user.", NULL
},
857 { "expression", bppy_get_expression
, NULL
,
858 "Expression of the breakpoint, as specified by the user.", NULL
},
859 { "condition", bppy_get_condition
, bppy_set_condition
,
860 "Condition of the breakpoint, as specified by the user,\
861 or None if no condition set."},
862 { "commands", bppy_get_commands
, NULL
,
863 "Commands of the breakpoint, as specified by the user."},
864 { "type", bppy_get_type
, NULL
,
865 "Type of breakpoint."},
866 { "visible", bppy_get_visibility
, NULL
,
867 "Whether the breakpoint is visible to the user."},
868 { NULL
} /* Sentinel. */
871 static PyMethodDef breakpoint_object_methods
[] =
873 { "is_valid", bppy_is_valid
, METH_NOARGS
,
874 "Return true if this breakpoint is valid, false if not." },
875 { "delete", bppy_delete_breakpoint
, METH_NOARGS
,
876 "Delete the underlying GDB breakpoint." },
877 { NULL
} /* Sentinel. */
880 static PyTypeObject breakpoint_object_type
=
882 PyObject_HEAD_INIT (NULL
)
884 "gdb.Breakpoint", /*tp_name*/
885 sizeof (breakpoint_object
), /*tp_basicsize*/
894 0, /*tp_as_sequence*/
902 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
903 "GDB breakpoint object", /* tp_doc */
906 0, /* tp_richcompare */
907 0, /* tp_weaklistoffset */
910 breakpoint_object_methods
, /* tp_methods */
912 breakpoint_object_getset
/* tp_getset */