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);
461 if (newvalue
!= Py_None
)
464 GDB_PY_SET_HANDLE_EXCEPTION (except
);
469 /* Python function to get the commands attached to a breakpoint. */
471 bppy_get_commands (PyObject
*self
, void *closure
)
473 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
474 struct breakpoint
*bp
= self_bp
->bp
;
476 volatile struct gdb_exception except
;
477 struct ui_file
*string_file
;
478 struct cleanup
*chain
;
482 BPPY_REQUIRE_VALID (self_bp
);
484 if (! self_bp
->bp
->commands
)
487 string_file
= mem_fileopen ();
488 chain
= make_cleanup_ui_file_delete (string_file
);
490 ui_out_redirect (uiout
, string_file
);
491 TRY_CATCH (except
, RETURN_MASK_ALL
)
493 print_command_lines (uiout
, breakpoint_commands (bp
), 0);
495 ui_out_redirect (uiout
, NULL
);
496 GDB_PY_HANDLE_EXCEPTION (except
);
498 cmdstr
= ui_file_xstrdup (string_file
, &length
);
499 make_cleanup (xfree
, cmdstr
);
500 result
= PyString_Decode (cmdstr
, strlen (cmdstr
), host_charset (), NULL
);
505 /* Python function to get the breakpoint type. */
507 bppy_get_type (PyObject
*self
, void *closure
)
509 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
511 BPPY_REQUIRE_VALID (self_bp
);
513 return PyInt_FromLong (self_bp
->bp
->type
);
516 /* Python function to get the visibility of the breakpoint. */
519 bppy_get_visibility (PyObject
*self
, void *closure
)
521 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
523 BPPY_REQUIRE_VALID (self_bp
);
525 if (self_bp
->bp
->number
< 0)
531 /* Python function to get the breakpoint's number. */
533 bppy_get_number (PyObject
*self
, void *closure
)
535 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
537 BPPY_REQUIRE_VALID (self_bp
);
539 return PyInt_FromLong (self_bp
->number
);
542 /* Python function to get the breakpoint's thread ID. */
544 bppy_get_thread (PyObject
*self
, void *closure
)
546 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
548 BPPY_REQUIRE_VALID (self_bp
);
550 if (self_bp
->bp
->thread
== -1)
553 return PyInt_FromLong (self_bp
->bp
->thread
);
556 /* Python function to get the breakpoint's task ID (in Ada). */
558 bppy_get_task (PyObject
*self
, void *closure
)
560 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
562 BPPY_REQUIRE_VALID (self_bp
);
564 if (self_bp
->bp
->task
== 0)
567 return PyInt_FromLong (self_bp
->bp
->task
);
570 /* Python function to get the breakpoint's hit count. */
572 bppy_get_hit_count (PyObject
*self
, void *closure
)
574 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
576 BPPY_REQUIRE_VALID (self_bp
);
578 return PyInt_FromLong (self_bp
->bp
->hit_count
);
581 /* Python function to get the breakpoint's ignore count. */
583 bppy_get_ignore_count (PyObject
*self
, void *closure
)
585 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
587 BPPY_REQUIRE_VALID (self_bp
);
589 return PyInt_FromLong (self_bp
->bp
->ignore_count
);
592 /* Python function to create a new breakpoint. */
594 bppy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*kwargs
)
597 static char *keywords
[] = { "spec", "type", "wp_class", "internal", NULL
};
599 int type
= bp_breakpoint
;
600 int access_type
= hw_write
;
601 PyObject
*internal
= NULL
;
603 volatile struct gdb_exception except
;
605 if (! PyArg_ParseTupleAndKeywords (args
, kwargs
, "s|iiO", keywords
,
606 &spec
, &type
, &access_type
, &internal
))
611 internal_bp
= PyObject_IsTrue (internal
);
612 if (internal_bp
== -1)
616 result
= subtype
->tp_alloc (subtype
, 0);
619 bppy_pending_object
= (breakpoint_object
*) result
;
620 bppy_pending_object
->number
= -1;
621 bppy_pending_object
->bp
= NULL
;
623 TRY_CATCH (except
, RETURN_MASK_ALL
)
629 create_breakpoint (python_gdbarch
,
635 NULL
, 0, 1, internal_bp
);
640 if (access_type
== hw_write
)
641 watch_command_wrapper (spec
, 0, internal_bp
);
642 else if (access_type
== hw_access
)
643 awatch_command_wrapper (spec
, 0, internal_bp
);
644 else if (access_type
== hw_read
)
645 rwatch_command_wrapper (spec
, 0, internal_bp
);
647 error(_("Cannot understand watchpoint access type."));
651 error(_("Do not understand breakpoint type to set."));
654 if (except
.reason
< 0)
656 subtype
->tp_free (result
);
657 return PyErr_Format (except
.reason
== RETURN_QUIT
658 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
659 "%s", except
.message
);
662 BPPY_REQUIRE_VALID ((breakpoint_object
*) result
);
669 build_bp_list (struct breakpoint
*b
, void *arg
)
671 PyObject
*list
= arg
;
672 PyObject
*bp
= (PyObject
*) b
->py_bp_object
;
675 /* Not all breakpoints will have a companion Python object.
676 Only breakpoints that were created via bppy_new, or
677 breakpoints that were created externally and are tracked by
678 the Python Scripting API. */
680 iserr
= PyList_Append (list
, bp
);
688 /* Static function to return a tuple holding all breakpoints. */
691 gdbpy_breakpoints (PyObject
*self
, PyObject
*args
)
698 list
= PyList_New (0);
702 /* If iteratre_over_breakpoints returns non NULL it signals an error
703 condition. In that case abandon building the list and return
705 if (iterate_over_breakpoints (build_bp_list
, list
) != NULL
)
711 return PyList_AsTuple (list
);
716 /* Event callback functions. */
718 /* Callback that is used when a breakpoint is created. This function
719 will create a new Python breakpoint object. */
721 gdbpy_breakpoint_created (int num
)
723 breakpoint_object
*newbp
;
724 struct breakpoint
*bp
= NULL
;
725 PyGILState_STATE state
;
727 bp
= get_breakpoint (num
);
731 if (num
< 0 && bppy_pending_object
== NULL
)
734 if (bp
->type
!= bp_breakpoint
735 && bp
->type
!= bp_watchpoint
736 && bp
->type
!= bp_hardware_watchpoint
737 && bp
->type
!= bp_read_watchpoint
738 && bp
->type
!= bp_access_watchpoint
)
741 state
= PyGILState_Ensure ();
743 if (bppy_pending_object
)
745 newbp
= bppy_pending_object
;
746 bppy_pending_object
= NULL
;
749 newbp
= PyObject_New (breakpoint_object
, &breakpoint_object_type
);
754 newbp
->bp
->py_bp_object
= newbp
;
760 PyErr_SetString (PyExc_RuntimeError
,
761 _("Error while creating breakpoint from GDB."));
762 gdbpy_print_stack ();
765 PyGILState_Release (state
);
768 /* Callback that is used when a breakpoint is deleted. This will
769 invalidate the corresponding Python object. */
771 gdbpy_breakpoint_deleted (int num
)
773 PyGILState_STATE state
;
774 struct breakpoint
*bp
= NULL
;
775 breakpoint_object
*bp_obj
;
777 state
= PyGILState_Ensure ();
778 bp
= get_breakpoint (num
);
782 bp_obj
= bp
->py_bp_object
;
789 PyGILState_Release (state
);
794 /* Initialize the Python breakpoint code. */
796 gdbpy_initialize_breakpoints (void)
800 breakpoint_object_type
.tp_new
= bppy_new
;
801 if (PyType_Ready (&breakpoint_object_type
) < 0)
804 Py_INCREF (&breakpoint_object_type
);
805 PyModule_AddObject (gdb_module
, "Breakpoint",
806 (PyObject
*) &breakpoint_object_type
);
808 observer_attach_breakpoint_created (gdbpy_breakpoint_created
);
809 observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted
);
811 /* Add breakpoint types constants. */
812 for (i
= 0; pybp_codes
[i
].name
; ++i
)
814 if (PyModule_AddIntConstant (gdb_module
,
815 /* Cast needed for Python 2.4. */
816 (char *) pybp_codes
[i
].name
,
817 pybp_codes
[i
].code
) < 0)
821 /* Add watchpoint types constants. */
822 for (i
= 0; pybp_watch_types
[i
].name
; ++i
)
824 if (PyModule_AddIntConstant (gdb_module
,
825 /* Cast needed for Python 2.4. */
826 (char *) pybp_watch_types
[i
].name
,
827 pybp_watch_types
[i
].code
) < 0)
835 static PyGetSetDef breakpoint_object_getset
[] = {
836 { "enabled", bppy_get_enabled
, bppy_set_enabled
,
837 "Boolean telling whether the breakpoint is enabled.", NULL
},
838 { "silent", bppy_get_silent
, bppy_set_silent
,
839 "Boolean telling whether the breakpoint is silent.", NULL
},
840 { "thread", bppy_get_thread
, bppy_set_thread
,
841 "Thread ID for the breakpoint.\n\
842 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
843 If the value is None, then this breakpoint is not thread-specific.\n\
844 No other type of value can be used.", NULL
},
845 { "task", bppy_get_task
, bppy_set_task
,
846 "Thread ID for the breakpoint.\n\
847 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
848 If the value is None, then this breakpoint is not task-specific.\n\
849 No other type of value can be used.", NULL
},
850 { "ignore_count", bppy_get_ignore_count
, bppy_set_ignore_count
,
851 "Number of times this breakpoint should be automatically continued.",
853 { "number", bppy_get_number
, NULL
,
854 "Breakpoint's number assigned by GDB.", NULL
},
855 { "hit_count", bppy_get_hit_count
, bppy_set_hit_count
,
856 "Number of times the breakpoint has been hit.\n\
857 Can be set to zero to clear the count. No other value is valid\n\
858 when setting this property.", NULL
},
859 { "location", bppy_get_location
, NULL
,
860 "Location of the breakpoint, as specified by the user.", NULL
},
861 { "expression", bppy_get_expression
, NULL
,
862 "Expression of the breakpoint, as specified by the user.", NULL
},
863 { "condition", bppy_get_condition
, bppy_set_condition
,
864 "Condition of the breakpoint, as specified by the user,\
865 or None if no condition set."},
866 { "commands", bppy_get_commands
, NULL
,
867 "Commands of the breakpoint, as specified by the user."},
868 { "type", bppy_get_type
, NULL
,
869 "Type of breakpoint."},
870 { "visible", bppy_get_visibility
, NULL
,
871 "Whether the breakpoint is visible to the user."},
872 { NULL
} /* Sentinel. */
875 static PyMethodDef breakpoint_object_methods
[] =
877 { "is_valid", bppy_is_valid
, METH_NOARGS
,
878 "Return true if this breakpoint is valid, false if not." },
879 { "delete", bppy_delete_breakpoint
, METH_NOARGS
,
880 "Delete the underlying GDB breakpoint." },
881 { NULL
} /* Sentinel. */
884 static PyTypeObject breakpoint_object_type
=
886 PyObject_HEAD_INIT (NULL
)
888 "gdb.Breakpoint", /*tp_name*/
889 sizeof (breakpoint_object
), /*tp_basicsize*/
898 0, /*tp_as_sequence*/
906 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
907 "GDB breakpoint object", /* tp_doc */
910 0, /* tp_richcompare */
911 0, /* tp_weaklistoffset */
914 breakpoint_object_methods
, /* tp_methods */
916 breakpoint_object_getset
/* tp_getset */