1 /* Convenience functions implemented in Python.
3 Copyright (C) 2008-2017 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/>. */
23 #include "python-internal.h"
26 #include "cli/cli-decode.h"
27 #include "completer.h"
28 #include "expression.h"
32 extern PyTypeObject fnpy_object_type
33 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("PyObject");
38 convert_values_to_python (int argc
, struct value
**argv
)
41 gdbpy_ref
result (PyTuple_New (argc
));
46 for (i
= 0; i
< argc
; ++i
)
48 gdbpy_ref
elt (value_to_value_object (argv
[i
]));
51 PyTuple_SetItem (result
.get (), i
, elt
.release ());
53 return result
.release ();
56 /* Call a Python function object's invoke method. */
59 fnpy_call (struct gdbarch
*gdbarch
, const struct language_defn
*language
,
60 void *cookie
, int argc
, struct value
**argv
)
62 struct value
*value
= NULL
;
63 /* 'result' must be set to NULL, this initially indicates whether
64 the function was called, or not. */
67 gdbpy_enter
enter_py (gdbarch
, language
);
69 gdbpy_ref
args (convert_values_to_python (argc
, argv
));
70 /* convert_values_to_python can return NULL on error. If we
71 encounter this, do not call the function, but allow the Python ->
72 error code conversion below to deal with the Python exception.
73 Note, that this is different if the function simply does not
78 gdbpy_ref
callable (PyObject_GetAttrString ((PyObject
*) cookie
,
81 error (_("No method named 'invoke' in object."));
83 result
.reset (PyObject_Call (callable
.get (), args
.get (), NULL
));
88 PyObject
*ptype
, *pvalue
, *ptraceback
;
90 PyErr_Fetch (&ptype
, &pvalue
, &ptraceback
);
92 /* Try to fetch an error message contained within ptype, pvalue.
93 When fetching the error message we need to make our own copy,
94 we no longer own ptype, pvalue after the call to PyErr_Restore. */
96 gdb::unique_xmalloc_ptr
<char>
97 msg (gdbpy_exception_to_string (ptype
, pvalue
));
101 /* An error occurred computing the string representation of the
102 error message. This is rare, but we should inform the user. */
104 printf_filtered (_("An error occurred in a Python "
105 "convenience function\n"
106 "and then another occurred computing the "
107 "error message.\n"));
108 gdbpy_print_stack ();
111 /* Don't print the stack for gdb.GdbError exceptions.
112 It is generally used to flag user errors.
114 We also don't want to print "Error occurred in Python command"
115 for user errors. However, a missing message for gdb.GdbError
116 exceptions is arguably a bug, so we flag it as such. */
118 if (!PyErr_GivenExceptionMatches (ptype
, gdbpy_gdberror_exc
)
119 || msg
== NULL
|| *msg
== '\0')
121 PyErr_Restore (ptype
, pvalue
, ptraceback
);
122 gdbpy_print_stack ();
123 if (msg
!= NULL
&& *msg
!= '\0')
124 error (_("Error occurred in Python convenience function: %s"),
127 error (_("Error occurred in Python convenience function."));
133 Py_XDECREF (ptraceback
);
134 error ("%s", msg
.get ());
138 value
= convert_value_from_python (result
.get ());
141 gdbpy_print_stack ();
142 error (_("Error while executing Python code."));
148 /* Initializer for a Function object. It takes one argument, the name
152 fnpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
155 gdb::unique_xmalloc_ptr
<char> docstring
;
157 if (! PyArg_ParseTuple (args
, "s", &name
))
161 if (PyObject_HasAttrString (self
, "__doc__"))
163 gdbpy_ref
ds_obj (PyObject_GetAttrString (self
, "__doc__"));
166 if (gdbpy_is_string (ds_obj
.get ()))
168 docstring
= python_string_to_host_string (ds_obj
.get ());
169 if (docstring
== NULL
)
178 docstring
.reset (xstrdup (_("This function is not documented.")));
180 add_internal_function (name
, docstring
.release (), fnpy_call
, self
);
184 /* Initialize internal function support. */
187 gdbpy_initialize_functions (void)
189 fnpy_object_type
.tp_new
= PyType_GenericNew
;
190 if (PyType_Ready (&fnpy_object_type
) < 0)
193 return gdb_pymodule_addobject (gdb_module
, "Function",
194 (PyObject
*) &fnpy_object_type
);
199 PyTypeObject fnpy_object_type
=
201 PyVarObject_HEAD_INIT (NULL
, 0)
202 "gdb.Function", /*tp_name*/
203 sizeof (PyObject
), /*tp_basicsize*/
212 0, /*tp_as_sequence*/
220 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
221 "GDB function object", /* tp_doc */
224 0, /* tp_richcompare */
225 0, /* tp_weaklistoffset */
233 0, /* tp_descr_get */
234 0, /* tp_descr_set */
235 0, /* tp_dictoffset */
236 fnpy_init
, /* tp_init */