Use unique_xmalloc_ptr in Python code
[deliverable/binutils-gdb.git] / gdb / python / py-function.c
CommitLineData
bc3b79fd
TJB
1/* Convenience functions implemented in Python.
2
618f726f 3 Copyright (C) 2008-2016 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"
bc3b79fd
TJB
23#include "python-internal.h"
24#include "charset.h"
25#include "gdbcmd.h"
26#include "cli/cli-decode.h"
27#include "completer.h"
28#include "expression.h"
d452c4bc 29#include "language.h"
bc3b79fd 30
e36122e9 31extern PyTypeObject fnpy_object_type
62eec1a5 32 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("PyObject");
bc3b79fd
TJB
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);
256458bc 41
f77b9a5d
PM
42 if (! result)
43 return NULL;
d59b6f6c 44
bc3b79fd
TJB
45 for (i = 0; i < argc; ++i)
46 {
47 PyObject *elt = value_to_value_object (argv[i]);
48 if (! elt)
49 {
50 Py_DECREF (result);
f77b9a5d 51 return NULL;
bc3b79fd
TJB
52 }
53 PyTuple_SetItem (result, i, elt);
54 }
55 return result;
56}
57
58/* Call a Python function object's invoke method. */
59
60static struct value *
d452c4bc
UW
61fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language,
62 void *cookie, int argc, struct value **argv)
bc3b79fd 63{
bc3b79fd 64 struct value *value = NULL;
f77b9a5d
PM
65 /* 'result' must be set to NULL, this initially indicates whether
66 the function was called, or not. */
67 PyObject *result = NULL;
68 PyObject *callable, *args;
bc3b79fd 69 struct cleanup *cleanup;
bc3b79fd 70
d452c4bc 71 cleanup = ensure_python_env (gdbarch, language);
bc3b79fd
TJB
72
73 args = convert_values_to_python (argc, argv);
f77b9a5d
PM
74 /* convert_values_to_python can return NULL on error. If we
75 encounter this, do not call the function, but allow the Python ->
76 error code conversion below to deal with the Python exception.
77 Note, that this is different if the function simply does not
78 have arguments. */
bc3b79fd 79
f77b9a5d 80 if (args)
bc3b79fd 81 {
f77b9a5d
PM
82 callable = PyObject_GetAttrString ((PyObject *) cookie, "invoke");
83 if (! callable)
84 {
85 Py_DECREF (args);
86 error (_("No method named 'invoke' in object."));
87 }
88
89 result = PyObject_Call (callable, args, NULL);
90 Py_DECREF (callable);
bc3b79fd 91 Py_DECREF (args);
bc3b79fd
TJB
92 }
93
bc3b79fd
TJB
94 if (!result)
95 {
05775840 96 PyObject *ptype, *pvalue, *ptraceback;
05775840
PM
97
98 PyErr_Fetch (&ptype, &pvalue, &ptraceback);
99
100 /* Try to fetch an error message contained within ptype, pvalue.
101 When fetching the error message we need to make our own copy,
102 we no longer own ptype, pvalue after the call to PyErr_Restore. */
103
9b972014
TT
104 gdb::unique_xmalloc_ptr<char>
105 msg (gdbpy_exception_to_string (ptype, pvalue));
05775840
PM
106
107 if (msg == NULL)
108 {
109 /* An error occurred computing the string representation of the
110 error message. This is rare, but we should inform the user. */
111
112 printf_filtered (_("An error occurred in a Python "
113 "convenience function\n"
114 "and then another occurred computing the "
115 "error message.\n"));
116 gdbpy_print_stack ();
117 }
118
119 /* Don't print the stack for gdb.GdbError exceptions.
120 It is generally used to flag user errors.
121
122 We also don't want to print "Error occurred in Python command"
123 for user errors. However, a missing message for gdb.GdbError
124 exceptions is arguably a bug, so we flag it as such. */
125
126 if (!PyErr_GivenExceptionMatches (ptype, gdbpy_gdberror_exc)
127 || msg == NULL || *msg == '\0')
128 {
129 PyErr_Restore (ptype, pvalue, ptraceback);
130 gdbpy_print_stack ();
131 if (msg != NULL && *msg != '\0')
132 error (_("Error occurred in Python convenience function: %s"),
9b972014 133 msg.get ());
05775840
PM
134 else
135 error (_("Error occurred in Python convenience function."));
136 }
137 else
138 {
139 Py_XDECREF (ptype);
140 Py_XDECREF (pvalue);
141 Py_XDECREF (ptraceback);
9b972014 142 error ("%s", msg.get ());
05775840 143 }
bc3b79fd
TJB
144 }
145
146 value = convert_value_from_python (result);
147 if (value == NULL)
148 {
149 Py_DECREF (result);
150 gdbpy_print_stack ();
151 error (_("Error while executing Python code."));
152 }
153
154 Py_DECREF (result);
155 do_cleanups (cleanup);
156
157 return value;
158}
159
160/* Initializer for a Function object. It takes one argument, the name
161 of the function. */
162
163static int
164fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
165{
ddd49eee 166 const char *name;
9b972014 167 gdb::unique_xmalloc_ptr<char> docstring;
d59b6f6c 168
bc3b79fd
TJB
169 if (! PyArg_ParseTuple (args, "s", &name))
170 return -1;
171 Py_INCREF (self);
172
173 if (PyObject_HasAttrString (self, "__doc__"))
174 {
175 PyObject *ds_obj = PyObject_GetAttrString (self, "__doc__");
764123e4 176 if (ds_obj != NULL)
8dc78533 177 {
764123e4 178 if (gdbpy_is_string (ds_obj))
8dc78533 179 {
764123e4
TT
180 docstring = python_string_to_host_string (ds_obj);
181 if (docstring == NULL)
182 {
183 Py_DECREF (self);
184 Py_DECREF (ds_obj);
185 return -1;
186 }
8dc78533 187 }
764123e4
TT
188
189 Py_DECREF (ds_obj);
8dc78533 190 }
bc3b79fd
TJB
191 }
192 if (! docstring)
9b972014 193 docstring.reset (xstrdup (_("This function is not documented.")));
bc3b79fd 194
9b972014 195 add_internal_function (name, docstring.release (), fnpy_call, self);
bc3b79fd
TJB
196 return 0;
197}
198
199/* Initialize internal function support. */
200
999633ed 201int
bc3b79fd
TJB
202gdbpy_initialize_functions (void)
203{
6a1b1664 204 fnpy_object_type.tp_new = PyType_GenericNew;
bc3b79fd 205 if (PyType_Ready (&fnpy_object_type) < 0)
999633ed 206 return -1;
bc3b79fd 207
aa36459a
TT
208 return gdb_pymodule_addobject (gdb_module, "Function",
209 (PyObject *) &fnpy_object_type);
bc3b79fd
TJB
210}
211
212\f
213
e36122e9 214PyTypeObject fnpy_object_type =
bc3b79fd 215{
9a27f2c6 216 PyVarObject_HEAD_INIT (NULL, 0)
bc3b79fd
TJB
217 "gdb.Function", /*tp_name*/
218 sizeof (PyObject), /*tp_basicsize*/
219 0, /*tp_itemsize*/
220 0, /*tp_dealloc*/
221 0, /*tp_print*/
222 0, /*tp_getattr*/
223 0, /*tp_setattr*/
224 0, /*tp_compare*/
225 0, /*tp_repr*/
226 0, /*tp_as_number*/
227 0, /*tp_as_sequence*/
228 0, /*tp_as_mapping*/
229 0, /*tp_hash */
230 0, /*tp_call*/
231 0, /*tp_str*/
232 0, /*tp_getattro*/
233 0, /*tp_setattro*/
234 0, /*tp_as_buffer*/
235 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
236 "GDB function object", /* tp_doc */
237 0, /* tp_traverse */
238 0, /* tp_clear */
239 0, /* tp_richcompare */
240 0, /* tp_weaklistoffset */
241 0, /* tp_iter */
242 0, /* tp_iternext */
243 0, /* tp_methods */
244 0, /* tp_members */
245 0, /* tp_getset */
246 0, /* tp_base */
247 0, /* tp_dict */
248 0, /* tp_descr_get */
249 0, /* tp_descr_set */
250 0, /* tp_dictoffset */
251 fnpy_init, /* tp_init */
252 0, /* tp_alloc */
bc3b79fd 253};
This page took 0.800594 seconds and 4 git commands to generate.