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