GDB (xrefs)
Loading...
Searching...
No Matches
py-function.c
Go to the documentation of this file.
1/* Convenience functions implemented in Python.
2
3 Copyright (C) 2008-2023 Free Software Foundation, Inc.
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 "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"
29#include "language.h"
30
31extern PyTypeObject fnpy_object_type
33
34
35
36/* Return a reference to a tuple ARGC elements long. Each element of the
37 tuple is a PyObject converted from the corresponding element of ARGV. */
38
39static gdbpy_ref<>
40convert_values_to_python (int argc, struct value **argv)
41{
42 int i;
43 gdbpy_ref<> result (PyTuple_New (argc));
44
45 if (result == NULL)
46 return NULL;
47
48 for (i = 0; i < argc; ++i)
49 {
50 gdbpy_ref<> elt (value_to_value_object (argv[i]));
51 if (elt == NULL)
52 return NULL;
53 PyTuple_SetItem (result.get (), i, elt.release ());
54 }
55 return result;
56}
57
58/* Call a Python function object's invoke method. */
59
60static struct value *
62 void *cookie, int argc, struct value **argv)
63{
64 /* The gdbpy_enter object needs to be placed first, so that it's the last to
65 be destroyed. */
66 gdbpy_enter enter_py (gdbarch, language);
67 struct value *value;
68 gdbpy_ref<> result;
69 gdbpy_ref<> args = convert_values_to_python (argc, argv);
70
71 /* convert_values_to_python can return NULL on error. If we
72 encounter this, do not call the function, but allow the Python ->
73 error code conversion below to deal with the Python exception.
74 Note, that this is different if the function simply does not
75 have arguments. */
76
77 if (args != NULL)
78 {
79 gdbpy_ref<> callable (PyObject_GetAttrString ((PyObject *) cookie,
80 "invoke"));
81 if (callable == NULL)
82 error (_("No method named 'invoke' in object."));
83
84 result.reset (PyObject_Call (callable.get (), args.get (), NULL));
85 }
86
87 if (result == NULL)
89
90 value = convert_value_from_python (result.get ());
91 if (value == NULL)
92 {
94 error (_("Error while executing Python code."));
95 }
96
97 return value;
98}
99
100/* Initializer for a Function object. It takes one argument, the name
101 of the function. */
102
103static int
104fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
105{
106 const char *name;
107 gdb::unique_xmalloc_ptr<char> docstring;
108
109 if (! PyArg_ParseTuple (args, "s", &name))
110 return -1;
111
112 gdbpy_ref<> self_ref = gdbpy_ref<>::new_reference (self);
113
114 if (PyObject_HasAttrString (self, "__doc__"))
115 {
116 gdbpy_ref<> ds_obj (PyObject_GetAttrString (self, "__doc__"));
117 if (ds_obj != NULL)
118 {
119 if (gdbpy_is_string (ds_obj.get ()))
120 {
121 docstring = python_string_to_host_string (ds_obj.get ());
122 if (docstring == NULL)
123 return -1;
124 }
125 }
126 }
127 if (! docstring)
128 docstring.reset (xstrdup (_("This function is not documented.")));
129
130 add_internal_function (make_unique_xstrdup (name), std::move (docstring),
131 fnpy_call, self_ref.release ());
132 return 0;
133}
134
135/* Initialize internal function support. */
136
139{
140 fnpy_object_type.tp_new = PyType_GenericNew;
141 if (PyType_Ready (&fnpy_object_type) < 0)
142 return -1;
143
144 return gdb_pymodule_addobject (gdb_module, "Function",
146}
147
149
150
151
152PyTypeObject fnpy_object_type =
153{
154 PyVarObject_HEAD_INIT (NULL, 0)
155 "gdb.Function", /*tp_name*/
156 sizeof (PyObject), /*tp_basicsize*/
157 0, /*tp_itemsize*/
158 0, /*tp_dealloc*/
159 0, /*tp_print*/
160 0, /*tp_getattr*/
161 0, /*tp_setattr*/
162 0, /*tp_compare*/
163 0, /*tp_repr*/
164 0, /*tp_as_number*/
165 0, /*tp_as_sequence*/
166 0, /*tp_as_mapping*/
167 0, /*tp_hash */
168 0, /*tp_call*/
169 0, /*tp_str*/
170 0, /*tp_getattro*/
171 0, /*tp_setattro*/
172 0, /*tp_as_buffer*/
173 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
174 "GDB function object", /* tp_doc */
175 0, /* tp_traverse */
176 0, /* tp_clear */
177 0, /* tp_richcompare */
178 0, /* tp_weaklistoffset */
179 0, /* tp_iter */
180 0, /* tp_iternext */
181 0, /* tp_methods */
182 0, /* tp_members */
183 0, /* tp_getset */
184 0, /* tp_base */
185 0, /* tp_dict */
186 0, /* tp_descr_get */
187 0, /* tp_descr_set */
188 0, /* tp_dictoffset */
189 fnpy_init, /* tp_init */
190 0, /* tp_alloc */
191};
const char *const name
language
Definition defs.h:211
static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION gdbpy_initialize_functions(void)
PyTypeObject fnpy_object_type
static struct value * fnpy_call(struct gdbarch *gdbarch, const struct language_defn *language, void *cookie, int argc, struct value **argv)
Definition py-function.c:61
static int fnpy_init(PyObject *self, PyObject *args, PyObject *kwds)
static gdbpy_ref convert_values_to_python(int argc, struct value **argv)
Definition py-function.c:40
int value
Definition py-param.c:79
gdb::ref_ptr< T, gdbpy_ref_policy< T > > gdbpy_ref
Definition py-ref.h:42
int gdb_pymodule_addobject(PyObject *module, const char *name, PyObject *object)
Definition py-utils.c:334
gdb::unique_xmalloc_ptr< char > python_string_to_host_string(PyObject *obj)
Definition py-utils.c:142
void gdbpy_handle_exception()
Definition py-utils.c:369
int gdbpy_is_string(PyObject *obj)
Definition py-utils.c:164
struct value * convert_value_from_python(PyObject *obj)
Definition py-value.c:1892
PyObject * value_to_value_object(struct value *val)
Definition py-value.c:1854
void gdbpy_print_stack(void)
PyObject * gdb_module
#define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(ARG)
#define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
#define GDBPY_INITIALIZE_FILE(INIT,...)
Definition value.h:130
void add_internal_function(const char *name, const char *doc, internal_function_fn handler, void *cookie)
Definition value.c:2360
int PyObject
Definition varobj.c:41