include/elf/
[deliverable/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 {
82 gdbpy_print_stack ();
83 error (_("Error while executing Python code."));
84 }
85
86 value = convert_value_from_python (result);
87 if (value == NULL)
88 {
89 Py_DECREF (result);
90 gdbpy_print_stack ();
91 error (_("Error while executing Python code."));
92 }
93
94 Py_DECREF (result);
95 do_cleanups (cleanup);
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 char *name, *docstring = NULL;
d59b6f6c 107
bc3b79fd
TJB
108 if (! PyArg_ParseTuple (args, "s", &name))
109 return -1;
110 Py_INCREF (self);
111
112 if (PyObject_HasAttrString (self, "__doc__"))
113 {
114 PyObject *ds_obj = PyObject_GetAttrString (self, "__doc__");
115 if (ds_obj && gdbpy_is_string (ds_obj))
8dc78533
JK
116 {
117 docstring = python_string_to_host_string (ds_obj);
118 if (docstring == NULL)
119 {
120 Py_DECREF (self);
121 return -1;
122 }
123 }
bc3b79fd
TJB
124 }
125 if (! docstring)
ce0420dc 126 docstring = xstrdup (_("This function is not documented."));
bc3b79fd
TJB
127
128 add_internal_function (name, docstring, fnpy_call, self);
129 return 0;
130}
131
132/* Initialize internal function support. */
133
134void
135gdbpy_initialize_functions (void)
136{
137 if (PyType_Ready (&fnpy_object_type) < 0)
138 return;
139
140 Py_INCREF (&fnpy_object_type);
141 PyModule_AddObject (gdb_module, "Function", (PyObject *) &fnpy_object_type);
142}
143
144\f
145
146static PyTypeObject fnpy_object_type =
147{
148 PyObject_HEAD_INIT (NULL)
149 0, /*ob_size*/
150 "gdb.Function", /*tp_name*/
151 sizeof (PyObject), /*tp_basicsize*/
152 0, /*tp_itemsize*/
153 0, /*tp_dealloc*/
154 0, /*tp_print*/
155 0, /*tp_getattr*/
156 0, /*tp_setattr*/
157 0, /*tp_compare*/
158 0, /*tp_repr*/
159 0, /*tp_as_number*/
160 0, /*tp_as_sequence*/
161 0, /*tp_as_mapping*/
162 0, /*tp_hash */
163 0, /*tp_call*/
164 0, /*tp_str*/
165 0, /*tp_getattro*/
166 0, /*tp_setattro*/
167 0, /*tp_as_buffer*/
168 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
169 "GDB function object", /* tp_doc */
170 0, /* tp_traverse */
171 0, /* tp_clear */
172 0, /* tp_richcompare */
173 0, /* tp_weaklistoffset */
174 0, /* tp_iter */
175 0, /* tp_iternext */
176 0, /* tp_methods */
177 0, /* tp_members */
178 0, /* tp_getset */
179 0, /* tp_base */
180 0, /* tp_dict */
181 0, /* tp_descr_get */
182 0, /* tp_descr_set */
183 0, /* tp_dictoffset */
184 fnpy_init, /* tp_init */
185 0, /* tp_alloc */
186 PyType_GenericNew /* tp_new */
187};
This page took 0.222146 seconds and 4 git commands to generate.