]>
Commit | Line | Data |
---|---|---|
f3e9a817 PM |
1 | /* Python interface to symbols. |
2 | ||
30243af8 | 3 | Copyright (C) 2008-2025 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 | ||
ef0f16cc | 20 | #include "top.h" |
f3e9a817 | 21 | #include "block.h" |
f3e9a817 PM |
22 | #include "frame.h" |
23 | #include "symtab.h" | |
24 | #include "python-internal.h" | |
25 | #include "objfiles.h" | |
086baaf1 | 26 | #include "symfile.h" |
f3e9a817 | 27 | |
f99b5177 | 28 | struct symbol_object { |
f3e9a817 PM |
29 | PyObject_HEAD |
30 | /* The GDB symbol structure this object is wrapping. */ | |
31 | struct symbol *symbol; | |
f99b5177 | 32 | }; |
f3e9a817 PM |
33 | |
34 | /* Require a valid symbol. All access to symbol_object->symbol should be | |
35 | gated by this call. */ | |
36 | #define SYMPY_REQUIRE_VALID(symbol_obj, symbol) \ | |
37 | do { \ | |
38 | symbol = symbol_object_to_symbol (symbol_obj); \ | |
39 | if (symbol == NULL) \ | |
40 | { \ | |
41 | PyErr_SetString (PyExc_RuntimeError, \ | |
42 | _("Symbol is invalid.")); \ | |
43 | return NULL; \ | |
44 | } \ | |
45 | } while (0) | |
46 | ||
980ceeca JV |
47 | static const gdbpy_registry<gdbpy_memoizing_registry_storage<symbol_object, |
48 | symbol, &symbol_object::symbol>> sympy_registry; | |
f3e9a817 PM |
49 | |
50 | static PyObject * | |
51 | sympy_str (PyObject *self) | |
52 | { | |
53 | PyObject *result; | |
54 | struct symbol *symbol = NULL; | |
55 | ||
56 | SYMPY_REQUIRE_VALID (self, symbol); | |
57 | ||
5aee4587 | 58 | result = PyUnicode_FromString (symbol->print_name ()); |
f3e9a817 PM |
59 | |
60 | return result; | |
61 | } | |
62 | ||
457e09f0 DE |
63 | static PyObject * |
64 | sympy_get_type (PyObject *self, void *closure) | |
65 | { | |
66 | struct symbol *symbol = NULL; | |
67 | ||
68 | SYMPY_REQUIRE_VALID (self, symbol); | |
69 | ||
5f9c5a63 | 70 | if (symbol->type () == NULL) |
457e09f0 DE |
71 | { |
72 | Py_INCREF (Py_None); | |
73 | return Py_None; | |
74 | } | |
75 | ||
5f9c5a63 | 76 | return type_to_type_object (symbol->type ()); |
457e09f0 DE |
77 | } |
78 | ||
f3e9a817 PM |
79 | static PyObject * |
80 | sympy_get_symtab (PyObject *self, void *closure) | |
81 | { | |
82 | struct symbol *symbol = NULL; | |
83 | ||
84 | SYMPY_REQUIRE_VALID (self, symbol); | |
85 | ||
7b3ecc75 | 86 | if (!symbol->is_objfile_owned ()) |
1994afbf DE |
87 | Py_RETURN_NONE; |
88 | ||
4206d69e | 89 | return symtab_to_symtab_object (symbol->symtab ()); |
f3e9a817 PM |
90 | } |
91 | ||
92 | static PyObject * | |
93 | sympy_get_name (PyObject *self, void *closure) | |
94 | { | |
95 | struct symbol *symbol = NULL; | |
96 | ||
97 | SYMPY_REQUIRE_VALID (self, symbol); | |
98 | ||
5aee4587 | 99 | return PyUnicode_FromString (symbol->natural_name ()); |
f3e9a817 PM |
100 | } |
101 | ||
102 | static PyObject * | |
103 | sympy_get_linkage_name (PyObject *self, void *closure) | |
104 | { | |
105 | struct symbol *symbol = NULL; | |
106 | ||
107 | SYMPY_REQUIRE_VALID (self, symbol); | |
108 | ||
5aee4587 | 109 | return PyUnicode_FromString (symbol->linkage_name ()); |
f3e9a817 PM |
110 | } |
111 | ||
112 | static PyObject * | |
113 | sympy_get_print_name (PyObject *self, void *closure) | |
114 | { | |
115 | struct symbol *symbol = NULL; | |
116 | ||
117 | SYMPY_REQUIRE_VALID (self, symbol); | |
118 | ||
119 | return sympy_str (self); | |
120 | } | |
121 | ||
122 | static PyObject * | |
123 | sympy_get_addr_class (PyObject *self, void *closure) | |
124 | { | |
125 | struct symbol *symbol = NULL; | |
126 | ||
127 | SYMPY_REQUIRE_VALID (self, symbol); | |
128 | ||
66d7f48f | 129 | return gdb_py_object_from_longest (symbol->aclass ()).release (); |
f3e9a817 PM |
130 | } |
131 | ||
30243af8 JV |
132 | /* Implement gdb.Symbol.domain attribute. Return the domain as an |
133 | integer. */ | |
134 | ||
135 | static PyObject * | |
136 | sympy_get_domain (PyObject *self, void *closure) | |
137 | { | |
138 | struct symbol *symbol = nullptr; | |
139 | ||
140 | SYMPY_REQUIRE_VALID (self, symbol); | |
141 | ||
142 | return gdb_py_object_from_longest (symbol->domain ()).release (); | |
143 | } | |
144 | ||
f3e9a817 PM |
145 | static PyObject * |
146 | sympy_is_argument (PyObject *self, void *closure) | |
147 | { | |
148 | struct symbol *symbol = NULL; | |
149 | ||
150 | SYMPY_REQUIRE_VALID (self, symbol); | |
151 | ||
d9743061 | 152 | return PyBool_FromLong (symbol->is_argument ()); |
f3e9a817 PM |
153 | } |
154 | ||
155 | static PyObject * | |
156 | sympy_is_constant (PyObject *self, void *closure) | |
157 | { | |
158 | struct symbol *symbol = NULL; | |
fe978cb0 | 159 | enum address_class theclass; |
f3e9a817 PM |
160 | |
161 | SYMPY_REQUIRE_VALID (self, symbol); | |
162 | ||
66d7f48f | 163 | theclass = symbol->aclass (); |
f3e9a817 | 164 | |
fe978cb0 | 165 | return PyBool_FromLong (theclass == LOC_CONST || theclass == LOC_CONST_BYTES); |
f3e9a817 PM |
166 | } |
167 | ||
168 | static PyObject * | |
169 | sympy_is_function (PyObject *self, void *closure) | |
170 | { | |
171 | struct symbol *symbol = NULL; | |
fe978cb0 | 172 | enum address_class theclass; |
f3e9a817 PM |
173 | |
174 | SYMPY_REQUIRE_VALID (self, symbol); | |
175 | ||
66d7f48f | 176 | theclass = symbol->aclass (); |
f3e9a817 | 177 | |
fe978cb0 | 178 | return PyBool_FromLong (theclass == LOC_BLOCK); |
f3e9a817 PM |
179 | } |
180 | ||
181 | static PyObject * | |
182 | sympy_is_variable (PyObject *self, void *closure) | |
183 | { | |
184 | struct symbol *symbol = NULL; | |
fe978cb0 | 185 | enum address_class theclass; |
f3e9a817 PM |
186 | |
187 | SYMPY_REQUIRE_VALID (self, symbol); | |
188 | ||
66d7f48f | 189 | theclass = symbol->aclass (); |
f3e9a817 | 190 | |
d9743061 | 191 | return PyBool_FromLong (!symbol->is_argument () |
fe978cb0 PA |
192 | && (theclass == LOC_LOCAL || theclass == LOC_REGISTER |
193 | || theclass == LOC_STATIC || theclass == LOC_COMPUTED | |
194 | || theclass == LOC_OPTIMIZED_OUT)); | |
f3e9a817 PM |
195 | } |
196 | ||
9ba5ef4b TT |
197 | /* Implementation of Symbol.is_artificial. */ |
198 | ||
199 | static PyObject * | |
200 | sympy_is_artificial (PyObject *self, void *closure) | |
201 | { | |
202 | struct symbol *symbol = nullptr; | |
203 | ||
204 | SYMPY_REQUIRE_VALID (self, symbol); | |
205 | ||
206 | return PyBool_FromLong (symbol->is_artificial ()); | |
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 | 225 | { |
1ccb6f10 | 226 | return gdbpy_handle_gdb_exception (nullptr, except); |
492d29ea | 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 | 310 | { |
1ccb6f10 | 311 | return gdbpy_handle_gdb_exception (nullptr, except); |
492d29ea | 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; | |
d518cb9b | 326 | if (symbol->is_objfile_owned ()) |
f3e9a817 | 327 | { |
d518cb9b JV |
328 | /* Can it really happen that symbol->symtab () is NULL? */ |
329 | if (symbol->symtab () != nullptr) | |
330 | { | |
980ceeca | 331 | sympy_registry.add (symbol->objfile (), obj); |
d518cb9b JV |
332 | } |
333 | } | |
334 | else | |
335 | { | |
980ceeca | 336 | sympy_registry.add (symbol->arch (), obj); |
f3e9a817 | 337 | } |
f3e9a817 PM |
338 | } |
339 | ||
340 | /* Create a new symbol object (gdb.Symbol) that encapsulates the struct | |
341 | symbol object from GDB. */ | |
342 | PyObject * | |
343 | symbol_to_symbol_object (struct symbol *sym) | |
344 | { | |
345 | symbol_object *sym_obj; | |
346 | ||
aaefa902 | 347 | /* Look if there's already a gdb.Symbol object for given SYMBOL |
d518cb9b JV |
348 | and if so, return it. */ |
349 | if (sym->is_objfile_owned ()) | |
980ceeca | 350 | sym_obj = sympy_registry.lookup (sym->objfile (), sym); |
d518cb9b | 351 | else |
980ceeca JV |
352 | sym_obj = sympy_registry.lookup (sym->arch (), sym); |
353 | if (sym_obj != nullptr) | |
354 | return (PyObject*)sym_obj; | |
d518cb9b | 355 | |
f3e9a817 PM |
356 | sym_obj = PyObject_New (symbol_object, &symbol_object_type); |
357 | if (sym_obj) | |
358 | set_symbol (sym_obj, sym); | |
359 | ||
360 | return (PyObject *) sym_obj; | |
361 | } | |
362 | ||
363 | /* Return the symbol that is wrapped by this symbol object. */ | |
364 | struct symbol * | |
365 | symbol_object_to_symbol (PyObject *obj) | |
366 | { | |
367 | if (! PyObject_TypeCheck (obj, &symbol_object_type)) | |
368 | return NULL; | |
369 | return ((symbol_object *) obj)->symbol; | |
370 | } | |
371 | ||
372 | static void | |
373 | sympy_dealloc (PyObject *obj) | |
374 | { | |
375 | symbol_object *sym_obj = (symbol_object *) obj; | |
376 | ||
980ceeca | 377 | if (sym_obj->symbol != nullptr) |
d518cb9b JV |
378 | { |
379 | if (sym_obj->symbol->is_objfile_owned ()) | |
980ceeca | 380 | sympy_registry.remove (sym_obj->symbol->objfile (), sym_obj); |
d518cb9b | 381 | else |
980ceeca | 382 | sympy_registry.remove (sym_obj->symbol->arch (), sym_obj); |
d518cb9b | 383 | } |
980ceeca | 384 | |
2e953aca | 385 | Py_TYPE (obj)->tp_free (obj); |
f3e9a817 PM |
386 | } |
387 | ||
bb2bd584 MBB |
388 | /* __repr__ implementation for gdb.Symbol. */ |
389 | ||
390 | static PyObject * | |
391 | sympy_repr (PyObject *self) | |
392 | { | |
393 | const auto symbol = symbol_object_to_symbol (self); | |
394 | if (symbol == nullptr) | |
aef117b7 | 395 | return gdb_py_invalid_object_repr (self); |
bb2bd584 MBB |
396 | |
397 | return PyUnicode_FromFormat ("<%s print_name=%s>", Py_TYPE (self)->tp_name, | |
398 | symbol->print_name ()); | |
399 | } | |
400 | ||
f3e9a817 PM |
401 | /* Implementation of |
402 | gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this) | |
403 | A tuple with 2 elements is always returned. The first is the symbol | |
404 | object or None, the second is a boolean with the value of | |
405 | is_a_field_of_this (see comment in lookup_symbol_in_language). */ | |
6e6fbe60 | 406 | |
f3e9a817 PM |
407 | PyObject * |
408 | gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw) | |
409 | { | |
1993b719 TT |
410 | int domain = VAR_DOMAIN; |
411 | struct field_of_this_result is_a_field_of_this; | |
f3e9a817 | 412 | const char *name; |
2adadf51 | 413 | static const char *keywords[] = { "name", "block", "domain", NULL }; |
76dce0be | 414 | struct symbol *symbol = NULL; |
37fce74f | 415 | PyObject *block_obj = NULL, *sym_obj, *bool_obj; |
9df2fbc4 | 416 | const struct block *block = NULL; |
f3e9a817 | 417 | |
2adadf51 PA |
418 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name, |
419 | &block_object_type, &block_obj, | |
420 | &domain)) | |
f3e9a817 PM |
421 | return NULL; |
422 | ||
423 | if (block_obj) | |
424 | block = block_object_to_block (block_obj); | |
425 | else | |
426 | { | |
bd2b40ac | 427 | frame_info_ptr selected_frame; |
f3e9a817 | 428 | |
a70b8144 | 429 | try |
f3e9a817 | 430 | { |
626e7282 JK |
431 | selected_frame = get_selected_frame (_("No frame selected.")); |
432 | block = get_frame_block (selected_frame, NULL); | |
f3e9a817 | 433 | } |
230d2906 | 434 | catch (const gdb_exception &except) |
492d29ea | 435 | { |
1ccb6f10 | 436 | return gdbpy_handle_gdb_exception (nullptr, except); |
492d29ea | 437 | } |
f3e9a817 PM |
438 | } |
439 | ||
a70b8144 | 440 | try |
76dce0be | 441 | { |
ccf41c24 TT |
442 | domain_search_flags flags = from_scripting_domain (domain); |
443 | symbol = lookup_symbol (name, block, flags, &is_a_field_of_this).symbol; | |
76dce0be | 444 | } |
230d2906 | 445 | catch (const gdb_exception &except) |
492d29ea | 446 | { |
1ccb6f10 | 447 | return gdbpy_handle_gdb_exception (nullptr, except); |
492d29ea | 448 | } |
f3e9a817 | 449 | |
7780f186 | 450 | gdbpy_ref<> ret_tuple (PyTuple_New (2)); |
37fce74f | 451 | if (ret_tuple == NULL) |
f3e9a817 PM |
452 | return NULL; |
453 | ||
454 | if (symbol) | |
455 | { | |
456 | sym_obj = symbol_to_symbol_object (symbol); | |
457 | if (!sym_obj) | |
37fce74f | 458 | return NULL; |
f3e9a817 PM |
459 | } |
460 | else | |
461 | { | |
462 | sym_obj = Py_None; | |
463 | Py_INCREF (Py_None); | |
464 | } | |
37fce74f | 465 | PyTuple_SET_ITEM (ret_tuple.get (), 0, sym_obj); |
f3e9a817 | 466 | |
c86acd3f | 467 | bool_obj = PyBool_FromLong (is_a_field_of_this.type != NULL); |
37fce74f | 468 | PyTuple_SET_ITEM (ret_tuple.get (), 1, bool_obj); |
f3e9a817 | 469 | |
37fce74f | 470 | return ret_tuple.release (); |
f3e9a817 PM |
471 | } |
472 | ||
6e6fbe60 DE |
473 | /* Implementation of |
474 | gdb.lookup_global_symbol (name [, domain]) -> symbol or None. */ | |
475 | ||
476 | PyObject * | |
477 | gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw) | |
478 | { | |
479 | int domain = VAR_DOMAIN; | |
480 | const char *name; | |
2adadf51 | 481 | static const char *keywords[] = { "name", "domain", NULL }; |
76dce0be | 482 | struct symbol *symbol = NULL; |
6e6fbe60 DE |
483 | PyObject *sym_obj; |
484 | ||
2adadf51 PA |
485 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name, |
486 | &domain)) | |
6e6fbe60 DE |
487 | return NULL; |
488 | ||
a70b8144 | 489 | try |
76dce0be | 490 | { |
ccf41c24 TT |
491 | domain_search_flags flags = from_scripting_domain (domain); |
492 | symbol = lookup_global_symbol (name, NULL, flags).symbol; | |
76dce0be | 493 | } |
230d2906 | 494 | catch (const gdb_exception &except) |
492d29ea | 495 | { |
1ccb6f10 | 496 | return gdbpy_handle_gdb_exception (nullptr, except); |
492d29ea | 497 | } |
6e6fbe60 DE |
498 | |
499 | if (symbol) | |
500 | { | |
501 | sym_obj = symbol_to_symbol_object (symbol); | |
502 | if (!sym_obj) | |
503 | return NULL; | |
504 | } | |
505 | else | |
506 | { | |
507 | sym_obj = Py_None; | |
508 | Py_INCREF (Py_None); | |
509 | } | |
510 | ||
511 | return sym_obj; | |
512 | } | |
513 | ||
2906593f | 514 | /* Implementation of |
09ff83af | 515 | gdb.lookup_static_symbol (name [, domain]) -> symbol or None. */ |
2906593f CB |
516 | |
517 | PyObject * | |
518 | gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw) | |
519 | { | |
520 | const char *name; | |
521 | int domain = VAR_DOMAIN; | |
522 | static const char *keywords[] = { "name", "domain", NULL }; | |
523 | struct symbol *symbol = NULL; | |
524 | PyObject *sym_obj; | |
525 | ||
526 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name, | |
527 | &domain)) | |
528 | return NULL; | |
529 | ||
09ff83af AB |
530 | /* In order to find static symbols associated with the "current" object |
531 | file ahead of those from other object files, we first need to see if | |
532 | we can acquire a current block. If this fails however, then we still | |
533 | want to search all static symbols, so don't throw an exception just | |
534 | yet. */ | |
535 | const struct block *block = NULL; | |
2906593f CB |
536 | try |
537 | { | |
bd2b40ac | 538 | frame_info_ptr selected_frame |
09ff83af AB |
539 | = get_selected_frame (_("No frame selected.")); |
540 | block = get_frame_block (selected_frame, NULL); | |
541 | } | |
b940a061 KB |
542 | catch (const gdb_exception_forced_quit &e) |
543 | { | |
544 | quit_force (NULL, 0); | |
545 | } | |
09ff83af AB |
546 | catch (const gdb_exception &except) |
547 | { | |
548 | /* Nothing. */ | |
549 | } | |
550 | ||
551 | try | |
552 | { | |
ccf41c24 TT |
553 | domain_search_flags flags = from_scripting_domain (domain); |
554 | ||
09ff83af AB |
555 | if (block != nullptr) |
556 | symbol | |
ccf41c24 | 557 | = lookup_symbol_in_static_block (name, block, flags).symbol; |
09ff83af AB |
558 | |
559 | if (symbol == nullptr) | |
ccf41c24 | 560 | symbol = lookup_static_symbol (name, flags).symbol; |
2906593f CB |
561 | } |
562 | catch (const gdb_exception &except) | |
563 | { | |
1ccb6f10 | 564 | return gdbpy_handle_gdb_exception (nullptr, except); |
2906593f CB |
565 | } |
566 | ||
567 | if (symbol) | |
568 | { | |
569 | sym_obj = symbol_to_symbol_object (symbol); | |
570 | if (!sym_obj) | |
571 | return NULL; | |
572 | } | |
573 | else | |
574 | { | |
575 | sym_obj = Py_None; | |
576 | Py_INCREF (Py_None); | |
577 | } | |
578 | ||
579 | return sym_obj; | |
580 | } | |
581 | ||
086baaf1 AB |
582 | /* Implementation of |
583 | gdb.lookup_static_symbols (name [, domain]) -> symbol list. | |
584 | ||
585 | Returns a list of all static symbols matching NAME in DOMAIN. */ | |
586 | ||
587 | PyObject * | |
588 | gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw) | |
589 | { | |
590 | const char *name; | |
591 | int domain = VAR_DOMAIN; | |
592 | static const char *keywords[] = { "name", "domain", NULL }; | |
593 | ||
594 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name, | |
595 | &domain)) | |
596 | return NULL; | |
597 | ||
598 | gdbpy_ref<> return_list (PyList_New (0)); | |
599 | if (return_list == NULL) | |
600 | return NULL; | |
601 | ||
602 | try | |
603 | { | |
ccf41c24 TT |
604 | domain_search_flags flags = from_scripting_domain (domain); |
605 | ||
086baaf1 AB |
606 | /* Expand any symtabs that contain potentially matching symbols. */ |
607 | lookup_name_info lookup_name (name, symbol_name_match_type::FULL); | |
03a8ea51 | 608 | expand_symtabs_matching (NULL, lookup_name, NULL, NULL, |
1c7d9f96 | 609 | SEARCH_STATIC_BLOCK, flags); |
086baaf1 AB |
610 | |
611 | for (objfile *objfile : current_program_space->objfiles ()) | |
612 | { | |
613 | for (compunit_symtab *cust : objfile->compunits ()) | |
614 | { | |
e061219f TV |
615 | /* Skip included compunits to prevent including compunits from |
616 | being searched twice. */ | |
617 | if (cust->user != nullptr) | |
618 | continue; | |
086baaf1 | 619 | |
e061219f | 620 | const struct blockvector *bv = cust->blockvector (); |
63d609de | 621 | const struct block *block = bv->static_block (); |
086baaf1 AB |
622 | |
623 | if (block != nullptr) | |
624 | { | |
625 | symbol *symbol = lookup_symbol_in_static_block | |
ccf41c24 | 626 | (name, block, flags).symbol; |
086baaf1 AB |
627 | |
628 | if (symbol != nullptr) | |
629 | { | |
cd676397 TT |
630 | PyObject *sym_obj = symbol_to_symbol_object (symbol); |
631 | if (sym_obj == nullptr) | |
632 | return nullptr; | |
086baaf1 | 633 | if (PyList_Append (return_list.get (), sym_obj) == -1) |
cd676397 | 634 | return nullptr; |
086baaf1 AB |
635 | } |
636 | } | |
637 | } | |
638 | } | |
639 | } | |
640 | catch (const gdb_exception &except) | |
641 | { | |
1ccb6f10 | 642 | return gdbpy_handle_gdb_exception (nullptr, except); |
086baaf1 AB |
643 | } |
644 | ||
645 | return return_list.release (); | |
646 | } | |
647 | ||
3965bff5 | 648 | static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION |
8e3685bf AB |
649 | gdbpy_initialize_symbols (void) |
650 | { | |
336bb2a1 | 651 | if (gdbpy_type_ready (&symbol_object_type) < 0) |
8e3685bf | 652 | return -1; |
f3e9a817 | 653 | |
999633ed TT |
654 | if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0 |
655 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST", | |
656 | LOC_CONST) < 0 | |
657 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC", | |
658 | LOC_STATIC) < 0 | |
659 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER", | |
660 | LOC_REGISTER) < 0 | |
661 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG", | |
662 | LOC_ARG) < 0 | |
663 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG", | |
664 | LOC_REF_ARG) < 0 | |
665 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL", | |
666 | LOC_LOCAL) < 0 | |
667 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF", | |
668 | LOC_TYPEDEF) < 0 | |
669 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL", | |
670 | LOC_LABEL) < 0 | |
671 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK", | |
672 | LOC_BLOCK) < 0 | |
673 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES", | |
674 | LOC_CONST_BYTES) < 0 | |
675 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED", | |
676 | LOC_UNRESOLVED) < 0 | |
677 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT", | |
678 | LOC_OPTIMIZED_OUT) < 0 | |
679 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED", | |
680 | LOC_COMPUTED) < 0 | |
51e78fc5 TT |
681 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMMON_BLOCK", |
682 | LOC_COMMON_BLOCK) < 0 | |
999633ed | 683 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR", |
6771fc6f | 684 | LOC_REGPARM_ADDR) < 0) |
51e78fc5 TT |
685 | return -1; |
686 | ||
878e8948 | 687 | #define SYM_DOMAIN(X) \ |
6771fc6f | 688 | if (PyModule_AddIntConstant (gdb_module, "SYMBOL_" #X "_DOMAIN", \ |
ccf41c24 TT |
689 | to_scripting_domain (X ## _DOMAIN)) < 0 \ |
690 | || PyModule_AddIntConstant (gdb_module, "SEARCH_" #X "_DOMAIN", \ | |
691 | to_scripting_domain (SEARCH_ ## X ## _DOMAIN)) < 0) \ | |
6771fc6f TT |
692 | return -1; |
693 | #include "sym-domains.def" | |
878e8948 | 694 | #undef SYM_DOMAIN |
6771fc6f | 695 | |
336bb2a1 | 696 | return 0; |
f3e9a817 PM |
697 | } |
698 | ||
3965bff5 AB |
699 | GDBPY_INITIALIZE_FILE (gdbpy_initialize_symbols); |
700 | ||
f3e9a817 PM |
701 | \f |
702 | ||
0d1f4ceb | 703 | static gdb_PyGetSetDef symbol_object_getset[] = { |
457e09f0 DE |
704 | { "type", sympy_get_type, NULL, |
705 | "Type of the symbol.", NULL }, | |
f3e9a817 PM |
706 | { "symtab", sympy_get_symtab, NULL, |
707 | "Symbol table in which the symbol appears.", NULL }, | |
708 | { "name", sympy_get_name, NULL, | |
709 | "Name of the symbol, as it appears in the source code.", NULL }, | |
710 | { "linkage_name", sympy_get_linkage_name, NULL, | |
9a2b4c1b MS |
711 | "Name of the symbol, as used by the linker (i.e., may be mangled).", |
712 | NULL }, | |
f3e9a817 PM |
713 | { "print_name", sympy_get_print_name, NULL, |
714 | "Name of the symbol in a form suitable for output.\n\ | |
715 | This is either name or linkage_name, depending on whether the user asked GDB\n\ | |
716 | to display demangled or mangled names.", NULL }, | |
717 | { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." }, | |
30243af8 | 718 | { "domain", sympy_get_domain, nullptr, "Domain of the symbol." }, |
f3e9a817 PM |
719 | { "is_argument", sympy_is_argument, NULL, |
720 | "True if the symbol is an argument of a function." }, | |
9ba5ef4b TT |
721 | { "is_artificial", sympy_is_artificial, nullptr, |
722 | "True if the symbol is marked artificial." }, | |
f3e9a817 PM |
723 | { "is_constant", sympy_is_constant, NULL, |
724 | "True if the symbol is a constant." }, | |
725 | { "is_function", sympy_is_function, NULL, | |
726 | "True if the symbol is a function or method." }, | |
727 | { "is_variable", sympy_is_variable, NULL, | |
728 | "True if the symbol is a variable." }, | |
f0823d2c TT |
729 | { "needs_frame", sympy_needs_frame, NULL, |
730 | "True if the symbol requires a frame for evaluation." }, | |
64e7d9dd TT |
731 | { "line", sympy_line, NULL, |
732 | "The source line number at which the symbol was defined." }, | |
f3e9a817 PM |
733 | { NULL } /* Sentinel */ |
734 | }; | |
735 | ||
29703da4 PM |
736 | static PyMethodDef symbol_object_methods[] = { |
737 | { "is_valid", sympy_is_valid, METH_NOARGS, | |
738 | "is_valid () -> Boolean.\n\ | |
739 | Return true if this symbol is valid, false if not." }, | |
f0823d2c TT |
740 | { "value", sympy_value, METH_VARARGS, |
741 | "value ([frame]) -> gdb.Value\n\ | |
742 | Return the value of the symbol." }, | |
29703da4 PM |
743 | {NULL} /* Sentinel */ |
744 | }; | |
745 | ||
f3e9a817 | 746 | PyTypeObject symbol_object_type = { |
9a27f2c6 | 747 | PyVarObject_HEAD_INIT (NULL, 0) |
f3e9a817 PM |
748 | "gdb.Symbol", /*tp_name*/ |
749 | sizeof (symbol_object), /*tp_basicsize*/ | |
750 | 0, /*tp_itemsize*/ | |
751 | sympy_dealloc, /*tp_dealloc*/ | |
752 | 0, /*tp_print*/ | |
753 | 0, /*tp_getattr*/ | |
754 | 0, /*tp_setattr*/ | |
755 | 0, /*tp_compare*/ | |
bb2bd584 | 756 | sympy_repr, /*tp_repr*/ |
f3e9a817 PM |
757 | 0, /*tp_as_number*/ |
758 | 0, /*tp_as_sequence*/ | |
759 | 0, /*tp_as_mapping*/ | |
760 | 0, /*tp_hash */ | |
761 | 0, /*tp_call*/ | |
762 | sympy_str, /*tp_str*/ | |
763 | 0, /*tp_getattro*/ | |
764 | 0, /*tp_setattro*/ | |
765 | 0, /*tp_as_buffer*/ | |
766 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
767 | "GDB symbol object", /*tp_doc */ | |
768 | 0, /*tp_traverse */ | |
769 | 0, /*tp_clear */ | |
770 | 0, /*tp_richcompare */ | |
771 | 0, /*tp_weaklistoffset */ | |
772 | 0, /*tp_iter */ | |
773 | 0, /*tp_iternext */ | |
29703da4 | 774 | symbol_object_methods, /*tp_methods */ |
f3e9a817 PM |
775 | 0, /*tp_members */ |
776 | symbol_object_getset /*tp_getset */ | |
777 | }; |