1 /* Python interface to line tables.
3 Copyright (C) 2013-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "python-internal.h"
25 /* The line table source line. */
27 /* The pc associated with the source line. */
29 } linetable_entry_object
;
31 extern PyTypeObject linetable_entry_object_type
32 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("linetable_entry_object");
36 /* The symtab python object. We store the Python object here as the
37 underlying symtab can become invalid, and we have to run validity
42 extern PyTypeObject linetable_object_type
43 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("linetable_object");
47 /* The current entry in the line table for the iterator */
49 /* Pointer back to the original source line table object. Needed to
50 check if the line table is still valid, and has not been invalidated
51 when an object file has been freed. */
53 } ltpy_iterator_object
;
55 extern PyTypeObject ltpy_iterator_object_type
56 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("ltpy_iterator_object");
58 /* Internal helper function to extract gdb.Symtab from a gdb.LineTable
62 get_symtab (PyObject
*linetable
)
64 linetable_object
*lt
= (linetable_object
*) linetable
;
69 #define LTPY_REQUIRE_VALID(lt_obj, symtab) \
71 symtab = symtab_object_to_symtab (get_symtab (lt_obj)); \
74 PyErr_SetString (PyExc_RuntimeError, \
75 _("Symbol Table in line table is invalid."));\
81 /* Helper function to create a line table object that wraps a
85 symtab_to_linetable_object (PyObject
*symtab
)
87 linetable_object
*ltable
;
89 ltable
= PyObject_New (linetable_object
, &linetable_object_type
);
92 ltable
->symtab
= symtab
;
95 return (PyObject
*) ltable
;
98 /* Internal helper function to build a line table object from a line
102 build_linetable_entry (int line
, CORE_ADDR address
)
104 linetable_entry_object
*obj
;
106 obj
= PyObject_New (linetable_entry_object
,
107 &linetable_entry_object_type
);
114 return (PyObject
*) obj
;
117 /* Internal helper function to build a Python Tuple from a vector.
118 A line table entry can have multiple PCs for a given source line.
119 Construct a Tuple of all entries for the given source line, LINE
120 from the line table PCS. Construct one line table entry object per
124 build_line_table_tuple_from_pcs (int line
, const std::vector
<CORE_ADDR
> &pcs
)
131 gdbpy_ref
<> tuple (PyTuple_New (pcs
.size ()));
136 for (i
= 0; i
< pcs
.size (); ++i
)
138 CORE_ADDR pc
= pcs
[i
];
139 gdbpy_ref
<> obj (build_linetable_entry (line
, pc
));
143 else if (PyTuple_SetItem (tuple
.get (), i
, obj
.release ()) != 0)
147 return tuple
.release ();
150 /* Implementation of gdb.LineTable.line (self) -> Tuple. Returns a
151 tuple of LineTableEntry objects associated with this line from the
152 in the line table. */
155 ltpy_get_pcs_for_line (PyObject
*self
, PyObject
*args
)
157 struct symtab
*symtab
;
158 gdb_py_longest py_line
;
159 struct linetable_entry
*best_entry
= NULL
;
160 std::vector
<CORE_ADDR
> pcs
;
162 LTPY_REQUIRE_VALID (self
, symtab
);
164 if (! PyArg_ParseTuple (args
, GDB_PY_LL_ARG
, &py_line
))
169 pcs
= find_pcs_for_symtab_line (symtab
, py_line
, &best_entry
);
171 CATCH (except
, RETURN_MASK_ALL
)
173 GDB_PY_HANDLE_EXCEPTION (except
);
177 return build_line_table_tuple_from_pcs (py_line
, pcs
);
180 /* Implementation of gdb.LineTable.has_line (self, line) -> Boolean.
181 Returns a Python Boolean indicating whether a source line has any
182 line table entries corresponding to it. */
185 ltpy_has_line (PyObject
*self
, PyObject
*args
)
187 struct symtab
*symtab
;
188 gdb_py_longest py_line
;
191 LTPY_REQUIRE_VALID (self
, symtab
);
193 if (! PyArg_ParseTuple (args
, GDB_PY_LL_ARG
, &py_line
))
196 if (SYMTAB_LINETABLE (symtab
) == NULL
)
198 PyErr_SetString (PyExc_RuntimeError
,
199 _("Linetable information not found in symbol table"));
203 for (index
= 0; index
< SYMTAB_LINETABLE (symtab
)->nitems
; index
++)
205 struct linetable_entry
*item
= &(SYMTAB_LINETABLE (symtab
)->item
[index
]);
206 if (item
->line
== py_line
)
213 /* Implementation of gdb.LineTable.source_lines (self) -> List.
214 Returns a Python List that contains source line entries in the
215 line table. This function will just return the source lines
216 without corresponding addresses. */
219 ltpy_get_all_source_lines (PyObject
*self
, PyObject
*args
)
221 struct symtab
*symtab
;
223 struct linetable_entry
*item
;
225 LTPY_REQUIRE_VALID (self
, symtab
);
227 if (SYMTAB_LINETABLE (symtab
) == NULL
)
229 PyErr_SetString (PyExc_RuntimeError
,
230 _("Linetable information not found in symbol table"));
234 gdbpy_ref
<> source_dict (PyDict_New ());
235 if (source_dict
== NULL
)
238 for (index
= 0; index
< SYMTAB_LINETABLE (symtab
)->nitems
; index
++)
240 item
= &(SYMTAB_LINETABLE (symtab
)->item
[index
]);
242 /* 0 is used to signify end of line table information. Do not
243 include in the source set. */
246 gdbpy_ref
<> line
= gdb_py_object_from_longest (item
->line
);
251 if (PyDict_SetItem (source_dict
.get (), line
.get (), Py_None
) == -1)
256 return PyDict_Keys (source_dict
.get ());
259 /* Implementation of gdb.LineTable.is_valid (self) -> Boolean.
260 Returns True if this line table object still exists in GDB. */
263 ltpy_is_valid (PyObject
*self
, PyObject
*args
)
265 struct symtab
*symtab
= NULL
;
267 symtab
= symtab_object_to_symtab (get_symtab (self
));
275 /* Deconstructor for the line table object. Decrement the reference
276 to the symbol table object before calling the default free. */
279 ltpy_dealloc (PyObject
*self
)
281 linetable_object
*obj
= (linetable_object
*) self
;
283 Py_DECREF (obj
->symtab
);
284 Py_TYPE (self
)->tp_free (self
);
287 /* Initialize LineTable, LineTableEntry and LineTableIterator
291 gdbpy_initialize_linetable (void)
293 if (PyType_Ready (&linetable_object_type
) < 0)
295 if (PyType_Ready (&linetable_entry_object_type
) < 0)
297 if (PyType_Ready (<py_iterator_object_type
) < 0)
300 Py_INCREF (&linetable_object_type
);
301 Py_INCREF (&linetable_entry_object_type
);
302 Py_INCREF (<py_iterator_object_type
);
304 if (gdb_pymodule_addobject (gdb_module
, "LineTable",
305 (PyObject
*) &linetable_object_type
) < 0)
308 if (gdb_pymodule_addobject (gdb_module
, "LineTableEntry",
309 (PyObject
*) &linetable_entry_object_type
) < 0)
312 if (gdb_pymodule_addobject (gdb_module
, "LineTableIterator",
313 (PyObject
*) <py_iterator_object_type
) < 0)
319 /* LineTable entry object get functions. */
321 /* Implementation of gdb.LineTableEntry.line (self) -> Long. Returns
322 a long integer associated with the line table entry. */
325 ltpy_entry_get_line (PyObject
*self
, void *closure
)
327 linetable_entry_object
*obj
= (linetable_entry_object
*) self
;
329 return gdb_py_object_from_longest (obj
->line
).release ();
332 /* Implementation of gdb.LineTableEntry.pc (self) -> Long. Returns a
333 a long integer associated with the PC of the line table entry. */
336 ltpy_entry_get_pc (PyObject
*self
, void *closure
)
338 linetable_entry_object
*obj
= (linetable_entry_object
*) self
;
340 return gdb_py_object_from_longest (obj
->pc
).release ();
343 /* LineTable iterator functions. */
345 /* Return a new line table iterator. */
348 ltpy_iter (PyObject
*self
)
350 ltpy_iterator_object
*ltpy_iter_obj
;
351 struct symtab
*symtab
= NULL
;
353 LTPY_REQUIRE_VALID (self
, symtab
);
355 ltpy_iter_obj
= PyObject_New (ltpy_iterator_object
,
356 <py_iterator_object_type
);
357 if (ltpy_iter_obj
== NULL
)
360 ltpy_iter_obj
->current_index
= 0;
361 ltpy_iter_obj
->source
= self
;
364 return (PyObject
*) ltpy_iter_obj
;
368 ltpy_iterator_dealloc (PyObject
*obj
)
370 ltpy_iterator_object
*iter_obj
= (ltpy_iterator_object
*) obj
;
372 Py_DECREF (iter_obj
->source
);
375 /* Return a reference to the line table iterator. */
378 ltpy_iterator (PyObject
*self
)
380 ltpy_iterator_object
*iter_obj
= (ltpy_iterator_object
*) self
;
381 struct symtab
*symtab
;
383 LTPY_REQUIRE_VALID (iter_obj
->source
, symtab
);
389 /* Return the next line table entry in the iteration through the line
390 table data structure. */
393 ltpy_iternext (PyObject
*self
)
395 ltpy_iterator_object
*iter_obj
= (ltpy_iterator_object
*) self
;
396 struct symtab
*symtab
;
398 struct linetable_entry
*item
;
400 LTPY_REQUIRE_VALID (iter_obj
->source
, symtab
);
402 if (iter_obj
->current_index
>= SYMTAB_LINETABLE (symtab
)->nitems
)
404 PyErr_SetNone (PyExc_StopIteration
);
408 item
= &(SYMTAB_LINETABLE (symtab
)->item
[iter_obj
->current_index
]);
410 /* Skip over internal entries such as 0. 0 signifies the end of
411 line table data and is not useful to the API user. */
412 while (item
->line
< 1)
414 iter_obj
->current_index
++;
416 /* Exit if the internal value is the last item in the line table. */
417 if (iter_obj
->current_index
>= SYMTAB_LINETABLE (symtab
)->nitems
)
419 PyErr_SetNone (PyExc_StopIteration
);
422 item
= &(SYMTAB_LINETABLE (symtab
)->item
[iter_obj
->current_index
]);
425 obj
= build_linetable_entry (item
->line
, item
->pc
);
426 iter_obj
->current_index
++;
431 /* Implementation of gdb.LineTableIterator.is_valid (self) -> Boolean.
432 Returns True if this line table iterator object still exists in
436 ltpy_iter_is_valid (PyObject
*self
, PyObject
*args
)
438 struct symtab
*symtab
= NULL
;
439 ltpy_iterator_object
*iter_obj
= (ltpy_iterator_object
*) self
;
441 symtab
= symtab_object_to_symtab (get_symtab (iter_obj
->source
));
451 static PyMethodDef linetable_object_methods
[] = {
452 { "line", ltpy_get_pcs_for_line
, METH_VARARGS
,
453 "line (lineno) -> Tuple\n\
454 Return executable locations for a given source line." },
455 { "has_line", ltpy_has_line
, METH_VARARGS
,
456 "has_line (lineno) -> Boolean\n\
457 Return TRUE if this line has executable information, FALSE if not." },
458 { "source_lines", ltpy_get_all_source_lines
, METH_NOARGS
,
459 "source_lines () -> List\n\
460 Return a list of all executable source lines." },
461 { "is_valid", ltpy_is_valid
, METH_NOARGS
,
462 "is_valid () -> Boolean.\n\
463 Return True if this LineTable is valid, False if not." },
464 {NULL
} /* Sentinel */
467 PyTypeObject linetable_object_type
= {
468 PyVarObject_HEAD_INIT (NULL
, 0)
469 "gdb.LineTable", /*tp_name*/
470 sizeof (linetable_object
), /*tp_basicsize*/
472 ltpy_dealloc
, /*tp_dealloc*/
479 0, /*tp_as_sequence*/
487 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
488 "GDB line table object", /* tp_doc */
491 0, /* tp_richcompare */
492 0, /* tp_weaklistoffset */
493 ltpy_iter
, /* tp_iter */
495 linetable_object_methods
, /* tp_methods */
500 0, /* tp_descr_get */
501 0, /* tp_descr_set */
502 0, /* tp_dictoffset */
507 static PyMethodDef ltpy_iterator_methods
[] = {
508 { "is_valid", ltpy_iter_is_valid
, METH_NOARGS
,
509 "is_valid () -> Boolean.\n\
510 Return True if this LineTable iterator is valid, False if not." },
511 {NULL
} /* Sentinel */
514 PyTypeObject ltpy_iterator_object_type
= {
515 PyVarObject_HEAD_INIT (NULL
, 0)
516 "gdb.LineTableIterator", /*tp_name*/
517 sizeof (ltpy_iterator_object
), /*tp_basicsize*/
519 ltpy_iterator_dealloc
, /*tp_dealloc*/
526 0, /*tp_as_sequence*/
534 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
535 "GDB line table iterator object", /*tp_doc */
538 0, /*tp_richcompare */
539 0, /*tp_weaklistoffset */
540 ltpy_iterator
, /*tp_iter */
541 ltpy_iternext
, /*tp_iternext */
542 ltpy_iterator_methods
/*tp_methods */
546 static gdb_PyGetSetDef linetable_entry_object_getset
[] = {
547 { "line", ltpy_entry_get_line
, NULL
,
548 "The line number in the source file.", NULL
},
549 { "pc", ltpy_entry_get_pc
, NULL
,
550 "The memory address for this line number.", NULL
},
551 { NULL
} /* Sentinel */
554 PyTypeObject linetable_entry_object_type
= {
555 PyVarObject_HEAD_INIT (NULL
, 0)
556 "gdb.LineTableEntry", /*tp_name*/
557 sizeof (linetable_entry_object
), /*tp_basicsize*/
566 0, /*tp_as_sequence*/
574 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
575 "GDB line table entry object", /* tp_doc */
578 0, /* tp_richcompare */
579 0, /* tp_weaklistoffset */
584 linetable_entry_object_getset
, /* tp_getset */
587 0, /* tp_descr_get */
588 0, /* tp_descr_set */
589 0, /* tp_dictoffset */