]>
Commit | Line | Data |
---|---|---|
1 | /* Python interface to symbols. | |
2 | ||
3 | Copyright (C) 2008-2025 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 "top.h" | |
21 | #include "block.h" | |
22 | #include "frame.h" | |
23 | #include "symtab.h" | |
24 | #include "python-internal.h" | |
25 | #include "objfiles.h" | |
26 | #include "symfile.h" | |
27 | ||
28 | struct symbol_object { | |
29 | PyObject_HEAD | |
30 | /* The GDB symbol structure this object is wrapping. */ | |
31 | struct symbol *symbol; | |
32 | }; | |
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 | ||
47 | static const gdbpy_registry<gdbpy_memoizing_registry_storage<symbol_object, | |
48 | symbol, &symbol_object::symbol>> sympy_registry; | |
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 | ||
58 | result = PyUnicode_FromString (symbol->print_name ()); | |
59 | ||
60 | return result; | |
61 | } | |
62 | ||
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 | ||
70 | if (symbol->type () == NULL) | |
71 | { | |
72 | Py_INCREF (Py_None); | |
73 | return Py_None; | |
74 | } | |
75 | ||
76 | return type_to_type_object (symbol->type ()); | |
77 | } | |
78 | ||
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 | ||
86 | if (!symbol->is_objfile_owned ()) | |
87 | Py_RETURN_NONE; | |
88 | ||
89 | return symtab_to_symtab_object (symbol->symtab ()); | |
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 | ||
99 | return PyUnicode_FromString (symbol->natural_name ()); | |
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 | ||
109 | return PyUnicode_FromString (symbol->linkage_name ()); | |
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 | ||
129 | return gdb_py_object_from_longest (symbol->aclass ()).release (); | |
130 | } | |
131 | ||
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 | ||
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 | ||
152 | return PyBool_FromLong (symbol->is_argument ()); | |
153 | } | |
154 | ||
155 | static PyObject * | |
156 | sympy_is_constant (PyObject *self, void *closure) | |
157 | { | |
158 | struct symbol *symbol = NULL; | |
159 | enum address_class theclass; | |
160 | ||
161 | SYMPY_REQUIRE_VALID (self, symbol); | |
162 | ||
163 | theclass = symbol->aclass (); | |
164 | ||
165 | return PyBool_FromLong (theclass == LOC_CONST || theclass == LOC_CONST_BYTES); | |
166 | } | |
167 | ||
168 | static PyObject * | |
169 | sympy_is_function (PyObject *self, void *closure) | |
170 | { | |
171 | struct symbol *symbol = NULL; | |
172 | enum address_class theclass; | |
173 | ||
174 | SYMPY_REQUIRE_VALID (self, symbol); | |
175 | ||
176 | theclass = symbol->aclass (); | |
177 | ||
178 | return PyBool_FromLong (theclass == LOC_BLOCK); | |
179 | } | |
180 | ||
181 | static PyObject * | |
182 | sympy_is_variable (PyObject *self, void *closure) | |
183 | { | |
184 | struct symbol *symbol = NULL; | |
185 | enum address_class theclass; | |
186 | ||
187 | SYMPY_REQUIRE_VALID (self, symbol); | |
188 | ||
189 | theclass = symbol->aclass (); | |
190 | ||
191 | return PyBool_FromLong (!symbol->is_argument () | |
192 | && (theclass == LOC_LOCAL || theclass == LOC_REGISTER | |
193 | || theclass == LOC_STATIC || theclass == LOC_COMPUTED | |
194 | || theclass == LOC_OPTIMIZED_OUT)); | |
195 | } | |
196 | ||
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 | ||
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; | |
216 | int result = 0; | |
217 | ||
218 | SYMPY_REQUIRE_VALID (self, symbol); | |
219 | ||
220 | try | |
221 | { | |
222 | result = symbol_read_needs_frame (symbol); | |
223 | } | |
224 | catch (const gdb_exception &except) | |
225 | { | |
226 | return gdbpy_handle_gdb_exception (nullptr, except); | |
227 | } | |
228 | ||
229 | if (result) | |
230 | Py_RETURN_TRUE; | |
231 | Py_RETURN_FALSE; | |
232 | } | |
233 | ||
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 | ||
244 | return gdb_py_object_from_longest (symbol->line ()).release (); | |
245 | } | |
246 | ||
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 | ||
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; | |
269 | frame_info_ptr frame_info = NULL; | |
270 | PyObject *frame_obj = NULL; | |
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); | |
282 | if (symbol->aclass () == LOC_TYPEDEF) | |
283 | { | |
284 | PyErr_SetString (PyExc_TypeError, "cannot get the value of a typedef"); | |
285 | return NULL; | |
286 | } | |
287 | ||
288 | PyObject *result = nullptr; | |
289 | try | |
290 | { | |
291 | if (frame_obj != NULL) | |
292 | { | |
293 | frame_info = frame_object_to_frame_info (frame_obj); | |
294 | if (frame_info == NULL) | |
295 | error (_("invalid frame")); | |
296 | } | |
297 | ||
298 | if (symbol_read_needs_frame (symbol) && frame_info == NULL) | |
299 | error (_("symbol requires a frame to compute its value")); | |
300 | ||
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). */ | |
305 | scoped_value_mark free_values; | |
306 | struct value *value = read_var_value (symbol, NULL, frame_info); | |
307 | result = value_to_value_object (value); | |
308 | } | |
309 | catch (const gdb_exception &except) | |
310 | { | |
311 | return gdbpy_handle_gdb_exception (nullptr, except); | |
312 | } | |
313 | ||
314 | return result; | |
315 | } | |
316 | ||
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 | |
320 | with the life-cycle of the object file associated with this | |
321 | symbol, if needed. */ | |
322 | static void | |
323 | set_symbol (symbol_object *obj, struct symbol *symbol) | |
324 | { | |
325 | obj->symbol = symbol; | |
326 | if (symbol->is_objfile_owned ()) | |
327 | { | |
328 | /* Can it really happen that symbol->symtab () is NULL? */ | |
329 | if (symbol->symtab () != nullptr) | |
330 | { | |
331 | sympy_registry.add (symbol->objfile (), obj); | |
332 | } | |
333 | } | |
334 | else | |
335 | { | |
336 | sympy_registry.add (symbol->arch (), obj); | |
337 | } | |
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 | ||
347 | /* Look if there's already a gdb.Symbol object for given SYMBOL | |
348 | and if so, return it. */ | |
349 | if (sym->is_objfile_owned ()) | |
350 | sym_obj = sympy_registry.lookup (sym->objfile (), sym); | |
351 | else | |
352 | sym_obj = sympy_registry.lookup (sym->arch (), sym); | |
353 | if (sym_obj != nullptr) | |
354 | return (PyObject*)sym_obj; | |
355 | ||
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 | ||
377 | if (sym_obj->symbol != nullptr) | |
378 | { | |
379 | if (sym_obj->symbol->is_objfile_owned ()) | |
380 | sympy_registry.remove (sym_obj->symbol->objfile (), sym_obj); | |
381 | else | |
382 | sympy_registry.remove (sym_obj->symbol->arch (), sym_obj); | |
383 | } | |
384 | ||
385 | Py_TYPE (obj)->tp_free (obj); | |
386 | } | |
387 | ||
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) | |
395 | return gdb_py_invalid_object_repr (self); | |
396 | ||
397 | return PyUnicode_FromFormat ("<%s print_name=%s>", Py_TYPE (self)->tp_name, | |
398 | symbol->print_name ()); | |
399 | } | |
400 | ||
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). */ | |
406 | ||
407 | PyObject * | |
408 | gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw) | |
409 | { | |
410 | int domain = VAR_DOMAIN; | |
411 | struct field_of_this_result is_a_field_of_this; | |
412 | const char *name; | |
413 | static const char *keywords[] = { "name", "block", "domain", NULL }; | |
414 | struct symbol *symbol = NULL; | |
415 | PyObject *block_obj = NULL, *sym_obj, *bool_obj; | |
416 | const struct block *block = NULL; | |
417 | ||
418 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name, | |
419 | &block_object_type, &block_obj, | |
420 | &domain)) | |
421 | return NULL; | |
422 | ||
423 | if (block_obj) | |
424 | block = block_object_to_block (block_obj); | |
425 | else | |
426 | { | |
427 | frame_info_ptr selected_frame; | |
428 | ||
429 | try | |
430 | { | |
431 | selected_frame = get_selected_frame (_("No frame selected.")); | |
432 | block = get_frame_block (selected_frame, NULL); | |
433 | } | |
434 | catch (const gdb_exception &except) | |
435 | { | |
436 | return gdbpy_handle_gdb_exception (nullptr, except); | |
437 | } | |
438 | } | |
439 | ||
440 | try | |
441 | { | |
442 | domain_search_flags flags = from_scripting_domain (domain); | |
443 | symbol = lookup_symbol (name, block, flags, &is_a_field_of_this).symbol; | |
444 | } | |
445 | catch (const gdb_exception &except) | |
446 | { | |
447 | return gdbpy_handle_gdb_exception (nullptr, except); | |
448 | } | |
449 | ||
450 | gdbpy_ref<> ret_tuple (PyTuple_New (2)); | |
451 | if (ret_tuple == NULL) | |
452 | return NULL; | |
453 | ||
454 | if (symbol) | |
455 | { | |
456 | sym_obj = symbol_to_symbol_object (symbol); | |
457 | if (!sym_obj) | |
458 | return NULL; | |
459 | } | |
460 | else | |
461 | { | |
462 | sym_obj = Py_None; | |
463 | Py_INCREF (Py_None); | |
464 | } | |
465 | PyTuple_SET_ITEM (ret_tuple.get (), 0, sym_obj); | |
466 | ||
467 | bool_obj = PyBool_FromLong (is_a_field_of_this.type != NULL); | |
468 | PyTuple_SET_ITEM (ret_tuple.get (), 1, bool_obj); | |
469 | ||
470 | return ret_tuple.release (); | |
471 | } | |
472 | ||
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; | |
481 | static const char *keywords[] = { "name", "domain", NULL }; | |
482 | struct symbol *symbol = NULL; | |
483 | PyObject *sym_obj; | |
484 | ||
485 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name, | |
486 | &domain)) | |
487 | return NULL; | |
488 | ||
489 | try | |
490 | { | |
491 | domain_search_flags flags = from_scripting_domain (domain); | |
492 | symbol = lookup_global_symbol (name, NULL, flags).symbol; | |
493 | } | |
494 | catch (const gdb_exception &except) | |
495 | { | |
496 | return gdbpy_handle_gdb_exception (nullptr, except); | |
497 | } | |
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 | ||
514 | /* Implementation of | |
515 | gdb.lookup_static_symbol (name [, domain]) -> symbol or None. */ | |
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 | ||
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; | |
536 | try | |
537 | { | |
538 | frame_info_ptr selected_frame | |
539 | = get_selected_frame (_("No frame selected.")); | |
540 | block = get_frame_block (selected_frame, NULL); | |
541 | } | |
542 | catch (const gdb_exception_forced_quit &e) | |
543 | { | |
544 | quit_force (NULL, 0); | |
545 | } | |
546 | catch (const gdb_exception &except) | |
547 | { | |
548 | /* Nothing. */ | |
549 | } | |
550 | ||
551 | try | |
552 | { | |
553 | domain_search_flags flags = from_scripting_domain (domain); | |
554 | ||
555 | if (block != nullptr) | |
556 | symbol | |
557 | = lookup_symbol_in_static_block (name, block, flags).symbol; | |
558 | ||
559 | if (symbol == nullptr) | |
560 | symbol = lookup_static_symbol (name, flags).symbol; | |
561 | } | |
562 | catch (const gdb_exception &except) | |
563 | { | |
564 | return gdbpy_handle_gdb_exception (nullptr, except); | |
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 | ||
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 | { | |
604 | domain_search_flags flags = from_scripting_domain (domain); | |
605 | ||
606 | /* Expand any symtabs that contain potentially matching symbols. */ | |
607 | lookup_name_info lookup_name (name, symbol_name_match_type::FULL); | |
608 | expand_symtabs_matching (NULL, lookup_name, NULL, NULL, | |
609 | SEARCH_STATIC_BLOCK, flags); | |
610 | ||
611 | for (objfile *objfile : current_program_space->objfiles ()) | |
612 | { | |
613 | for (compunit_symtab *cust : objfile->compunits ()) | |
614 | { | |
615 | /* Skip included compunits to prevent including compunits from | |
616 | being searched twice. */ | |
617 | if (cust->user != nullptr) | |
618 | continue; | |
619 | ||
620 | const struct blockvector *bv = cust->blockvector (); | |
621 | const struct block *block = bv->static_block (); | |
622 | ||
623 | if (block != nullptr) | |
624 | { | |
625 | symbol *symbol = lookup_symbol_in_static_block | |
626 | (name, block, flags).symbol; | |
627 | ||
628 | if (symbol != nullptr) | |
629 | { | |
630 | PyObject *sym_obj = symbol_to_symbol_object (symbol); | |
631 | if (sym_obj == nullptr) | |
632 | return nullptr; | |
633 | if (PyList_Append (return_list.get (), sym_obj) == -1) | |
634 | return nullptr; | |
635 | } | |
636 | } | |
637 | } | |
638 | } | |
639 | } | |
640 | catch (const gdb_exception &except) | |
641 | { | |
642 | return gdbpy_handle_gdb_exception (nullptr, except); | |
643 | } | |
644 | ||
645 | return return_list.release (); | |
646 | } | |
647 | ||
648 | static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION | |
649 | gdbpy_initialize_symbols (void) | |
650 | { | |
651 | if (gdbpy_type_ready (&symbol_object_type) < 0) | |
652 | return -1; | |
653 | ||
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 | |
681 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMMON_BLOCK", | |
682 | LOC_COMMON_BLOCK) < 0 | |
683 | || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR", | |
684 | LOC_REGPARM_ADDR) < 0) | |
685 | return -1; | |
686 | ||
687 | #define SYM_DOMAIN(X) \ | |
688 | if (PyModule_AddIntConstant (gdb_module, "SYMBOL_" #X "_DOMAIN", \ | |
689 | to_scripting_domain (X ## _DOMAIN)) < 0 \ | |
690 | || PyModule_AddIntConstant (gdb_module, "SEARCH_" #X "_DOMAIN", \ | |
691 | to_scripting_domain (SEARCH_ ## X ## _DOMAIN)) < 0) \ | |
692 | return -1; | |
693 | #include "sym-domains.def" | |
694 | #undef SYM_DOMAIN | |
695 | ||
696 | return 0; | |
697 | } | |
698 | ||
699 | GDBPY_INITIALIZE_FILE (gdbpy_initialize_symbols); | |
700 | ||
701 | \f | |
702 | ||
703 | static gdb_PyGetSetDef symbol_object_getset[] = { | |
704 | { "type", sympy_get_type, NULL, | |
705 | "Type of the symbol.", NULL }, | |
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, | |
711 | "Name of the symbol, as used by the linker (i.e., may be mangled).", | |
712 | NULL }, | |
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." }, | |
718 | { "domain", sympy_get_domain, nullptr, "Domain of the symbol." }, | |
719 | { "is_argument", sympy_is_argument, NULL, | |
720 | "True if the symbol is an argument of a function." }, | |
721 | { "is_artificial", sympy_is_artificial, nullptr, | |
722 | "True if the symbol is marked artificial." }, | |
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." }, | |
729 | { "needs_frame", sympy_needs_frame, NULL, | |
730 | "True if the symbol requires a frame for evaluation." }, | |
731 | { "line", sympy_line, NULL, | |
732 | "The source line number at which the symbol was defined." }, | |
733 | { NULL } /* Sentinel */ | |
734 | }; | |
735 | ||
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." }, | |
740 | { "value", sympy_value, METH_VARARGS, | |
741 | "value ([frame]) -> gdb.Value\n\ | |
742 | Return the value of the symbol." }, | |
743 | {NULL} /* Sentinel */ | |
744 | }; | |
745 | ||
746 | PyTypeObject symbol_object_type = { | |
747 | PyVarObject_HEAD_INIT (NULL, 0) | |
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*/ | |
756 | sympy_repr, /*tp_repr*/ | |
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 */ | |
774 | symbol_object_methods, /*tp_methods */ | |
775 | 0, /*tp_members */ | |
776 | symbol_object_getset /*tp_getset */ | |
777 | }; |