1 /* Python interface to program spaces.
2 
3    Copyright (C) 2010-2012 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 #include "arch-utils.h"
27 
28 typedef 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;
37 } pspace_object;
38 
39 static PyTypeObject pspace_object_type;
40 
41 static const struct program_space_data *pspy_pspace_data_key;
42 
43 
44 
45 /* An Objfile method which returns the objfile's file name, or None.  */
46 
47 static PyObject *
48 pspy_get_filename (PyObject *self, void *closure)
49 {
50   pspace_object *obj = (pspace_object *) self;
51 
52   if (obj->pspace)
53     {
54       struct objfile *objfile = obj->pspace->symfile_object_file;
55 
56       if (objfile)
57 	return PyString_Decode (objfile->name, strlen (objfile->name),
58 				host_charset (), NULL);
59     }
60   Py_RETURN_NONE;
61 }
62 
63 static void
64 pspy_dealloc (PyObject *self)
65 {
66   pspace_object *ps_self = (pspace_object *) self;
67 
68   Py_XDECREF (ps_self->printers);
69   self->ob_type->tp_free (self);
70 }
71 
72 static PyObject *
73 pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
74 {
75   pspace_object *self = (pspace_object *) type->tp_alloc (type, 0);
76 
77   if (self)
78     {
79       self->pspace = NULL;
80 
81       self->printers = PyList_New (0);
82       if (!self->printers)
83 	{
84 	  Py_DECREF (self);
85 	  return NULL;
86 	}
87     }
88   return (PyObject *) self;
89 }
90 
91 PyObject *
92 pspy_get_printers (PyObject *o, void *ignore)
93 {
94   pspace_object *self = (pspace_object *) o;
95 
96   Py_INCREF (self->printers);
97   return self->printers;
98 }
99 
100 static int
101 pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
102 {
103   PyObject *tmp;
104   pspace_object *self = (pspace_object *) o;
105 
106   if (! value)
107     {
108       PyErr_SetString (PyExc_TypeError,
109 		       "cannot delete the pretty_printers attribute");
110       return -1;
111     }
112 
113   if (! PyList_Check (value))
114     {
115       PyErr_SetString (PyExc_TypeError,
116 		       "the pretty_printers attribute must be a list");
117       return -1;
118     }
119 
120   /* Take care in case the LHS and RHS are related somehow.  */
121   tmp = self->printers;
122   Py_INCREF (value);
123   self->printers = value;
124   Py_XDECREF (tmp);
125 
126   return 0;
127 }
128 
129 
130 
131 /* Clear the PSPACE pointer in a Pspace object and remove the reference.  */
132 
133 static void
134 py_free_pspace (struct program_space *pspace, void *datum)
135 {
136   struct cleanup *cleanup;
137   pspace_object *object = datum;
138   struct gdbarch *arch = get_current_arch ();
139 
140   cleanup = ensure_python_env (arch, current_language);
141   object->pspace = NULL;
142   Py_DECREF ((PyObject *) object);
143   do_cleanups (cleanup);
144 }
145 
146 /* Return a borrowed reference to the Python object of type Pspace
147    representing PSPACE.  If the object has already been created,
148    return it.  Otherwise, create it.  Return NULL and set the Python
149    error on failure.  */
150 
151 PyObject *
152 pspace_to_pspace_object (struct program_space *pspace)
153 {
154   pspace_object *object;
155 
156   object = program_space_data (pspace, pspy_pspace_data_key);
157   if (!object)
158     {
159       object = PyObject_New (pspace_object, &pspace_object_type);
160       if (object)
161 	{
162 	  object->pspace = pspace;
163 
164 	  object->printers = PyList_New (0);
165 	  if (!object->printers)
166 	    {
167 	      Py_DECREF (object);
168 	      return NULL;
169 	    }
170 
171 	  set_program_space_data (pspace, pspy_pspace_data_key, object);
172 	}
173     }
174 
175   return (PyObject *) object;
176 }
177 
178 void
179 gdbpy_initialize_pspace (void)
180 {
181   pspy_pspace_data_key
182     = register_program_space_data_with_cleanup (py_free_pspace);
183 
184   if (PyType_Ready (&pspace_object_type) < 0)
185     return;
186 
187   Py_INCREF (&pspace_object_type);
188   PyModule_AddObject (gdb_module, "Progspace",
189 		      (PyObject *) &pspace_object_type);
190 }
191 
192 
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 };
245