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
);
424 bppy_set_condition (PyObject
*self
, PyObject
*newvalue
, void *closure
)
427 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
428 volatile struct gdb_exception except
;
430 BPPY_SET_REQUIRE_VALID (self_bp
);
432 if (newvalue
== NULL
)
434 PyErr_SetString (PyExc_TypeError
,
435 _("Cannot delete `condition' attribute."));
438 else if (newvalue
== Py_None
)
442 exp
= python_string_to_host_string (newvalue
);
447 TRY_CATCH (except
, RETURN_MASK_ALL
)
449 set_breakpoint_condition (self_bp
->bp
, exp
, 0);
451 GDB_PY_SET_HANDLE_EXCEPTION (except
);
456 /* Python function to get the commands attached to a breakpoint. */
458 bppy_get_commands (PyObject
*self
, void *closure
)
460 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
461 struct breakpoint
*bp
= self_bp
->bp
;
463 volatile struct gdb_exception except
;
464 struct ui_file
*string_file
;
465 struct cleanup
*chain
;
469 BPPY_REQUIRE_VALID (self_bp
);
471 if (! self_bp
->bp
->commands
)
474 string_file
= mem_fileopen ();
475 chain
= make_cleanup_ui_file_delete (string_file
);
477 TRY_CATCH (except
, RETURN_MASK_ALL
)
479 ui_out_redirect (uiout
, string_file
);
480 print_command_lines (uiout
, breakpoint_commands (bp
), 0);
481 ui_out_redirect (uiout
, NULL
);
483 cmdstr
= ui_file_xstrdup (string_file
, &length
);
484 GDB_PY_HANDLE_EXCEPTION (except
);
486 result
= PyString_Decode (cmdstr
, strlen (cmdstr
), host_charset (), NULL
);
492 /* Python function to get the breakpoint type. */
494 bppy_get_type (PyObject
*self
, void *closure
)
496 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
498 BPPY_REQUIRE_VALID (self_bp
);
500 return PyInt_FromLong (self_bp
->bp
->type
);
503 /* Python function to get the breakpoint's number. */
505 bppy_get_number (PyObject
*self
, void *closure
)
507 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
509 BPPY_REQUIRE_VALID (self_bp
);
511 return PyInt_FromLong (self_bp
->number
);
514 /* Python function to get the breakpoint's thread ID. */
516 bppy_get_thread (PyObject
*self
, void *closure
)
518 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
520 BPPY_REQUIRE_VALID (self_bp
);
522 if (self_bp
->bp
->thread
== -1)
525 return PyInt_FromLong (self_bp
->bp
->thread
);
528 /* Python function to get the breakpoint's task ID (in Ada). */
530 bppy_get_task (PyObject
*self
, void *closure
)
532 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
534 BPPY_REQUIRE_VALID (self_bp
);
536 if (self_bp
->bp
->task
== 0)
539 return PyInt_FromLong (self_bp
->bp
->task
);
542 /* Python function to get the breakpoint's hit count. */
544 bppy_get_hit_count (PyObject
*self
, void *closure
)
546 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
548 BPPY_REQUIRE_VALID (self_bp
);
550 return PyInt_FromLong (self_bp
->bp
->hit_count
);
553 /* Python function to get the breakpoint's ignore count. */
555 bppy_get_ignore_count (PyObject
*self
, void *closure
)
557 breakpoint_object
*self_bp
= (breakpoint_object
*) self
;
559 BPPY_REQUIRE_VALID (self_bp
);
561 return PyInt_FromLong (self_bp
->bp
->ignore_count
);
564 /* Python function to create a new breakpoint. */
566 bppy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*kwargs
)
569 static char *keywords
[] = { "spec", "type", "wp_class", NULL
};
571 int type
= bp_breakpoint
;
572 int access_type
= hw_write
;
573 volatile struct gdb_exception except
;
575 if (! PyArg_ParseTupleAndKeywords (args
, kwargs
, "s|ii", keywords
,
576 &spec
, &type
, &access_type
))
579 result
= subtype
->tp_alloc (subtype
, 0);
582 bppy_pending_object
= (breakpoint_object
*) result
;
583 bppy_pending_object
->number
= -1;
584 bppy_pending_object
->bp
= NULL
;
586 TRY_CATCH (except
, RETURN_MASK_ALL
)
592 create_breakpoint (python_gdbarch
,
603 if (access_type
== hw_write
)
604 watch_command_wrapper (spec
, 0);
605 else if (access_type
== hw_access
)
606 awatch_command_wrapper (spec
, 0);
607 else if (access_type
== hw_read
)
608 rwatch_command_wrapper (spec
, 0);
610 error(_("Cannot understand watchpoint access type."));
614 error(_("Do not understand breakpoint type to set."));
617 if (except
.reason
< 0)
619 subtype
->tp_free (result
);
620 return PyErr_Format (except
.reason
== RETURN_QUIT
621 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
622 "%s", except
.message
);
625 BPPY_REQUIRE_VALID ((breakpoint_object
*) result
);
631 /* Static function to return a tuple holding all breakpoints. */
634 gdbpy_breakpoints (PyObject
*self
, PyObject
*args
)
641 result
= PyTuple_New (bppy_live
);
646 for (i
= 0; out
< bppy_live
; ++i
)
648 if (! bppy_breakpoints
[i
])
650 Py_INCREF (bppy_breakpoints
[i
]);
651 PyTuple_SetItem (result
, out
, (PyObject
*) bppy_breakpoints
[i
]);
660 /* Event callback functions. */
662 /* Callback that is used when a breakpoint is created. This function
663 will create a new Python breakpoint object. */
665 gdbpy_breakpoint_created (int num
)
667 breakpoint_object
*newbp
;
668 struct breakpoint
*bp
= NULL
;
669 PyGILState_STATE state
;
674 bp
= get_breakpoint (num
);
678 if (bp
->type
!= bp_breakpoint
679 && bp
->type
!= bp_watchpoint
680 && bp
->type
!= bp_hardware_watchpoint
681 && bp
->type
!= bp_read_watchpoint
682 && bp
->type
!= bp_access_watchpoint
)
685 if (num
>= bppy_slots
)
687 int old
= bppy_slots
;
689 bppy_slots
= bppy_slots
* 2 + 10;
691 = (breakpoint_object
**) xrealloc (bppy_breakpoints
,
693 * sizeof (breakpoint_object
*)));
694 memset (&bppy_breakpoints
[old
], 0,
695 (bppy_slots
- old
) * sizeof (PyObject
*));
700 state
= PyGILState_Ensure ();
702 if (bppy_pending_object
)
704 newbp
= bppy_pending_object
;
705 bppy_pending_object
= NULL
;
708 newbp
= PyObject_New (breakpoint_object
, &breakpoint_object_type
);
713 bppy_breakpoints
[num
] = newbp
;
717 /* Just ignore errors here. */
720 PyGILState_Release (state
);
723 /* Callback that is used when a breakpoint is deleted. This will
724 invalidate the corresponding Python object. */
726 gdbpy_breakpoint_deleted (int num
)
728 PyGILState_STATE state
;
730 state
= PyGILState_Ensure ();
731 if (bpnum_is_valid (num
))
733 bppy_breakpoints
[num
]->bp
= NULL
;
734 Py_DECREF (bppy_breakpoints
[num
]);
735 bppy_breakpoints
[num
] = NULL
;
738 PyGILState_Release (state
);
743 /* Initialize the Python breakpoint code. */
745 gdbpy_initialize_breakpoints (void)
749 breakpoint_object_type
.tp_new
= bppy_new
;
750 if (PyType_Ready (&breakpoint_object_type
) < 0)
753 Py_INCREF (&breakpoint_object_type
);
754 PyModule_AddObject (gdb_module
, "Breakpoint",
755 (PyObject
*) &breakpoint_object_type
);
757 observer_attach_breakpoint_created (gdbpy_breakpoint_created
);
758 observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted
);
760 /* Add breakpoint types constants. */
761 for (i
= 0; pybp_codes
[i
].name
; ++i
)
763 if (PyModule_AddIntConstant (gdb_module
,
764 /* Cast needed for Python 2.4. */
765 (char *) pybp_codes
[i
].name
,
766 pybp_codes
[i
].code
) < 0)
770 /* Add watchpoint types constants. */
771 for (i
= 0; pybp_watch_types
[i
].name
; ++i
)
773 if (PyModule_AddIntConstant (gdb_module
,
774 /* Cast needed for Python 2.4. */
775 (char *) pybp_watch_types
[i
].name
,
776 pybp_watch_types
[i
].code
) < 0)
784 static PyGetSetDef breakpoint_object_getset
[] = {
785 { "enabled", bppy_get_enabled
, bppy_set_enabled
,
786 "Boolean telling whether the breakpoint is enabled.", NULL
},
787 { "silent", bppy_get_silent
, bppy_set_silent
,
788 "Boolean telling whether the breakpoint is silent.", NULL
},
789 { "thread", bppy_get_thread
, bppy_set_thread
,
790 "Thread ID for the breakpoint.\n\
791 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
792 If the value is None, then this breakpoint is not thread-specific.\n\
793 No other type of value can be used.", NULL
},
794 { "task", bppy_get_task
, bppy_set_task
,
795 "Thread ID for the breakpoint.\n\
796 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
797 If the value is None, then this breakpoint is not task-specific.\n\
798 No other type of value can be used.", NULL
},
799 { "ignore_count", bppy_get_ignore_count
, bppy_set_ignore_count
,
800 "Number of times this breakpoint should be automatically continued.",
802 { "number", bppy_get_number
, NULL
,
803 "Breakpoint's number assigned by GDB.", NULL
},
804 { "hit_count", bppy_get_hit_count
, bppy_set_hit_count
,
805 "Number of times the breakpoint has been hit.\n\
806 Can be set to zero to clear the count. No other value is valid\n\
807 when setting this property.", NULL
},
808 { "location", bppy_get_location
, NULL
,
809 "Location of the breakpoint, as specified by the user.", NULL
},
810 { "expression", bppy_get_expression
, NULL
,
811 "Expression of the breakpoint, as specified by the user.", NULL
},
812 { "condition", bppy_get_condition
, bppy_set_condition
,
813 "Condition of the breakpoint, as specified by the user,\
814 or None if no condition set."},
815 { "commands", bppy_get_commands
, NULL
,
816 "Commands of the breakpoint, as specified by the user."},
817 { "type", bppy_get_type
, NULL
,
818 "Type of breakpoint."},
819 { NULL
} /* Sentinel. */
822 static PyMethodDef breakpoint_object_methods
[] =
824 { "is_valid", bppy_is_valid
, METH_NOARGS
,
825 "Return true if this breakpoint is valid, false if not." },
826 { NULL
} /* Sentinel. */
829 static PyTypeObject breakpoint_object_type
=
831 PyObject_HEAD_INIT (NULL
)
833 "gdb.Breakpoint", /*tp_name*/
834 sizeof (breakpoint_object
), /*tp_basicsize*/
843 0, /*tp_as_sequence*/
851 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
852 "GDB breakpoint object", /* tp_doc */
855 0, /* tp_richcompare */
856 0, /* tp_weaklistoffset */
859 breakpoint_object_methods
, /* tp_methods */
861 breakpoint_object_getset
/* tp_getset */