]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-function.c
* varobj.c (update_dynamic_varobj_children): Make 'name' const.
[thirdparty/binutils-gdb.git] / gdb / python / py-function.c
CommitLineData
bc3b79fd
TJB
1/* Convenience functions implemented in Python.
2
7b6bb8da 3 Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
bc3b79fd
TJB
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20
21#include "defs.h"
22#include "value.h"
23#include "exceptions.h"
24#include "python-internal.h"
25#include "charset.h"
26#include "gdbcmd.h"
27#include "cli/cli-decode.h"
28#include "completer.h"
29#include "expression.h"
d452c4bc 30#include "language.h"
bc3b79fd
TJB
31
32static PyTypeObject fnpy_object_type;
33
34\f
35
36static PyObject *
37convert_values_to_python (int argc, struct value **argv)
38{
39 int i;
40 PyObject *result = PyTuple_New (argc);
d59b6f6c 41
bc3b79fd
TJB
42 for (i = 0; i < argc; ++i)
43 {
44 PyObject *elt = value_to_value_object (argv[i]);
45 if (! elt)
46 {
47 Py_DECREF (result);
48 error (_("Could not convert value to Python object."));
49 }
50 PyTuple_SetItem (result, i, elt);
51 }
52 return result;
53}
54
55/* Call a Python function object's invoke method. */
56
57static struct value *
d452c4bc
UW
58fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language,
59 void *cookie, int argc, struct value **argv)
bc3b79fd 60{
bc3b79fd
TJB
61 struct value *value = NULL;
62 PyObject *result, *callable, *args;
63 struct cleanup *cleanup;
bc3b79fd 64
d452c4bc 65 cleanup = ensure_python_env (gdbarch, language);
bc3b79fd
TJB
66
67 args = convert_values_to_python (argc, argv);
68
69 callable = PyObject_GetAttrString ((PyObject *) cookie, "invoke");
70 if (! callable)
71 {
72 Py_DECREF (args);
73 error (_("No method named 'invoke' in object."));
74 }
75
76 result = PyObject_Call (callable, args, NULL);
77 Py_DECREF (callable);
78 Py_DECREF (args);
79
80 if (!result)
81 {
05775840
PM
82 PyObject *ptype, *pvalue, *ptraceback;
83 char *msg;
84
85 PyErr_Fetch (&ptype, &pvalue, &ptraceback);
86
87 /* Try to fetch an error message contained within ptype, pvalue.
88 When fetching the error message we need to make our own copy,
89 we no longer own ptype, pvalue after the call to PyErr_Restore. */
90
91 msg = gdbpy_exception_to_string (ptype, pvalue);
92 make_cleanup (xfree, msg);
93
94 if (msg == NULL)
95 {
96 /* An error occurred computing the string representation of the
97 error message. This is rare, but we should inform the user. */
98
99 printf_filtered (_("An error occurred in a Python "
100 "convenience function\n"
101 "and then another occurred computing the "
102 "error message.\n"));
103 gdbpy_print_stack ();
104 }
105
106 /* Don't print the stack for gdb.GdbError exceptions.
107 It is generally used to flag user errors.
108
109 We also don't want to print "Error occurred in Python command"
110 for user errors. However, a missing message for gdb.GdbError
111 exceptions is arguably a bug, so we flag it as such. */
112
113 if (!PyErr_GivenExceptionMatches (ptype, gdbpy_gdberror_exc)
114 || msg == NULL || *msg == '\0')
115 {
116 PyErr_Restore (ptype, pvalue, ptraceback);
117 gdbpy_print_stack ();
118 if (msg != NULL && *msg != '\0')
119 error (_("Error occurred in Python convenience function: %s"),
120 msg);
121 else
122 error (_("Error occurred in Python convenience function."));
123 }
124 else
125 {
126 Py_XDECREF (ptype);
127 Py_XDECREF (pvalue);
128 Py_XDECREF (ptraceback);
129 error ("%s", msg);
130 }
bc3b79fd
TJB
131 }
132
133 value = convert_value_from_python (result);
134 if (value == NULL)
135 {
136 Py_DECREF (result);
137 gdbpy_print_stack ();
138 error (_("Error while executing Python code."));
139 }
140
141 Py_DECREF (result);
142 do_cleanups (cleanup);
143
144 return value;
145}
146
147/* Initializer for a Function object. It takes one argument, the name
148 of the function. */
149
150static int
151fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
152{
ddd49eee
TT
153 const char *name;
154 char *docstring = NULL;
d59b6f6c 155
bc3b79fd
TJB
156 if (! PyArg_ParseTuple (args, "s", &name))
157 return -1;
158 Py_INCREF (self);
159
160 if (PyObject_HasAttrString (self, "__doc__"))
161 {
162 PyObject *ds_obj = PyObject_GetAttrString (self, "__doc__");
163 if (ds_obj && gdbpy_is_string (ds_obj))
8dc78533
JK
164 {
165 docstring = python_string_to_host_string (ds_obj);
166 if (docstring == NULL)
167 {
168 Py_DECREF (self);
169 return -1;
170 }
171 }
bc3b79fd
TJB
172 }
173 if (! docstring)
ce0420dc 174 docstring = xstrdup (_("This function is not documented."));
bc3b79fd
TJB
175
176 add_internal_function (name, docstring, fnpy_call, self);
177 return 0;
178}
179
180/* Initialize internal function support. */
181
182void
183gdbpy_initialize_functions (void)
184{
185 if (PyType_Ready (&fnpy_object_type) < 0)
186 return;
187
188 Py_INCREF (&fnpy_object_type);
189 PyModule_AddObject (gdb_module, "Function", (PyObject *) &fnpy_object_type);
190}
191
192\f
193
194static PyTypeObject fnpy_object_type =
195{
196 PyObject_HEAD_INIT (NULL)
197 0, /*ob_size*/
198 "gdb.Function", /*tp_name*/
199 sizeof (PyObject), /*tp_basicsize*/
200 0, /*tp_itemsize*/
201 0, /*tp_dealloc*/
202 0, /*tp_print*/
203 0, /*tp_getattr*/
204 0, /*tp_setattr*/
205 0, /*tp_compare*/
206 0, /*tp_repr*/
207 0, /*tp_as_number*/
208 0, /*tp_as_sequence*/
209 0, /*tp_as_mapping*/
210 0, /*tp_hash */
211 0, /*tp_call*/
212 0, /*tp_str*/
213 0, /*tp_getattro*/
214 0, /*tp_setattro*/
215 0, /*tp_as_buffer*/
216 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
217 "GDB function object", /* tp_doc */
218 0, /* tp_traverse */
219 0, /* tp_clear */
220 0, /* tp_richcompare */
221 0, /* tp_weaklistoffset */
222 0, /* tp_iter */
223 0, /* tp_iternext */
224 0, /* tp_methods */
225 0, /* tp_members */
226 0, /* tp_getset */
227 0, /* tp_base */
228 0, /* tp_dict */
229 0, /* tp_descr_get */
230 0, /* tp_descr_set */
231 0, /* tp_dictoffset */
232 fnpy_init, /* tp_init */
233 0, /* tp_alloc */
234 PyType_GenericNew /* tp_new */
235};