From: Tom Tromey Date: Tue, 24 Oct 2023 14:05:06 +0000 (-0600) Subject: Add gdb.Frame.static_link method X-Git-Tag: gdb-14.1-release~31 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a3ca9693fff76ccddd60dbf00fd9a045726545f9;p=thirdparty%2Fbinutils-gdb.git Add gdb.Frame.static_link method This adds a new gdb.Frame.static_link method to the gdb Python layer. This can be used to find the static link frame for a given frame. Reviewed-By: Eli Zaretskii (cherry picked from commit 4ead09a294adbb718d642874a554e78d931c2830) --- diff --git a/gdb/NEWS b/gdb/NEWS index 240f2d16b50..e0271ef5c64 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -350,6 +350,9 @@ show tui mouse-events ** gdb.LazyString now implements the __str__ method. + ** New method gdb.Frame.static_link that returns the outer frame + of a nested function frame. + *** Changes in GDB 13 * MI version 1 is deprecated, and will be removed in GDB 14. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index a97e4451897..a1ed6429773 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -5671,6 +5671,16 @@ Set this frame to be the selected frame. @xref{Stack, ,Examining the Stack}. @end defun +@defun Frame.static_link () +In some languages (e.g., Ada, but also a GNU C extension), a nested +function can access the variables in the outer scope. This is done +via a ``static link'', which is a reference from the nested frame to +the appropriate outer frame. + +This method returns this frame's static link frame, if one exists. If +there is no static link, this method returns @code{None}. +@end defun + @defun Frame.level () Return an integer, the stack frame level for this frame. @xref{Frames, ,Stack Frames}. @end defun diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c index 1a55e514e39..0a7e10f09ff 100644 --- a/gdb/python/py-frame.c +++ b/gdb/python/py-frame.c @@ -622,6 +622,30 @@ frapy_language (PyObject *self, PyObject *args) Py_RETURN_NONE; } +/* The static link for this frame. */ + +static PyObject * +frapy_static_link (PyObject *self, PyObject *args) +{ + frame_info_ptr link; + + try + { + FRAPY_REQUIRE_VALID (self, link); + + link = frame_follow_static_link (link); + } + catch (const gdb_exception &except) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + + if (link == nullptr) + Py_RETURN_NONE; + + return frame_info_to_frame_object (link); +} + /* Implementation of gdb.newest_frame () -> gdb.Frame. Returns the newest frame object. */ @@ -800,6 +824,8 @@ Return the value of the variable in this frame." }, "The stack level of this frame." }, { "language", frapy_language, METH_NOARGS, "The language of this frame." }, + { "static_link", frapy_static_link, METH_NOARGS, + "The static link of this frame, or None." }, {NULL} /* Sentinel */ };