[PR/24474] Add gdb.lookup_static_symbol to the python API
authorChristian Biesinger <cbiesinger@google.com>
Tue, 30 Jul 2019 16:04:37 +0000 (11:04 -0500)
committerChristian Biesinger <cbiesinger@google.com>
Tue, 30 Jul 2019 16:04:37 +0000 (11:04 -0500)
Similar to lookup_global_symbol, except that it checks the
STATIC_SCOPE.

gdb/ChangeLog:

2019-07-30  Christian Biesinger  <cbiesinger@google.com>

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-30  Christian Biesinger  <cbiesinger@google.com>

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

gdb/testsuite/ChangeLog:

2019-07-30  Christian Biesinger  <cbiesinger@google.com>

* 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/ChangeLog
gdb/NEWS
gdb/doc/ChangeLog
gdb/doc/python.texi
gdb/python/py-symbol.c
gdb/python/python-internal.h
gdb/python/python.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-symbol.c
gdb/testsuite/gdb.python/py-symbol.exp

index 59fb8002e81f7c8c1094f145239324cf3cbf6968..00e681938e40b9a61124de59dce2a8a8cdcd3c13 100644 (file)
@@ -1,3 +1,11 @@
+2019-07-30  Christian Biesinger  <cbiesinger@google.com>
+
+       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.
+
 2019-07-29  Christian Biesinger  <cbiesinger@google.com>
 
        * NEWS: Mention new functions Objfile.lookup_{global,static}_symbol.
index 4e821eab4c70f01e70096717b130c4ef8f9391e2..ac44399304c97f988408eb37702cd06a86b7a3af 100644 (file)
--- 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.
 
index 9b91b998bdda6a161c04b06f196585272dbed64a..e469b6057d72879147d084223cd5357e75312e6e 100644 (file)
@@ -1,3 +1,8 @@
+2019-07-30  Christian Biesinger  <cbiesinger@google.com>
+
+       * python.texi (Symbols In Python): Document new function
+       gdb.lookup_static_symbol.
+
 2019-07-29  Christian Biesinger  <cbiesinger@google.com>
 
        * python.texi (Objfiles In Python): Document new functions
index bbba519ffc08effef851ff91ad00659a4592271f..3fdccd5e435f38acd044d6110f9ddae0ee4af8ac 100644 (file)
@@ -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
index 8605ae71a24d66405b10e05e45c259cda0ece8ba..2b10e21d872a3ff36e29be68f3c05c637755aef9 100644 (file)
@@ -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
index e6a3fe0ec1f8fec5b35f2f9a311d6bb565077af7..c5578430cfff9d596d542db91184634747cae7be 100644 (file)
@@ -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);
index 96bee7c3b04f0c1fff1b3ea96bc9e08060230f36..162470dcc022372664f64ac4a80441abae4b2c33 100644 (file)
@@ -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,
index 669b943b4beac3560ac375b0ca749fc8117db393..8dde64d4b0842a4c0fa6ff1285fc5aa400986740 100644 (file)
@@ -1,3 +1,9 @@
+2019-07-30  Christian Biesinger  <cbiesinger@google.com>
+
+       * 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.
+
 2019-07-30  Tom de Vries  <tdevries@suse.de>
 
        * lib/read1.c (read): Don't use unsetenv (v), use setenv (v, "", 1)
index f77c8c8585d5039979b380e1610ccf712e742261..06a931bf5d951784f01f611be47f114f67c24be8 100644 (file)
@@ -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)
 {
index 5b8a2be7c488ea2e4db81e37cd9b571c02f5c769..5617f127e5bd9953808a2fcff2a14ae8efd9b413 100644 (file)
@@ -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
This page took 0.050261 seconds and 4 git commands to generate.