This is the mail archive of the archer@sourceware.org mailing list for the Archer project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Add convenience predicates do gdb.Symbol


Hi,

This patch adds some predicates to gdb.Symbol, which make it easier to
reason about symbols. I'd appreciate closer review of them, since I'm
not well versed in GDB's symbol classes and domains (should I use symbol
domains here?).

The idea is to shield users from having to get their hands dirty with
arcane (and maybe internal to GDB?) things like symbol class and LOC_*
constants.

With this and previous patches, the following script implements the
in_scope convenience function:

class InScope (gdb.Function):
    """Check if all the given variables or macros are in scope.
       Receives as argument a list of names separated by whitespace."""

    def __init__ (self):
	super (InScope, self).__init__ ("in_scope")

    def invoke (self, var):
	vars = set (var.string().split())
	found = set ()
	block = gdb.get_block_for_pc (gdb.get_selected_frame ().get_pc ())
	while block:
	    for sym in block:
		if (sym.is_argument () or sym.is_constant ()
		      or sym.is_function () or sym.is_variable ()):
		    sym_name = sym.get_print_name ()
		    if sym_name in vars:
			found.add (sym_name)
	    block = block.get_superblock ()

	return vars == found

InScope ()

Example usage:

Temporary breakpoint 1, main (argc=1, argv=0x7fffffffe208) at /home/bauermann/pessoais/src/examples/funcs.c:16
16        int i = 41;
(gdb) p $in_scope ("argc i argv")
$1 = 1
(gdb) p $in_scope ("argc")
$2 = 1
(gdb) p $in_scope ("i")
$3 = 1
(gdb) p $in_scope ("argv blah")
$4 = 0
(gdb) p $in_scope ("blah")
$5 = 0

-- 
[]'s
Thiago Jung Bauermann
IBM Linux Technology Center


2008-11-22  Thiago Jung Bauermann  <bauerman@br.ibm.com>

	* python/python-symbol.c (sympy_is_constant, sympy_is_function,
	sympy_is_variable): New functions.
	(symbol_object_methods): Add is_constant, is_function, is_variable
	methods.

diff --git a/gdb/python/python-symbol.c b/gdb/python/python-symbol.c
index 99e92c7..d8237dc 100644
--- a/gdb/python/python-symbol.c
+++ b/gdb/python/python-symbol.c
@@ -109,6 +109,35 @@ sympy_is_argument (PyObject *self, PyObject *args)
   return PyBool_FromLong (SYMBOL_IS_ARGUMENT (self_sym->symbol));
 }
 
+static PyObject *
+sympy_is_constant (PyObject *self, PyObject *args)
+{
+  symbol_object *self_sym = (symbol_object *) self;
+  enum address_class class = SYMBOL_CLASS (self_sym->symbol);
+
+  return PyBool_FromLong (class == LOC_CONST || class == LOC_CONST_BYTES);
+}
+
+static PyObject *
+sympy_is_function (PyObject *self, PyObject *args)
+{
+  symbol_object *self_sym = (symbol_object *) self;
+  enum address_class class = SYMBOL_CLASS (self_sym->symbol);
+
+  return PyBool_FromLong (class == LOC_BLOCK);
+}
+
+static PyObject *
+sympy_is_variable (PyObject *self, PyObject *args)
+{
+  symbol_object *self_sym = (symbol_object *) self;
+  enum address_class class = SYMBOL_CLASS (self_sym->symbol);
+
+  return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (self_sym->symbol)
+      && (class == LOC_LOCAL || class == LOC_REGISTER || class == LOC_STATIC
+	  || class == LOC_COMPUTED || class == LOC_OPTIMIZED_OUT));
+}
+
 PyObject *
 symbol_to_symbol_object (struct symbol *sym)
 {
@@ -251,6 +280,12 @@ static PyMethodDef symbol_object_methods[] = {
     "Return the class of the symbol." },
   { "is_argument", sympy_is_argument, METH_NOARGS,
     "Return True if symbol is the argument of a function." },
+  { "is_constant", sympy_is_constant, METH_NOARGS,
+    "Return True if symbol is a function or method." },
+  { "is_function", sympy_is_function, METH_NOARGS,
+    "Return True if symbol is a function or method." },
+  { "is_variable", sympy_is_variable, METH_NOARGS,
+    "Return True if symbol is a variable." },
   {NULL}  /* Sentinel */
 };
 
-- 
1.5.6.5



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]