* NEWS: Add entry for python program space support.
[deliverable/binutils-gdb.git] / gdb / python / py-progspace.c
CommitLineData
fa33c3cd
DE
1/* Python interface to program spaces.
2
3 Copyright (C) 2010 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#include "defs.h"
21#include "python-internal.h"
22#include "charset.h"
23#include "progspace.h"
24#include "objfiles.h"
25#include "language.h"
26
27typedef struct
28{
29 PyObject_HEAD
30
31 /* The corresponding pspace. */
32 struct program_space *pspace;
33
34 /* The pretty-printer list of functions. */
35 PyObject *printers;
36} pspace_object;
37
38static PyTypeObject pspace_object_type;
39
40static const struct program_space_data *pspy_pspace_data_key;
41
42\f
43
44/* An Objfile method which returns the objfile's file name, or None. */
45
46static PyObject *
47pspy_get_filename (PyObject *self, void *closure)
48{
49 pspace_object *obj = (pspace_object *) self;
50 if (obj->pspace)
51 {
52 struct objfile *objfile = obj->pspace->symfile_object_file;
53 if (objfile && objfile->name)
54 return PyString_Decode (objfile->name, strlen (objfile->name),
55 host_charset (), NULL);
56 }
57 Py_RETURN_NONE;
58}
59
60static void
61pspy_dealloc (PyObject *self)
62{
63 pspace_object *ps_self = (pspace_object *) self;
64 Py_XDECREF (ps_self->printers);
65 self->ob_type->tp_free (self);
66}
67
68static PyObject *
69pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
70{
71 pspace_object *self = (pspace_object *) type->tp_alloc (type, 0);
72 if (self)
73 {
74 self->pspace = NULL;
75
76 self->printers = PyList_New (0);
77 if (!self->printers)
78 {
79 Py_DECREF (self);
80 return NULL;
81 }
82 }
83 return (PyObject *) self;
84}
85
86PyObject *
87pspy_get_printers (PyObject *o, void *ignore)
88{
89 pspace_object *self = (pspace_object *) o;
90 Py_INCREF (self->printers);
91 return self->printers;
92}
93
94static int
95pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
96{
97 PyObject *tmp;
98 pspace_object *self = (pspace_object *) o;
99 if (! value)
100 {
101 PyErr_SetString (PyExc_TypeError,
102 "cannot delete the pretty_printers attribute");
103 return -1;
104 }
105
106 if (! PyList_Check (value))
107 {
108 PyErr_SetString (PyExc_TypeError,
109 "the pretty_printers attribute must be a list");
110 return -1;
111 }
112
113 /* Take care in case the LHS and RHS are related somehow. */
114 tmp = self->printers;
115 Py_INCREF (value);
116 self->printers = value;
117 Py_XDECREF (tmp);
118
119 return 0;
120}
121
122\f
123
124/* Clear the PSPACE pointer in a Pspace object and remove the reference. */
125
126static void
127py_free_pspace (struct program_space *pspace, void *datum)
128{
129 struct cleanup *cleanup;
130 pspace_object *object = datum;
131 /* FIXME: What's the right way to get a program space's arch?
132 There may be multiple. */
133 struct gdbarch *arch = get_objfile_arch (pspace->symfile_object_file);
134
135 cleanup = ensure_python_env (arch, current_language);
136 object->pspace = NULL;
137 Py_DECREF ((PyObject *) object);
138 do_cleanups (cleanup);
139}
140
141/* Return a borrowed reference to the Python object of type Pspace
142 representing PSPACE. If the object has already been created,
143 return it. Otherwise, create it. Return NULL and set the Python
144 error on failure. */
145
146PyObject *
147pspace_to_pspace_object (struct program_space *pspace)
148{
149 pspace_object *object;
150
151 object = program_space_data (pspace, pspy_pspace_data_key);
152 if (!object)
153 {
154 object = PyObject_New (pspace_object, &pspace_object_type);
155 if (object)
156 {
157 PyObject *dict;
158
159 object->pspace = pspace;
160
161 object->printers = PyList_New (0);
162 if (!object->printers)
163 {
164 Py_DECREF (object);
165 return NULL;
166 }
167
168 set_program_space_data (pspace, pspy_pspace_data_key, object);
169 }
170 }
171
172 return (PyObject *) object;
173}
174
175void
176gdbpy_initialize_pspace (void)
177{
178 pspy_pspace_data_key
179 = register_program_space_data_with_cleanup (py_free_pspace);
180
181 if (PyType_Ready (&pspace_object_type) < 0)
182 return;
183
184 Py_INCREF (&pspace_object_type);
185 PyModule_AddObject (gdb_module, "Progspace", (PyObject *) &pspace_object_type);
186}
187
188\f
189
190static PyGetSetDef pspace_getset[] =
191{
192 { "filename", pspy_get_filename, NULL,
193 "The progspace's main filename, or None.", NULL },
194 { "pretty_printers", pspy_get_printers, pspy_set_printers,
195 "Pretty printers.", NULL },
196 { NULL }
197};
198
199static PyTypeObject pspace_object_type =
200{
201 PyObject_HEAD_INIT (NULL)
202 0, /*ob_size*/
203 "gdb.Progspace", /*tp_name*/
204 sizeof (pspace_object), /*tp_basicsize*/
205 0, /*tp_itemsize*/
206 pspy_dealloc, /*tp_dealloc*/
207 0, /*tp_print*/
208 0, /*tp_getattr*/
209 0, /*tp_setattr*/
210 0, /*tp_compare*/
211 0, /*tp_repr*/
212 0, /*tp_as_number*/
213 0, /*tp_as_sequence*/
214 0, /*tp_as_mapping*/
215 0, /*tp_hash */
216 0, /*tp_call*/
217 0, /*tp_str*/
218 0, /*tp_getattro*/
219 0, /*tp_setattro*/
220 0, /*tp_as_buffer*/
221 Py_TPFLAGS_DEFAULT, /*tp_flags*/
222 "GDB progspace object", /* tp_doc */
223 0, /* tp_traverse */
224 0, /* tp_clear */
225 0, /* tp_richcompare */
226 0, /* tp_weaklistoffset */
227 0, /* tp_iter */
228 0, /* tp_iternext */
229 0, /* tp_methods */
230 0, /* tp_members */
231 pspace_getset, /* tp_getset */
232 0, /* tp_base */
233 0, /* tp_dict */
234 0, /* tp_descr_get */
235 0, /* tp_descr_set */
236 0, /* tp_dictoffset */
237 0, /* tp_init */
238 0, /* tp_alloc */
239 pspy_new, /* tp_new */
240};
This page took 0.049488 seconds and 4 git commands to generate.