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-symbol.c] - Blame information for rev 330

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 330 jeremybenn
/* Python interface to symbols.
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 "block.h"
22
#include "exceptions.h"
23
#include "frame.h"
24
#include "symtab.h"
25
#include "python-internal.h"
26
#include "objfiles.h"
27
 
28
typedef struct sympy_symbol_object {
29
  PyObject_HEAD
30
  /* The GDB symbol structure this object is wrapping.  */
31
  struct symbol *symbol;
32
  /* A symbol object is associated with an objfile, so keep track with
33
     doubly-linked list, rooted in the objfile.  This lets us
34
     invalidate the underlying struct symbol when the objfile is
35
     deleted.  */
36
  struct sympy_symbol_object *prev;
37
  struct sympy_symbol_object *next;
38
} symbol_object;
39
 
40
/* Require a valid symbol.  All access to symbol_object->symbol should be
41
   gated by this call.  */
42
#define SYMPY_REQUIRE_VALID(symbol_obj, symbol)         \
43
  do {                                                  \
44
    symbol = symbol_object_to_symbol (symbol_obj);      \
45
    if (symbol == NULL)                                 \
46
      {                                                 \
47
        PyErr_SetString (PyExc_RuntimeError,            \
48
                         _("Symbol is invalid."));      \
49
        return NULL;                                    \
50
      }                                                 \
51
  } while (0)
52
 
53
static const struct objfile_data *sympy_objfile_data_key;
54
 
55
static PyObject *
56
sympy_str (PyObject *self)
57
{
58
  PyObject *result;
59
  struct symbol *symbol = NULL;
60
 
61
  SYMPY_REQUIRE_VALID (self, symbol);
62
 
63
  result = PyString_FromString (SYMBOL_PRINT_NAME (symbol));
64
 
65
  return result;
66
}
67
 
68
static PyObject *
69
sympy_get_symtab (PyObject *self, void *closure)
70
{
71
  struct symbol *symbol = NULL;
72
 
73
  SYMPY_REQUIRE_VALID (self, symbol);
74
 
75
  return symtab_to_symtab_object (SYMBOL_SYMTAB (symbol));
76
}
77
 
78
static PyObject *
79
sympy_get_name (PyObject *self, void *closure)
80
{
81
  struct symbol *symbol = NULL;
82
 
83
  SYMPY_REQUIRE_VALID (self, symbol);
84
 
85
  return PyString_FromString (SYMBOL_NATURAL_NAME (symbol));
86
}
87
 
88
static PyObject *
89
sympy_get_linkage_name (PyObject *self, void *closure)
90
{
91
  struct symbol *symbol = NULL;
92
 
93
  SYMPY_REQUIRE_VALID (self, symbol);
94
 
95
  return PyString_FromString (SYMBOL_LINKAGE_NAME (symbol));
96
}
97
 
98
static PyObject *
99
sympy_get_print_name (PyObject *self, void *closure)
100
{
101
  struct symbol *symbol = NULL;
102
 
103
  SYMPY_REQUIRE_VALID (self, symbol);
104
 
105
  return sympy_str (self);
106
}
107
 
108
static PyObject *
109
sympy_get_addr_class (PyObject *self, void *closure)
110
{
111
  struct symbol *symbol = NULL;
112
 
113
  SYMPY_REQUIRE_VALID (self, symbol);
114
 
115
  return PyInt_FromLong (SYMBOL_CLASS (symbol));
116
}
117
 
118
static PyObject *
119
sympy_is_argument (PyObject *self, void *closure)
120
{
121
  struct symbol *symbol = NULL;
122
 
123
  SYMPY_REQUIRE_VALID (self, symbol);
124
 
125
  return PyBool_FromLong (SYMBOL_IS_ARGUMENT (symbol));
126
}
127
 
128
static PyObject *
129
sympy_is_constant (PyObject *self, void *closure)
130
{
131
  struct symbol *symbol = NULL;
132
  enum address_class class;
133
 
134
  SYMPY_REQUIRE_VALID (self, symbol);
135
 
136
  class = SYMBOL_CLASS (symbol);
137
 
138
  return PyBool_FromLong (class == LOC_CONST || class == LOC_CONST_BYTES);
139
}
140
 
141
static PyObject *
142
sympy_is_function (PyObject *self, void *closure)
143
{
144
  struct symbol *symbol = NULL;
145
  enum address_class class;
146
 
147
  SYMPY_REQUIRE_VALID (self, symbol);
148
 
149
  class = SYMBOL_CLASS (symbol);
150
 
151
  return PyBool_FromLong (class == LOC_BLOCK);
152
}
153
 
154
static PyObject *
155
sympy_is_variable (PyObject *self, void *closure)
156
{
157
  struct symbol *symbol = NULL;
158
  enum address_class class;
159
 
160
  SYMPY_REQUIRE_VALID (self, symbol);
161
 
162
  class = SYMBOL_CLASS (symbol);
163
 
164
  return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (symbol)
165
                          && (class == LOC_LOCAL || class == LOC_REGISTER
166
                              || class == LOC_STATIC || class == LOC_COMPUTED
167
                              || class == LOC_OPTIMIZED_OUT));
168
}
169
 
170
/* Given a symbol, and a symbol_object that has previously been
171
   allocated and initialized, populate the symbol_object with the
172
   struct symbol data.  Also, register the symbol_object life-cycle
173
   with the life-cycle of the the object file associated with this
174
   symbol, if needed.  */
175
static void
176
set_symbol (symbol_object *obj, struct symbol *symbol)
177
{
178
  obj->symbol = symbol;
179
  obj->prev = NULL;
180
  if (SYMBOL_SYMTAB (symbol))
181
    {
182
      obj->next = objfile_data (SYMBOL_SYMTAB (symbol)->objfile,
183
                                sympy_objfile_data_key);
184
 
185
      if (obj->next)
186
        obj->next->prev = obj;
187
      set_objfile_data (SYMBOL_SYMTAB (symbol)->objfile,
188
                        sympy_objfile_data_key, obj);
189
    }
190
  else
191
    obj->next = NULL;
192
}
193
 
194
/* Create a new symbol object (gdb.Symbol) that encapsulates the struct
195
   symbol object from GDB.  */
196
PyObject *
197
symbol_to_symbol_object (struct symbol *sym)
198
{
199
  symbol_object *sym_obj;
200
 
201
  sym_obj = PyObject_New (symbol_object, &symbol_object_type);
202
  if (sym_obj)
203
    set_symbol (sym_obj, sym);
204
 
205
  return (PyObject *) sym_obj;
206
}
207
 
208
/* Return the symbol that is wrapped by this symbol object.  */
209
struct symbol *
210
symbol_object_to_symbol (PyObject *obj)
211
{
212
  if (! PyObject_TypeCheck (obj, &symbol_object_type))
213
    return NULL;
214
  return ((symbol_object *) obj)->symbol;
215
}
216
 
217
static void
218
sympy_dealloc (PyObject *obj)
219
{
220
  symbol_object *sym_obj = (symbol_object *) obj;
221
 
222
  if (sym_obj->prev)
223
    sym_obj->prev->next = sym_obj->next;
224
  else if (SYMBOL_SYMTAB (sym_obj->symbol))
225
    {
226
      set_objfile_data (SYMBOL_SYMTAB (sym_obj->symbol)->objfile,
227
                        sympy_objfile_data_key, sym_obj->next);
228
    }
229
  if (sym_obj->next)
230
    sym_obj->next->prev = sym_obj->prev;
231
  sym_obj->symbol = NULL;
232
}
233
 
234
/* Implementation of
235
   gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
236
   A tuple with 2 elements is always returned.  The first is the symbol
237
   object or None, the second is a boolean with the value of
238
   is_a_field_of_this (see comment in lookup_symbol_in_language).  */
239
PyObject *
240
gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
241
{
242
  int domain = VAR_DOMAIN, is_a_field_of_this = 0;
243
  const char *name;
244
  static char *keywords[] = { "name", "block", "domain", NULL };
245
  struct symbol *symbol;
246
  PyObject *block_obj = NULL, *ret_tuple, *sym_obj, *bool_obj;
247
  struct block *block = NULL;
248
 
249
  if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
250
                                     &block_object_type, &block_obj, &domain))
251
    return NULL;
252
 
253
  if (block_obj)
254
    block = block_object_to_block (block_obj);
255
  else
256
    {
257
      struct frame_info *selected_frame;
258
      volatile struct gdb_exception except;
259
 
260
      TRY_CATCH (except, RETURN_MASK_ALL)
261
        {
262
          selected_frame  = get_selected_frame (_("No frame selected."));
263
          block = block_for_pc (get_frame_address_in_block (selected_frame));
264
        }
265
      GDB_PY_HANDLE_EXCEPTION (except);
266
    }
267
 
268
  symbol = lookup_symbol (name, block, domain, &is_a_field_of_this);
269
 
270
  ret_tuple = PyTuple_New (2);
271
  if (!ret_tuple)
272
    return NULL;
273
 
274
  if (symbol)
275
    {
276
      sym_obj = symbol_to_symbol_object (symbol);
277
      if (!sym_obj)
278
        {
279
          Py_DECREF (ret_tuple);
280
          return NULL;
281
        }
282
    }
283
  else
284
    {
285
      sym_obj = Py_None;
286
      Py_INCREF (Py_None);
287
    }
288
  PyTuple_SET_ITEM (ret_tuple, 0, sym_obj);
289
 
290
  bool_obj = is_a_field_of_this? Py_True : Py_False;
291
  Py_INCREF (bool_obj);
292
  PyTuple_SET_ITEM (ret_tuple, 1, bool_obj);
293
 
294
  return ret_tuple;
295
}
296
 
297
/* This function is called when an objfile is about to be freed.
298
   Invalidate the symbol as further actions on the symbol would result
299
   in bad data.  All access to obj->symbol should be gated by
300
   SYMPY_REQUIRE_VALID which will raise an exception on invalid
301
   symbols.  */
302
static void
303
del_objfile_symbols (struct objfile *objfile, void *datum)
304
{
305
  symbol_object *obj = datum;
306
  while (obj)
307
    {
308
      symbol_object *next = obj->next;
309
 
310
      obj->symbol = NULL;
311
      obj->next = NULL;
312
      obj->prev = NULL;
313
 
314
      obj = next;
315
    }
316
}
317
 
318
void
319
gdbpy_initialize_symbols (void)
320
{
321
  if (PyType_Ready (&symbol_object_type) < 0)
322
    return;
323
 
324
  /* Register an objfile "free" callback so we can properly
325
     invalidate symbol when an object file that is about to be
326
     deleted.  */
327
  sympy_objfile_data_key
328
    = register_objfile_data_with_cleanup (NULL, del_objfile_symbols);
329
 
330
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF);
331
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST", LOC_CONST);
332
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC", LOC_STATIC);
333
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER", LOC_REGISTER);
334
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG", LOC_ARG);
335
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG", LOC_REF_ARG);
336
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL", LOC_LOCAL);
337
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF", LOC_TYPEDEF);
338
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL", LOC_LABEL);
339
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK", LOC_BLOCK);
340
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES",
341
                           LOC_CONST_BYTES);
342
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED", LOC_UNRESOLVED);
343
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT",
344
                           LOC_OPTIMIZED_OUT);
345
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED", LOC_COMPUTED);
346
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR",
347
                           LOC_REGPARM_ADDR);
348
  PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN", UNDEF_DOMAIN);
349
  PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN", VAR_DOMAIN);
350
  PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN", STRUCT_DOMAIN);
351
  PyModule_AddIntConstant (gdb_module, "SYMBOL_LABEL_DOMAIN", LABEL_DOMAIN);
352
  PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN",
353
                           VARIABLES_DOMAIN);
354
  PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN",
355
                           FUNCTIONS_DOMAIN);
356
  PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN", TYPES_DOMAIN);
357
 
358
  Py_INCREF (&symbol_object_type);
359
  PyModule_AddObject (gdb_module, "Symbol", (PyObject *) &symbol_object_type);
360
}
361
 
362
 
363
 
364
static PyGetSetDef symbol_object_getset[] = {
365
  { "symtab", sympy_get_symtab, NULL,
366
    "Symbol table in which the symbol appears.", NULL },
367
  { "name", sympy_get_name, NULL,
368
    "Name of the symbol, as it appears in the source code.", NULL },
369
  { "linkage_name", sympy_get_linkage_name, NULL,
370
    "Name of the symbol, as used by the linker (i.e., may be mangled).", NULL },
371
  { "print_name", sympy_get_print_name, NULL,
372
    "Name of the symbol in a form suitable for output.\n\
373
This is either name or linkage_name, depending on whether the user asked GDB\n\
374
to display demangled or mangled names.", NULL },
375
  { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." },
376
  { "is_argument", sympy_is_argument, NULL,
377
    "True if the symbol is an argument of a function." },
378
  { "is_constant", sympy_is_constant, NULL,
379
    "True if the symbol is a constant." },
380
  { "is_function", sympy_is_function, NULL,
381
    "True if the symbol is a function or method." },
382
  { "is_variable", sympy_is_variable, NULL,
383
    "True if the symbol is a variable." },
384
  { NULL }  /* Sentinel */
385
};
386
 
387
PyTypeObject symbol_object_type = {
388
  PyObject_HEAD_INIT (NULL)
389
  0,                               /*ob_size*/
390
  "gdb.Symbol",                   /*tp_name*/
391
  sizeof (symbol_object),         /*tp_basicsize*/
392
  0,                               /*tp_itemsize*/
393
  sympy_dealloc,                  /*tp_dealloc*/
394
  0,                               /*tp_print*/
395
  0,                               /*tp_getattr*/
396
  0,                               /*tp_setattr*/
397
  0,                               /*tp_compare*/
398
  0,                               /*tp_repr*/
399
  0,                               /*tp_as_number*/
400
  0,                               /*tp_as_sequence*/
401
  0,                               /*tp_as_mapping*/
402
  0,                               /*tp_hash */
403
  0,                               /*tp_call*/
404
  sympy_str,                      /*tp_str*/
405
  0,                               /*tp_getattro*/
406
  0,                               /*tp_setattro*/
407
  0,                               /*tp_as_buffer*/
408
  Py_TPFLAGS_DEFAULT,             /*tp_flags*/
409
  "GDB symbol object",            /*tp_doc */
410
  0,                               /*tp_traverse */
411
  0,                               /*tp_clear */
412
  0,                               /*tp_richcompare */
413
  0,                               /*tp_weaklistoffset */
414
  0,                               /*tp_iter */
415
  0,                               /*tp_iternext */
416
  0,                               /*tp_methods */
417
  0,                               /*tp_members */
418
  symbol_object_getset            /*tp_getset */
419
};

powered by: WebSVN 2.1.0

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