]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Add gdb.lookup_global_symbol python function.
authorDoug Evans <dje@google.com>
Tue, 22 Feb 2011 22:48:12 +0000 (22:48 +0000)
committerDoug Evans <dje@google.com>
Tue, 22 Feb 2011 22:48:12 +0000 (22:48 +0000)
* NEWS: Add entry.
* python/py-symbol.c (gdbpy_lookup_global_symbol): New function.
* python/python-internal.h (gdbpy_lookup_global_symbol): Declare it.
* python/python.c (GdbMethods): Add entry for lookup_global_symbol.

doc/
* gdb.texinfo (Symbols In Python): Document lookup_global_symbol.
Clarify behaviour of lookup_symbol when `block' argument is omitted,
add description of result, fix @defun formatting.

testsuite/
* gdb.python/py-symbol.exp: Test lookup_global_symbol.

gdb/ChangeLog
gdb/NEWS
gdb/doc/ChangeLog
gdb/doc/gdb.texinfo
gdb/python/py-symbol.c
gdb/python/python-internal.h
gdb/python/python.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-symbol.exp

index 79fd478c59e23010118364cea4d1a00526b4a506..8d5dadcda0831927d8efab21e4622616fc33b270 100644 (file)
@@ -1,3 +1,11 @@
+2011-02-22  Doug Evans  <dje@google.com>
+
+       Add gdb.lookup_global_symbol python function.
+       * NEWS: Add entry.
+       * python/py-symbol.c (gdbpy_lookup_global_symbol): New function.
+       * python/python-internal.h (gdbpy_lookup_global_symbol): Declare it.
+       * python/python.c (GdbMethods): Add entry for lookup_global_symbol.
+
 2011-02-22  Tom Tromey  <tromey@redhat.com>
 
        * language.c (language_class_name_from_physname): Rename
index a07d32dc8fd88d2baef0a63bfee4119db6c66b34..fb363832ea0ee7c18acab958218af1448a4607ee 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -38,6 +38,8 @@
 
 * Python scripting
 
+  ** New function gdb.lookup_global_symbol looks up a global symbol.
+
   ** GDB values in Python are now callable if the value represents a
      function.  For example, if 'some_value' represents a function that
      takes two integer parameters and returns a value, you can call
index ee9a63f21c16bcef4d9440be7a5758b11b4adcec..8d9ce1dd0054c0b21d57c4048538ea29c15c7a37 100644 (file)
@@ -1,3 +1,9 @@
+2011-02-22  Doug Evans  <dje@google.com>
+
+       * gdb.texinfo (Symbols In Python): Document lookup_global_symbol.
+       Clarify behaviour of lookup_symbol when `block' argument is omitted,
+       add description of result, fix @defun formatting.
+
 2011-02-21  Hui Zhu  <teawater@gmail.com>
 
        * agentexpr.texi (Bytecode Descriptions): Add printf.
index f8b7e2db1173c76f371637b53533e59983216657..74a626ef93c4fd367e3d435519280d0fee5c6962 100644 (file)
@@ -22829,7 +22829,7 @@ The following symbol-related functions are available in the @code{gdb}
 module:
 
 @findex gdb.lookup_symbol
-@defun lookup_symbol name [block] [domain]
+@defun lookup_symbol name @r{[}block@r{]} @r{[}domain@r{]}
 This function searches for a symbol by name.  The search scope can be
 restricted to the parameters defined in the optional domain and block
 arguments.
@@ -22837,10 +22837,33 @@ arguments.
 @var{name} is the name of the symbol.  It must be a string.  The
 optional @var{block} argument restricts the search to symbols visible
 in that @var{block}.  The @var{block} argument must be a
-@code{gdb.Block} object.  The optional @var{domain} argument restricts
+@code{gdb.Block} object.  If omitted, the block for the current frame
+is used.  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 tuple of two elements.
+The first element is a @code{gdb.Symbol} object or @code{None} if the symbol
+is not found.
+If the symbol is found, the second element is @code{True} if the symbol
+is a field of a method's object (e.g., @code{this} in @code{C++}),
+otherwise it is @code{False}.
+If the symbol is not found, the second element is @code{False}.
+@end defun
+
+@findex gdb.lookup_global_symbol
+@defun lookup_global_symbol name @r{[}domain@r{]}
+This function searches for a global symbol 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.
 @end defun
 
 A @code{gdb.Symbol} object has the following attributes:
index e072dc8dc2ec017a1d603dc2f50fbd875c7a1013..390d61248acdd0111b78df90e9b217c9f00fb362 100644 (file)
@@ -236,6 +236,7 @@ sympy_dealloc (PyObject *obj)
    A tuple with 2 elements is always returned.  The first is the symbol
    object or None, the second is a boolean with the value of
    is_a_field_of_this (see comment in lookup_symbol_in_language).  */
+
 PyObject *
 gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
 {
@@ -294,6 +295,39 @@ gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
   return ret_tuple;
 }
 
+/* Implementation of
+   gdb.lookup_global_symbol (name [, domain]) -> symbol or None.  */
+
+PyObject *
+gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
+{
+  int domain = VAR_DOMAIN;
+  const char *name;
+  static char *keywords[] = { "name", "domain", NULL };
+  struct symbol *symbol;
+  PyObject *sym_obj;
+
+  if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
+                                    &domain))
+    return NULL;
+
+  symbol = lookup_symbol_global (name, NULL, domain);
+
+  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 134268b2a2aa2f889adee79ac1d2e9b721d0f87e..49b6acf1063f807ce3c56843e89f98e69af71aea 100644 (file)
@@ -136,6 +136,8 @@ PyObject *gdbpy_history (PyObject *self, PyObject *args);
 PyObject *gdbpy_breakpoints (PyObject *, PyObject *);
 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_newest_frame (PyObject *self, PyObject *args);
 PyObject *gdbpy_selected_frame (PyObject *self, PyObject *args);
 PyObject *gdbpy_block_for_pc (PyObject *self, PyObject *args);
index b79504ab6e4500bc7c09cc6bb5efe513ebbc1d27..2977a5680997823c06ee4175fdb2fabdf8d1fbc4 100644 (file)
@@ -1145,6 +1145,10 @@ Return a Type corresponding to the given name." },
 Return a tuple with the symbol corresponding to the given name (or None) and\n\
 a boolean indicating if name is a field of the current implied argument\n\
 `this' (when the current language is object-oriented)." },
+  { "lookup_global_symbol", (PyCFunction) gdbpy_lookup_global_symbol,
+    METH_VARARGS | METH_KEYWORDS,
+    "lookup_global_symbol (name [, domain]) -> symbol\n\
+Return the symbol corresponding to the given name (or None)." },
   { "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,
index 86a176c4d9b15b9ef65347ba299f2d455210f032..f7dbde48c08cbfa55f57805f1239661bb882c25e 100644 (file)
@@ -1,3 +1,7 @@
+2011-02-22  Doug Evans  <dje@google.com>
+
+       * gdb.python/py-symbol.exp: Test lookup_global_symbol.
+
 2011-02-22  Michael Snyder  <msnyder@vmware.com>
 
        * Makefile.in: Make more clean.
index 8e3aec1f6179b3a7d1689a1bf662a642790e07aa..b30c639a174e4ea70af1b63c666ef9b9cea137d0 100644 (file)
@@ -39,6 +39,13 @@ gdb_load ${binfile}
 # Skip all tests if Python scripting is not enabled.
 if { [skip_python_tests] } { continue }
 
+# Test looking up a global symbol before we runto_main as this is the
+# point where we don't have a current frame, and we don't want to
+# require one.
+gdb_py_test_silent_cmd "python main_func = gdb.lookup_global_symbol(\"main\")" "Lookup main" 1
+gdb_test "python print main_func.is_function" "True" "Test main_func.is_function"
+gdb_test "python print gdb.lookup_global_symbol(\"junk\")" "None" "Test lookup_global_symbol(\"junk\")"
+
 if ![runto_main] then {
     fail "Can't run to main"
     return 0