]>
Commit | Line | Data |
---|---|---|
f3e9a817 PM |
1 | /* Python interface to symbols. |
2 | ||
213516ef | 3 | Copyright (C) 2008-2023 Free Software Foundation, Inc. |
f3e9a817 PM |
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" | |
b940a061 | 21 | #include "top.h" /* For force_quit (). */ |
f3e9a817 | 22 | #include "block.h" |
f3e9a817 PM |
23 | #include "frame.h" |
24 | #include "symtab.h" | |
25 | #include "python-internal.h" | |
26 | #include "objfiles.h" | |
086baaf1 | 27 | #include "symfile.h" |
f3e9a817 | 28 | |
f99b5177 | 29 | struct symbol_object { |
f3e9a817 PM |
30 | PyObject_HEAD |
31 | /* The GDB symbol structure this object is wrapping. */ | |
32 | struct symbol *symbol; | |
33 | /* A symbol object is associated with an objfile, so keep track with | |
34 | doubly-linked list, rooted in the objfile. This lets us | |
35 | invalidate the underlying struct symbol when the objfile is | |
36 | deleted. */ | |
f99b5177 TT |
37 | symbol_object *prev; |
38 | symbol_object *next; | |
39 | }; | |
f3e9a817 PM |
40 | |
41 | /* Require a valid symbol. All access to symbol_object->symbol should be | |
42 | gated by this call. */ | |
43 | #define SYMPY_REQUIRE_VALID(symbol_obj, symbol) \ | |
44 | do { \ | |
45 | symbol = symbol_object_to_symbol (symbol_obj); \ | |
46 | if (symbol == NULL) \ | |
47 | { \ | |
48 | PyErr_SetString (PyExc_RuntimeError, \ | |
49 | _("Symbol is invalid.")); \ | |
50 | return NULL; \ | |
51 | } \ | |
52 | } while (0) | |
53 | ||
08b8a139 TT |
54 | /* A deleter that is used when an objfile is about to be freed. */ |
55 | struct symbol_object_deleter | |
56 | { | |
57 | void operator() (symbol_object *obj) | |
58 | { | |
59 | while (obj) | |
60 | { | |
61 | symbol_object *next = obj->next; | |
62 | ||
63 | obj->symbol = NULL; | |
64 | obj->next = NULL; | |
65 | obj->prev = NULL; | |
66 | ||
67 | obj = next; | |
68 | } | |
69 | } | |
70 | }; | |
71 | ||
72 | static const registry<objfile>::key<symbol_object, symbol_object_deleter> | |
73 | sympy_objfile_data_key; | |
f3e9a817 PM |
74 | |
75 | static PyObject * | |
76 | sympy_str (PyObject *self) | |
77 | { | |
78 | PyObject *result; | |
79 | struct symbol *symbol = NULL; | |
80 | ||
81 | SYMPY_REQUIRE_VALID (self, symbol); | |
82 | ||
5aee4587 | 83 | result = PyUnicode_FromString (symbol->print_name ()); |
f3e9a817 PM |
84 | |
85 | return result; | |
86 | } | |
87 | ||
457e09f0 DE |
88 | static PyObject * |
89 | sympy_get_type (PyObject *self, void *closure) | |
90 | { | |
91 | struct symbol *symbol = NULL; | |
92 | ||
93 | SYMPY_REQUIRE_VALID (self, symbol); | |
94 | ||
5f9c5a63 | 95 | if (symbol->type () == NULL) |
457e09f0 DE |
96 | { |
97 | Py_INCREF (Py_None); | |
98 | return Py_None; | |
99 | } | |
100 | ||
5f9c5a63 | 101 | return type_to_type_object (symbol->type ()); |
457e09f0 DE |
102 | } |
103 | ||
f3e9a817 PM |
104 | static PyObject * |
105 | sympy_get_symtab (PyObject *self, void *closure) | |
106 | { | |
107 | struct symbol *symbol = NULL; | |
108 | ||
109 | SYMPY_REQUIRE_VALID (self, symbol); | |
110 | ||
7b3ecc75 | 111 | if (!symbol->is_objfile_owned ()) |
1994afbf DE |
112 | Py_RETURN_NONE; |
113 | ||
4206d69e | 114 | return symtab_to_symtab_object (symbol->symtab ()); |
f3e9a817 PM |
115 | } |
116 | ||
117 | static PyObject * | |
118 | sympy_get_name (PyObject *self, void *closure) | |
119 | { | |
120 | struct symbol *symbol = NULL; | |
121 | ||
122 | SYMPY_REQUIRE_VALID (self, symbol); | |
123 | ||
5aee4587 | 124 | return PyUnicode_FromString (symbol->natural_name ()); |
f3e9a817 PM |
125 | } |
126 | ||
127 | static PyObject * | |
128 | sympy_get_linkage_name (PyObject *self, void *closure) | |
129 | { | |
130 | struct symbol *symbol = NULL; | |
131 | ||
132 | SYMPY_REQUIRE_VALID (self, symbol); | |
133 | ||
5aee4587 | 134 | return PyUnicode_FromString (symbol->linkage_name ()); |
f3e9a817 PM |
135 | } |
136 | ||
137 | static PyObject * | |
138 | sympy_get_print_name (PyObject *self, void *closure) | |
139 | { | |
140 | struct symbol *symbol = NULL; | |
141 | ||
142 | SYMPY_REQUIRE_VALID (self, symbol); | |
143 | ||
144 | return sympy_str (self); | |
145 | } | |
146 | ||
147 | static PyObject * | |
148 | sympy_get_addr_class (PyObject *self, void *closure) | |
149 | { | |
150 | struct symbol *symbol = NULL; | |
151 | ||
152 | SYMPY_REQUIRE_VALID (self, symbol); | |
153 | ||
66d7f48f | 154 | return gdb_py_object_from_longest (symbol->aclass ()).release (); |
f3e9a817 PM |
155 | } |
156 | ||
157 | static PyObject * | |
158 | sympy_is_argument (PyObject *self, void *closure) | |
159 | { | |
160 | struct symbol *symbol = NULL; | |
161 | ||
162 | SYMPY_REQUIRE_VALID (self, symbol); | |
163 | ||
d9743061 | 164 | return PyBool_FromLong (symbol->is_argument ()); |
f3e9a817 PM |
165 | } |
166 | ||
167 | static PyObject * | |
168 | sympy_is_constant (PyObject *self, void *closure) | |
169 | { | |
170 | struct symbol *symbol = NULL; | |
fe978cb0 | 171 | enum address_class theclass; |
f3e9a817 PM |
172 | |
173 | SYMPY_REQUIRE_VALID (self, symbol); | |
174 | ||
66d7f48f | 175 | theclass = symbol->aclass (); |
f3e9a817 | 176 | |
fe978cb0 | 177 | return PyBool_FromLong (theclass == LOC_CONST || theclass == LOC_CONST_BYTES); |
f3e9a817 PM |
178 | } |
179 | ||
180 | static PyObject * | |
181 | sympy_is_function (PyObject *self, void *closure) | |
182 | { | |
183 | struct symbol *symbol = NULL; | |
fe978cb0 | 184 | enum address_class theclass; |
f3e9a817 PM |
185 | |
186 | SYMPY_REQUIRE_VALID (self, symbol); | |
187 | ||
66d7f48f | 188 | theclass = symbol->aclass (); |
f3e9a817 | 189 | |
fe978cb0 | 190 | return PyBool_FromLong (theclass == LOC_BLOCK); |
f3e9a817 PM |
191 | } |
192 | ||
193 | static PyObject * | |
194 | sympy_is_variable (PyObject *self, void *closure) | |
195 | { | |
196 | struct symbol *symbol = NULL; | |
fe978cb0 | 197 | enum address_class theclass; |
f3e9a817 PM |
198 | |
199 | SYMPY_REQUIRE_VALID (self, symbol); | |
200 | ||
66d7f48f | 201 | theclass = symbol->aclass (); |
f3e9a817 | 202 | |
d9743061 | 203 | return PyBool_FromLong (!symbol->is_argument () |
fe978cb0 PA |
204 | && (theclass == LOC_LOCAL || theclass == LOC_REGISTER |
205 | || theclass == LOC_STATIC || theclass == LOC_COMPUTED | |
206 | || theclass == LOC_OPTIMIZED_OUT)); | |
f3e9a817 PM |
207 | } |
208 | ||
f0823d2c TT |
209 | /* Implementation of gdb.Symbol.needs_frame -> Boolean. |
210 | Returns true iff the symbol needs a frame for evaluation. */ | |
211 | ||
212 | static PyObject * | |
213 | sympy_needs_frame (PyObject *self, void *closure) | |
214 | { | |
215 | struct symbol *symbol = NULL; | |
f0823d2c TT |
216 | int result = 0; |
217 | ||
218 | SYMPY_REQUIRE_VALID (self, symbol); | |
219 | ||
a70b8144 | 220 | try |
f0823d2c TT |
221 | { |
222 | result = symbol_read_needs_frame (symbol); | |
223 | } | |
230d2906 | 224 | catch (const gdb_exception &except) |
492d29ea PA |
225 | { |
226 | GDB_PY_HANDLE_EXCEPTION (except); | |
227 | } | |
f0823d2c TT |
228 | |
229 | if (result) | |
230 | Py_RETURN_TRUE; | |
231 | Py_RETURN_FALSE; | |
232 | } | |
233 | ||
64e7d9dd TT |
234 | /* Implementation of gdb.Symbol.line -> int. |
235 | Returns the line number at which the symbol was defined. */ | |
236 | ||
237 | static PyObject * | |
238 | sympy_line (PyObject *self, void *closure) | |
239 | { | |
240 | struct symbol *symbol = NULL; | |
241 | ||
242 | SYMPY_REQUIRE_VALID (self, symbol); | |
243 | ||
5d0027b9 | 244 | return gdb_py_object_from_longest (symbol->line ()).release (); |
64e7d9dd TT |
245 | } |
246 | ||
29703da4 PM |
247 | /* Implementation of gdb.Symbol.is_valid (self) -> Boolean. |
248 | Returns True if this Symbol still exists in GDB. */ | |
249 | ||
250 | static PyObject * | |
251 | sympy_is_valid (PyObject *self, PyObject *args) | |
252 | { | |
253 | struct symbol *symbol = NULL; | |
254 | ||
255 | symbol = symbol_object_to_symbol (self); | |
256 | if (symbol == NULL) | |
257 | Py_RETURN_FALSE; | |
258 | ||
259 | Py_RETURN_TRUE; | |
260 | } | |
261 | ||
f0823d2c TT |
262 | /* Implementation of gdb.Symbol.value (self[, frame]) -> gdb.Value. Returns |
263 | the value of the symbol, or an error in various circumstances. */ | |
264 | ||
265 | static PyObject * | |
266 | sympy_value (PyObject *self, PyObject *args) | |
267 | { | |
268 | struct symbol *symbol = NULL; | |
bd2b40ac | 269 | frame_info_ptr frame_info = NULL; |
f0823d2c | 270 | PyObject *frame_obj = NULL; |
f0823d2c TT |
271 | |
272 | if (!PyArg_ParseTuple (args, "|O", &frame_obj)) | |
273 | return NULL; | |
274 | ||
275 | if (frame_obj != NULL && !PyObject_TypeCheck (frame_obj, &frame_object_type)) | |
276 | { | |
277 | PyErr_SetString (PyExc_TypeError, "argument is not a frame"); | |
278 | return NULL; | |
279 | } | |
280 | ||
281 | SYMPY_REQUIRE_VALID (self, symbol); | |
66d7f48f | 282 | if (symbol->aclass () == LOC_TYPEDEF) |
f0823d2c TT |
283 | { |
284 | PyErr_SetString (PyExc_TypeError, "cannot get the value of a typedef"); | |
285 | return NULL; | |
286 | } | |
287 | ||
f3d3bbbc | 288 | PyObject *result = nullptr; |
a70b8144 | 289 | try |
f0823d2c TT |
290 | { |
291 | if (frame_obj != NULL) | |
292 | { | |
293 | frame_info = frame_object_to_frame_info (frame_obj); | |
294 | if (frame_info == NULL) | |
c6910659 | 295 | error (_("invalid frame")); |
f0823d2c | 296 | } |
256458bc | 297 | |
f0823d2c | 298 | if (symbol_read_needs_frame (symbol) && frame_info == NULL) |
c6910659 | 299 | error (_("symbol requires a frame to compute its value")); |
f0823d2c | 300 | |
63e43d3a PMR |
301 | /* TODO: currently, we have no way to recover the block in which SYMBOL |
302 | was found, so we have no block to pass to read_var_value. This will | |
303 | yield an incorrect value when symbol is not local to FRAME_INFO (this | |
304 | can happen with nested functions). */ | |
f3d3bbbc TT |
305 | scoped_value_mark free_values; |
306 | struct value *value = read_var_value (symbol, NULL, frame_info); | |
307 | result = value_to_value_object (value); | |
f0823d2c | 308 | } |
230d2906 | 309 | catch (const gdb_exception &except) |
492d29ea PA |
310 | { |
311 | GDB_PY_HANDLE_EXCEPTION (except); | |
312 | } | |
f0823d2c | 313 | |
f3d3bbbc | 314 | return result; |
f0823d2c TT |
315 | } |
316 | ||
f3e9a817 PM |
317 | /* Given a symbol, and a symbol_object that has previously been |
318 | allocated and initialized, populate the symbol_object with the | |
319 | struct symbol data. Also, register the symbol_object life-cycle | |
b021a221 | 320 | with the life-cycle of the object file associated with this |
f3e9a817 PM |
321 | symbol, if needed. */ |
322 | static void | |
323 | set_symbol (symbol_object *obj, struct symbol *symbol) | |
324 | { | |
325 | obj->symbol = symbol; | |
326 | obj->prev = NULL; | |
7b3ecc75 | 327 | if (symbol->is_objfile_owned () |
4206d69e | 328 | && symbol->symtab () != NULL) |
f3e9a817 | 329 | { |
e19b2d94 | 330 | struct objfile *objfile = symbol->objfile (); |
f3e9a817 | 331 | |
08b8a139 | 332 | obj->next = sympy_objfile_data_key.get (objfile); |
f3e9a817 PM |
333 | if (obj->next) |
334 | obj->next->prev = obj; | |
08b8a139 | 335 | sympy_objfile_data_key.set (objfile, obj); |
f3e9a817 PM |
336 | } |
337 | else | |
338 | obj->next = NULL; | |
339 | } | |
340 | ||
341 | /* Create a new symbol object (gdb.Symbol) that encapsulates the struct | |
342 | symbol object from GDB. */ | |
343 | PyObject * | |
344 | symbol_to_symbol_object (struct symbol *sym) | |
345 | { | |
346 | symbol_object *sym_obj; | |
347 | ||
348 | sym_obj = PyObject_New (symbol_object, &symbol_object_type); | |
349 | if (sym_obj) | |
350 | set_symbol (sym_obj, sym); | |
351 | ||
352 | return (PyObject *) sym_obj; | |
353 | } | |
354 | ||
355 | /* Return the symbol that is wrapped by this symbol object. */ | |
356 | struct symbol * | |
357 | symbol_object_to_symbol (PyObject *obj) | |
358 | { | |
359 | if (! PyObject_TypeCheck (obj, &symbol_object_type)) | |
360 | return NULL; | |
361 | return ((symbol_object *) obj)->symbol; | |
362 | } | |
363 | ||
364 | static void | |
365 | sympy_dealloc (PyObject *obj) | |
366 | { | |
367 | symbol_object *sym_obj = (symbol_object *) obj; | |
368 | ||
369 | if (sym_obj->prev) | |
370 | sym_obj->prev->next = sym_obj->next; | |
08be3fe3 | 371 | else if (sym_obj->symbol != NULL |
7b3ecc75 | 372 | && sym_obj->symbol->is_objfile_owned () |
4206d69e | 373 | && sym_obj->symbol->symtab () != NULL) |
08b8a139 | 374 | sympy_objfile_data_key.set (sym_obj->symbol->objfile (), sym_obj->next); |
f3e9a817 PM |
375 | if (sym_obj->next) |
376 | sym_obj->next->prev = sym_obj->prev; | |
377 | sym_obj->symbol = NULL; | |
2e953aca | 378 | Py_TYPE (obj)->tp_free (obj); |
f3e9a817 PM |
379 | } |
380 | ||
bb2bd584 MBB |
381 | /* __repr__ implementation for gdb.Symbol. */ |
382 | ||
383 | static PyObject * | |
384 | sympy_repr (PyObject *self) | |
385 | { | |
386 | const auto symbol = symbol_object_to_symbol (self); | |
387 | if (symbol == nullptr) | |
388 | return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name); | |
389 | ||
390 | return PyUnicode_FromFormat ("<%s print_name=%s>", Py_TYPE (self)->tp_name, | |
391 | symbol->print_name ()); | |
392 | } | |
393 | ||
f3e9a817 PM |
394 | /* Implementation of |
395 | gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this) | |
396 | A tuple with 2 elements is always returned. The first is the symbol | |
397 | object or None, the second is a boolean with the value of | |
398 | is_a_field_of_this (see comment in lookup_symbol_in_language). */ | |
6e6fbe60 | 399 | |
f3e9a817 PM |
400 | PyObject * |
401 | gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw) | |
402 | { | |
1993b719 TT |
403 | int domain = VAR_DOMAIN; |
404 | struct field_of_this_result is_a_field_of_this; | |
f3e9a817 | 405 | const char *name; |
2adadf51 | 406 | static const char *keywords[] = { "name", "block", "domain", NULL }; |
76dce0be | 407 | struct symbol *symbol = NULL; |
37fce74f | 408 | PyObject *block_obj = NULL, *sym_obj, *bool_obj; |
9df2fbc4 | 409 | const struct block *block = NULL; |
f3e9a817 | 410 | |
2adadf51 PA |
411 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name, |
412 | &block_object_type, &block_obj, | |
413 | &domain)) | |
f3e9a817 PM |
414 | return NULL; |
415 | ||
416 | if (block_obj) | |
417 | block = block_object_to_block (block_obj); | |
418 | else | |
419 | { | |
bd2b40ac | 420 | frame_info_ptr selected_frame; |
f3e9a817 | 421 | |
a70b8144 | 422 | try |
f3e9a817 | 423 | { |
626e7282 JK |
424 | selected_frame = get_selected_frame (_("No frame selected.")); |
425 | block = get_frame_block (selected_frame, NULL); | |
f3e9a817 | 426 | } |
230d2906 | 427 | catch (const gdb_exception &except) |
492d29ea PA |
428 | { |
429 | GDB_PY_HANDLE_EXCEPTION (except); | |
430 | } | |
f3e9a817 PM |
431 | } |
432 | ||
a70b8144 | 433 | try |
76dce0be | 434 | { |
aead7601 SM |
435 | symbol = lookup_symbol (name, block, (domain_enum) domain, |
436 | &is_a_field_of_this).symbol; | |
76dce0be | 437 | } |
230d2906 | 438 | catch (const gdb_exception &except) |
492d29ea PA |
439 | { |
440 | GDB_PY_HANDLE_EXCEPTION (except); | |
441 | } | |
f3e9a817 | 442 | |
7780f186 | 443 | gdbpy_ref<> ret_tuple (PyTuple_New (2)); |
37fce74f | 444 | if (ret_tuple == NULL) |
f3e9a817 PM |
445 | return NULL; |
446 | ||
447 | if (symbol) | |
448 | { | |
449 | sym_obj = symbol_to_symbol_object (symbol); | |
450 | if (!sym_obj) | |
37fce74f | 451 | return NULL; |
f3e9a817 PM |
452 | } |
453 | else | |
454 | { | |
455 | sym_obj = Py_None; | |
456 | Py_INCREF (Py_None); | |
457 | } | |
37fce74f | 458 | PyTuple_SET_ITEM (ret_tuple.get (), 0, sym_obj); |
f3e9a817 | 459 | |
c86acd3f | 460 | bool_obj = PyBool_FromLong (is_a_field_of_this.type != NULL); |
37fce74f | 461 | PyTuple_SET_ITEM (ret_tuple.get (), 1, bool_obj); |
f3e9a817 | 462 | |
37fce74f | 463 | return ret_tuple.release (); |
f3e9a817 PM |
464 | } |
465 | ||
6e6fbe60 DE |
466 | /* Implementation of |
467 | gdb.lookup_global_symbol (name [, domain]) -> symbol or None. */ | |
468 | ||
469 | PyObject * | |
470 | gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw) | |
471 | { | |
472 | int domain = VAR_DOMAIN; | |
473 | const char *name; | |
2adadf51 | 474 | static const char *keywords[] = { "name", "domain", NULL }; |
76dce0be | 475 | struct symbol *symbol = NULL; |
6e6fbe60 DE |
476 | PyObject *sym_obj; |
477 | ||
2adadf51 PA |
478 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name, |
479 | &domain)) | |
6e6fbe60 DE |
480 | return NULL; |
481 | ||
a70b8144 | 482 | try |
76dce0be | 483 | { |
aead7601 | 484 | symbol = lookup_global_symbol (name, NULL, (domain_enum) domain).symbol; |
76dce0be | 485 | } |
230d2906 | 486 | catch (const gdb_exception &except) |
492d29ea PA |
487 | { |
488 | GDB_PY_HANDLE_EXCEPTION (except); | |
489 | } | |
6e6fbe60 DE |
490 | |
491 | if (symbol) | |
492 | { | |
493 | sym_obj = symbol_to_symbol_object (symbol); | |
494 | if (!sym_obj) | |
495 | return NULL; | |
496 | } | |
497 | else | |
498 | { | |
499 | sym_obj = Py_None; | |
500 | Py_INCREF (Py_None); | |
501 | } | |
502 | ||
503 | return sym_obj; | |
504 | } | |
505 | ||
2906593f | 506 | /* Implementation of |
09ff83af | 507 | gdb.lookup_static_symbol (name [, domain]) -> symbol or None. */ |
2906593f CB |
508 | |
509 | PyObject * | |
510 | gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw) | |
511 | { | |
512 | const char *name; | |
513 | int domain = VAR_DOMAIN; | |
514 | static const char *keywords[] = { "name", "domain", NULL }; | |
515 | struct symbol *symbol = NULL; | |
516 | PyObject *sym_obj; | |
517 | ||
518 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name, | |
519 | &domain)) | |
520 | return NULL; | |
521 | ||
09ff83af AB |
522 | /* In order to find static symbols associated with the "current" object |
523 | file ahead of those from other object files, we first need to see if | |
524 | we can acquire a current block. If this fails however, then we still | |
525 | want to search all static symbols, so don't throw an exception just | |
526 | yet. */ | |
527 | const struct block *block = NULL; | |
2906593f CB |
528 | try |
529 | { | |
bd2b40ac | 530 | frame_info_ptr selected_frame |
09ff83af AB |
531 | = get_selected_frame (_("No frame selected.")); |
532 | block = get_frame_block (selected_frame, NULL); | |
533 | } | |
b940a061 KB |
534 | catch (const gdb_exception_forced_quit &e) |
535 | { | |
536 | quit_force (NULL, 0); | |
537 | } | |
09ff83af AB |
538 | catch (const gdb_exception &except) |
539 | { | |
540 | /* Nothing. */ | |
541 | } | |
542 | ||
543 | try | |
544 | { | |
545 | if (block != nullptr) | |
546 | symbol | |
547 | = lookup_symbol_in_static_block (name, block, | |
548 | (domain_enum) domain).symbol; | |
549 | ||
550 | if (symbol == nullptr) | |
551 | symbol = lookup_static_symbol (name, (domain_enum) domain).symbol; | |
2906593f CB |
552 | } |
553 | catch (const gdb_exception &except) | |
554 | { | |
555 | GDB_PY_HANDLE_EXCEPTION (except); | |
556 | } | |
557 | ||
558 | if (symbol) | |
559 | { | |
560 | sym_obj = symbol_to_symbol_object (symbol); | |
561 | if (!sym_obj) | |
562 | return NULL; | |
563 | } | |
564 | else | |
565 | { | |
566 | sym_obj = Py_None; | |
567 | Py_INCREF (Py_None); | |
568 | } | |
569 | ||
570 | return sym_obj; | |
571 | } | |
572 | ||
086baaf1 AB |
573 | /* Implementation of |
574 | gdb.lookup_static_symbols (name [, domain]) -> symbol list. | |
575 | ||
576 | Returns a list of all static symbols matching NAME in DOMAIN. */ | |
577 | ||
578 | PyObject * | |
579 | gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw) | |
580 | { | |
581 | const char *name; | |
582 | int domain = VAR_DOMAIN; | |
583 | static const char *keywords[] = { "name", "domain", NULL }; | |
584 | ||
585 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name, | |
586 | &domain)) | |
587 | return NULL; | |
588 | ||
589 | gdbpy_ref<> return_list (PyList_New (0)); | |
590 | if (return_list == NULL) | |
591 | return NULL; | |
592 | ||
593 | try | |
594 | { | |
595 | /* Expand any symtabs that contain potentially matching symbols. */ | |
596 | lookup_name_info lookup_name (name, symbol_name_match_type::FULL); | |
03a8ea51 TT |
597 | expand_symtabs_matching (NULL, lookup_name, NULL, NULL, |
598 | SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK, | |
599 | ALL_DOMAIN); | |
086baaf1 AB |
600 | |
601 | for (objfile *objfile : current_program_space->objfiles ()) | |
602 | { | |
603 | for (compunit_symtab *cust : objfile->compunits ()) | |
604 | { | |
605 | const struct blockvector *bv; | |
086baaf1 | 606 | |
af39c5c8 | 607 | bv = cust->blockvector (); |
63d609de | 608 | const struct block *block = bv->static_block (); |
086baaf1 AB |
609 | |
610 | if (block != nullptr) | |
611 | { | |
612 | symbol *symbol = lookup_symbol_in_static_block | |
613 | (name, block, (domain_enum) domain).symbol; | |
614 | ||
615 | if (symbol != nullptr) | |
616 | { | |
617 | PyObject *sym_obj | |
618 | = symbol_to_symbol_object (symbol); | |
619 | if (PyList_Append (return_list.get (), sym_obj) == -1) | |
620 | return NULL; | |
621 | } | |
622 | } | |
623 | } | |
624 | } | |
625 | } | |
626 | catch (const gdb_exception &except) | |
627 | { | |
628 | GDB_PY_HANDLE_EXCEPTION (except); | |
629 | } | |
630 | ||
631 | return return_list.release (); | |
632 | } | |
633 | ||
3965bff5 | 634 | static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION |
8e3685bf AB |
635 | gdbpy_initialize_symbols (void) |
636 | { | |
637 | if (PyType_Ready (&symbol_object_type) < 0) | |
638 | return -1; | |
f3e9a817 | 639 | |
999633ed TT |
640 | if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0 |
641 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST", | |
642 | LOC_CONST) < 0 | |
643 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC", | |
644 | LOC_STATIC) < 0 | |
645 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER", | |
646 | LOC_REGISTER) < 0 | |
647 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG", | |
648 | LOC_ARG) < 0 | |
649 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG", | |
650 | LOC_REF_ARG) < 0 | |
651 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL", | |
652 | LOC_LOCAL) < 0 | |
653 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF", | |
654 | LOC_TYPEDEF) < 0 | |
655 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL", | |
656 | LOC_LABEL) < 0 | |
657 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK", | |
658 | LOC_BLOCK) < 0 | |
659 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES", | |
660 | LOC_CONST_BYTES) < 0 | |
661 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED", | |
662 | LOC_UNRESOLVED) < 0 | |
663 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT", | |
664 | LOC_OPTIMIZED_OUT) < 0 | |
665 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED", | |
666 | LOC_COMPUTED) < 0 | |
51e78fc5 TT |
667 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMMON_BLOCK", |
668 | LOC_COMMON_BLOCK) < 0 | |
999633ed TT |
669 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR", |
670 | LOC_REGPARM_ADDR) < 0 | |
671 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN", | |
672 | UNDEF_DOMAIN) < 0 | |
673 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN", | |
674 | VAR_DOMAIN) < 0 | |
675 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN", | |
676 | STRUCT_DOMAIN) < 0 | |
6ac5237c HD |
677 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LABEL_DOMAIN", |
678 | LABEL_DOMAIN) < 0 | |
51e78fc5 TT |
679 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_MODULE_DOMAIN", |
680 | MODULE_DOMAIN) < 0 | |
681 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_COMMON_BLOCK_DOMAIN", | |
682 | COMMON_BLOCK_DOMAIN) < 0) | |
683 | return -1; | |
684 | ||
685 | /* These remain defined for compatibility, but as they were never | |
686 | correct, they are no longer documented. Eventually we can remove | |
687 | them. These exist because at one time, enum search_domain and | |
688 | enum domain_enum_tag were combined -- but different values were | |
689 | used differently. Here we try to give them values that will make | |
690 | sense if they are passed to gdb.lookup_symbol. */ | |
691 | if (PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN", | |
692 | VAR_DOMAIN) < 0 | |
999633ed | 693 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN", |
51e78fc5 | 694 | VAR_DOMAIN) < 0 |
999633ed | 695 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN", |
51e78fc5 | 696 | VAR_DOMAIN) < 0) |
999633ed | 697 | return -1; |
f3e9a817 | 698 | |
aa36459a TT |
699 | return gdb_pymodule_addobject (gdb_module, "Symbol", |
700 | (PyObject *) &symbol_object_type); | |
f3e9a817 PM |
701 | } |
702 | ||
3965bff5 AB |
703 | GDBPY_INITIALIZE_FILE (gdbpy_initialize_symbols); |
704 | ||
f3e9a817 PM |
705 | \f |
706 | ||
0d1f4ceb | 707 | static gdb_PyGetSetDef symbol_object_getset[] = { |
457e09f0 DE |
708 | { "type", sympy_get_type, NULL, |
709 | "Type of the symbol.", NULL }, | |
f3e9a817 PM |
710 | { "symtab", sympy_get_symtab, NULL, |
711 | "Symbol table in which the symbol appears.", NULL }, | |
712 | { "name", sympy_get_name, NULL, | |
713 | "Name of the symbol, as it appears in the source code.", NULL }, | |
714 | { "linkage_name", sympy_get_linkage_name, NULL, | |
9a2b4c1b MS |
715 | "Name of the symbol, as used by the linker (i.e., may be mangled).", |
716 | NULL }, | |
f3e9a817 PM |
717 | { "print_name", sympy_get_print_name, NULL, |
718 | "Name of the symbol in a form suitable for output.\n\ | |
719 | This is either name or linkage_name, depending on whether the user asked GDB\n\ | |
720 | to display demangled or mangled names.", NULL }, | |
721 | { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." }, | |
722 | { "is_argument", sympy_is_argument, NULL, | |
723 | "True if the symbol is an argument of a function." }, | |
724 | { "is_constant", sympy_is_constant, NULL, | |
725 | "True if the symbol is a constant." }, | |
726 | { "is_function", sympy_is_function, NULL, | |
727 | "True if the symbol is a function or method." }, | |
728 | { "is_variable", sympy_is_variable, NULL, | |
729 | "True if the symbol is a variable." }, | |
f0823d2c TT |
730 | { "needs_frame", sympy_needs_frame, NULL, |
731 | "True if the symbol requires a frame for evaluation." }, | |
64e7d9dd TT |
732 | { "line", sympy_line, NULL, |
733 | "The source line number at which the symbol was defined." }, | |
f3e9a817 PM |
734 | { NULL } /* Sentinel */ |
735 | }; | |
736 | ||
29703da4 PM |
737 | static PyMethodDef symbol_object_methods[] = { |
738 | { "is_valid", sympy_is_valid, METH_NOARGS, | |
739 | "is_valid () -> Boolean.\n\ | |
740 | Return true if this symbol is valid, false if not." }, | |
f0823d2c TT |
741 | { "value", sympy_value, METH_VARARGS, |
742 | "value ([frame]) -> gdb.Value\n\ | |
743 | Return the value of the symbol." }, | |
29703da4 PM |
744 | {NULL} /* Sentinel */ |
745 | }; | |
746 | ||
f3e9a817 | 747 | PyTypeObject symbol_object_type = { |
9a27f2c6 | 748 | PyVarObject_HEAD_INIT (NULL, 0) |
f3e9a817 PM |
749 | "gdb.Symbol", /*tp_name*/ |
750 | sizeof (symbol_object), /*tp_basicsize*/ | |
751 | 0, /*tp_itemsize*/ | |
752 | sympy_dealloc, /*tp_dealloc*/ | |
753 | 0, /*tp_print*/ | |
754 | 0, /*tp_getattr*/ | |
755 | 0, /*tp_setattr*/ | |
756 | 0, /*tp_compare*/ | |
bb2bd584 | 757 | sympy_repr, /*tp_repr*/ |
f3e9a817 PM |
758 | 0, /*tp_as_number*/ |
759 | 0, /*tp_as_sequence*/ | |
760 | 0, /*tp_as_mapping*/ | |
761 | 0, /*tp_hash */ | |
762 | 0, /*tp_call*/ | |
763 | sympy_str, /*tp_str*/ | |
764 | 0, /*tp_getattro*/ | |
765 | 0, /*tp_setattro*/ | |
766 | 0, /*tp_as_buffer*/ | |
767 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
768 | "GDB symbol object", /*tp_doc */ | |
769 | 0, /*tp_traverse */ | |
770 | 0, /*tp_clear */ | |
771 | 0, /*tp_richcompare */ | |
772 | 0, /*tp_weaklistoffset */ | |
773 | 0, /*tp_iter */ | |
774 | 0, /*tp_iternext */ | |
29703da4 | 775 | symbol_object_methods, /*tp_methods */ |
f3e9a817 PM |
776 | 0, /*tp_members */ |
777 | symbol_object_getset /*tp_getset */ | |
778 | }; |