[PATCH][PR/24474] Make gdb.lookup_static_symbol also check the STATIC_SCOPE

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH][PR/24474] Make gdb.lookup_static_symbol also check the STATIC_SCOPE

Sourceware - gdb-patches mailing list
gdb/ChangeLog:

2019-06-04  Christian Biesinger  <[hidden email]>

        PR/24474: Make gdb.lookup_global_symbol also check the STATIC_SCOPE
        because from a C/C++ developer's perspective, these are also globals.
        * NEWS: Mention this change.
        * python/py-symbol.c (gdbpy_lookup_global_symbol): Call
        lookup_static_symbol if lookup_global_symbol returns NULL.

gdb/testsuite/ChangeLog:

2019-06-04  Christian Biesinger  <[hidden email]>

        * gdb.python/py-symbol.c: Add a static variable and one in an anonymous
        namespace.
        * gdb.python/py-symbol.exp: Check that lookup_global_symbol finds those
        variables.


---
 gdb/NEWS                               |  2 ++
 gdb/python/py-symbol.c                 | 11 +++++++++++
 gdb/testsuite/gdb.python/py-symbol.c   |  5 +++++
 gdb/testsuite/gdb.python/py-symbol.exp | 13 +++++++++++++
 4 files changed, 31 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index ded1fce406..bfd8d0a191 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -30,6 +30,8 @@
   ** gdb.Type has a new property 'objfile' which returns the objfile the
      type was defined in.
 
+  ** gdb.lookup_global_symbol will now also find symbols with static linkage.
+
 * New built-in convenience variables $_shell_exitcode and $_shell_exitsignal
   provide the exitcode or exit status of the shell commands launched by
   GDB commands such as "shell", "pipe" and "make".
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 8605ae71a2..5636ef9013 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -456,6 +456,17 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
       GDB_PY_HANDLE_EXCEPTION (except);
     }
 
+  if (symbol == NULL) {
+    try
+      {
+        symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
+      }
+    catch (const gdb_exception &except)
+      {
+        GDB_PY_HANDLE_EXCEPTION (except);
+      }
+  }
+
   if (symbol)
     {
       sym_obj = symbol_to_symbol_object (symbol);
diff --git a/gdb/testsuite/gdb.python/py-symbol.c b/gdb/testsuite/gdb.python/py-symbol.c
index f77c8c8585..06a931bf5d 100644
--- a/gdb/testsuite/gdb.python/py-symbol.c
+++ b/gdb/testsuite/gdb.python/py-symbol.c
@@ -32,9 +32,14 @@ class SimpleClass
     return i; /* Break in class. */
   }
 };
+
+namespace {
+  int anon = 10;
+};
 #endif
 
 int qq = 72; /* line of qq */
+static int rr = 42; /* line of rr */
 
 int func (int arg)
 {
diff --git a/gdb/testsuite/gdb.python/py-symbol.exp b/gdb/testsuite/gdb.python/py-symbol.exp
index 5b8a2be7c4..722fcda0f0 100644
--- a/gdb/testsuite/gdb.python/py-symbol.exp
+++ b/gdb/testsuite/gdb.python/py-symbol.exp
@@ -48,6 +48,16 @@ gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
     "False" \
     "print whether qq needs a frame"
 
+set rr_line [gdb_get_line_number "line of rr"]
+gdb_test "python print (gdb.lookup_global_symbol ('rr').line)" "$rr_line" \
+    "print line number of rr"
+
+gdb_test "python print (gdb.lookup_global_symbol ('rr').value ())" "42" \
+    "print value of rr"
+
+gdb_test "python print (gdb.lookup_global_symbol ('rr').needs_frame)" \
+    "False" \
+    "print whether rr needs a frame"
 
 if ![runto_main] then {
     fail "can't run to main"
@@ -137,6 +147,9 @@ gdb_start
 gdb_reinitialize_dir $srcdir/$subdir
 gdb_load ${binfile}-cxx
 
+gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon').value ())" "10" \
+    "print value of rr"
+
 if ![runto_main] then {
     fail "can't run to main"
     return 0
--
2.22.0.rc1.311.g5d7573a151-goog

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH][PR/24474] Make gdb.lookup_static_symbol also check the STATIC_SCOPE

Sourceware - gdb-patches mailing list
ping?

On Tue, Jun 4, 2019 at 8:24 PM Christian Biesinger
<[hidden email]> wrote:

>
> gdb/ChangeLog:
>
> 2019-06-04  Christian Biesinger  <[hidden email]>
>
>         PR/24474: Make gdb.lookup_global_symbol also check the STATIC_SCOPE
>         because from a C/C++ developer's perspective, these are also globals.
>         * NEWS: Mention this change.
>         * python/py-symbol.c (gdbpy_lookup_global_symbol): Call
>         lookup_static_symbol if lookup_global_symbol returns NULL.
>
> gdb/testsuite/ChangeLog:
>
> 2019-06-04  Christian Biesinger  <[hidden email]>
>
>         * gdb.python/py-symbol.c: Add a static variable and one in an anonymous
>         namespace.
>         * gdb.python/py-symbol.exp: Check that lookup_global_symbol finds those
>         variables.
>
>
> ---
>  gdb/NEWS                               |  2 ++
>  gdb/python/py-symbol.c                 | 11 +++++++++++
>  gdb/testsuite/gdb.python/py-symbol.c   |  5 +++++
>  gdb/testsuite/gdb.python/py-symbol.exp | 13 +++++++++++++
>  4 files changed, 31 insertions(+)
>
> diff --git a/gdb/NEWS b/gdb/NEWS
> index ded1fce406..bfd8d0a191 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -30,6 +30,8 @@
>    ** gdb.Type has a new property 'objfile' which returns the objfile the
>       type was defined in.
>
> +  ** gdb.lookup_global_symbol will now also find symbols with static linkage.
> +
>  * New built-in convenience variables $_shell_exitcode and $_shell_exitsignal
>    provide the exitcode or exit status of the shell commands launched by
>    GDB commands such as "shell", "pipe" and "make".
> diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
> index 8605ae71a2..5636ef9013 100644
> --- a/gdb/python/py-symbol.c
> +++ b/gdb/python/py-symbol.c
> @@ -456,6 +456,17 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
>        GDB_PY_HANDLE_EXCEPTION (except);
>      }
>
> +  if (symbol == NULL) {
> +    try
> +      {
> +        symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
> +      }
> +    catch (const gdb_exception &except)
> +      {
> +        GDB_PY_HANDLE_EXCEPTION (except);
> +      }
> +  }
> +
>    if (symbol)
>      {
>        sym_obj = symbol_to_symbol_object (symbol);
> diff --git a/gdb/testsuite/gdb.python/py-symbol.c b/gdb/testsuite/gdb.python/py-symbol.c
> index f77c8c8585..06a931bf5d 100644
> --- a/gdb/testsuite/gdb.python/py-symbol.c
> +++ b/gdb/testsuite/gdb.python/py-symbol.c
> @@ -32,9 +32,14 @@ class SimpleClass
>      return i; /* Break in class. */
>    }
>  };
> +
> +namespace {
> +  int anon = 10;
> +};
>  #endif
>
>  int qq = 72;                   /* line of qq */
> +static int rr = 42;            /* line of rr */
>
>  int func (int arg)
>  {
> diff --git a/gdb/testsuite/gdb.python/py-symbol.exp b/gdb/testsuite/gdb.python/py-symbol.exp
> index 5b8a2be7c4..722fcda0f0 100644
> --- a/gdb/testsuite/gdb.python/py-symbol.exp
> +++ b/gdb/testsuite/gdb.python/py-symbol.exp
> @@ -48,6 +48,16 @@ gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
>      "False" \
>      "print whether qq needs a frame"
>
> +set rr_line [gdb_get_line_number "line of rr"]
> +gdb_test "python print (gdb.lookup_global_symbol ('rr').line)" "$rr_line" \
> +    "print line number of rr"
> +
> +gdb_test "python print (gdb.lookup_global_symbol ('rr').value ())" "42" \
> +    "print value of rr"
> +
> +gdb_test "python print (gdb.lookup_global_symbol ('rr').needs_frame)" \
> +    "False" \
> +    "print whether rr needs a frame"
>
>  if ![runto_main] then {
>      fail "can't run to main"
> @@ -137,6 +147,9 @@ gdb_start
>  gdb_reinitialize_dir $srcdir/$subdir
>  gdb_load ${binfile}-cxx
>
> +gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon').value ())" "10" \
> +    "print value of rr"
> +
>  if ![runto_main] then {
>      fail "can't run to main"
>      return 0
> --
> 2.22.0.rc1.311.g5d7573a151-goog
>
Reply | Threaded
Open this post in threaded view
|

Re: [PING] [PATCH][PR/24474] Make gdb.lookup_static_symbol also check the STATIC_SCOPE

Sourceware - gdb-patches mailing list
In reply to this post by Sourceware - gdb-patches mailing list
ping?

On Tue, Jun 4, 2019 at 8:24 PM Christian Biesinger
<[hidden email]> wrote:

>
> gdb/ChangeLog:
>
> 2019-06-04  Christian Biesinger  <[hidden email]>
>
>         PR/24474: Make gdb.lookup_global_symbol also check the STATIC_SCOPE
>         because from a C/C++ developer's perspective, these are also globals.
>         * NEWS: Mention this change.
>         * python/py-symbol.c (gdbpy_lookup_global_symbol): Call
>         lookup_static_symbol if lookup_global_symbol returns NULL.
>
> gdb/testsuite/ChangeLog:
>
> 2019-06-04  Christian Biesinger  <[hidden email]>
>
>         * gdb.python/py-symbol.c: Add a static variable and one in an anonymous
>         namespace.
>         * gdb.python/py-symbol.exp: Check that lookup_global_symbol finds those
>         variables.
>
>
> ---
>  gdb/NEWS                               |  2 ++
>  gdb/python/py-symbol.c                 | 11 +++++++++++
>  gdb/testsuite/gdb.python/py-symbol.c   |  5 +++++
>  gdb/testsuite/gdb.python/py-symbol.exp | 13 +++++++++++++
>  4 files changed, 31 insertions(+)
>
> diff --git a/gdb/NEWS b/gdb/NEWS
> index ded1fce406..bfd8d0a191 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -30,6 +30,8 @@
>    ** gdb.Type has a new property 'objfile' which returns the objfile the
>       type was defined in.
>
> +  ** gdb.lookup_global_symbol will now also find symbols with static linkage.
> +
>  * New built-in convenience variables $_shell_exitcode and $_shell_exitsignal
>    provide the exitcode or exit status of the shell commands launched by
>    GDB commands such as "shell", "pipe" and "make".
> diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
> index 8605ae71a2..5636ef9013 100644
> --- a/gdb/python/py-symbol.c
> +++ b/gdb/python/py-symbol.c
> @@ -456,6 +456,17 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
>        GDB_PY_HANDLE_EXCEPTION (except);
>      }
>
> +  if (symbol == NULL) {
> +    try
> +      {
> +        symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
> +      }
> +    catch (const gdb_exception &except)
> +      {
> +        GDB_PY_HANDLE_EXCEPTION (except);
> +      }
> +  }
> +
>    if (symbol)
>      {
>        sym_obj = symbol_to_symbol_object (symbol);
> diff --git a/gdb/testsuite/gdb.python/py-symbol.c b/gdb/testsuite/gdb.python/py-symbol.c
> index f77c8c8585..06a931bf5d 100644
> --- a/gdb/testsuite/gdb.python/py-symbol.c
> +++ b/gdb/testsuite/gdb.python/py-symbol.c
> @@ -32,9 +32,14 @@ class SimpleClass
>      return i; /* Break in class. */
>    }
>  };
> +
> +namespace {
> +  int anon = 10;
> +};
>  #endif
>
>  int qq = 72;                   /* line of qq */
> +static int rr = 42;            /* line of rr */
>
>  int func (int arg)
>  {
> diff --git a/gdb/testsuite/gdb.python/py-symbol.exp b/gdb/testsuite/gdb.python/py-symbol.exp
> index 5b8a2be7c4..722fcda0f0 100644
> --- a/gdb/testsuite/gdb.python/py-symbol.exp
> +++ b/gdb/testsuite/gdb.python/py-symbol.exp
> @@ -48,6 +48,16 @@ gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
>      "False" \
>      "print whether qq needs a frame"
>
> +set rr_line [gdb_get_line_number "line of rr"]
> +gdb_test "python print (gdb.lookup_global_symbol ('rr').line)" "$rr_line" \
> +    "print line number of rr"
> +
> +gdb_test "python print (gdb.lookup_global_symbol ('rr').value ())" "42" \
> +    "print value of rr"
> +
> +gdb_test "python print (gdb.lookup_global_symbol ('rr').needs_frame)" \
> +    "False" \
> +    "print whether rr needs a frame"
>
>  if ![runto_main] then {
>      fail "can't run to main"
> @@ -137,6 +147,9 @@ gdb_start
>  gdb_reinitialize_dir $srcdir/$subdir
>  gdb_load ${binfile}-cxx
>
> +gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon').value ())" "10" \
> +    "print value of rr"
> +
>  if ![runto_main] then {
>      fail "can't run to main"
>      return 0
> --
> 2.22.0.rc1.311.g5d7573a151-goog
>
Reply | Threaded
Open this post in threaded view
|

Re: [PING] [PATCH][PR/24474] Make gdb.lookup_static_symbol also check the STATIC_SCOPE

Sourceware - gdb-patches mailing list
weekly ping

On Mon, Jul 8, 2019 at 5:04 PM Christian Biesinger
<[hidden email]> wrote:

>
> ping?
>
> On Tue, Jun 4, 2019 at 8:24 PM Christian Biesinger
> <[hidden email]> wrote:
> >
> > gdb/ChangeLog:
> >
> > 2019-06-04  Christian Biesinger  <[hidden email]>
> >
> >         PR/24474: Make gdb.lookup_global_symbol also check the STATIC_SCOPE
> >         because from a C/C++ developer's perspective, these are also globals.
> >         * NEWS: Mention this change.
> >         * python/py-symbol.c (gdbpy_lookup_global_symbol): Call
> >         lookup_static_symbol if lookup_global_symbol returns NULL.
> >
> > gdb/testsuite/ChangeLog:
> >
> > 2019-06-04  Christian Biesinger  <[hidden email]>
> >
> >         * gdb.python/py-symbol.c: Add a static variable and one in an anonymous
> >         namespace.
> >         * gdb.python/py-symbol.exp: Check that lookup_global_symbol finds those
> >         variables.
> >
> >
> > ---
> >  gdb/NEWS                               |  2 ++
> >  gdb/python/py-symbol.c                 | 11 +++++++++++
> >  gdb/testsuite/gdb.python/py-symbol.c   |  5 +++++
> >  gdb/testsuite/gdb.python/py-symbol.exp | 13 +++++++++++++
> >  4 files changed, 31 insertions(+)
> >
> > diff --git a/gdb/NEWS b/gdb/NEWS
> > index ded1fce406..bfd8d0a191 100644
> > --- a/gdb/NEWS
> > +++ b/gdb/NEWS
> > @@ -30,6 +30,8 @@
> >    ** gdb.Type has a new property 'objfile' which returns the objfile the
> >       type was defined in.
> >
> > +  ** gdb.lookup_global_symbol will now also find symbols with static linkage.
> > +
> >  * New built-in convenience variables $_shell_exitcode and $_shell_exitsignal
> >    provide the exitcode or exit status of the shell commands launched by
> >    GDB commands such as "shell", "pipe" and "make".
> > diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
> > index 8605ae71a2..5636ef9013 100644
> > --- a/gdb/python/py-symbol.c
> > +++ b/gdb/python/py-symbol.c
> > @@ -456,6 +456,17 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
> >        GDB_PY_HANDLE_EXCEPTION (except);
> >      }
> >
> > +  if (symbol == NULL) {
> > +    try
> > +      {
> > +        symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
> > +      }
> > +    catch (const gdb_exception &except)
> > +      {
> > +        GDB_PY_HANDLE_EXCEPTION (except);
> > +      }
> > +  }
> > +
> >    if (symbol)
> >      {
> >        sym_obj = symbol_to_symbol_object (symbol);
> > diff --git a/gdb/testsuite/gdb.python/py-symbol.c b/gdb/testsuite/gdb.python/py-symbol.c
> > index f77c8c8585..06a931bf5d 100644
> > --- a/gdb/testsuite/gdb.python/py-symbol.c
> > +++ b/gdb/testsuite/gdb.python/py-symbol.c
> > @@ -32,9 +32,14 @@ class SimpleClass
> >      return i; /* Break in class. */
> >    }
> >  };
> > +
> > +namespace {
> > +  int anon = 10;
> > +};
> >  #endif
> >
> >  int qq = 72;                   /* line of qq */
> > +static int rr = 42;            /* line of rr */
> >
> >  int func (int arg)
> >  {
> > diff --git a/gdb/testsuite/gdb.python/py-symbol.exp b/gdb/testsuite/gdb.python/py-symbol.exp
> > index 5b8a2be7c4..722fcda0f0 100644
> > --- a/gdb/testsuite/gdb.python/py-symbol.exp
> > +++ b/gdb/testsuite/gdb.python/py-symbol.exp
> > @@ -48,6 +48,16 @@ gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
> >      "False" \
> >      "print whether qq needs a frame"
> >
> > +set rr_line [gdb_get_line_number "line of rr"]
> > +gdb_test "python print (gdb.lookup_global_symbol ('rr').line)" "$rr_line" \
> > +    "print line number of rr"
> > +
> > +gdb_test "python print (gdb.lookup_global_symbol ('rr').value ())" "42" \
> > +    "print value of rr"
> > +
> > +gdb_test "python print (gdb.lookup_global_symbol ('rr').needs_frame)" \
> > +    "False" \
> > +    "print whether rr needs a frame"
> >
> >  if ![runto_main] then {
> >      fail "can't run to main"
> > @@ -137,6 +147,9 @@ gdb_start
> >  gdb_reinitialize_dir $srcdir/$subdir
> >  gdb_load ${binfile}-cxx
> >
> > +gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon').value ())" "10" \
> > +    "print value of rr"
> > +
> >  if ![runto_main] then {
> >      fail "can't run to main"
> >      return 0
> > --
> > 2.22.0.rc1.311.g5d7573a151-goog
> >
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH][PR/24474] Make gdb.lookup_static_symbol also check the STATIC_SCOPE

Simon Marchi-4
In reply to this post by Sourceware - gdb-patches mailing list
On 2019-06-04 9:24 p.m., Christian Biesinger via gdb-patches wrote:

> gdb/ChangeLog:
>
> 2019-06-04  Christian Biesinger  <[hidden email]>
>
> PR/24474: Make gdb.lookup_global_symbol also check the STATIC_SCOPE
> because from a C/C++ developer's perspective, these are also globals.
> * NEWS: Mention this change.
> * python/py-symbol.c (gdbpy_lookup_global_symbol): Call
> lookup_static_symbol if lookup_global_symbol returns NULL.
>
> gdb/testsuite/ChangeLog:
>
> 2019-06-04  Christian Biesinger  <[hidden email]>
>
> * gdb.python/py-symbol.c: Add a static variable and one in an anonymous
> namespace.
> * gdb.python/py-symbol.exp: Check that lookup_global_symbol finds those
> variables.

Hi Christian,

Sorry for the delay, I'll try to help you with this.

I am not sure this is the right thing to do, or at least some things need to be
clarified.  Global symbols are visible through the entire program (there can only
be one of that name), whereas there can be many static symbols with the same name.
What should the user expect if there are multiple static symbols with the same name?
I suppose we'll return the first one that matches, without any particular guarantee
about which one.

It is possible to get to a particular static symbol using gdb.lookup_symbol, provided
you pass a block that is under the right static block, and that no other symbol shadows
this one from the point of view of the block.  But IIRC, it can prove difficult to get
access the static block of a particular compilation unit (if you don't yet have a reference
to a frame block under that static block).

<thought drift>
I have been thinking for a while that we are missing the concept of compilation units in
our Python API, that would be the bridge between objfiles and global/static blocks.

- From an objfile, you could list all included compilation units or search through them
- From the compilation unit, you could obtain its global/static block.

From there, you could use gdb.lookup_symbol, passing the static block of the compilation unit.

However, in my (naive) attempt at adding compunits to the Python API [1], which consisted of
wrapping compunit_symtab objects in Python, the problem I faced is that compunit_symtab objects
are not created until full symbols are read.  So unless you used -readnow, you would not
see all the compilation units you would expect (because they are lazily created).
</thought drift>

Would it be an option to add a gdb.lookup_static_symbol function, that would only look through
the static blocks?  Its behavior could be that if you don't pass a block, it searches through
all the static blocks until it finds a matching symbol (just like your patch does with
gdb.lookup_global_symbol if no symbol is found).  And if you pass a block, it restricts the
search to the static block linked to that block, ensuring you find the static symbol you want.

Simon

[1] https://github.com/simark/binutils-gdb/commits/py-compunit
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH][PR/24474] Make gdb.lookup_static_symbol also check the STATIC_SCOPE

Sourceware - gdb-patches mailing list
On Mon, Jul 15, 2019 at 8:28 PM Simon Marchi <[hidden email]> wrote:
> Would it be an option to add a gdb.lookup_static_symbol function, that would only look through
> the static blocks?  Its behavior could be that if you don't pass a block, it searches through
> all the static blocks until it finds a matching symbol (just like your patch does with
> gdb.lookup_global_symbol if no symbol is found).  And if you pass a block, it restricts the
> search to the static block linked to that block, ensuring you find the static symbol you want.

Thanks for your response! I have started implementing this and
concluded that I would prefer not to add a block argument with this
behavior to lookup_static_symbol:
- If I add it with the behavior you suggest, this will be very
confusing to use because it won't find function-local static variables
(they are not part of the static block)
- It does not add new functionality. You can already access static
symbols if you have a block: [sym for sym in block if sym.addr_class
== gdb.SYMBOL_LOC_STATIC]. And you can already do that in a function's
static block too, using block.static_block.
- I'd be happy to add a patch that adds makes block['foo'] work, in
addition to the currently-existing iteration

Conversely, lookup_static_symbol without a block does add new functionality.

I will send a new patch with this in a moment.

Christian
Reply | Threaded
Open this post in threaded view
|

[PATCH v2] [PR/24474] Add gdb.lookup_static_symbol to the python API

Sourceware - gdb-patches mailing list
Similar to lookup_global_symbol, except that it checks the
STATIC_SCOPE.

gdb/ChangeLog:

2019-07-26  Christian Biesinger  <[hidden email]>

        PR/24474: Add a function to lookup static variables.
        * NEWS: Mention this new function.
        * python/py-symbol.c (gdbpy_lookup_static_symbol): New function.
        * python/python-internal.h (gdbpy_lookup_static_symbol): New function.
        * python/python.c (python_GdbMethods): Add new function.

gdb/doc/ChangeLog:

2019-07-26  Christian Biesinger  <[hidden email]>

        * python.texi (Symbols In Python): Document new function
        gdb.lookup_static_symbol.

gdb/testsuite/ChangeLog:

2019-07-26  Christian Biesinger  <[hidden email]>

        * gdb.python/py-symbol.c: Add a static variable and one in an anonymous
        namespace.
        * gdb.python/py-symbol.exp: Test gdb.lookup_static_symbol.
---
 gdb/NEWS                               |  3 ++
 gdb/doc/python.texi                    | 19 ++++++++++++
 gdb/python/py-symbol.c                 | 40 ++++++++++++++++++++++++++
 gdb/python/python-internal.h           |  2 ++
 gdb/python/python.c                    |  4 +++
 gdb/testsuite/gdb.python/py-symbol.c   |  5 ++++
 gdb/testsuite/gdb.python/py-symbol.exp | 21 ++++++++++++++
 7 files changed, 94 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index cc1d58520d..383295b3d2 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -36,6 +36,9 @@
   ** gdb.Type has a new property 'objfile' which returns the objfile the
      type was defined in.
 
+  ** The new function gdb.lookup_static_symbol can be used to look up
+     symbols with static linkage.
+
 * New commands
 
 | [COMMAND] | SHELL_COMMAND
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 034623513b..c5da7ad6b6 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -4830,6 +4830,25 @@ The result is a @code{gdb.Symbol} object or @code{None} if the symbol
 is not found.
 @end defun
 
+@findex gdb.lookup_static_symbol
+@defun gdb.lookup_static_symbol (name @r{[}, domain@r{]})
+This function searches for a global symbol with static linkage by name.
+The search scope can be restricted to by the domain argument.
+
+@var{name} is the name of the symbol.  It must be a string.
+The optional @var{domain} argument restricts the search to the domain type.
+The @var{domain} argument must be a domain constant defined in the @code{gdb}
+module and described later in this chapter.
+
+The result is a @code{gdb.Symbol} object or @code{None} if the symbol
+is not found.
+
+Note that this function will not find function-scoped static variables. To look
+up such variables, iterate over the variables of the function's
+@code{gdb.Block} and check that @code{block.addr_class} is
+@code{gdb.SYMBOL_LOC_STATIC}.
+@end defun
+
 A @code{gdb.Symbol} object has the following attributes:
 
 @defvar Symbol.type
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 8605ae71a2..2b10e21d87 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -471,6 +471,46 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
   return sym_obj;
 }
 
+/* Implementation of
+   gdb.lookup_static_symbol (name [, domain) -> symbol or None.  */
+
+PyObject *
+gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
+{
+  const char *name;
+  int domain = VAR_DOMAIN;
+  static const char *keywords[] = { "name", "domain", NULL };
+  struct symbol *symbol = NULL;
+  PyObject *sym_obj;
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
+ &domain))
+    return NULL;
+
+  try
+    {
+      symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
+    }
+  catch (const gdb_exception &except)
+    {
+      GDB_PY_HANDLE_EXCEPTION (except);
+    }
+
+  if (symbol)
+    {
+      sym_obj = symbol_to_symbol_object (symbol);
+      if (!sym_obj)
+ return NULL;
+    }
+  else
+    {
+      sym_obj = Py_None;
+      Py_INCREF (Py_None);
+    }
+
+  return sym_obj;
+}
+
 /* This function is called when an objfile is about to be freed.
    Invalidate the symbol as further actions on the symbol would result
    in bad data.  All access to obj->symbol should be gated by
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index e6a3fe0ec1..c5578430cf 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -424,6 +424,8 @@ PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *);
 PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw);
 PyObject *gdbpy_lookup_global_symbol (PyObject *self, PyObject *args,
       PyObject *kw);
+PyObject *gdbpy_lookup_static_symbol (PyObject *self, PyObject *args,
+      PyObject *kw);
 PyObject *gdbpy_start_recording (PyObject *self, PyObject *args);
 PyObject *gdbpy_current_recording (PyObject *self, PyObject *args);
 PyObject *gdbpy_stop_recording (PyObject *self, PyObject *args);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 96bee7c3b0..162470dcc0 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1978,6 +1978,10 @@ a boolean indicating if name is a field of the current implied argument\n\
     METH_VARARGS | METH_KEYWORDS,
     "lookup_global_symbol (name [, domain]) -> symbol\n\
 Return the symbol corresponding to the given name (or None)." },
+  { "lookup_static_symbol", (PyCFunction) gdbpy_lookup_static_symbol,
+    METH_VARARGS | METH_KEYWORDS,
+    "lookup_static_symbol (name [, domain]) -> symbol\n\
+Return the static-linkage symbol corresponding to the given name (or None)." },
 
   { "lookup_objfile", (PyCFunction) gdbpy_lookup_objfile,
     METH_VARARGS | METH_KEYWORDS,
diff --git a/gdb/testsuite/gdb.python/py-symbol.c b/gdb/testsuite/gdb.python/py-symbol.c
index f77c8c8585..06a931bf5d 100644
--- a/gdb/testsuite/gdb.python/py-symbol.c
+++ b/gdb/testsuite/gdb.python/py-symbol.c
@@ -32,9 +32,14 @@ class SimpleClass
     return i; /* Break in class. */
   }
 };
+
+namespace {
+  int anon = 10;
+};
 #endif
 
 int qq = 72; /* line of qq */
+static int rr = 42; /* line of rr */
 
 int func (int arg)
 {
diff --git a/gdb/testsuite/gdb.python/py-symbol.exp b/gdb/testsuite/gdb.python/py-symbol.exp
index 5b8a2be7c4..5d45e35da0 100644
--- a/gdb/testsuite/gdb.python/py-symbol.exp
+++ b/gdb/testsuite/gdb.python/py-symbol.exp
@@ -48,6 +48,22 @@ gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
     "False" \
     "print whether qq needs a frame"
 
+set rr_line [gdb_get_line_number "line of rr"]
+gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \
+    "lookup_global_symbol for static var"
+
+gdb_test "python print (gdb.lookup_static_symbol ('rr').line)" "$rr_line" \
+    "print line number of rr"
+
+gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
+    "print value of rr"
+
+gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \
+    "False" \
+    "print whether rr needs a frame"
+
+gdb_test "python print (gdb.lookup_global_symbol ('nonexistant') is None)" \
+    "True" "lookup_static_symbol for nonexistant var"
 
 if ![runto_main] then {
     fail "can't run to main"
@@ -137,6 +153,11 @@ gdb_start
 gdb_reinitialize_dir $srcdir/$subdir
 gdb_load ${binfile}-cxx
 
+gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon') is None)" \
+    "True" "anon is None"
+gdb_test "python print (gdb.lookup_static_symbol ('(anonymous namespace)::anon').value ())" \
+    "10" "print value of anon"
+
 if ![runto_main] then {
     fail "can't run to main"
     return 0
--
2.22.0.709.g102302147b-goog

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH][PR/24474] Make gdb.lookup_static_symbol also check the STATIC_SCOPE

Simon Marchi-4
In reply to this post by Sourceware - gdb-patches mailing list
On 2019-07-26 18:04, Christian Biesinger wrote:
> Thanks for your response! I have started implementing this and
> concluded that I would prefer not to add a block argument with this
> behavior to lookup_static_symbol:
> - If I add it with the behavior you suggest, this will be very
> confusing to use because it won't find function-local static variables
> (they are not part of the static block)

Agreed, it would be a bit confusing to pass a block to a 'lookup'
function, and have the search actually done in another block.

> - It does not add new functionality. You can already access static
> symbols if you have a block: [sym for sym in block if sym.addr_class
> == gdb.SYMBOL_LOC_STATIC]. And you can already do that in a function's
> static block too, using block.static_block.

I agree.

> - I'd be happy to add a patch that adds makes block['foo'] work, in
> addition to the currently-existing iteration

That is a separate issue, but yeah, if blocks can be seen as containers
of symbols, and symbol names are guaranteed to be unique within a block,
then it sounds like it would be handy.

>
> Conversely, lookup_static_symbol without a block does add new
> functionality.

Yes, and it's always possible to add parameters after if needed since
it's Python.

> I will send a new patch with this in a moment.

Thanks,

Simon
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2] [PR/24474] Add gdb.lookup_static_symbol to the python API

Simon Marchi-4
In reply to this post by Sourceware - gdb-patches mailing list
Hi Christian,

On 2019-07-26 6:22 p.m., Christian Biesinger via gdb-patches wrote:

> diff --git a/gdb/testsuite/gdb.python/py-symbol.exp b/gdb/testsuite/gdb.python/py-symbol.exp
> index 5b8a2be7c4..5d45e35da0 100644
> --- a/gdb/testsuite/gdb.python/py-symbol.exp
> +++ b/gdb/testsuite/gdb.python/py-symbol.exp
> @@ -48,6 +48,22 @@ gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
>      "False" \
>      "print whether qq needs a frame"
>  
> +set rr_line [gdb_get_line_number "line of rr"]
> +gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \
> +    "lookup_global_symbol for static var"
> +
> +gdb_test "python print (gdb.lookup_static_symbol ('rr').line)" "$rr_line" \
> +    "print line number of rr"
> +
> +gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
> +    "print value of rr"
> +
> +gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \
> +    "False" \
> +    "print whether rr needs a frame"
> +
> +gdb_test "python print (gdb.lookup_global_symbol ('nonexistant') is None)" \
> +    "True" "lookup_static_symbol for nonexistant var
The test name says "lookup_static_symbol" but the command uses lookup_global_symbol.  I suppose you actually want to test lookup_static_symbol.

Is there a test that checks that lookup_static_symbol won't find a global symbol?  If not, it would be good to add.

Simon
Reply | Threaded
Open this post in threaded view
|

[PATCH v3] [PR/24474] Add gdb.lookup_static_symbol to the python API

Sourceware - gdb-patches mailing list
Thanks Simon; I fixed that typo and added a test that
lookup_static_symbol doesn't find a global.

Similar to lookup_global_symbol, except that it checks the
STATIC_SCOPE.

gdb/ChangeLog:

2019-07-26  Christian Biesinger  <[hidden email]>

        PR/24474: Add a function to lookup static variables.
        * NEWS: Mention this new function.
        * python/py-symbol.c (gdbpy_lookup_static_symbol): New function.
        * python/python-internal.h (gdbpy_lookup_static_symbol): New function.
        * python/python.c (python_GdbMethods): Add new function.

gdb/doc/ChangeLog:

2019-07-26  Christian Biesinger  <[hidden email]>

        * python.texi (Symbols In Python): Document new function
        gdb.lookup_static_symbol.

gdb/testsuite/ChangeLog:

2019-07-26  Christian Biesinger  <[hidden email]>

        * gdb.python/py-symbol.c: Add a static variable and one in an anonymous
        namespace.
        * gdb.python/py-symbol.exp: Test gdb.lookup_static_symbol.
---
 gdb/NEWS                               |  3 ++
 gdb/doc/python.texi                    | 19 ++++++++++++
 gdb/python/py-symbol.c                 | 40 ++++++++++++++++++++++++++
 gdb/python/python-internal.h           |  2 ++
 gdb/python/python.c                    |  4 +++
 gdb/testsuite/gdb.python/py-symbol.c   |  5 ++++
 gdb/testsuite/gdb.python/py-symbol.exp | 24 ++++++++++++++++
 7 files changed, 97 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index a08265cae7..7f8b236ba1 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -41,6 +41,9 @@
      there are no filters, or when the 'backtrace' '-no-filters' option
      is given.
 
+  ** The new function gdb.lookup_static_symbol can be used to look up
+     symbols with static linkage.
+
 * New commands
 
 | [COMMAND] | SHELL_COMMAND
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 034623513b..c5da7ad6b6 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -4830,6 +4830,25 @@ The result is a @code{gdb.Symbol} object or @code{None} if the symbol
 is not found.
 @end defun
 
+@findex gdb.lookup_static_symbol
+@defun gdb.lookup_static_symbol (name @r{[}, domain@r{]})
+This function searches for a global symbol with static linkage by name.
+The search scope can be restricted to by the domain argument.
+
+@var{name} is the name of the symbol.  It must be a string.
+The optional @var{domain} argument restricts the search to the domain type.
+The @var{domain} argument must be a domain constant defined in the @code{gdb}
+module and described later in this chapter.
+
+The result is a @code{gdb.Symbol} object or @code{None} if the symbol
+is not found.
+
+Note that this function will not find function-scoped static variables. To look
+up such variables, iterate over the variables of the function's
+@code{gdb.Block} and check that @code{block.addr_class} is
+@code{gdb.SYMBOL_LOC_STATIC}.
+@end defun
+
 A @code{gdb.Symbol} object has the following attributes:
 
 @defvar Symbol.type
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 8605ae71a2..2b10e21d87 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -471,6 +471,46 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
   return sym_obj;
 }
 
+/* Implementation of
+   gdb.lookup_static_symbol (name [, domain) -> symbol or None.  */
+
+PyObject *
+gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
+{
+  const char *name;
+  int domain = VAR_DOMAIN;
+  static const char *keywords[] = { "name", "domain", NULL };
+  struct symbol *symbol = NULL;
+  PyObject *sym_obj;
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
+ &domain))
+    return NULL;
+
+  try
+    {
+      symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
+    }
+  catch (const gdb_exception &except)
+    {
+      GDB_PY_HANDLE_EXCEPTION (except);
+    }
+
+  if (symbol)
+    {
+      sym_obj = symbol_to_symbol_object (symbol);
+      if (!sym_obj)
+ return NULL;
+    }
+  else
+    {
+      sym_obj = Py_None;
+      Py_INCREF (Py_None);
+    }
+
+  return sym_obj;
+}
+
 /* This function is called when an objfile is about to be freed.
    Invalidate the symbol as further actions on the symbol would result
    in bad data.  All access to obj->symbol should be gated by
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index e6a3fe0ec1..c5578430cf 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -424,6 +424,8 @@ PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *);
 PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw);
 PyObject *gdbpy_lookup_global_symbol (PyObject *self, PyObject *args,
       PyObject *kw);
+PyObject *gdbpy_lookup_static_symbol (PyObject *self, PyObject *args,
+      PyObject *kw);
 PyObject *gdbpy_start_recording (PyObject *self, PyObject *args);
 PyObject *gdbpy_current_recording (PyObject *self, PyObject *args);
 PyObject *gdbpy_stop_recording (PyObject *self, PyObject *args);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 96bee7c3b0..162470dcc0 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1978,6 +1978,10 @@ a boolean indicating if name is a field of the current implied argument\n\
     METH_VARARGS | METH_KEYWORDS,
     "lookup_global_symbol (name [, domain]) -> symbol\n\
 Return the symbol corresponding to the given name (or None)." },
+  { "lookup_static_symbol", (PyCFunction) gdbpy_lookup_static_symbol,
+    METH_VARARGS | METH_KEYWORDS,
+    "lookup_static_symbol (name [, domain]) -> symbol\n\
+Return the static-linkage symbol corresponding to the given name (or None)." },
 
   { "lookup_objfile", (PyCFunction) gdbpy_lookup_objfile,
     METH_VARARGS | METH_KEYWORDS,
diff --git a/gdb/testsuite/gdb.python/py-symbol.c b/gdb/testsuite/gdb.python/py-symbol.c
index f77c8c8585..06a931bf5d 100644
--- a/gdb/testsuite/gdb.python/py-symbol.c
+++ b/gdb/testsuite/gdb.python/py-symbol.c
@@ -32,9 +32,14 @@ class SimpleClass
     return i; /* Break in class. */
   }
 };
+
+namespace {
+  int anon = 10;
+};
 #endif
 
 int qq = 72; /* line of qq */
+static int rr = 42; /* line of rr */
 
 int func (int arg)
 {
diff --git a/gdb/testsuite/gdb.python/py-symbol.exp b/gdb/testsuite/gdb.python/py-symbol.exp
index 5b8a2be7c4..ab72a4cb9d 100644
--- a/gdb/testsuite/gdb.python/py-symbol.exp
+++ b/gdb/testsuite/gdb.python/py-symbol.exp
@@ -48,6 +48,25 @@ gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
     "False" \
     "print whether qq needs a frame"
 
+set rr_line [gdb_get_line_number "line of rr"]
+gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \
+    "lookup_global_symbol for static var"
+
+gdb_test "python print (gdb.lookup_static_symbol ('rr').line)" "$rr_line" \
+    "print line number of rr"
+
+gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
+    "print value of rr"
+
+gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \
+    "False" \
+    "print whether rr needs a frame"
+
+gdb_test "python print (gdb.lookup_static_symbol ('nonexistant') is None)" \
+    "True" "lookup_static_symbol for nonexistant var"
+
+gdb_test "python print (gdb.lookup_static_symbol ('qq') is None)" \
+    "True" "lookup_static_symbol for global var"
 
 if ![runto_main] then {
     fail "can't run to main"
@@ -137,6 +156,11 @@ gdb_start
 gdb_reinitialize_dir $srcdir/$subdir
 gdb_load ${binfile}-cxx
 
+gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon') is None)" \
+    "True" "anon is None"
+gdb_test "python print (gdb.lookup_static_symbol ('(anonymous namespace)::anon').value ())" \
+    "10" "print value of anon"
+
 if ![runto_main] then {
     fail "can't run to main"
     return 0
--
2.22.0.709.g102302147b-goog

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3] [PR/24474] Add gdb.lookup_static_symbol to the python API

Simon Marchi-4
> diff --git a/gdb/testsuite/gdb.python/py-symbol.exp b/gdb/testsuite/gdb.python/py-symbol.exp
> index 5b8a2be7c4..ab72a4cb9d 100644
> --- a/gdb/testsuite/gdb.python/py-symbol.exp
> +++ b/gdb/testsuite/gdb.python/py-symbol.exp
> @@ -48,6 +48,25 @@ gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
>      "False" \
>      "print whether qq needs a frame"
>  
> +set rr_line [gdb_get_line_number "line of rr"]
> +gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \
> +    "lookup_global_symbol for static var"
> +
> +gdb_test "python print (gdb.lookup_static_symbol ('rr').line)" "$rr_line" \
> +    "print line number of rr"
> +
> +gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
> +    "print value of rr"
> +
> +gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \
> +    "False" \
> +    "print whether rr needs a frame"
> +
> +gdb_test "python print (gdb.lookup_static_symbol ('nonexistant') is None)" \
> +    "True" "lookup_static_symbol for nonexistant var"

Ah, just noticed this, "nonexistant" -> "nonexistent".

The patch LGTM with that fixed.

Thanks,

Simon
Reply | Threaded
Open this post in threaded view
|

[PATCH v4] [PR/24474] Add gdb.lookup_static_symbol to the python API

Sourceware - gdb-patches mailing list
Thanks Simon; fixed the typo. Can I push this or should I wait for Eli
to review the documentation?

Similar to lookup_global_symbol, except that it checks the
STATIC_SCOPE.

gdb/ChangeLog:

2019-07-29  Christian Biesinger  <[hidden email]>

        PR/24474: Add a function to lookup static variables.
        * NEWS: Mention this new function.
        * python/py-symbol.c (gdbpy_lookup_static_symbol): New function.
        * python/python-internal.h (gdbpy_lookup_static_symbol): New function.
        * python/python.c (python_GdbMethods): Add new function.

gdb/doc/ChangeLog:

2019-07-29  Christian Biesinger  <[hidden email]>

        * python.texi (Symbols In Python): Document new function
        gdb.lookup_static_symbol.

gdb/testsuite/ChangeLog:

2019-07-29  Christian Biesinger  <[hidden email]>

        * gdb.python/py-symbol.c: Add a static variable and one in an anonymous
        namespace.
        * gdb.python/py-symbol.exp: Test gdb.lookup_static_symbol.
---
 gdb/NEWS                               |  3 ++
 gdb/doc/python.texi                    | 19 ++++++++++++
 gdb/python/py-symbol.c                 | 40 ++++++++++++++++++++++++++
 gdb/python/python-internal.h           |  2 ++
 gdb/python/python.c                    |  4 +++
 gdb/testsuite/gdb.python/py-symbol.c   |  5 ++++
 gdb/testsuite/gdb.python/py-symbol.exp | 24 ++++++++++++++++
 7 files changed, 97 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index 4e821eab4c..ac44399304 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -41,6 +41,9 @@
      there are no filters, or when the 'backtrace' '-no-filters' option
      is given.
 
+  ** The new function gdb.lookup_static_symbol can be used to look up
+     symbols with static linkage.
+
   ** gdb.Objfile has new methods 'lookup_global_symbol' and
      'lookup_static_symbol' to lookup a symbol from this objfile only.
 
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index bbba519ffc..3fdccd5e43 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -4847,6 +4847,25 @@ The result is a @code{gdb.Symbol} object or @code{None} if the symbol
 is not found.
 @end defun
 
+@findex gdb.lookup_static_symbol
+@defun gdb.lookup_static_symbol (name @r{[}, domain@r{]})
+This function searches for a global symbol with static linkage by name.
+The search scope can be restricted to by the domain argument.
+
+@var{name} is the name of the symbol.  It must be a string.
+The optional @var{domain} argument restricts the search to the domain type.
+The @var{domain} argument must be a domain constant defined in the @code{gdb}
+module and described later in this chapter.
+
+The result is a @code{gdb.Symbol} object or @code{None} if the symbol
+is not found.
+
+Note that this function will not find function-scoped static variables. To look
+up such variables, iterate over the variables of the function's
+@code{gdb.Block} and check that @code{block.addr_class} is
+@code{gdb.SYMBOL_LOC_STATIC}.
+@end defun
+
 A @code{gdb.Symbol} object has the following attributes:
 
 @defvar Symbol.type
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 8605ae71a2..2b10e21d87 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -471,6 +471,46 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
   return sym_obj;
 }
 
+/* Implementation of
+   gdb.lookup_static_symbol (name [, domain) -> symbol or None.  */
+
+PyObject *
+gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
+{
+  const char *name;
+  int domain = VAR_DOMAIN;
+  static const char *keywords[] = { "name", "domain", NULL };
+  struct symbol *symbol = NULL;
+  PyObject *sym_obj;
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
+ &domain))
+    return NULL;
+
+  try
+    {
+      symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
+    }
+  catch (const gdb_exception &except)
+    {
+      GDB_PY_HANDLE_EXCEPTION (except);
+    }
+
+  if (symbol)
+    {
+      sym_obj = symbol_to_symbol_object (symbol);
+      if (!sym_obj)
+ return NULL;
+    }
+  else
+    {
+      sym_obj = Py_None;
+      Py_INCREF (Py_None);
+    }
+
+  return sym_obj;
+}
+
 /* This function is called when an objfile is about to be freed.
    Invalidate the symbol as further actions on the symbol would result
    in bad data.  All access to obj->symbol should be gated by
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index e6a3fe0ec1..c5578430cf 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -424,6 +424,8 @@ PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *);
 PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw);
 PyObject *gdbpy_lookup_global_symbol (PyObject *self, PyObject *args,
       PyObject *kw);
+PyObject *gdbpy_lookup_static_symbol (PyObject *self, PyObject *args,
+      PyObject *kw);
 PyObject *gdbpy_start_recording (PyObject *self, PyObject *args);
 PyObject *gdbpy_current_recording (PyObject *self, PyObject *args);
 PyObject *gdbpy_stop_recording (PyObject *self, PyObject *args);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 96bee7c3b0..162470dcc0 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1978,6 +1978,10 @@ a boolean indicating if name is a field of the current implied argument\n\
     METH_VARARGS | METH_KEYWORDS,
     "lookup_global_symbol (name [, domain]) -> symbol\n\
 Return the symbol corresponding to the given name (or None)." },
+  { "lookup_static_symbol", (PyCFunction) gdbpy_lookup_static_symbol,
+    METH_VARARGS | METH_KEYWORDS,
+    "lookup_static_symbol (name [, domain]) -> symbol\n\
+Return the static-linkage symbol corresponding to the given name (or None)." },
 
   { "lookup_objfile", (PyCFunction) gdbpy_lookup_objfile,
     METH_VARARGS | METH_KEYWORDS,
diff --git a/gdb/testsuite/gdb.python/py-symbol.c b/gdb/testsuite/gdb.python/py-symbol.c
index f77c8c8585..06a931bf5d 100644
--- a/gdb/testsuite/gdb.python/py-symbol.c
+++ b/gdb/testsuite/gdb.python/py-symbol.c
@@ -32,9 +32,14 @@ class SimpleClass
     return i; /* Break in class. */
   }
 };
+
+namespace {
+  int anon = 10;
+};
 #endif
 
 int qq = 72; /* line of qq */
+static int rr = 42; /* line of rr */
 
 int func (int arg)
 {
diff --git a/gdb/testsuite/gdb.python/py-symbol.exp b/gdb/testsuite/gdb.python/py-symbol.exp
index 5b8a2be7c4..5617f127e5 100644
--- a/gdb/testsuite/gdb.python/py-symbol.exp
+++ b/gdb/testsuite/gdb.python/py-symbol.exp
@@ -48,6 +48,25 @@ gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
     "False" \
     "print whether qq needs a frame"
 
+set rr_line [gdb_get_line_number "line of rr"]
+gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \
+    "lookup_global_symbol for static var"
+
+gdb_test "python print (gdb.lookup_static_symbol ('rr').line)" "$rr_line" \
+    "print line number of rr"
+
+gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
+    "print value of rr"
+
+gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \
+    "False" \
+    "print whether rr needs a frame"
+
+gdb_test "python print (gdb.lookup_static_symbol ('nonexistent') is None)" \
+    "True" "lookup_static_symbol for nonexistent var"
+
+gdb_test "python print (gdb.lookup_static_symbol ('qq') is None)" \
+    "True" "lookup_static_symbol for global var"
 
 if ![runto_main] then {
     fail "can't run to main"
@@ -137,6 +156,11 @@ gdb_start
 gdb_reinitialize_dir $srcdir/$subdir
 gdb_load ${binfile}-cxx
 
+gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon') is None)" \
+    "True" "anon is None"
+gdb_test "python print (gdb.lookup_static_symbol ('(anonymous namespace)::anon').value ())" \
+    "10" "print value of anon"
+
 if ![runto_main] then {
     fail "can't run to main"
     return 0
--
2.22.0.709.g102302147b-goog

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v4] [PR/24474] Add gdb.lookup_static_symbol to the python API

Sourceware - gdb-patches mailing list
Hi Eli,

could you take a look at the documentation parts of the below patch
before I push? Thanks!

Christian

On Mon, Jul 29, 2019 at 8:59 PM Christian Biesinger
<[hidden email]> wrote:

>
> Thanks Simon; fixed the typo. Can I push this or should I wait for Eli
> to review the documentation?
>
> Similar to lookup_global_symbol, except that it checks the
> STATIC_SCOPE.
>
> gdb/ChangeLog:
>
> 2019-07-29  Christian Biesinger  <[hidden email]>
>
>         PR/24474: Add a function to lookup static variables.
>         * NEWS: Mention this new function.
>         * python/py-symbol.c (gdbpy_lookup_static_symbol): New function.
>         * python/python-internal.h (gdbpy_lookup_static_symbol): New function.
>         * python/python.c (python_GdbMethods): Add new function.
>
> gdb/doc/ChangeLog:
>
> 2019-07-29  Christian Biesinger  <[hidden email]>
>
>         * python.texi (Symbols In Python): Document new function
>         gdb.lookup_static_symbol.
>
> gdb/testsuite/ChangeLog:
>
> 2019-07-29  Christian Biesinger  <[hidden email]>
>
>         * gdb.python/py-symbol.c: Add a static variable and one in an anonymous
>         namespace.
>         * gdb.python/py-symbol.exp: Test gdb.lookup_static_symbol.
> ---
>  gdb/NEWS                               |  3 ++
>  gdb/doc/python.texi                    | 19 ++++++++++++
>  gdb/python/py-symbol.c                 | 40 ++++++++++++++++++++++++++
>  gdb/python/python-internal.h           |  2 ++
>  gdb/python/python.c                    |  4 +++
>  gdb/testsuite/gdb.python/py-symbol.c   |  5 ++++
>  gdb/testsuite/gdb.python/py-symbol.exp | 24 ++++++++++++++++
>  7 files changed, 97 insertions(+)
>
> diff --git a/gdb/NEWS b/gdb/NEWS
> index 4e821eab4c..ac44399304 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -41,6 +41,9 @@
>       there are no filters, or when the 'backtrace' '-no-filters' option
>       is given.
>
> +  ** The new function gdb.lookup_static_symbol can be used to look up
> +     symbols with static linkage.
> +
>    ** gdb.Objfile has new methods 'lookup_global_symbol' and
>       'lookup_static_symbol' to lookup a symbol from this objfile only.
>
> diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
> index bbba519ffc..3fdccd5e43 100644
> --- a/gdb/doc/python.texi
> +++ b/gdb/doc/python.texi
> @@ -4847,6 +4847,25 @@ The result is a @code{gdb.Symbol} object or @code{None} if the symbol
>  is not found.
>  @end defun
>
> +@findex gdb.lookup_static_symbol
> +@defun gdb.lookup_static_symbol (name @r{[}, domain@r{]})
> +This function searches for a global symbol with static linkage by name.
> +The search scope can be restricted to by the domain argument.
> +
> +@var{name} is the name of the symbol.  It must be a string.
> +The optional @var{domain} argument restricts the search to the domain type.
> +The @var{domain} argument must be a domain constant defined in the @code{gdb}
> +module and described later in this chapter.
> +
> +The result is a @code{gdb.Symbol} object or @code{None} if the symbol
> +is not found.
> +
> +Note that this function will not find function-scoped static variables. To look
> +up such variables, iterate over the variables of the function's
> +@code{gdb.Block} and check that @code{block.addr_class} is
> +@code{gdb.SYMBOL_LOC_STATIC}.
> +@end defun
> +
>  A @code{gdb.Symbol} object has the following attributes:
>
>  @defvar Symbol.type
> diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
> index 8605ae71a2..2b10e21d87 100644
> --- a/gdb/python/py-symbol.c
> +++ b/gdb/python/py-symbol.c
> @@ -471,6 +471,46 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
>    return sym_obj;
>  }
>
> +/* Implementation of
> +   gdb.lookup_static_symbol (name [, domain) -> symbol or None.  */
> +
> +PyObject *
> +gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
> +{
> +  const char *name;
> +  int domain = VAR_DOMAIN;
> +  static const char *keywords[] = { "name", "domain", NULL };
> +  struct symbol *symbol = NULL;
> +  PyObject *sym_obj;
> +
> +  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
> +                                       &domain))
> +    return NULL;
> +
> +  try
> +    {
> +      symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
> +    }
> +  catch (const gdb_exception &except)
> +    {
> +      GDB_PY_HANDLE_EXCEPTION (except);
> +    }
> +
> +  if (symbol)
> +    {
> +      sym_obj = symbol_to_symbol_object (symbol);
> +      if (!sym_obj)
> +       return NULL;
> +    }
> +  else
> +    {
> +      sym_obj = Py_None;
> +      Py_INCREF (Py_None);
> +    }
> +
> +  return sym_obj;
> +}
> +
>  /* This function is called when an objfile is about to be freed.
>     Invalidate the symbol as further actions on the symbol would result
>     in bad data.  All access to obj->symbol should be gated by
> diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
> index e6a3fe0ec1..c5578430cf 100644
> --- a/gdb/python/python-internal.h
> +++ b/gdb/python/python-internal.h
> @@ -424,6 +424,8 @@ PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *);
>  PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw);
>  PyObject *gdbpy_lookup_global_symbol (PyObject *self, PyObject *args,
>                                       PyObject *kw);
> +PyObject *gdbpy_lookup_static_symbol (PyObject *self, PyObject *args,
> +                                     PyObject *kw);
>  PyObject *gdbpy_start_recording (PyObject *self, PyObject *args);
>  PyObject *gdbpy_current_recording (PyObject *self, PyObject *args);
>  PyObject *gdbpy_stop_recording (PyObject *self, PyObject *args);
> diff --git a/gdb/python/python.c b/gdb/python/python.c
> index 96bee7c3b0..162470dcc0 100644
> --- a/gdb/python/python.c
> +++ b/gdb/python/python.c
> @@ -1978,6 +1978,10 @@ a boolean indicating if name is a field of the current implied argument\n\
>      METH_VARARGS | METH_KEYWORDS,
>      "lookup_global_symbol (name [, domain]) -> symbol\n\
>  Return the symbol corresponding to the given name (or None)." },
> +  { "lookup_static_symbol", (PyCFunction) gdbpy_lookup_static_symbol,
> +    METH_VARARGS | METH_KEYWORDS,
> +    "lookup_static_symbol (name [, domain]) -> symbol\n\
> +Return the static-linkage symbol corresponding to the given name (or None)." },
>
>    { "lookup_objfile", (PyCFunction) gdbpy_lookup_objfile,
>      METH_VARARGS | METH_KEYWORDS,
> diff --git a/gdb/testsuite/gdb.python/py-symbol.c b/gdb/testsuite/gdb.python/py-symbol.c
> index f77c8c8585..06a931bf5d 100644
> --- a/gdb/testsuite/gdb.python/py-symbol.c
> +++ b/gdb/testsuite/gdb.python/py-symbol.c
> @@ -32,9 +32,14 @@ class SimpleClass
>      return i; /* Break in class. */
>    }
>  };
> +
> +namespace {
> +  int anon = 10;
> +};
>  #endif
>
>  int qq = 72;                   /* line of qq */
> +static int rr = 42;            /* line of rr */
>
>  int func (int arg)
>  {
> diff --git a/gdb/testsuite/gdb.python/py-symbol.exp b/gdb/testsuite/gdb.python/py-symbol.exp
> index 5b8a2be7c4..5617f127e5 100644
> --- a/gdb/testsuite/gdb.python/py-symbol.exp
> +++ b/gdb/testsuite/gdb.python/py-symbol.exp
> @@ -48,6 +48,25 @@ gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
>      "False" \
>      "print whether qq needs a frame"
>
> +set rr_line [gdb_get_line_number "line of rr"]
> +gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \
> +    "lookup_global_symbol for static var"
> +
> +gdb_test "python print (gdb.lookup_static_symbol ('rr').line)" "$rr_line" \
> +    "print line number of rr"
> +
> +gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
> +    "print value of rr"
> +
> +gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \
> +    "False" \
> +    "print whether rr needs a frame"
> +
> +gdb_test "python print (gdb.lookup_static_symbol ('nonexistent') is None)" \
> +    "True" "lookup_static_symbol for nonexistent var"
> +
> +gdb_test "python print (gdb.lookup_static_symbol ('qq') is None)" \
> +    "True" "lookup_static_symbol for global var"
>
>  if ![runto_main] then {
>      fail "can't run to main"
> @@ -137,6 +156,11 @@ gdb_start
>  gdb_reinitialize_dir $srcdir/$subdir
>  gdb_load ${binfile}-cxx
>
> +gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon') is None)" \
> +    "True" "anon is None"
> +gdb_test "python print (gdb.lookup_static_symbol ('(anonymous namespace)::anon').value ())" \
> +    "10" "print value of anon"
> +
>  if ![runto_main] then {
>      fail "can't run to main"
>      return 0
> --
> 2.22.0.709.g102302147b-goog
>
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v4] [PR/24474] Add gdb.lookup_static_symbol to the python API

Eli Zaretskii
> From: Christian Biesinger <[hidden email]>
> Date: Tue, 30 Jul 2019 10:40:22 -0500
>
> Hi Eli,
>
> could you take a look at the documentation parts of the below patch
> before I push? Thanks!

Didn't I just approve it?  Or maybe I got confused by the many
versions of similar patches?

Anyway, this is OK.
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v4] [PR/24474] Add gdb.lookup_static_symbol to the python API

Sourceware - gdb-patches mailing list
On Tue, Jul 30, 2019 at 10:57 AM Eli Zaretskii <[hidden email]> wrote:

>
> > From: Christian Biesinger <[hidden email]>
> > Date: Tue, 30 Jul 2019 10:40:22 -0500
> >
> > Hi Eli,
> >
> > could you take a look at the documentation parts of the below patch
> > before I push? Thanks!
>
> Didn't I just approve it?  Or maybe I got confused by the many
> versions of similar patches?
>
> Anyway, this is OK.

I'm sorry for the confusing mass of patches. I believe you commented
on a couple of versions of the patch adding methods to Objfile; this
one adds a method to "gdb".

Thanks; pushing now.

Christian
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH][PR/24474] Make gdb.lookup_static_symbol also check the STATIC_SCOPE

Sourceware - gdb-patches mailing list
In reply to this post by Simon Marchi-4
On Mon, Jul 15, 2019 at 10:28 PM Simon Marchi <[hidden email]> wrote:
> I am not sure this is the right thing to do, or at least some things need to be
> clarified.  Global symbols are visible through the entire program (there can only
> be one of that name), whereas there can be many static symbols with the same name.
> What should the user expect if there are multiple static symbols with the same name?
> I suppose we'll return the first one that matches, without any particular guarantee
> about which one.

By the way, to follow up on that comment-- the symbol cache (or, for
that matter, symtab.c:lookup_static_symbol) does not take that into
account at all, it will only ever have one cache entry for one static
symbol name. In fact it does not even differentiate by objfile, unlike
for global symbols (??). Should maybe be fixed someday...

Christian