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 /* A dynamically allocated vector of breakpoint objects. Each
38 breakpoint has a number. A breakpoint is valid if its slot in this
39 vector is non-null. When a breakpoint is deleted, we drop our
40 reference to it and zero its slot; this is how we let the Python
41 object have a lifetime which is independent from that of the gdb
43 static breakpoint_object **bppy_breakpoints;
45 /* Number of slots in bppy_breakpoints. */
46 static int bppy_slots;
48 /* Number of live breakpoints. */
51 /* Variables used to pass information between the Breakpoint
52 constructor and the breakpoint-created hook function. */
53 static breakpoint_object *bppy_pending_object;
55 struct breakpoint_object
59 /* The breakpoint number according to gdb. */
62 /* The gdb breakpoint object, or NULL if the breakpoint has been
64 struct breakpoint *bp;
67 /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
68 exception if it is invalid. */
69 #define BPPY_REQUIRE_VALID(Breakpoint) \
71 if (! bpnum_is_valid ((Breakpoint)->number)) \
72 return PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \
73 (Breakpoint)->number); \
76 /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
77 exception if it is invalid. This macro is for use in setter functions. */
78 #define BPPY_SET_REQUIRE_VALID(Breakpoint) \
80 if (! bpnum_is_valid ((Breakpoint)->number)) \
82 PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \
83 (Breakpoint)->number); \
88 /* This is used to initialize various gdb.bp_* constants. */
97 /* Entries related to the type of user set breakpoints. */
98 static struct pybp_code pybp_codes[] =
100 { "BP_NONE", bp_none},
101 { "BP_BREAKPOINT", bp_breakpoint},
102 { "BP_WATCHPOINT", bp_watchpoint},
103 { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint},
104 { "BP_READ_WATCHPOINT", bp_read_watchpoint},
105 { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint},
106 {NULL} /* Sentinel. */
109 /* Entries related to the type of watchpoint. */
110 static struct pybp_code pybp_watch_types[] =
112 { "WP_READ", hw_read},
113 { "WP_WRITE", hw_write},
114 { "WP_ACCESS", hw_access},
115 {NULL} /* Sentinel. */
118 /* Evaluate to true if the breakpoint NUM is valid, false otherwise. */
120 bpnum_is_valid (int num)
124 && bppy_breakpoints[num] != NULL)
130 /* Python function which checks the validity of a breakpoint object. */
132 bppy_is_valid (PyObject *self, PyObject *args)
134 breakpoint_object *self_bp = (breakpoint_object *) self;
141 /* Python function to test whether or not the breakpoint is enabled. */
143 bppy_get_enabled (PyObject *self, void *closure)
145 breakpoint_object *self_bp = (breakpoint_object *) self;
147 BPPY_REQUIRE_VALID (self_bp);
150 if (self_bp->bp->enable_state == bp_enabled)
155 /* Python function to test whether or not the breakpoint is silent. */
157 bppy_get_silent (PyObject *self, void *closure)
159 breakpoint_object *self_bp = (breakpoint_object *) self;
161 BPPY_REQUIRE_VALID (self_bp);
162 if (self_bp->bp->silent)
167 /* Python function to set the enabled state of a breakpoint. */
169 bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
171 breakpoint_object *self_bp = (breakpoint_object *) self;
174 BPPY_SET_REQUIRE_VALID (self_bp);
176 if (newvalue == NULL)
178 PyErr_SetString (PyExc_TypeError,
179 _("Cannot delete `enabled' attribute."));
183 else if (! PyBool_Check (newvalue))
185 PyErr_SetString (PyExc_TypeError,
186 _("The value of `enabled' must be a boolean."));
190 cmp = PyObject_IsTrue (newvalue);
194 enable_breakpoint (self_bp->bp);
196 disable_breakpoint (self_bp->bp);
200 /* Python function to set the 'silent' state of a breakpoint. */
202 bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure)
204 breakpoint_object *self_bp = (breakpoint_object *) self;
207 BPPY_SET_REQUIRE_VALID (self_bp);
209 if (newvalue == NULL)
211 PyErr_SetString (PyExc_TypeError,
212 _("Cannot delete `silent' attribute."));
215 else if (! PyBool_Check (newvalue))
217 PyErr_SetString (PyExc_TypeError,
218 _("The value of `silent' must be a boolean."));
222 cmp = PyObject_IsTrue (newvalue);
226 self_bp->bp->silent = cmp;
231 /* Python function to set the thread of a breakpoint. */
233 bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure)
235 breakpoint_object *self_bp = (breakpoint_object *) self;
238 BPPY_SET_REQUIRE_VALID (self_bp);
240 if (newvalue == NULL)
242 PyErr_SetString (PyExc_TypeError,
243 _("Cannot delete `thread' attribute."));
246 else if (PyInt_Check (newvalue))
248 id = (int) PyInt_AsLong (newvalue);
249 if (! valid_thread_id (id))
251 PyErr_SetString (PyExc_RuntimeError,
252 _("Invalid thread ID."));
256 else if (newvalue == Py_None)
260 PyErr_SetString (PyExc_TypeError,
261 _("The value of `thread' must be an integer or None."));
265 self_bp->bp->thread = id;
270 /* Python function to set the (Ada) task of a breakpoint. */
272 bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
274 breakpoint_object *self_bp = (breakpoint_object *) self;
277 BPPY_SET_REQUIRE_VALID (self_bp);
279 if (newvalue == NULL)
281 PyErr_SetString (PyExc_TypeError,
282 _("Cannot delete `task' attribute."));
285 else if (PyInt_Check (newvalue))
287 id = (int) PyInt_AsLong (newvalue);
288 if (! valid_task_id (id))
290 PyErr_SetString (PyExc_RuntimeError,
291 _("Invalid task ID."));
295 else if (newvalue == Py_None)
299 PyErr_SetString (PyExc_TypeError,
300 _("The value of `task' must be an integer or None."));
304 self_bp->bp->task = id;
310 /* Python function to set the ignore count of a breakpoint. */
312 bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure)
314 breakpoint_object *self_bp = (breakpoint_object *) self;
317 BPPY_SET_REQUIRE_VALID (self_bp);
319 if (newvalue == NULL)
321 PyErr_SetString (PyExc_TypeError,
322 _("Cannot delete `ignore_count' attribute."));
325 else if (! PyInt_Check (newvalue))
327 PyErr_SetString (PyExc_TypeError,
328 _("The value of `ignore_count' must be an integer."));
332 value = PyInt_AsLong (newvalue);
335 set_ignore_count (self_bp->number, (int) value, 0);
340 /* Python function to set the hit count of a breakpoint. */
342 bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure)
344 breakpoint_object *self_bp = (breakpoint_object *) self;
346 BPPY_SET_REQUIRE_VALID (self_bp);
348 if (newvalue == NULL)
350 PyErr_SetString (PyExc_TypeError,
351 _("Cannot delete `hit_count' attribute."));
354 else if (! PyInt_Check (newvalue) || PyInt_AsLong (newvalue) != 0)
356 PyErr_SetString (PyExc_AttributeError,
357 _("The value of `hit_count' must be zero."));
361 self_bp->bp->hit_count = 0;
366 /* Python function to get the location of a breakpoint. */
368 bppy_get_location (PyObject *self, void *closure)
371 breakpoint_object *obj = (breakpoint_object *) self;
373 BPPY_REQUIRE_VALID (obj);
375 if (obj->bp->type != bp_breakpoint)
378 str = obj->bp->addr_string;
382 return PyString_Decode (str, strlen (str), host_charset (), NULL);
385 /* Python function to get the breakpoint expression. */
387 bppy_get_expression (PyObject *self, void *closure)
390 breakpoint_object *obj = (breakpoint_object *) self;
392 BPPY_REQUIRE_VALID (obj);
394 if (obj->bp->type != bp_watchpoint
395 && obj->bp->type != bp_hardware_watchpoint
396 && obj->bp->type != bp_read_watchpoint
397 && obj->bp->type != bp_access_watchpoint)
400 str = obj->bp->exp_string;
404 return PyString_Decode (str, strlen (str), host_charset (), NULL);
407 /* Python function to get the condition expression of a breakpoint. */
409 bppy_get_condition (PyObject *self, void *closure)
412 breakpoint_object *obj = (breakpoint_object *) self;
414 BPPY_REQUIRE_VALID (obj);
416 str = obj->bp->cond_string;
420 return PyString_Decode (str, strlen (str), host_charset (), NULL);
423 /* Returns 0 on success. Returns -1 on error, with a python exception set.
427 bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
430 breakpoint_object *self_bp = (breakpoint_object *) self;
431 volatile struct gdb_exception except;
433 BPPY_SET_REQUIRE_VALID (self_bp);
435 if (newvalue == NULL)
437 PyErr_SetString (PyExc_TypeError,
438 _("Cannot delete `condition' attribute."));
441 else if (newvalue == Py_None)
445 exp = python_string_to_host_string (newvalue);
450 TRY_CATCH (except, RETURN_MASK_ALL)
452 set_breakpoint_condition (self_bp->bp, exp, 0);
454 GDB_PY_SET_HANDLE_EXCEPTION (except);
459 /* Python function to get the commands attached to a breakpoint. */
461 bppy_get_commands (PyObject *self, void *closure)
463 breakpoint_object *self_bp = (breakpoint_object *) self;
464 struct breakpoint *bp = self_bp->bp;
466 volatile struct gdb_exception except;
467 struct ui_file *string_file;
468 struct cleanup *chain;
472 BPPY_REQUIRE_VALID (self_bp);
474 if (! self_bp->bp->commands)
477 string_file = mem_fileopen ();
478 chain = make_cleanup_ui_file_delete (string_file);
480 ui_out_redirect (uiout, string_file);
481 TRY_CATCH (except, RETURN_MASK_ALL)
483 print_command_lines (uiout, breakpoint_commands (bp), 0);
485 ui_out_redirect (uiout, NULL);
486 cmdstr = ui_file_xstrdup (string_file, &length);
487 GDB_PY_HANDLE_EXCEPTION (except);
489 result = PyString_Decode (cmdstr, strlen (cmdstr), host_charset (), NULL);
495 /* Python function to get the breakpoint type. */
497 bppy_get_type (PyObject *self, void *closure)
499 breakpoint_object *self_bp = (breakpoint_object *) self;
501 BPPY_REQUIRE_VALID (self_bp);
503 return PyInt_FromLong (self_bp->bp->type);
506 /* Python function to get the breakpoint's number. */
508 bppy_get_number (PyObject *self, void *closure)
510 breakpoint_object *self_bp = (breakpoint_object *) self;
512 BPPY_REQUIRE_VALID (self_bp);
514 return PyInt_FromLong (self_bp->number);
517 /* Python function to get the breakpoint's thread ID. */
519 bppy_get_thread (PyObject *self, void *closure)
521 breakpoint_object *self_bp = (breakpoint_object *) self;
523 BPPY_REQUIRE_VALID (self_bp);
525 if (self_bp->bp->thread == -1)
528 return PyInt_FromLong (self_bp->bp->thread);
531 /* Python function to get the breakpoint's task ID (in Ada). */
533 bppy_get_task (PyObject *self, void *closure)
535 breakpoint_object *self_bp = (breakpoint_object *) self;
537 BPPY_REQUIRE_VALID (self_bp);
539 if (self_bp->bp->task == 0)
542 return PyInt_FromLong (self_bp->bp->task);
545 /* Python function to get the breakpoint's hit count. */
547 bppy_get_hit_count (PyObject *self, void *closure)
549 breakpoint_object *self_bp = (breakpoint_object *) self;
551 BPPY_REQUIRE_VALID (self_bp);
553 return PyInt_FromLong (self_bp->bp->hit_count);
556 /* Python function to get the breakpoint's ignore count. */
558 bppy_get_ignore_count (PyObject *self, void *closure)
560 breakpoint_object *self_bp = (breakpoint_object *) self;
562 BPPY_REQUIRE_VALID (self_bp);
564 return PyInt_FromLong (self_bp->bp->ignore_count);
567 /* Python function to create a new breakpoint. */
569 bppy_new (PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
572 static char *keywords[] = { "spec", "type", "wp_class", NULL };
574 int type = bp_breakpoint;
575 int access_type = hw_write;
576 volatile struct gdb_exception except;
578 if (! PyArg_ParseTupleAndKeywords (args, kwargs, "s|ii", keywords,
579 &spec, &type, &access_type))
582 result = subtype->tp_alloc (subtype, 0);
585 bppy_pending_object = (breakpoint_object *) result;
586 bppy_pending_object->number = -1;
587 bppy_pending_object->bp = NULL;
589 TRY_CATCH (except, RETURN_MASK_ALL)
595 create_breakpoint (python_gdbarch,
606 if (access_type == hw_write)
607 watch_command_wrapper (spec, 0);
608 else if (access_type == hw_access)
609 awatch_command_wrapper (spec, 0);
610 else if (access_type == hw_read)
611 rwatch_command_wrapper (spec, 0);
613 error(_("Cannot understand watchpoint access type."));
617 error(_("Do not understand breakpoint type to set."));
620 if (except.reason < 0)
622 subtype->tp_free (result);
623 return PyErr_Format (except.reason == RETURN_QUIT
624 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
625 "%s", except.message);
628 BPPY_REQUIRE_VALID ((breakpoint_object *) result);
634 /* Static function to return a tuple holding all breakpoints. */
637 gdbpy_breakpoints (PyObject *self, PyObject *args)
644 result = PyTuple_New (bppy_live);
649 for (i = 0; out < bppy_live; ++i)
651 if (! bppy_breakpoints[i])
653 Py_INCREF (bppy_breakpoints[i]);
654 PyTuple_SetItem (result, out, (PyObject *) bppy_breakpoints[i]);
663 /* Event callback functions. */
665 /* Callback that is used when a breakpoint is created. This function
666 will create a new Python breakpoint object. */
668 gdbpy_breakpoint_created (int num)
670 breakpoint_object *newbp;
671 struct breakpoint *bp = NULL;
672 PyGILState_STATE state;
677 bp = get_breakpoint (num);
681 if (bp->type != bp_breakpoint
682 && bp->type != bp_watchpoint
683 && bp->type != bp_hardware_watchpoint
684 && bp->type != bp_read_watchpoint
685 && bp->type != bp_access_watchpoint)
688 if (num >= bppy_slots)
690 int old = bppy_slots;
692 bppy_slots = bppy_slots * 2 + 10;
694 = (breakpoint_object **) xrealloc (bppy_breakpoints,
696 * sizeof (breakpoint_object *)));
697 memset (&bppy_breakpoints[old], 0,
698 (bppy_slots - old) * sizeof (PyObject *));
703 state = PyGILState_Ensure ();
705 if (bppy_pending_object)
707 newbp = bppy_pending_object;
708 bppy_pending_object = NULL;
711 newbp = PyObject_New (breakpoint_object, &breakpoint_object_type);
716 bppy_breakpoints[num] = newbp;
720 /* Just ignore errors here. */
723 PyGILState_Release (state);
726 /* Callback that is used when a breakpoint is deleted. This will
727 invalidate the corresponding Python object. */
729 gdbpy_breakpoint_deleted (int num)
731 PyGILState_STATE state;
733 state = PyGILState_Ensure ();
734 if (bpnum_is_valid (num))
736 bppy_breakpoints[num]->bp = NULL;
737 Py_DECREF (bppy_breakpoints[num]);
738 bppy_breakpoints[num] = NULL;
741 PyGILState_Release (state);
746 /* Initialize the Python breakpoint code. */
748 gdbpy_initialize_breakpoints (void)
752 breakpoint_object_type.tp_new = bppy_new;
753 if (PyType_Ready (&breakpoint_object_type) < 0)
756 Py_INCREF (&breakpoint_object_type);
757 PyModule_AddObject (gdb_module, "Breakpoint",
758 (PyObject *) &breakpoint_object_type);
760 observer_attach_breakpoint_created (gdbpy_breakpoint_created);
761 observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted);
763 /* Add breakpoint types constants. */
764 for (i = 0; pybp_codes[i].name; ++i)
766 if (PyModule_AddIntConstant (gdb_module,
767 /* Cast needed for Python 2.4. */
768 (char *) pybp_codes[i].name,
769 pybp_codes[i].code) < 0)
773 /* Add watchpoint types constants. */
774 for (i = 0; pybp_watch_types[i].name; ++i)
776 if (PyModule_AddIntConstant (gdb_module,
777 /* Cast needed for Python 2.4. */
778 (char *) pybp_watch_types[i].name,
779 pybp_watch_types[i].code) < 0)
787 static PyGetSetDef breakpoint_object_getset[] = {
788 { "enabled", bppy_get_enabled, bppy_set_enabled,
789 "Boolean telling whether the breakpoint is enabled.", NULL },
790 { "silent", bppy_get_silent, bppy_set_silent,
791 "Boolean telling whether the breakpoint is silent.", NULL },
792 { "thread", bppy_get_thread, bppy_set_thread,
793 "Thread ID for the breakpoint.\n\
794 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
795 If the value is None, then this breakpoint is not thread-specific.\n\
796 No other type of value can be used.", NULL },
797 { "task", bppy_get_task, bppy_set_task,
798 "Thread ID for the breakpoint.\n\
799 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
800 If the value is None, then this breakpoint is not task-specific.\n\
801 No other type of value can be used.", NULL },
802 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
803 "Number of times this breakpoint should be automatically continued.",
805 { "number", bppy_get_number, NULL,
806 "Breakpoint's number assigned by GDB.", NULL },
807 { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
808 "Number of times the breakpoint has been hit.\n\
809 Can be set to zero to clear the count. No other value is valid\n\
810 when setting this property.", NULL },
811 { "location", bppy_get_location, NULL,
812 "Location of the breakpoint, as specified by the user.", NULL},
813 { "expression", bppy_get_expression, NULL,
814 "Expression of the breakpoint, as specified by the user.", NULL},
815 { "condition", bppy_get_condition, bppy_set_condition,
816 "Condition of the breakpoint, as specified by the user,\
817 or None if no condition set."},
818 { "commands", bppy_get_commands, NULL,
819 "Commands of the breakpoint, as specified by the user."},
820 { "type", bppy_get_type, NULL,
821 "Type of breakpoint."},
822 { NULL } /* Sentinel. */
825 static PyMethodDef breakpoint_object_methods[] =
827 { "is_valid", bppy_is_valid, METH_NOARGS,
828 "Return true if this breakpoint is valid, false if not." },
829 { NULL } /* Sentinel. */
832 static PyTypeObject breakpoint_object_type =
834 PyObject_HEAD_INIT (NULL)
836 "gdb.Breakpoint", /*tp_name*/
837 sizeof (breakpoint_object), /*tp_basicsize*/
846 0, /*tp_as_sequence*/
854 Py_TPFLAGS_DEFAULT, /*tp_flags*/
855 "GDB breakpoint object", /* tp_doc */
858 0, /* tp_richcompare */
859 0, /* tp_weaklistoffset */
862 breakpoint_object_methods, /* tp_methods */
864 breakpoint_object_getset /* tp_getset */