Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / python / py-function.c
CommitLineData
bc3b79fd
TJB
1/* Convenience functions implemented in Python.
2
88b9d363 3 Copyright (C) 2008-2022 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
b352ceb6
AB
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<>
bc3b79fd
TJB
40convert_values_to_python (int argc, struct value **argv)
41{
42 int i;
7780f186 43 gdbpy_ref<> result (PyTuple_New (argc));
256458bc 44
80bd970a 45 if (result == NULL)
f77b9a5d 46 return NULL;
d59b6f6c 47
bc3b79fd
TJB
48 for (i = 0; i < argc; ++i)
49 {
7780f186 50 gdbpy_ref<> elt (value_to_value_object (argv[i]));
80bd970a
TT
51 if (elt == NULL)
52 return NULL;
53 PyTuple_SetItem (result.get (), i, elt.release ());
bc3b79fd 54 }
b352ceb6 55 return result;
bc3b79fd
TJB
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{
b1ce6568
SM
64 /* The gdbpy_enter object needs to be placed first, so that it's the last to
65 be destroyed. */
0e9dcc75 66 gdbpy_enter enter_py (gdbarch, language);
b1ce6568 67 struct value *value;
7780f186 68 gdbpy_ref<> result;
b352ceb6 69 gdbpy_ref<> args = convert_values_to_python (argc, argv);
b1ce6568 70
f77b9a5d
PM
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. */
bc3b79fd 76
0e9dcc75 77 if (args != NULL)
bc3b79fd 78 {
7780f186
TT
79 gdbpy_ref<> callable (PyObject_GetAttrString ((PyObject *) cookie,
80 "invoke"));
0e9dcc75
TT
81 if (callable == NULL)
82 error (_("No method named 'invoke' in object."));
f77b9a5d 83
0e9dcc75 84 result.reset (PyObject_Call (callable.get (), args.get (), NULL));
bc3b79fd
TJB
85 }
86
0e9dcc75 87 if (result == NULL)
2b4ad2fe 88 gdbpy_handle_exception ();
bc3b79fd 89
0e9dcc75 90 value = convert_value_from_python (result.get ());
bc3b79fd
TJB
91 if (value == NULL)
92 {
bc3b79fd
TJB
93 gdbpy_print_stack ();
94 error (_("Error while executing Python code."));
95 }
96
bc3b79fd
TJB
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{
ddd49eee 106 const char *name;
9b972014 107 gdb::unique_xmalloc_ptr<char> docstring;
d59b6f6c 108
bc3b79fd
TJB
109 if (! PyArg_ParseTuple (args, "s", &name))
110 return -1;
2a3c71d6
TT
111
112 gdbpy_ref<> self_ref = gdbpy_ref<>::new_reference (self);
bc3b79fd
TJB
113
114 if (PyObject_HasAttrString (self, "__doc__"))
115 {
7780f186 116 gdbpy_ref<> ds_obj (PyObject_GetAttrString (self, "__doc__"));
764123e4 117 if (ds_obj != NULL)
8dc78533 118 {
80bd970a 119 if (gdbpy_is_string (ds_obj.get ()))
8dc78533 120 {
80bd970a 121 docstring = python_string_to_host_string (ds_obj.get ());
764123e4 122 if (docstring == NULL)
2a3c71d6 123 return -1;
8dc78533
JK
124 }
125 }
bc3b79fd
TJB
126 }
127 if (! docstring)
9b972014 128 docstring.reset (xstrdup (_("This function is not documented.")));
bc3b79fd 129
3ea16160
TT
130 add_internal_function (make_unique_xstrdup (name), std::move (docstring),
131 fnpy_call, self_ref.release ());
bc3b79fd
TJB
132 return 0;
133}
134
135/* Initialize internal function support. */
136
999633ed 137int
bc3b79fd
TJB
138gdbpy_initialize_functions (void)
139{
6a1b1664 140 fnpy_object_type.tp_new = PyType_GenericNew;
bc3b79fd 141 if (PyType_Ready (&fnpy_object_type) < 0)
999633ed 142 return -1;
bc3b79fd 143
aa36459a
TT
144 return gdb_pymodule_addobject (gdb_module, "Function",
145 (PyObject *) &fnpy_object_type);
bc3b79fd
TJB
146}
147
148\f
149
e36122e9 150PyTypeObject fnpy_object_type =
bc3b79fd 151{
9a27f2c6 152 PyVarObject_HEAD_INIT (NULL, 0)
bc3b79fd
TJB
153 "gdb.Function", /*tp_name*/
154 sizeof (PyObject), /*tp_basicsize*/
155 0, /*tp_itemsize*/
156 0, /*tp_dealloc*/
157 0, /*tp_print*/
158 0, /*tp_getattr*/
159 0, /*tp_setattr*/
160 0, /*tp_compare*/
161 0, /*tp_repr*/
162 0, /*tp_as_number*/
163 0, /*tp_as_sequence*/
164 0, /*tp_as_mapping*/
165 0, /*tp_hash */
166 0, /*tp_call*/
167 0, /*tp_str*/
168 0, /*tp_getattro*/
169 0, /*tp_setattro*/
170 0, /*tp_as_buffer*/
171 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
172 "GDB function object", /* tp_doc */
173 0, /* tp_traverse */
174 0, /* tp_clear */
175 0, /* tp_richcompare */
176 0, /* tp_weaklistoffset */
177 0, /* tp_iter */
178 0, /* tp_iternext */
179 0, /* tp_methods */
180 0, /* tp_members */
181 0, /* tp_getset */
182 0, /* tp_base */
183 0, /* tp_dict */
184 0, /* tp_descr_get */
185 0, /* tp_descr_set */
186 0, /* tp_dictoffset */
187 fnpy_init, /* tp_init */
188 0, /* tp_alloc */
bc3b79fd 189};
This page took 1.326316 seconds and 4 git commands to generate.