1 /* Python interface to breakpoints
3 Copyright (C) 2008, 2009, 2010 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 /* From breakpoint.c. */
33 typedef struct breakpoint_object breakpoint_object
;
35 static PyTypeObject breakpoint_object_type
;
37 /* Number of live breakpoints. */
40 /* Variables used to pass information between the Breakpoint
41 constructor and the breakpoint-created hook function. */
42 static breakpoint_object
*bppy_pending_object
;
44 struct breakpoint_object
48 /* The breakpoint number according to gdb. */
51 /* The gdb breakpoint object, or NULL if the breakpoint has been
53 struct breakpoint
*bp
;
56 /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
57 exception if it is invalid. */
58 #define BPPY_REQUIRE_VALID(Breakpoint) \
60 if ((Breakpoint)->bp == NULL) \
61 return PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \
62 (Breakpoint)->number); \
65 /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
66 exception if it is invalid. This macro is for use in setter functions. */
67 #define BPPY_SET_REQUIRE_VALID(Breakpoint) \
69 if ((Breakpoint)->bp == NULL) \
71 PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \
72 (Breakpoint)->number); \
77 /* This is used to initialize various gdb.bp_* constants. */
86 /* Entries related to the type of user set breakpoints. */
87 static struct pybp_code pybp_codes
[] =
89 { "BP_NONE", bp_none
},
90 { "BP_BREAKPOINT", bp_breakpoint
},
91 { "BP_WATCHPOINT", bp_watchpoint
},
92 { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint
},
93 { "BP_READ_WATCHPOINT", bp_read_watchpoint
},
94 { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint
},
95 {NULL
} /* Sentinel. */
98 /* Entries related to the type of watchpoint. */
99 static struct pybp_code pybp_watch_types
[] =
101 { "WP_READ", hw_read
},
102 { "WP_WRITE", hw_write
},
103 { "WP_ACCESS", hw_access
},
104 {NULL
} /* Sentinel. */
107 /* Python function which checks the validity of a breakpoint object. */
109 bppy_is_valid (PyObject
*self
, PyObject
*args
)
111 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
118 /* Python function to test whether or not the breakpoint is enabled. */
120 bppy_get_enabled (PyObject
*self
, void *closure
)
122 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
124 BPPY_REQUIRE_VALID (self_bp
);
127 if (self_bp
->bp
->enable_state
== bp_enabled
)
132 /* Python function to test whether or not the breakpoint is silent. */
134 bppy_get_silent (PyObject
*self
, void *closure
)
136 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
138 BPPY_REQUIRE_VALID (self_bp
);
139 if (self_bp
->bp
->silent
)
144 /* Python function to set the enabled state of a breakpoint. */
146 bppy_set_enabled (PyObject
*self
, PyObject
*newvalue
, void *closure
)
148 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
151 BPPY_SET_REQUIRE_VALID (self_bp
);
153 if (newvalue
== NULL
)
155 PyErr_SetString (PyExc_TypeError
,
156 _("Cannot delete `enabled' attribute."));
160 else if (! PyBool_Check (newvalue
))
162 PyErr_SetString (PyExc_TypeError
,
163 _("The value of `enabled' must be a boolean."));
167 cmp
= PyObject_IsTrue (newvalue
);
171 enable_breakpoint (self_bp
->bp
);
173 disable_breakpoint (self_bp
->bp
);
177 /* Python function to set the 'silent' state of a breakpoint. */
179 bppy_set_silent (PyObject
*self
, PyObject
*newvalue
, void *closure
)
181 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
184 BPPY_SET_REQUIRE_VALID (self_bp
);
186 if (newvalue
== NULL
)
188 PyErr_SetString (PyExc_TypeError
,
189 _("Cannot delete `silent' attribute."));
192 else if (! PyBool_Check (newvalue
))
194 PyErr_SetString (PyExc_TypeError
,
195 _("The value of `silent' must be a boolean."));
199 cmp
= PyObject_IsTrue (newvalue
);
203 self_bp
->bp
->silent
= cmp
;
208 /* Python function to set the thread of a breakpoint. */
210 bppy_set_thread (PyObject
*self
, PyObject
*newvalue
, void *closure
)
212 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
215 BPPY_SET_REQUIRE_VALID (self_bp
);
217 if (newvalue
== NULL
)
219 PyErr_SetString (PyExc_TypeError
,
220 _("Cannot delete `thread' attribute."));
223 else if (PyInt_Check (newvalue
))
225 id
= (int) PyInt_AsLong (newvalue
);
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 self_bp
->bp
->thread
= 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 id
= (int) PyInt_AsLong (newvalue
);
265 if (! valid_task_id (id
))
267 PyErr_SetString (PyExc_RuntimeError
,
268 _("Invalid task ID."));
272 else if (newvalue
== Py_None
)
276 PyErr_SetString (PyExc_TypeError
,
277 _("The value of `task' must be an integer or None."));
281 self_bp
->bp
->task
= id
;
287 /* Python function to set the ignore count of a breakpoint. */
289 bppy_set_ignore_count (PyObject
*self
, PyObject
*newvalue
, void *closure
)
291 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
294 BPPY_SET_REQUIRE_VALID (self_bp
);
296 if (newvalue
== NULL
)
298 PyErr_SetString (PyExc_TypeError
,
299 _("Cannot delete `ignore_count' attribute."));
302 else if (! PyInt_Check (newvalue
))
304 PyErr_SetString (PyExc_TypeError
,
305 _("The value of `ignore_count' must be an integer."));
309 value
= PyInt_AsLong (newvalue
);
312 set_ignore_count (self_bp
->number
, (int) value
, 0);
317 /* Python function to set the hit count of a breakpoint. */
319 bppy_set_hit_count (PyObject
*self
, PyObject
*newvalue
, void *closure
)
321 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
323 BPPY_SET_REQUIRE_VALID (self_bp
);
325 if (newvalue
== NULL
)
327 PyErr_SetString (PyExc_TypeError
,
328 _("Cannot delete `hit_count' attribute."));
331 else if (! PyInt_Check (newvalue
) || PyInt_AsLong (newvalue
) != 0)
333 PyErr_SetString (PyExc_AttributeError
,
334 _("The value of `hit_count' must be zero."));
338 self_bp
->bp
->hit_count
= 0;
343 /* Python function to get the location of a breakpoint. */
345 bppy_get_location (PyObject
*self
, void *closure
)
348 breakpoint_object
*obj
= (breakpoint_object
*) self
;
350 BPPY_REQUIRE_VALID (obj
);
352 if (obj
->bp
->type
!= bp_breakpoint
)
355 str
= obj
->bp
->addr_string
;
359 return PyString_Decode (str
, strlen (str
), host_charset (), NULL
);
362 /* Python function to get the breakpoint expression. */
364 bppy_get_expression (PyObject
*self
, void *closure
)
367 breakpoint_object
*obj
= (breakpoint_object
*) self
;
369 BPPY_REQUIRE_VALID (obj
);
371 if (obj
->bp
->type
!= bp_watchpoint
372 && obj
->bp
->type
!= bp_hardware_watchpoint
373 && obj
->bp
->type
!= bp_read_watchpoint
374 && obj
->bp
->type
!= bp_access_watchpoint
)
377 str
= obj
->bp
->exp_string
;
381 return PyString_Decode (str
, strlen (str
), host_charset (), NULL
);
384 /* Python function to get the condition expression of a breakpoint. */
386 bppy_get_condition (PyObject
*self
, void *closure
)
389 breakpoint_object
*obj
= (breakpoint_object
*) self
;
391 BPPY_REQUIRE_VALID (obj
);
393 str
= obj
->bp
->cond_string
;
397 return PyString_Decode (str
, strlen (str
), host_charset (), NULL
);
400 /* Returns 0 on success. Returns -1 on error, with a python exception set.
404 bppy_set_condition (PyObject
*self
, PyObject
*newvalue
, void *closure
)
407 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
408 volatile struct gdb_exception except
;
410 BPPY_SET_REQUIRE_VALID (self_bp
);
412 if (newvalue
== NULL
)
414 PyErr_SetString (PyExc_TypeError
,
415 _("Cannot delete `condition' attribute."));
418 else if (newvalue
== Py_None
)
422 exp
= python_string_to_host_string (newvalue
);
427 TRY_CATCH (except
, RETURN_MASK_ALL
)
429 set_breakpoint_condition (self_bp
->bp
, exp
, 0);
431 GDB_PY_SET_HANDLE_EXCEPTION (except
);
436 /* Python function to get the commands attached to a breakpoint. */
438 bppy_get_commands (PyObject
*self
, void *closure
)
440 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
441 struct breakpoint
*bp
= self_bp
->bp
;
443 volatile struct gdb_exception except
;
444 struct ui_file
*string_file
;
445 struct cleanup
*chain
;
449 BPPY_REQUIRE_VALID (self_bp
);
451 if (! self_bp
->bp
->commands
)
454 string_file
= mem_fileopen ();
455 chain
= make_cleanup_ui_file_delete (string_file
);
457 ui_out_redirect (uiout
, string_file
);
458 TRY_CATCH (except
, RETURN_MASK_ALL
)
460 print_command_lines (uiout
, breakpoint_commands (bp
), 0);
462 ui_out_redirect (uiout
, NULL
);
463 cmdstr
= ui_file_xstrdup (string_file
, &length
);
464 GDB_PY_HANDLE_EXCEPTION (except
);
466 result
= PyString_Decode (cmdstr
, strlen (cmdstr
), host_charset (), NULL
);
472 /* Python function to get the breakpoint type. */
474 bppy_get_type (PyObject
*self
, void *closure
)
476 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
478 BPPY_REQUIRE_VALID (self_bp
);
480 return PyInt_FromLong (self_bp
->bp
->type
);
483 /* Python function to get the visibility of the breakpoint. */
486 bppy_get_visibility (PyObject
*self
, void *closure
)
488 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
490 BPPY_REQUIRE_VALID (self_bp
);
492 if (self_bp
->bp
->number
< 0)
498 /* Python function to get the breakpoint's number. */
500 bppy_get_number (PyObject
*self
, void *closure
)
502 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
504 BPPY_REQUIRE_VALID (self_bp
);
506 return PyInt_FromLong (self_bp
->number
);
509 /* Python function to get the breakpoint's thread ID. */
511 bppy_get_thread (PyObject
*self
, void *closure
)
513 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
515 BPPY_REQUIRE_VALID (self_bp
);
517 if (self_bp
->bp
->thread
== -1)
520 return PyInt_FromLong (self_bp
->bp
->thread
);
523 /* Python function to get the breakpoint's task ID (in Ada). */
525 bppy_get_task (PyObject
*self
, void *closure
)
527 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
529 BPPY_REQUIRE_VALID (self_bp
);
531 if (self_bp
->bp
->task
== 0)
534 return PyInt_FromLong (self_bp
->bp
->task
);
537 /* Python function to get the breakpoint's hit count. */
539 bppy_get_hit_count (PyObject
*self
, void *closure
)
541 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
543 BPPY_REQUIRE_VALID (self_bp
);
545 return PyInt_FromLong (self_bp
->bp
->hit_count
);
548 /* Python function to get the breakpoint's ignore count. */
550 bppy_get_ignore_count (PyObject
*self
, void *closure
)
552 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
554 BPPY_REQUIRE_VALID (self_bp
);
556 return PyInt_FromLong (self_bp
->bp
->ignore_count
);
559 /* Python function to create a new breakpoint. */
561 bppy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*kwargs
)
564 static char *keywords
[] = { "spec", "type", "wp_class", "internal", NULL
};
566 int type
= bp_breakpoint
;
567 int access_type
= hw_write
;
568 PyObject
*internal
= NULL
;
570 volatile struct gdb_exception except
;
572 if (! PyArg_ParseTupleAndKeywords (args
, kwargs
, "s|iiO", keywords
,
573 &spec
, &type
, &access_type
, &internal
))
578 internal_bp
= PyObject_IsTrue (internal
);
579 if (internal_bp
== -1)
583 result
= subtype
->tp_alloc (subtype
, 0);
586 bppy_pending_object
= (breakpoint_object
*) result
;
587 bppy_pending_object
->number
= -1;
588 bppy_pending_object
->bp
= NULL
;
590 TRY_CATCH (except
, RETURN_MASK_ALL
)
596 create_breakpoint (python_gdbarch
,
602 NULL
, 0, 1, internal_bp
);
607 if (access_type
== hw_write
)
608 watch_command_wrapper (spec
, 0, internal_bp
);
609 else if (access_type
== hw_access
)
610 awatch_command_wrapper (spec
, 0, internal_bp
);
611 else if (access_type
== hw_read
)
612 rwatch_command_wrapper (spec
, 0, internal_bp
);
614 error(_("Cannot understand watchpoint access type."));
618 error(_("Do not understand breakpoint type to set."));
621 if (except
.reason
< 0)
623 subtype
->tp_free (result
);
624 return PyErr_Format (except
.reason
== RETURN_QUIT
625 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
626 "%s", except
.message
);
629 BPPY_REQUIRE_VALID ((breakpoint_object
*) result
);
636 build_bp_list (struct breakpoint
*b
, void *arg
)
638 PyObject
*list
= arg
;
639 PyObject
*bp
= (PyObject
*) b
->py_bp_object
;
642 /* Not all breakpoints will have a companion Python object.
643 Only breakpoints that were created via bppy_new, or
644 breakpoints that were created externally and are tracked by
645 the Python Scripting API. */
647 iserr
= PyList_Append (list
, bp
);
655 /* Static function to return a tuple holding all breakpoints. */
658 gdbpy_breakpoints (PyObject
*self
, PyObject
*args
)
665 list
= PyList_New (0);
669 /* If iteratre_over_breakpoints returns non NULL it signals an error
670 condition. In that case abandon building the list and return
672 if (iterate_over_breakpoints (build_bp_list
, list
) != NULL
)
678 return PyList_AsTuple (list
);
683 /* Event callback functions. */
685 /* Callback that is used when a breakpoint is created. This function
686 will create a new Python breakpoint object. */
688 gdbpy_breakpoint_created (int num
)
690 breakpoint_object
*newbp
;
691 struct breakpoint
*bp
= NULL
;
692 PyGILState_STATE state
;
694 bp
= get_breakpoint (num
);
698 if (num
< 0 && bppy_pending_object
== NULL
)
701 if (bp
->type
!= bp_breakpoint
702 && bp
->type
!= bp_watchpoint
703 && bp
->type
!= bp_hardware_watchpoint
704 && bp
->type
!= bp_read_watchpoint
705 && bp
->type
!= bp_access_watchpoint
)
708 state
= PyGILState_Ensure ();
710 if (bppy_pending_object
)
712 newbp
= bppy_pending_object
;
713 bppy_pending_object
= NULL
;
716 newbp
= PyObject_New (breakpoint_object
, &breakpoint_object_type
);
721 newbp
->bp
->py_bp_object
= newbp
;
727 PyErr_SetString (PyExc_RuntimeError
,
728 _("Error while creating breakpoint from GDB."));
729 gdbpy_print_stack ();
732 PyGILState_Release (state
);
735 /* Callback that is used when a breakpoint is deleted. This will
736 invalidate the corresponding Python object. */
738 gdbpy_breakpoint_deleted (int num
)
740 PyGILState_STATE state
;
741 struct breakpoint
*bp
= NULL
;
742 breakpoint_object
*bp_obj
;
744 state
= PyGILState_Ensure ();
745 bp
= get_breakpoint (num
);
749 bp_obj
= bp
->py_bp_object
;
756 PyGILState_Release (state
);
761 /* Initialize the Python breakpoint code. */
763 gdbpy_initialize_breakpoints (void)
767 breakpoint_object_type
.tp_new
= bppy_new
;
768 if (PyType_Ready (&breakpoint_object_type
) < 0)
771 Py_INCREF (&breakpoint_object_type
);
772 PyModule_AddObject (gdb_module
, "Breakpoint",
773 (PyObject
*) &breakpoint_object_type
);
775 observer_attach_breakpoint_created (gdbpy_breakpoint_created
);
776 observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted
);
778 /* Add breakpoint types constants. */
779 for (i
= 0; pybp_codes
[i
].name
; ++i
)
781 if (PyModule_AddIntConstant (gdb_module
,
782 /* Cast needed for Python 2.4. */
783 (char *) pybp_codes
[i
].name
,
784 pybp_codes
[i
].code
) < 0)
788 /* Add watchpoint types constants. */
789 for (i
= 0; pybp_watch_types
[i
].name
; ++i
)
791 if (PyModule_AddIntConstant (gdb_module
,
792 /* Cast needed for Python 2.4. */
793 (char *) pybp_watch_types
[i
].name
,
794 pybp_watch_types
[i
].code
) < 0)
802 static PyGetSetDef breakpoint_object_getset
[] = {
803 { "enabled", bppy_get_enabled
, bppy_set_enabled
,
804 "Boolean telling whether the breakpoint is enabled.", NULL
},
805 { "silent", bppy_get_silent
, bppy_set_silent
,
806 "Boolean telling whether the breakpoint is silent.", NULL
},
807 { "thread", bppy_get_thread
, bppy_set_thread
,
808 "Thread ID for the breakpoint.\n\
809 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
810 If the value is None, then this breakpoint is not thread-specific.\n\
811 No other type of value can be used.", NULL
},
812 { "task", bppy_get_task
, bppy_set_task
,
813 "Thread ID for the breakpoint.\n\
814 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
815 If the value is None, then this breakpoint is not task-specific.\n\
816 No other type of value can be used.", NULL
},
817 { "ignore_count", bppy_get_ignore_count
, bppy_set_ignore_count
,
818 "Number of times this breakpoint should be automatically continued.",
820 { "number", bppy_get_number
, NULL
,
821 "Breakpoint's number assigned by GDB.", NULL
},
822 { "hit_count", bppy_get_hit_count
, bppy_set_hit_count
,
823 "Number of times the breakpoint has been hit.\n\
824 Can be set to zero to clear the count. No other value is valid\n\
825 when setting this property.", NULL
},
826 { "location", bppy_get_location
, NULL
,
827 "Location of the breakpoint, as specified by the user.", NULL
},
828 { "expression", bppy_get_expression
, NULL
,
829 "Expression of the breakpoint, as specified by the user.", NULL
},
830 { "condition", bppy_get_condition
, bppy_set_condition
,
831 "Condition of the breakpoint, as specified by the user,\
832 or None if no condition set."},
833 { "commands", bppy_get_commands
, NULL
,
834 "Commands of the breakpoint, as specified by the user."},
835 { "type", bppy_get_type
, NULL
,
836 "Type of breakpoint."},
837 { "visible", bppy_get_visibility
, NULL
,
838 "Whether the breakpoint is visible to the user."},
839 { NULL
} /* Sentinel. */
842 static PyMethodDef breakpoint_object_methods
[] =
844 { "is_valid", bppy_is_valid
, METH_NOARGS
,
845 "Return true if this breakpoint is valid, false if not." },
846 { NULL
} /* Sentinel. */
849 static PyTypeObject breakpoint_object_type
=
851 PyObject_HEAD_INIT (NULL
)
853 "gdb.Breakpoint", /*tp_name*/
854 sizeof (breakpoint_object
), /*tp_basicsize*/
863 0, /*tp_as_sequence*/
871 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
872 "GDB breakpoint object", /* tp_doc */
875 0, /* tp_richcompare */
876 0, /* tp_weaklistoffset */
879 breakpoint_object_methods
, /* tp_methods */
881 breakpoint_object_getset
/* tp_getset */