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