Xmethod support in Python.
[deliverable/binutils-gdb.git] / gdb / python / py-progspace.c
CommitLineData
fa33c3cd
DE
1/* Python interface to program spaces.
2
ecd75fc8 3 Copyright (C) 2010-2014 Free Software Foundation, Inc.
fa33c3cd
DE
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#include "defs.h"
21#include "python-internal.h"
22#include "charset.h"
23#include "progspace.h"
24#include "objfiles.h"
25#include "language.h"
b3422a0d 26#include "arch-utils.h"
fa33c3cd
DE
27
28typedef struct
29{
30 PyObject_HEAD
31
32 /* The corresponding pspace. */
33 struct program_space *pspace;
34
35 /* The pretty-printer list of functions. */
36 PyObject *printers;
18a9fc12 37
1e611234
PM
38 /* The frame filter list of functions. */
39 PyObject *frame_filters;
18a9fc12
TT
40 /* The type-printer list. */
41 PyObject *type_printers;
883964a7
SC
42
43 /* The debug method list. */
44 PyObject *xmethods;
fa33c3cd
DE
45} pspace_object;
46
62eec1a5
TT
47static PyTypeObject pspace_object_type
48 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
fa33c3cd
DE
49
50static const struct program_space_data *pspy_pspace_data_key;
51
52\f
53
54/* An Objfile method which returns the objfile's file name, or None. */
55
56static PyObject *
57pspy_get_filename (PyObject *self, void *closure)
58{
59 pspace_object *obj = (pspace_object *) self;
d59b6f6c 60
fa33c3cd
DE
61 if (obj->pspace)
62 {
63 struct objfile *objfile = obj->pspace->symfile_object_file;
d59b6f6c 64
d31d2fc3 65 if (objfile)
4262abfb
JK
66 return PyString_Decode (objfile_name (objfile),
67 strlen (objfile_name (objfile)),
fa33c3cd
DE
68 host_charset (), NULL);
69 }
70 Py_RETURN_NONE;
71}
72
73static void
74pspy_dealloc (PyObject *self)
75{
76 pspace_object *ps_self = (pspace_object *) self;
d59b6f6c 77
fa33c3cd 78 Py_XDECREF (ps_self->printers);
1e611234 79 Py_XDECREF (ps_self->frame_filters);
18a9fc12 80 Py_XDECREF (ps_self->type_printers);
883964a7 81 Py_XDECREF (ps_self->xmethods);
9a27f2c6 82 Py_TYPE (self)->tp_free (self);
fa33c3cd
DE
83}
84
85static PyObject *
86pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
87{
88 pspace_object *self = (pspace_object *) type->tp_alloc (type, 0);
d59b6f6c 89
fa33c3cd
DE
90 if (self)
91 {
92 self->pspace = NULL;
93
94 self->printers = PyList_New (0);
95 if (!self->printers)
96 {
97 Py_DECREF (self);
98 return NULL;
99 }
18a9fc12 100
1e611234
PM
101 self->frame_filters = PyDict_New ();
102 if (!self->frame_filters)
103 {
104 Py_DECREF (self);
105 return NULL;
106 }
107
18a9fc12
TT
108 self->type_printers = PyList_New (0);
109 if (!self->type_printers)
110 {
111 Py_DECREF (self);
112 return NULL;
113 }
883964a7
SC
114
115 self->xmethods = PyList_New (0);
116 if (self->xmethods == NULL)
117 {
118 Py_DECREF (self);
119 return NULL;
120 }
fa33c3cd
DE
121 }
122 return (PyObject *) self;
123}
124
125PyObject *
126pspy_get_printers (PyObject *o, void *ignore)
127{
128 pspace_object *self = (pspace_object *) o;
d59b6f6c 129
fa33c3cd
DE
130 Py_INCREF (self->printers);
131 return self->printers;
132}
133
134static int
135pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
136{
137 PyObject *tmp;
138 pspace_object *self = (pspace_object *) o;
d59b6f6c 139
fa33c3cd
DE
140 if (! value)
141 {
142 PyErr_SetString (PyExc_TypeError,
143 "cannot delete the pretty_printers attribute");
144 return -1;
145 }
146
147 if (! PyList_Check (value))
148 {
149 PyErr_SetString (PyExc_TypeError,
150 "the pretty_printers attribute must be a list");
151 return -1;
152 }
153
154 /* Take care in case the LHS and RHS are related somehow. */
155 tmp = self->printers;
156 Py_INCREF (value);
157 self->printers = value;
158 Py_XDECREF (tmp);
159
160 return 0;
161}
162
1e611234
PM
163/* Return the Python dictionary attribute containing frame filters for
164 this program space. */
165PyObject *
166pspy_get_frame_filters (PyObject *o, void *ignore)
167{
168 pspace_object *self = (pspace_object *) o;
169
170 Py_INCREF (self->frame_filters);
171 return self->frame_filters;
172}
173
174/* Set this object file's frame filters dictionary to FILTERS. */
175static int
176pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore)
177{
178 PyObject *tmp;
179 pspace_object *self = (pspace_object *) o;
180
181 if (! frame)
182 {
183 PyErr_SetString (PyExc_TypeError,
184 "cannot delete the frame filter attribute");
185 return -1;
186 }
187
188 if (! PyDict_Check (frame))
189 {
190 PyErr_SetString (PyExc_TypeError,
191 "the frame filter attribute must be a dictionary");
192 return -1;
193 }
194
195 /* Take care in case the LHS and RHS are related somehow. */
196 tmp = self->frame_filters;
197 Py_INCREF (frame);
198 self->frame_filters = frame;
199 Py_XDECREF (tmp);
200
201 return 0;
202}
203
18a9fc12
TT
204/* Get the 'type_printers' attribute. */
205
206static PyObject *
207pspy_get_type_printers (PyObject *o, void *ignore)
208{
209 pspace_object *self = (pspace_object *) o;
210
211 Py_INCREF (self->type_printers);
212 return self->type_printers;
213}
214
883964a7
SC
215/* Get the 'xmethods' attribute. */
216
217PyObject *
218pspy_get_xmethods (PyObject *o, void *ignore)
219{
220 pspace_object *self = (pspace_object *) o;
221
222 Py_INCREF (self->xmethods);
223 return self->xmethods;
224}
225
18a9fc12
TT
226/* Set the 'type_printers' attribute. */
227
228static int
229pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
230{
231 PyObject *tmp;
232 pspace_object *self = (pspace_object *) o;
233
234 if (! value)
235 {
236 PyErr_SetString (PyExc_TypeError,
237 "cannot delete the type_printers attribute");
238 return -1;
239 }
240
241 if (! PyList_Check (value))
242 {
243 PyErr_SetString (PyExc_TypeError,
244 "the type_printers attribute must be a list");
245 return -1;
246 }
247
248 /* Take care in case the LHS and RHS are related somehow. */
249 tmp = self->type_printers;
250 Py_INCREF (value);
251 self->type_printers = value;
252 Py_XDECREF (tmp);
253
254 return 0;
255}
256
fa33c3cd
DE
257\f
258
259/* Clear the PSPACE pointer in a Pspace object and remove the reference. */
260
261static void
262py_free_pspace (struct program_space *pspace, void *datum)
263{
264 struct cleanup *cleanup;
265 pspace_object *object = datum;
227533ac
DE
266 /* This is a fiction, but we're in a nasty spot: The pspace is in the
267 process of being deleted, we can't rely on anything in it. Plus
268 this is one time when the current program space and current inferior
269 are not in sync: All inferiors that use PSPACE may no longer exist.
270 We don't need to do much here, and since "there is always an inferior"
271 using target_gdbarch suffices.
272 Note: We cannot call get_current_arch because it may try to access
273 the target, which may involve accessing data in the pspace currently
274 being deleted. */
275 struct gdbarch *arch = target_gdbarch ();
fa33c3cd
DE
276
277 cleanup = ensure_python_env (arch, current_language);
278 object->pspace = NULL;
279 Py_DECREF ((PyObject *) object);
280 do_cleanups (cleanup);
281}
282
283/* Return a borrowed reference to the Python object of type Pspace
284 representing PSPACE. If the object has already been created,
285 return it. Otherwise, create it. Return NULL and set the Python
286 error on failure. */
287
288PyObject *
289pspace_to_pspace_object (struct program_space *pspace)
290{
291 pspace_object *object;
292
293 object = program_space_data (pspace, pspy_pspace_data_key);
294 if (!object)
295 {
296 object = PyObject_New (pspace_object, &pspace_object_type);
297 if (object)
298 {
fa33c3cd
DE
299 object->pspace = pspace;
300
301 object->printers = PyList_New (0);
302 if (!object->printers)
303 {
304 Py_DECREF (object);
305 return NULL;
306 }
307
1e611234
PM
308 object->frame_filters = PyDict_New ();
309 if (!object->frame_filters)
310 {
311 Py_DECREF (object);
312 return NULL;
313 }
314
18a9fc12
TT
315 object->type_printers = PyList_New (0);
316 if (!object->type_printers)
317 {
318 Py_DECREF (object);
319 return NULL;
320 }
321
883964a7
SC
322 object->xmethods = PyList_New (0);
323 if (object->xmethods == NULL)
324 {
325 Py_DECREF (object);
326 return NULL;
327 }
328
fa33c3cd
DE
329 set_program_space_data (pspace, pspy_pspace_data_key, object);
330 }
331 }
332
333 return (PyObject *) object;
334}
335
999633ed 336int
fa33c3cd
DE
337gdbpy_initialize_pspace (void)
338{
339 pspy_pspace_data_key
8e260fc0 340 = register_program_space_data_with_cleanup (NULL, py_free_pspace);
fa33c3cd
DE
341
342 if (PyType_Ready (&pspace_object_type) < 0)
999633ed 343 return -1;
fa33c3cd 344
aa36459a
TT
345 return gdb_pymodule_addobject (gdb_module, "Progspace",
346 (PyObject *) &pspace_object_type);
fa33c3cd
DE
347}
348
349\f
350
351static PyGetSetDef pspace_getset[] =
352{
353 { "filename", pspy_get_filename, NULL,
354 "The progspace's main filename, or None.", NULL },
355 { "pretty_printers", pspy_get_printers, pspy_set_printers,
356 "Pretty printers.", NULL },
1e611234
PM
357 { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters,
358 "Frame filters.", NULL },
18a9fc12
TT
359 { "type_printers", pspy_get_type_printers, pspy_set_type_printers,
360 "Type printers.", NULL },
883964a7
SC
361 { "xmethods", pspy_get_xmethods, NULL,
362 "Debug methods.", NULL },
fa33c3cd
DE
363 { NULL }
364};
365
366static PyTypeObject pspace_object_type =
367{
9a27f2c6 368 PyVarObject_HEAD_INIT (NULL, 0)
fa33c3cd
DE
369 "gdb.Progspace", /*tp_name*/
370 sizeof (pspace_object), /*tp_basicsize*/
371 0, /*tp_itemsize*/
372 pspy_dealloc, /*tp_dealloc*/
373 0, /*tp_print*/
374 0, /*tp_getattr*/
375 0, /*tp_setattr*/
376 0, /*tp_compare*/
377 0, /*tp_repr*/
378 0, /*tp_as_number*/
379 0, /*tp_as_sequence*/
380 0, /*tp_as_mapping*/
381 0, /*tp_hash */
382 0, /*tp_call*/
383 0, /*tp_str*/
384 0, /*tp_getattro*/
385 0, /*tp_setattro*/
386 0, /*tp_as_buffer*/
387 Py_TPFLAGS_DEFAULT, /*tp_flags*/
388 "GDB progspace object", /* tp_doc */
389 0, /* tp_traverse */
390 0, /* tp_clear */
391 0, /* tp_richcompare */
392 0, /* tp_weaklistoffset */
393 0, /* tp_iter */
394 0, /* tp_iternext */
395 0, /* tp_methods */
396 0, /* tp_members */
397 pspace_getset, /* tp_getset */
398 0, /* tp_base */
399 0, /* tp_dict */
400 0, /* tp_descr_get */
401 0, /* tp_descr_set */
402 0, /* tp_dictoffset */
403 0, /* tp_init */
404 0, /* tp_alloc */
405 pspy_new, /* tp_new */
406};
This page took 0.457447 seconds and 4 git commands to generate.