OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-7.2/] [gdb/] [python/] [py-symtab.c] - Blame information for rev 330

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 330 jeremybenn
/* Python interface to symbol tables.
2
 
3
   Copyright (C) 2008, 2009, 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 "charset.h"
22
#include "symtab.h"
23
#include "source.h"
24
#include "python-internal.h"
25
#include "objfiles.h"
26
 
27
typedef struct stpy_symtab_object {
28
  PyObject_HEAD
29
  /* The GDB Symbol table structure.  */
30
  struct symtab *symtab;
31
  /* A symtab object is associated with an objfile, so keep track with
32
     a doubly-linked list, rooted in the objfile.  This allows
33
     invalidation of the underlying struct symtab when the objfile is
34
     deleted.  */
35
  struct stpy_symtab_object *prev;
36
  struct stpy_symtab_object *next;
37
} symtab_object;
38
 
39
static PyTypeObject symtab_object_type;
40
static const struct objfile_data *stpy_objfile_data_key;
41
 
42
/* Require a valid symbol table.  All access to symtab_object->symtab
43
   should be gated by this call.  */
44
#define STPY_REQUIRE_VALID(symtab_obj, symtab)           \
45
  do {                                                   \
46
    symtab = symtab_object_to_symtab (symtab_obj);       \
47
    if (symtab == NULL)                                  \
48
      {                                                  \
49
        PyErr_SetString (PyExc_RuntimeError,             \
50
                         _("Symbol Table is invalid.")); \
51
        return NULL;                                     \
52
      }                                                  \
53
  } while (0)
54
 
55
typedef struct salpy_sal_object {
56
  PyObject_HEAD
57
  /* The GDB Symbol table structure.  */
58
  symtab_object *symtab;
59
  /* The GDB Symbol table and line structure.  */
60
  struct symtab_and_line *sal;
61
  /* A Symtab and line object is associated with an objfile, so keep
62
     track with a doubly-linked list, rooted in the objfile.  This
63
     allows invalidation of the underlying struct symtab_and_line
64
     when the objfile is deleted.  */
65
  struct salpy_sal_object *prev;
66
  struct salpy_sal_object *next;
67
} sal_object;
68
 
69
static PyTypeObject sal_object_type;
70
static const struct objfile_data *salpy_objfile_data_key;
71
 
72
/* Require a valid symbol table and line object.  All access to
73
   sal_object->sal should be gated by this call.  */
74
#define SALPY_REQUIRE_VALID(sal_obj, sal)                               \
75
  do {                                                                  \
76
    sal = sal_object_to_symtab_and_line (sal_obj);                      \
77
    if (sal == NULL)                                                    \
78
      {                                                                 \
79
          PyErr_SetString (PyExc_RuntimeError,                          \
80
                           _("Symbol Table and Line is invalid."));     \
81
          return NULL;                                                  \
82
        }                                                               \
83
  } while (0)
84
 
85
static PyObject *
86
stpy_str (PyObject *self)
87
{
88
  PyObject *result;
89
  struct symtab *symtab = NULL;
90
 
91
  STPY_REQUIRE_VALID (self, symtab);
92
 
93
  result = PyString_FromString (symtab->filename);
94
 
95
  return result;
96
}
97
 
98
static PyObject *
99
stpy_get_filename (PyObject *self, void *closure)
100
{
101
  PyObject *str_obj;
102
  struct symtab *symtab = NULL;
103
 
104
  STPY_REQUIRE_VALID (self, symtab);
105
 
106
  str_obj = PyString_Decode (symtab->filename,
107
                             strlen (symtab->filename),
108
                             host_charset (), NULL);
109
  return str_obj;
110
}
111
 
112
static PyObject *
113
stpy_get_objfile (PyObject *self, void *closure)
114
{
115
  struct symtab *symtab = NULL;
116
  PyObject *result;
117
 
118
  STPY_REQUIRE_VALID (self, symtab);
119
 
120
  result = objfile_to_objfile_object (symtab->objfile);
121
  Py_XINCREF (result);
122
  return result;
123
}
124
 
125
static PyObject *
126
stpy_fullname (PyObject *self, PyObject *args)
127
{
128
  char *fullname;
129
  struct symtab *symtab = NULL;
130
 
131
  STPY_REQUIRE_VALID (self, symtab);
132
 
133
  fullname = symtab_to_fullname (symtab);
134
  if (fullname)
135
    return PyString_Decode (fullname, strlen (fullname),
136
                            host_charset (), NULL);
137
 
138
  Py_RETURN_NONE;
139
}
140
 
141
static PyObject *
142
salpy_str (PyObject *self)
143
{
144
  char *s, *filename;
145
  sal_object *sal_obj;
146
  PyObject *result;
147
  struct symtab_and_line *sal = NULL;
148
 
149
  SALPY_REQUIRE_VALID (self, sal);
150
 
151
  sal_obj = (sal_object *) self;
152
  filename = (sal_obj->symtab == (symtab_object *) Py_None)
153
    ? "<unknown>" : sal_obj->symtab->symtab->filename;
154
 
155
  s = xstrprintf ("symbol and line for %s, line %d", filename,
156
                  sal->line);
157
 
158
  result = PyString_FromString (s);
159
  xfree (s);
160
 
161
  return result;
162
}
163
 
164
static void
165
stpy_dealloc (PyObject *obj)
166
{
167
  symtab_object *symtab = (symtab_object *) obj;
168
 
169
  if (symtab->prev)
170
    symtab->prev->next = symtab->next;
171
  else if (symtab->symtab)
172
    {
173
      set_objfile_data (symtab->symtab->objfile,
174
                        stpy_objfile_data_key, symtab->next);
175
    }
176
  if (symtab->next)
177
    symtab->next->prev = symtab->prev;
178
  symtab->symtab = NULL;
179
}
180
 
181
 
182
static PyObject *
183
salpy_get_pc (PyObject *self, void *closure)
184
{
185
  struct symtab_and_line *sal = NULL;
186
 
187
  SALPY_REQUIRE_VALID (self, sal);
188
 
189
  return PyLong_FromUnsignedLongLong (sal->pc);
190
}
191
 
192
static PyObject *
193
salpy_get_line (PyObject *self, void *closure)
194
{
195
  struct symtab_and_line *sal = NULL;
196
 
197
  SALPY_REQUIRE_VALID (self, sal);
198
 
199
  return PyLong_FromUnsignedLongLong (sal->line);
200
}
201
 
202
static PyObject *
203
salpy_get_symtab (PyObject *self, void *closure)
204
{
205
  struct symtab_and_line *sal;
206
  sal_object *self_sal = (sal_object *) self;
207
 
208
  SALPY_REQUIRE_VALID (self, sal);
209
 
210
  Py_INCREF (self_sal->symtab);
211
 
212
  return (PyObject *) self_sal->symtab;
213
}
214
 
215
static void
216
salpy_dealloc (PyObject *self)
217
{
218
  sal_object *self_sal = (sal_object *) self;
219
 
220
  if (self_sal->prev)
221
    self_sal->prev->next = self_sal->next;
222
  else if (self_sal->symtab != (symtab_object * ) Py_None)
223
    set_objfile_data (self_sal->symtab->symtab->objfile,
224
                      salpy_objfile_data_key, self_sal->next);
225
 
226
  if (self_sal->next)
227
    self_sal->next->prev = self_sal->prev;
228
 
229
  Py_DECREF (self_sal->symtab);
230
  xfree (self_sal->sal);
231
  self_sal->ob_type->tp_free (self);
232
}
233
 
234
/* Given a sal, and a sal_object that has previously been
235
   allocated and initialized, populate the sal_object with the
236
   struct sal data.  Also, register the sal_object life-cycle with the
237
   life-cycle of the the object file associated with this sal, if
238
   needed.  If a failure occurs during the sal population,  this
239
   function will return NULL.  */
240
static int
241
set_sal (sal_object *sal_obj, struct symtab_and_line sal)
242
{
243
  symtab_object *symtab_obj;
244
 
245
  if (sal.symtab)
246
    {
247
      symtab_obj = (symtab_object *) symtab_to_symtab_object  (sal.symtab);
248
      /* If a symtab existed in the sal, but it cannot be duplicated,
249
         we exit.  */
250
      if (symtab_obj == NULL)
251
        return 0;
252
    }
253
  else
254
    {
255
      symtab_obj = (symtab_object *) Py_None;
256
      Py_INCREF (Py_None);
257
    }
258
 
259
  sal_obj->sal = xmemdup (&sal, sizeof (struct symtab_and_line),
260
                          sizeof (struct symtab_and_line));
261
  sal_obj->symtab = symtab_obj;
262
  sal_obj->prev = NULL;
263
 
264
  /* If the SAL does not have a symtab, we do not add it to the
265
     objfile cleanup observer linked list.  */
266
  if (sal_obj->symtab != (symtab_object *)Py_None)
267
    {
268
      sal_obj->next = objfile_data (sal_obj->symtab->symtab->objfile,
269
                                    salpy_objfile_data_key);
270
      if (sal_obj->next)
271
        sal_obj->next->prev = sal_obj;
272
 
273
      set_objfile_data (sal_obj->symtab->symtab->objfile,
274
                        salpy_objfile_data_key, sal_obj);
275
    }
276
  else
277
    sal_obj->next = NULL;
278
 
279
  return 1;
280
}
281
 
282
/* Given a symtab, and a symtab_object that has previously been
283
   allocated and initialized, populate the symtab_object with the
284
   struct symtab data.  Also, register the symtab_object life-cycle
285
   with the life-cycle of the the object file associated with this
286
   symtab, if needed.  */
287
static void
288
set_symtab (symtab_object *obj, struct symtab *symtab)
289
{
290
  obj->symtab = symtab;
291
  obj->prev = NULL;
292
  if (symtab)
293
    {
294
      obj->next = objfile_data (symtab->objfile, stpy_objfile_data_key);
295
      if (obj->next)
296
        obj->next->prev = obj;
297
      set_objfile_data (symtab->objfile, stpy_objfile_data_key, obj);
298
    }
299
  else
300
    obj->next = NULL;
301
}
302
 
303
/* Create a new symbol table (gdb.Symtab) object that encapsulates the
304
   symtab structure from GDB.  */
305
PyObject *
306
symtab_to_symtab_object (struct symtab *symtab)
307
{
308
  symtab_object *symtab_obj;
309
 
310
  symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
311
  if (symtab_obj)
312
    set_symtab (symtab_obj, symtab);
313
 
314
  return (PyObject *) symtab_obj;
315
}
316
 
317
/* Create a new symtab and line (gdb.Symtab_and_line) object
318
   that encapsulates the symtab_and_line structure from GDB.  */
319
PyObject *
320
symtab_and_line_to_sal_object (struct symtab_and_line sal)
321
 
322
{
323
  sal_object *sal_obj;
324
  int success = 0;
325
 
326
  sal_obj = PyObject_New (sal_object, &sal_object_type);
327
  if (sal_obj)
328
    {
329
      success = set_sal (sal_obj, sal);
330
      if (!success)
331
        {
332
          Py_DECREF (sal_obj);
333
          return NULL;
334
        }
335
    }
336
 
337
  return (PyObject *) sal_obj;
338
}
339
 
340
/* Return struct symtab_and_line reference that is wrapped by this
341
   object.  */
342
struct symtab_and_line *
343
sal_object_to_symtab_and_line (PyObject *obj)
344
{
345
  if (! PyObject_TypeCheck (obj, &sal_object_type))
346
    return NULL;
347
  return ((sal_object *) obj)->sal;
348
}
349
 
350
/* Return struct symtab reference that is wrapped by this object.  */
351
struct symtab *
352
symtab_object_to_symtab (PyObject *obj)
353
{
354
  if (! PyObject_TypeCheck (obj, &symtab_object_type))
355
    return NULL;
356
  return ((symtab_object *) obj)->symtab;
357
}
358
 
359
/* This function is called when an objfile is about to be freed.
360
   Invalidate the symbol table as further actions on the symbol table
361
   would result in bad data.  All access to obj->symtab should be
362
   gated by STPY_REQUIRE_VALID which will raise an exception on
363
   invalid symbol tables.  */
364
static void
365
del_objfile_symtab (struct objfile *objfile, void *datum)
366
{
367
  symtab_object *obj = datum;
368
 
369
  while (obj)
370
    {
371
      symtab_object *next = obj->next;
372
 
373
      obj->symtab = NULL;
374
      obj->next = NULL;
375
      obj->prev = NULL;
376
      obj = next;
377
    }
378
}
379
 
380
/* This function is called when an objfile is about to be freed.
381
   Invalidate the sal object as further actions on the sal
382
   would result in bad data.  All access to obj->sal should be
383
   gated by SALPY_REQUIRE_VALID which will raise an exception on
384
   invalid symbol table and line objects.  */
385
static void
386
del_objfile_sal (struct objfile *objfile, void *datum)
387
{
388
  sal_object *obj = datum;
389
 
390
  while (obj)
391
    {
392
      sal_object *next = obj->next;
393
 
394
      obj->symtab = NULL;
395
      obj->next = NULL;
396
      obj->prev = NULL;
397
      xfree (obj->sal);
398
      obj->sal = NULL;
399
 
400
      obj = next;
401
    }
402
}
403
 
404
void
405
gdbpy_initialize_symtabs (void)
406
{
407
  symtab_object_type.tp_new = PyType_GenericNew;
408
  if (PyType_Ready (&symtab_object_type) < 0)
409
    return;
410
 
411
  sal_object_type.tp_new = PyType_GenericNew;
412
  if (PyType_Ready (&sal_object_type) < 0)
413
    return;
414
 
415
  /* Register an objfile "free" callback so we can properly
416
     invalidate symbol tables, and symbol table and line data
417
     structures when an object file that is about to be
418
     deleted.  */
419
  stpy_objfile_data_key
420
    = register_objfile_data_with_cleanup (NULL, del_objfile_symtab);
421
  salpy_objfile_data_key
422
    = register_objfile_data_with_cleanup (NULL, del_objfile_sal);
423
 
424
  Py_INCREF (&symtab_object_type);
425
  PyModule_AddObject (gdb_module, "Symtab",
426
                      (PyObject *) &symtab_object_type);
427
 
428
  Py_INCREF (&sal_object_type);
429
  PyModule_AddObject (gdb_module, "Symtab_and_line",
430
                      (PyObject *) &sal_object_type);
431
}
432
 
433
 
434
 
435
static PyGetSetDef symtab_object_getset[] = {
436
  { "filename", stpy_get_filename, NULL,
437
    "The symbol table's source filename.", NULL },
438
  { "objfile", stpy_get_objfile, NULL, "The symtab's objfile.",
439
    NULL },
440
  {NULL}  /* Sentinel */
441
};
442
 
443
static PyMethodDef symtab_object_methods[] = {
444
  { "fullname", stpy_fullname, METH_NOARGS,
445
    "fullname () -> String.\n\
446
Return the symtab's full source filename." },
447
  {NULL}  /* Sentinel */
448
};
449
 
450
static PyTypeObject symtab_object_type = {
451
  PyObject_HEAD_INIT (NULL)
452
  0,                               /*ob_size*/
453
  "gdb.Symtab",                   /*tp_name*/
454
  sizeof (symtab_object),         /*tp_basicsize*/
455
  0,                               /*tp_itemsize*/
456
  stpy_dealloc,                   /*tp_dealloc*/
457
  0,                               /*tp_print*/
458
  0,                               /*tp_getattr*/
459
  0,                               /*tp_setattr*/
460
  0,                               /*tp_compare*/
461
  0,                               /*tp_repr*/
462
  0,                               /*tp_as_number*/
463
  0,                               /*tp_as_sequence*/
464
  0,                               /*tp_as_mapping*/
465
  0,                               /*tp_hash */
466
  0,                               /*tp_call*/
467
  stpy_str,                       /*tp_str*/
468
  0,                               /*tp_getattro*/
469
  0,                               /*tp_setattro*/
470
  0,                               /*tp_as_buffer*/
471
  Py_TPFLAGS_DEFAULT,             /*tp_flags*/
472
  "GDB symtab object",            /*tp_doc */
473
  0,                               /*tp_traverse */
474
  0,                               /*tp_clear */
475
  0,                               /*tp_richcompare */
476
  0,                               /*tp_weaklistoffset */
477
  0,                               /*tp_iter */
478
  0,                               /*tp_iternext */
479
  symtab_object_methods,          /*tp_methods */
480
  0,                               /*tp_members */
481
  symtab_object_getset            /*tp_getset */
482
};
483
 
484
static PyGetSetDef sal_object_getset[] = {
485
  { "symtab", salpy_get_symtab, NULL, "Symtab object.", NULL },
486
  { "pc", salpy_get_pc, NULL, "Return the symtab_and_line's pc.", NULL },
487
  { "line", salpy_get_line, NULL,
488
    "Return the symtab_and_line's line.", NULL },
489
  {NULL}  /* Sentinel */
490
};
491
 
492
static PyTypeObject sal_object_type = {
493
  PyObject_HEAD_INIT (NULL)
494
  0,                               /*ob_size*/
495
  "gdb.Symtab_and_line",          /*tp_name*/
496
  sizeof (sal_object),            /*tp_basicsize*/
497
  0,                               /*tp_itemsize*/
498
  salpy_dealloc,                  /*tp_dealloc*/
499
  0,                               /*tp_print*/
500
  0,                               /*tp_getattr*/
501
  0,                               /*tp_setattr*/
502
  0,                               /*tp_compare*/
503
  0,                               /*tp_repr*/
504
  0,                               /*tp_as_number*/
505
  0,                               /*tp_as_sequence*/
506
  0,                               /*tp_as_mapping*/
507
  0,                               /*tp_hash */
508
  0,                               /*tp_call*/
509
  salpy_str,                      /*tp_str*/
510
  0,                               /*tp_getattro*/
511
  0,                               /*tp_setattro*/
512
  0,                               /*tp_as_buffer*/
513
  Py_TPFLAGS_DEFAULT,             /*tp_flags*/
514
  "GDB symtab_and_line object",   /*tp_doc */
515
  0,                               /*tp_traverse */
516
  0,                               /*tp_clear */
517
  0,                               /*tp_richcompare */
518
  0,                               /*tp_weaklistoffset */
519
  0,                               /*tp_iter */
520
  0,                               /*tp_iternext */
521
  0,                               /*tp_methods */
522
  0,                               /*tp_members */
523
  sal_object_getset               /*tp_getset */
524
};

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.