From: Tom Tromey Date: Fri, 27 Dec 2013 02:50:05 +0000 (-0700) Subject: Add more methods to gdb.Progspace X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=8743a9cdd20055e02a20c24c37b5312e3e626691;p=binutils-gdb.git Add more methods to gdb.Progspace There are a number of global functions in the gdb Python module which really should be methods on Progspace. This patch adds new methods to Progspace and then redefines these globals in terms of these new methods. This version has been rebased on the related changes that Simon recently put in. Built and regtested on x86-64 Fedora 28. gdb/ChangeLog 2018-09-16 Tom Tromey * python/lib/gdb/__init__.py (current_progspace, objfiles) (solib_name, block_for_pc, find_pc_line): New functions. (execute_unwinders): Update. * python/py-block.c (gdbpy_block_for_pc): Remove. * python/py-inferior.c (infpy_get_progspace): New function. (inferior_object_getset) : Add. * python/py-progspace.c (pspy_objfiles): Rewrite. (pspy_solib_name, pspy_block_for_pc) (pspy_find_pc_line, pspy_is_valid): New functions. (progspace_object_methods): Add entries for solib_name, block_for_pc, find_pc_line, is_valid. * python/python-internal.h (gdbpy_block_for_pc) (build_objfiles_list): Don't declare. * python/python.c: Don't include solib.h. (gdbpy_solib_name, gdbpy_find_pc_line) (gdbpy_get_current_progspace, build_objfiles_list) (gdbpy_objfiles): Remove. (GdbMethods) : Remove entries. gdb/doc/ChangeLog 2018-09-16 Tom Tromey * python.texi (Basic Python): Update docs for find_pc_line, solib_name. (Progspaces In Python): Update docs for current_progspace. Document block_for_pc, find_pc_line, is_valid, nsolib_name. Move method documentation before example. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 0330cd9f698..deb3f67054d 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,25 @@ +2018-09-16 Tom Tromey + + * python/lib/gdb/__init__.py (current_progspace, objfiles) + (solib_name, block_for_pc, find_pc_line): New functions. + (execute_unwinders): Update. + * python/py-block.c (gdbpy_block_for_pc): Remove. + * python/py-inferior.c (infpy_get_progspace): New function. + (inferior_object_getset) : Add. + * python/py-progspace.c (pspy_objfiles): Rewrite. + (pspy_solib_name, pspy_block_for_pc) + (pspy_find_pc_line, pspy_is_valid): New functions. + (progspace_object_methods): Add entries for solib_name, + block_for_pc, find_pc_line, is_valid. + * python/python-internal.h (gdbpy_block_for_pc) + (build_objfiles_list): Don't declare. + * python/python.c: Don't include solib.h. + (gdbpy_solib_name, gdbpy_find_pc_line) + (gdbpy_get_current_progspace, build_objfiles_list) + (gdbpy_objfiles): Remove. + (GdbMethods) : Remove entries. + 2018-09-16 Tom Tromey * top.c (new_ui_command): Use GNU style for metasyntactic diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog index 1a5c70bfc80..6d7566483f2 100644 --- a/gdb/doc/ChangeLog +++ b/gdb/doc/ChangeLog @@ -1,3 +1,11 @@ +2018-09-16 Tom Tromey + + * python.texi (Basic Python): Update docs for find_pc_line, + solib_name. + (Progspaces In Python): Update docs for current_progspace. + Document block_for_pc, find_pc_line, is_valid, nsolib_name. + Move method documentation before example. + 2018-09-13 Simon Marchi * python.texi (Objfiles In Python): Update gdb.objfiles() doc. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index aca6ec858cf..e98178a1216 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -328,7 +328,9 @@ Return the @code{gdb.Symtab_and_line} object corresponding to the @var{pc} value. @xref{Symbol Tables In Python}. If an invalid value of @var{pc} is passed as an argument, then the @code{symtab} and @code{line} attributes of the returned @code{gdb.Symtab_and_line} object -will be @code{None} and 0 respectively. +will be @code{None} and 0 respectively. This is identical to +@code{gdb.current_progspace().find_pc_line(pc)} and is included for +historical compatibility. @end defun @findex gdb.post_event @@ -448,7 +450,9 @@ never returned. @findex gdb.solib_name @defun gdb.solib_name (address) Return the name of the shared library holding the given @var{address} -as a string, or @code{None}. +as a string, or @code{None}. This is identical to +@code{gdb.current_progspace().solib_name(address)} and is included for +historical compatibility. @end defun @findex gdb.decode_line @@ -4035,6 +4039,45 @@ The @code{frame_filters} attribute is a dictionary of frame filter objects. @xref{Frame Filter API}, for more information. @end defvar +A program space has the following methods: + +@findex Progspace.block_for_pc +@defun Progspace.block_for_pc (pc) +Return the innermost @code{gdb.Block} containing the given @var{pc} +value. If the block cannot be found for the @var{pc} value specified, +the function will return @code{None}. +@end defun + +@findex Progspace.find_pc_line +@defun Progspace.find_pc_line (pc) +Return the @code{gdb.Symtab_and_line} object corresponding to the +@var{pc} value. @xref{Symbol Tables In Python}. If an invalid value +of @var{pc} is passed as an argument, then the @code{symtab} and +@code{line} attributes of the returned @code{gdb.Symtab_and_line} +object will be @code{None} and 0 respectively. +@end defun + +@findex Progspace.is_valid +@defun Progspace.is_valid () +Returns @code{True} if the @code{gdb.Progspace} object is valid, +@code{False} if not. A @code{gdb.Progspace} object can become invalid +if the program space file it refers to is not referenced by any +inferior. All other @code{gdb.Progspace} methods will throw an +exception if it is invalid at the time the method is called. +@end defun + +@findex Progspace.objfiles +@defun Progspace.objfiles () +Return a sequence of all the objfiles referenced by this program +space. @xref{Objfiles In Python}. +@end defun + +@findex Progspace.solib_name +@defun Progspace.solib_name (address) +Return the name of the shared library holding the given @var{address} +as a string, or @code{None}. +@end defun + One may add arbitrary attributes to @code{gdb.Progspace} objects in the usual Python way. This is useful if, for example, one needs to do some extra record keeping @@ -4079,14 +4122,6 @@ Hello. [Inferior 1 (process 4242) exited normally] @end smallexample -A @code{gdb.Progspace} object has the following methods: - -@findex Progspace.objfiles -@defun Progspace.objfiles () -Return a sequence of all the objfiles referenced by this program -space. @xref{Objfiles In Python}. -@end defun - @node Objfiles In Python @subsubsection Objfiles In Python @@ -4515,7 +4550,9 @@ module: @defun gdb.block_for_pc (pc) Return the innermost @code{gdb.Block} containing the given @var{pc} value. If the block cannot be found for the @var{pc} value specified, -the function will return @code{None}. +the function will return @code{None}. This is identical to +@code{gdb.current_progspace().block_for_pc(pc)} and is included for +historical compatibility. @end defun A @code{gdb.Block} object has the following methods: diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py index 7e7c299f3c7..c223f2bac3d 100644 --- a/gdb/python/lib/gdb/__init__.py +++ b/gdb/python/lib/gdb/__init__.py @@ -85,15 +85,14 @@ def execute_unwinders(pending_frame): Returns: gdb.UnwindInfo instance or None. """ - for objfile in _gdb.objfiles(): + for objfile in objfiles(): for unwinder in objfile.frame_unwinders: if unwinder.enabled: unwind_info = unwinder(pending_frame) if unwind_info is not None: return unwind_info - current_progspace = _gdb.current_progspace() - for unwinder in current_progspace.frame_unwinders: + for unwinder in current_progspace().frame_unwinders: if unwinder.enabled: unwind_info = unwinder(pending_frame) if unwind_info is not None: @@ -163,3 +162,25 @@ def GdbSetPythonDirectory(dir): # attributes reload(__import__(__name__)) auto_load_packages() + +def current_progspace(): + "Return the current Progspace." + return selected_inferior().progspace + +def objfiles(): + "Return a sequence of the current program space's objfiles." + return current_progspace().objfiles() + +def solib_name (addr): + """solib_name (Long) -> String.\n\ +Return the name of the shared library holding a given address, or None.""" + return current_progspace().solib_name(addr) + +def block_for_pc(pc): + "Return the block containing the given pc value, or None." + return current_progspace().block_for_pc(pc) + +def find_pc_line(pc): + """find_pc_line (pc) -> Symtab_and_line. +Return the gdb.Symtab_and_line object corresponding to the pc value.""" + return current_progspace().find_pc_line(pc) diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c index 70a0a76dc43..5fc33f0f0fb 100644 --- a/gdb/python/py-block.c +++ b/gdb/python/py-block.c @@ -366,44 +366,6 @@ blpy_iter_is_valid (PyObject *self, PyObject *args) Py_RETURN_TRUE; } -/* Return the innermost lexical block containing the specified pc value, - or 0 if there is none. */ -PyObject * -gdbpy_block_for_pc (PyObject *self, PyObject *args) -{ - gdb_py_ulongest pc; - const struct block *block = NULL; - struct compunit_symtab *cust = NULL; - - if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc)) - return NULL; - - TRY - { - cust = find_pc_compunit_symtab (pc); - - if (cust != NULL && COMPUNIT_OBJFILE (cust) != NULL) - block = block_for_pc (pc); - } - CATCH (except, RETURN_MASK_ALL) - { - GDB_PY_HANDLE_EXCEPTION (except); - } - END_CATCH - - if (cust == NULL || COMPUNIT_OBJFILE (cust) == NULL) - { - PyErr_SetString (PyExc_RuntimeError, - _("Cannot locate object file for block.")); - return NULL; - } - - if (block) - return block_to_block_object (block, COMPUNIT_OBJFILE (cust)); - - Py_RETURN_NONE; -} - /* This function is called when an objfile is about to be freed. Invalidate the block as further actions on the block would result in bad data. All access to obj->symbol should be gated by diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index e01338f43d3..b88ad3c1773 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -25,6 +25,8 @@ #include "language.h" #include "arch-utils.h" #include "py-ref.h" +#include "solib.h" +#include "block.h" typedef struct { @@ -332,9 +334,143 @@ pspy_get_objfiles (PyObject *self_, PyObject *args) PSPY_REQUIRE_VALID (self); - return build_objfiles_list (self->pspace).release (); + gdbpy_ref<> list (PyList_New (0)); + if (list == NULL) + return NULL; + + if (self->pspace != NULL) + { + struct objfile *objf; + + ALL_PSPACE_OBJFILES (self->pspace, objf) + { + PyObject *item = objfile_to_objfile_object (objf); + + if (!item || PyList_Append (list.get (), item) == -1) + return NULL; + } + } + + return list.release (); +} + +/* Implementation of solib_name (Long) -> String. + Returns the name of the shared library holding a given address, or None. */ + +static PyObject * +pspy_solib_name (PyObject *o, PyObject *args) +{ + char *soname; + gdb_py_longest pc; + pspace_object *self = (pspace_object *) o; + + PSPY_REQUIRE_VALID (self); + + if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc)) + return NULL; + + soname = solib_name_from_address (self->pspace, pc); + if (soname == nullptr) + Py_RETURN_NONE; + return host_string_to_python_string (soname); } +/* Return the innermost lexical block containing the specified pc value, + or 0 if there is none. */ +static PyObject * +pspy_block_for_pc (PyObject *o, PyObject *args) +{ + pspace_object *self = (pspace_object *) o; + gdb_py_ulongest pc; + const struct block *block = NULL; + struct compunit_symtab *cust = NULL; + + PSPY_REQUIRE_VALID (self); + + if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc)) + return NULL; + + TRY + { + scoped_restore_current_program_space saver; + + set_current_program_space (self->pspace); + cust = find_pc_compunit_symtab (pc); + + if (cust != NULL && COMPUNIT_OBJFILE (cust) != NULL) + block = block_for_pc (pc); + } + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH + + if (cust == NULL || COMPUNIT_OBJFILE (cust) == NULL) + { + PyErr_SetString (PyExc_RuntimeError, + _("Cannot locate object file for block.")); + return NULL; + } + + if (block) + return block_to_block_object (block, COMPUNIT_OBJFILE (cust)); + + Py_RETURN_NONE; +} + +/* Implementation of the find_pc_line function. + Returns the gdb.Symtab_and_line object corresponding to a PC value. */ + +static PyObject * +pspy_find_pc_line (PyObject *o, PyObject *args) +{ + gdb_py_ulongest pc_llu; + PyObject *result = NULL; /* init for gcc -Wall */ + pspace_object *self = (pspace_object *) o; + + PSPY_REQUIRE_VALID (self); + + if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu)) + return NULL; + + TRY + { + struct symtab_and_line sal; + CORE_ADDR pc; + scoped_restore_current_program_space saver; + + set_current_program_space (self->pspace); + + pc = (CORE_ADDR) pc_llu; + sal = find_pc_line (pc, 0); + result = symtab_and_line_to_sal_object (sal); + } + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH + + return result; +} + +/* Implementation of is_valid (self) -> Boolean. + Returns True if this program space still exists in GDB. */ + +static PyObject * +pspy_is_valid (PyObject *o, PyObject *args) +{ + pspace_object *self = (pspace_object *) o; + + if (self->pspace == NULL) + Py_RETURN_FALSE; + + Py_RETURN_TRUE; +} + + + /* Clear the PSPACE pointer in a Pspace object and remove the reference. */ static void @@ -420,6 +556,17 @@ static PyMethodDef progspace_object_methods[] = { { "objfiles", pspy_get_objfiles, METH_NOARGS, "Return a sequence of objfiles associated to this program space." }, + { "solib_name", pspy_solib_name, METH_VARARGS, + "solib_name (Long) -> String.\n\ +Return the name of the shared library holding a given address, or None." }, + { "block_for_pc", pspy_block_for_pc, METH_VARARGS, + "Return the block containing the given pc value, or None." }, + { "find_pc_line", pspy_find_pc_line, METH_VARARGS, + "find_pc_line (pc) -> Symtab_and_line.\n\ +Return the gdb.Symtab_and_line object corresponding to the pc value." }, + { "is_valid", pspy_is_valid, METH_NOARGS, + "is_valid () -> Boolean.\n\ +Return true if this program space is valid, false if not." }, { NULL } }; diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 785ad171511..1c526af1586 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -492,7 +492,6 @@ PyObject *gdbpy_current_recording (PyObject *self, PyObject *args); PyObject *gdbpy_stop_recording (PyObject *self, PyObject *args); PyObject *gdbpy_newest_frame (PyObject *self, PyObject *args); PyObject *gdbpy_selected_frame (PyObject *self, PyObject *args); -PyObject *gdbpy_block_for_pc (PyObject *self, PyObject *args); PyObject *gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw); int gdbpy_is_field (PyObject *obj); PyObject *gdbpy_create_lazy_string_object (CORE_ADDR address, long length, @@ -549,10 +548,6 @@ struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj); struct frame_info *frame_object_to_frame_info (PyObject *frame_obj); struct gdbarch *arch_object_to_gdbarch (PyObject *obj); -/* Return a Python list containing an Objfile object for each objfile in - PSPACE. */ -gdbpy_ref<> build_objfiles_list (program_space *pspace); - void gdbpy_initialize_gdb_readline (void); int gdbpy_initialize_auto_load (void) CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION; diff --git a/gdb/python/python.c b/gdb/python/python.c index e89c90f8d9f..f541a987894 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -90,7 +90,6 @@ const struct extension_language_defn extension_language_python = #include "cli/cli-decode.h" #include "charset.h" #include "top.h" -#include "solib.h" #include "python-internal.h" #include "linespec.h" #include "source.h" @@ -635,31 +634,6 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw) Py_RETURN_NONE; } -/* Implementation of gdb.solib_name (Long) -> String. - Returns the name of the shared library holding a given address, or None. */ - -static PyObject * -gdbpy_solib_name (PyObject *self, PyObject *args) -{ - char *soname; - PyObject *str_obj; - gdb_py_ulongest pc; - - if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc)) - return NULL; - - soname = solib_name_from_address (current_program_space, pc); - if (soname) - str_obj = host_string_to_python_string (soname); - else - { - str_obj = Py_None; - Py_INCREF (Py_None); - } - - return str_obj; -} - /* Implementation of Python rbreak command. Take a REGEX and optionally a MINSYMS, THROTTLE and SYMTABS keyword and return a Python list that contains newly set breakpoints that match that @@ -945,36 +919,6 @@ gdbpy_parse_and_eval (PyObject *self, PyObject *args) return value_to_value_object (result); } -/* Implementation of gdb.find_pc_line function. - Returns the gdb.Symtab_and_line object corresponding to a PC value. */ - -static PyObject * -gdbpy_find_pc_line (PyObject *self, PyObject *args) -{ - gdb_py_ulongest pc_llu; - PyObject *result = NULL; /* init for gcc -Wall */ - - if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu)) - return NULL; - - TRY - { - struct symtab_and_line sal; - CORE_ADDR pc; - - pc = (CORE_ADDR) pc_llu; - sal = find_pc_line (pc, 0); - result = symtab_and_line_to_sal_object (sal); - } - CATCH (except, RETURN_MASK_ALL) - { - GDB_PY_HANDLE_EXCEPTION (except); - } - END_CATCH - - return result; -} - /* Implementation of gdb.invalidate_cached_frames. */ static PyObject * @@ -1336,20 +1280,6 @@ gdbpy_print_stack (void) -/* Return the current Progspace. - There always is one. */ - -static PyObject * -gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2) -{ - PyObject *result; - - result = pspace_to_pspace_object (current_program_space); - if (result) - Py_INCREF (result); - return result; -} - /* Return a sequence holding all the Progspaces. */ static PyObject * @@ -1440,36 +1370,6 @@ gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2) return result; } -/* See python-internal.h. */ - -gdbpy_ref<> -build_objfiles_list (program_space *pspace) -{ - struct objfile *objf; - - gdbpy_ref<> list (PyList_New (0)); - if (list == NULL) - return NULL; - - ALL_PSPACE_OBJFILES (pspace, objf) - { - PyObject *item = objfile_to_objfile_object (objf); - - if (item == nullptr || PyList_Append (list.get (), item) == -1) - return NULL; - } - - return list; -} - -/* Return a sequence holding all the Objfiles. */ - -static PyObject * -gdbpy_objfiles (PyObject *unused1, PyObject *unused2) -{ - return build_objfiles_list (current_program_space).release (); -} - /* Compute the list of active python type printers and store them in EXT_PRINTERS->py_type_printers. The product of this function is used by gdbpy_apply_type_printers, and freed by gdbpy_free_type_printers. @@ -2045,15 +1945,11 @@ set to True." }, { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS, "Find the default visualizer for a Value." }, - { "current_progspace", gdbpy_get_current_progspace, METH_NOARGS, - "Return the current Progspace." }, { "progspaces", gdbpy_progspaces, METH_NOARGS, "Return a sequence of all progspaces." }, { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS, "Return the current Objfile being loaded, or None." }, - { "objfiles", gdbpy_objfiles, METH_NOARGS, - "Return a sequence of all loaded objfiles." }, { "newest_frame", gdbpy_newest_frame, METH_NOARGS, "newest_frame () -> gdb.Frame.\n\ @@ -2099,11 +1995,6 @@ Look up the specified objfile.\n\ If by_build_id is True, the objfile is looked up by using name\n\ as its build id." }, - { "block_for_pc", gdbpy_block_for_pc, METH_VARARGS, - "Return the block containing the given pc value, or None." }, - { "solib_name", gdbpy_solib_name, METH_VARARGS, - "solib_name (Long) -> String.\n\ -Return the name of the shared library holding a given address, or None." }, { "decode_line", gdbpy_decode_line, METH_VARARGS, "decode_line (String) -> Tuple. Decode a string argument the way\n\ that 'break' or 'edit' does. Return a tuple containing two elements.\n\ @@ -2115,9 +2006,6 @@ gdb.Symtab_and_line objects (or None)."}, "parse_and_eval (String) -> Value.\n\ Parse String as an expression, evaluate it, and return the result as a Value." }, - { "find_pc_line", gdbpy_find_pc_line, METH_VARARGS, - "find_pc_line (pc) -> Symtab_and_line.\n\ -Return the gdb.Symtab_and_line object corresponding to the pc value." }, { "post_event", gdbpy_post_event, METH_VARARGS, "Post an event into gdb's event loop." },