Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [RFC - Python Scripting] New method Objfile.symtabs () - docs included
@ 2012-04-02  6:17 Siva Chandra
  2012-04-02 17:08 ` Eli Zaretskii
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Siva Chandra @ 2012-04-02  6:17 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 1552 bytes --]

Hi all,

Attached is a patch which adds a new method Objfile.symtabs to
gdb.Objfile.  This new method returns a list of gdb.Symtab objects
associated with the underlying object file.

Code ChangeLog:

2012-04-02  Siva Chandra Reddy  <sivachandra@google.com>

	Python Scripting: New method Objfile.symtabs which returns
	the list of obj.Symtab objects associated with the underlying
	objfile.
	* NEWS: Add entery under 'Python scripting' about the new
	Objfile.symtabs method.
	* python/py-objfile.c (stpy_iterator_object): New iterator
	object type to iterate over a list of obj.Symtab objects.
	(stpy_node): New struct to aid the iterator.
	(stpy_iterator_free_symtab_list, new_stpy_iterator,
	 objfpy_symtabs, stpy_iterator_dealloc, stpy_iterator_iter,
	 stpy_iterator_iternext): New functions which manage the new
	iterator object type.
	(objfpy_dealloc, objfpy_new, objfile_to_objfile_object,
	 gdbpy_initialize_objfile): Add initializations and reference
	management for the new iterator object and type.

Docs ChangeLog:

2012-04-02  Siva Chandra Reddy  <sivachandra@google.com>

	* gdb.texinfo (Python API/Objfiles In Python): Add description
	about the new method Objfile.symtabs on gdb.Objfile objects.

Testsuite ChangeLog:

2012-04-02  Siva Chandra Reddy  <sivachandra@google.com>

	* gdb.python/py-objfile.exp: Add tests to test the method
	Objfile.symtabs.
	* gdb.python/py-objfile1.c: New file to help test the new
	method Objfile.symtabs.
	* gdb.python/py-objfile2.c: Likewise
	* gdb.python/py-objfile3.c: Likewise

Thanks,
Siva Chandra

[-- Attachment #2: objfile_symtabs_patch_v1.txt --]
[-- Type: text/plain, Size: 16415 bytes --]

Index: python/py-objfile.c
===================================================================
RCS file: /cvs/src/src/gdb/python/py-objfile.c,v
retrieving revision 1.11
diff -c -p -r1.11 py-objfile.c
*** python/py-objfile.c	4 Jan 2012 08:17:25 -0000	1.11
--- python/py-objfile.c	2 Apr 2012 06:09:17 -0000
***************
*** 22,27 ****
--- 22,28 ----
  #include "charset.h"
  #include "objfiles.h"
  #include "language.h"
+ #include "psympriv.h"
  
  typedef struct
  {
*************** typedef struct
*** 32,45 ****
--- 33,172 ----
  
    /* The pretty-printer list of functions.  */
    PyObject *printers;
+ 
+   /* The iterator object to iterate over the list of symtabs associated
+      with the corresponding objfile.  */
+   PyObject *symtab_iterator;
  } objfile_object;
  
+ typedef struct stpy_node_i
+ {
+   PyObject *symtab_object;
+   struct stpy_node_i *next;
+ } stpy_node;
+ 
+ /* The iterator object to iterate over the list of symtabs associated
+    with an Objfile.  */
+ 
+ typedef struct
+ {
+   PyObject_HEAD
+ 
+   stpy_node *symtab_list;
+   stpy_node *current_node;
+ } stpy_iterator_object;
+ 
  static PyTypeObject objfile_object_type;
+ static PyTypeObject stpy_iterator_object_type;
  
  static const struct objfile_data *objfpy_objfile_data_key;
  
  \f
  
+ /* Free the symtab list in ITER_OBJECT.  Decrement the references to
+    the individual Symtab objects in the list.  */
+ 
+ static void
+ stpy_iterator_free_symtab_list (stpy_iterator_object *iter_object)
+ {
+   stpy_node *node;
+ 
+   if (! iter_object || ! iter_object->symtab_list)
+     return;
+ 
+   node = iter_object->symtab_list;
+   while (node)
+     {
+       stpy_node *temp = node;
+ 
+       Py_XDECREF (node->symtab_object);
+       node = node->next;
+ 
+       xfree (temp);
+     }
+ 
+   iter_object->symtab_list = NULL;
+   iter_object->current_node = NULL; 
+ }
+ 
+ /* Return a new stpy_iterator_object given an OBJFILE.  The caller holds
+    the reference to the new object.  */
+ 
+ static stpy_iterator_object *
+ new_stpy_iterator (struct objfile *objfile)
+ {
+   stpy_iterator_object *iter_object;
+   struct symtab *symtab;
+   struct partial_symtab *psymtab;
+   stpy_node *last_node = NULL;
+   int first = 1;
+   int invalid = 0;
+ 
+   iter_object = PyObject_New (stpy_iterator_object,
+                               &stpy_iterator_object_type);
+ 
+   if (! iter_object)
+     return NULL;
+ 
+   iter_object->symtab_list = NULL;
+   if (! objfile)
+     {
+       /* If the objfile is NULL, return an iter with no elements.  */
+       iter_object->current_node = NULL;
+       return iter_object;
+     }
+ 
+   /* If the symtabs are not yet read, then read them.  */
+   ALL_OBJFILE_PSYMTABS (objfile, psymtab)
+     {
+       if (psymtab->readin)
+         continue;
+ 
+       if (psymtab->read_symtab)
+         psymtab->read_symtab (psymtab);
+     }
+ 
+   ALL_OBJFILE_SYMTABS (objfile, symtab)
+     {
+       PyObject *symtab_object;
+ 
+       symtab_object = symtab_to_symtab_object (symtab);
+       if (symtab_object)
+         {
+           stpy_node *new_node;
+ 
+           new_node = (stpy_node *) xmalloc (sizeof (stpy_node));
+           new_node->symtab_object = symtab_object;
+           new_node->next = NULL;
+           if (first)
+             {
+               iter_object->symtab_list = new_node;
+               first = 0;
+             }
+           else
+             {
+               last_node->next = new_node;
+             }
+ 
+           last_node = new_node;
+         }
+       else
+         {
+           /* Give up if it is not possible to get the symtab_object
+              for a symtab.  */
+           invalid = 1;
+           break;
+         }
+     }
+ 
+   iter_object->current_node = iter_object->symtab_list;
+ 
+   if (invalid)
+     stpy_iterator_free_symtab_list (iter_object);
+ 
+   return iter_object;
+ }
+ 
  /* An Objfile method which returns the objfile's file name, or None.  */
  static PyObject *
  objfpy_get_filename (PyObject *self, void *closure)
*************** objfpy_dealloc (PyObject *o)
*** 58,63 ****
--- 185,191 ----
    objfile_object *self = (objfile_object *) o;
  
    Py_XDECREF (self->printers);
+   Py_XDECREF (self->symtab_iterator);
    self->ob_type->tp_free ((PyObject *) self);
  }
  
*************** objfpy_new (PyTypeObject *type, PyObject
*** 69,74 ****
--- 197,203 ----
    if (self)
      {
        self->objfile = NULL;
+       self->symtab_iterator = NULL;
  
        self->printers = PyList_New (0);
        if (!self->printers)
*************** objfpy_set_printers (PyObject *o, PyObje
*** 118,123 ****
--- 247,281 ----
    return 0;
  }
  
+ /* Return a sequence of symtab objects associated with the corresponding
+    objfile.  Each element in the sequence is a gdb.Symtab object.  */
+ 
+ static PyObject *
+ objfpy_symtabs (PyObject *self, PyObject *args)
+ {
+   objfile_object *obj = (objfile_object *) self;
+   stpy_iterator_object *iter;
+ 
+   if (! obj->objfile)
+     return NULL;
+ 
+   if (obj->symtab_iterator)
+     {
+       iter = (stpy_iterator_object *) obj->symtab_iterator;
+       iter->current_node = iter->symtab_list;
+     }
+   else
+     {
+       iter = new_stpy_iterator (obj->objfile);
+       if (! iter)
+         return NULL;
+ 
+       obj->symtab_iterator = (PyObject *) iter;
+     }
+ 
+   return PySequence_List ((PyObject *) iter);
+ }
+ 
  /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
     Returns True if this object file still exists in GDB.  */
  
*************** objfile_to_objfile_object (struct objfil
*** 164,169 ****
--- 322,328 ----
        if (object)
  	{
  	  object->objfile = objfile;
+           object->symtab_iterator = NULL;
  
  	  object->printers = PyList_New (0);
  	  if (!object->printers)
*************** objfile_to_objfile_object (struct objfil
*** 179,184 ****
--- 338,380 ----
    return (PyObject *) object;
  }
  
+ /* 'tp_dealloc' function for stpy_iterator_object_type.  */
+ 
+ static void
+ stpy_iterator_dealloc (PyObject *o)
+ {
+   stpy_iterator_free_symtab_list ((stpy_iterator_object *) o);
+ }
+ 
+ /* 'tp_iter' function for stpy_iterator_object_type.  */
+ 
+ static PyObject *
+ stpy_iterator_iter (PyObject *self)
+ {
+   Py_INCREF (self);
+   return self;
+ }
+ 
+ /* 'tp_iternext' function for stpy_iterator_object_type.  */
+ 
+ static PyObject *
+ stpy_iterator_iternext (PyObject *self)
+ {
+   stpy_iterator_object *iter = (stpy_iterator_object *) self;
+   stpy_node *node;
+   PyObject *elem;
+ 
+   if (! iter || ! iter->current_node)
+     return NULL;
+ 
+   node = iter->current_node;
+   iter->current_node = node->next;
+ 
+   elem = node->symtab_object;
+   Py_INCREF ((PyObject *) elem);
+   return elem;
+ }
+ 
  void
  gdbpy_initialize_objfile (void)
  {
*************** gdbpy_initialize_objfile (void)
*** 188,196 ****
--- 384,399 ----
    if (PyType_Ready (&objfile_object_type) < 0)
      return;
  
+   if (PyType_Ready (&stpy_iterator_object_type) < 0)
+     return;
+ 
    Py_INCREF (&objfile_object_type);
    PyModule_AddObject (gdb_module, "Objfile",
  		      (PyObject *) &objfile_object_type);
+ 
+   Py_INCREF (&stpy_iterator_object_type);
+   PyModule_AddObject (gdb_module, "SymtabIterator",
+                       (PyObject *) &stpy_iterator_object_type);
  }
  
  \f
*************** static PyMethodDef objfile_object_method
*** 200,205 ****
--- 403,412 ----
    { "is_valid", objfpy_is_valid, METH_NOARGS,
      "is_valid () -> Boolean.\n\
  Return true if this object file is valid, false if not." },
+   { "symtabs", objfpy_symtabs, METH_NOARGS,
+     "symtabs () -> list.\n\
+ Return the list of gdb.Symtab objects associated with the underlying\n\
+ object file."},
  
    { NULL }
  };
*************** static PyTypeObject objfile_object_type 
*** 255,257 ****
--- 462,496 ----
    0,				  /* tp_alloc */
    objfpy_new,			  /* tp_new */
  };
+ 
+ static PyTypeObject stpy_iterator_object_type =
+ {
+   PyObject_HEAD_INIT (NULL)
+   0,				  /*ob_size*/
+   "gdb.SymtabIterator",		  /*tp_name*/
+   sizeof (stpy_iterator_object),  /*tp_basicsize*/
+   0,				  /*tp_itemsize*/
+   stpy_iterator_dealloc,	  /*tp_dealloc*/
+   0,				  /*tp_print*/
+   0,				  /*tp_getattr*/
+   0,				  /*tp_setattr*/
+   0,				  /*tp_compare*/
+   0,				  /*tp_repr*/
+   0,				  /*tp_as_number*/
+   0,				  /*tp_as_sequence*/
+   0,				  /*tp_as_mapping*/
+   0,				  /*tp_hash */
+   0,				  /*tp_call*/
+   0,				  /*tp_str*/
+   0,				  /*tp_getattro*/
+   0,				  /*tp_setattro*/
+   0,				  /*tp_as_buffer*/
+   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,  /*tp_flags*/
+   "GDB symtab iterator object",	  /* tp_doc */
+   0,				  /* tp_traverse */
+   0,				  /* tp_clear */
+   0,				  /* tp_richcompare */
+   0,				  /* tp_weaklistoffset */
+   stpy_iterator_iter,		  /* tp_iter */
+   stpy_iterator_iternext,	  /* tp_iternext */
+ };
Index: doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.939
diff -c -p -r1.939 gdb.texinfo
*** doc/gdb.texinfo	28 Mar 2012 21:31:46 -0000	1.939
--- doc/gdb.texinfo	2 Apr 2012 06:09:24 -0000
*************** longer.  All other @code{gdb.Objfile} me
*** 23812,23817 ****
--- 23812,23823 ----
  if it is invalid at the time the method is called.
  @end defun
  
+ @defun Objfile.symtabs ()
+ Returns the list of @code{gdb.Symtab} objects associated with the
+ underlying object file referred by the @code{gdb.Objfile} object.
+ @xref{Symbol Tables In Python}.
+ @end defun
+ 
  @node Frames In Python
  @subsubsection Accessing inferior stack frames from Python.
  
Index: testsuite/gdb.python/py-objfile.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.python/py-objfile.exp,v
retrieving revision 1.3
diff -c -p -r1.3 py-objfile.exp
*** testsuite/gdb.python/py-objfile.exp	16 Jan 2012 16:21:52 -0000	1.3
--- testsuite/gdb.python/py-objfile.exp	2 Apr 2012 06:09:24 -0000
***************
*** 19,25 ****
  load_lib gdb-python.exp
  
  set testfile "py-objfile"
! set srcfile ${testfile}.c
  
  if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
      return -1
--- 19,26 ----
  load_lib gdb-python.exp
  
  set testfile "py-objfile"
! set srcfile "${testfile}.c ${testfile}1.c ${testfile}2.c ${testfile}3.c"
! set fnameprefix "${srcdir}/${subdir}/"
  
  if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
      return -1
*************** gdb_py_test_silent_cmd "python sym = gdb
*** 37,47 ****
--- 38,67 ----
      "Find a symbol in objfile" 1
  gdb_py_test_silent_cmd "python objfile = sym\[0\].symtab.objfile" \
      "Get backing object file" 1
+ gdb_py_test_silent_cmd "python symtabs = objfile.symtabs()" \
+     "Get the symtabs associated with the objfile" 1
+ gdb_py_test_silent_cmd "python filenames = \['${fnameprefix}${testfile}.c', \
+                                              '${fnameprefix}${testfile}1.c', \
+                                              '${fnameprefix}${testfile}2.c', \
+                                              '${fnameprefix}${testfile}3.c'\]" \
+     "Create a list of source file names" 1
  
  gdb_test "python print objfile.filename" ".*py-objfile.*" \
    "Get objfile validity"
  gdb_test "python print objfile.is_valid()" "True" \
    "Get objfile validity"
+ 
+ gdb_test "python print len(symtabs)" "4" \
+   "Get the number of symtabs associated with the objfile."
+ gdb_test "python print symtabs\[0\].filename in filenames" "True" \
+   "Check if symtab file is present in the list of filenames."
+ gdb_test "python print symtabs\[1\].filename in filenames" "True" \
+   "Check if symtab file is present in the list of filenames."
+ gdb_test "python print symtabs\[2\].filename in filenames" "True" \
+   "Check if symtab file is present in the list of filenames."
+ gdb_test "python print symtabs\[3\].filename in filenames" "True" \
+   "Check if symtab file is present in the list of filenames."
+ 
  gdb_unload
  gdb_test "python print objfile.is_valid()" "False" \
    "Get objfile validity after unload"
Index: testsuite/gdb.python/py-objfile1.c
===================================================================
RCS file: testsuite/gdb.python/py-objfile1.c
diff -N testsuite/gdb.python/py-objfile1.c
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/gdb.python/py-objfile1.c	2 Apr 2012 06:09:24 -0000
***************
*** 0 ****
--- 1,22 ----
+ /* This testcase is part of GDB, the GNU debugger.
+ 
+    Copyright 2012 Free Software Foundation, Inc.
+ 
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+ 
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+ 
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+ 
+ static int
+ add (int a, int b)
+ {
+   return a + b;
+ }
Index: testsuite/gdb.python/py-objfile2.c
===================================================================
RCS file: testsuite/gdb.python/py-objfile2.c
diff -N testsuite/gdb.python/py-objfile2.c
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/gdb.python/py-objfile2.c	2 Apr 2012 06:09:24 -0000
***************
*** 0 ****
--- 1,23 ----
+ /* This testcase is part of GDB, the GNU debugger.
+ 
+    Copyright 2012 Free Software Foundation, Inc.
+ 
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+ 
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+ 
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+ 
+ static int
+ sub (int a, int b)
+ {
+   return a - b;
+ }
+ 
Index: testsuite/gdb.python/py-objfile3.c
===================================================================
RCS file: testsuite/gdb.python/py-objfile3.c
diff -N testsuite/gdb.python/py-objfile3.c
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/gdb.python/py-objfile3.c	2 Apr 2012 06:09:24 -0000
***************
*** 0 ****
--- 1,23 ----
+ /* This testcase is part of GDB, the GNU debugger.
+ 
+    Copyright 2012 Free Software Foundation, Inc.
+ 
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+ 
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+ 
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+ 
+ static int
+ mul (int a, int b)
+ {
+   return a * b;
+ }
+ 
Index: NEWS
===================================================================
RCS file: /cvs/src/src/gdb/NEWS,v
retrieving revision 1.505
diff -c -p -r1.505 NEWS
*** NEWS	28 Mar 2012 21:31:56 -0000	1.505
--- NEWS	2 Apr 2012 06:09:24 -0000
***************
*** 32,37 ****
--- 32,41 ----
    ** A new method 'referenced_value' on gdb.Value objects which can
       dereference pointer as well as C++ reference values.
  
+   ** A new method 'symtabs' on gdb.Objfile objects which returns the
+      list of gdb.Symtab objects associated with the underlying object
+      files.
+ 
  * GDBserver now supports stdio connections.
    E.g. (gdb) target remote | ssh myhost gdbserver - hello
  

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2012-04-09 17:59 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-02  6:17 [RFC - Python Scripting] New method Objfile.symtabs () - docs included Siva Chandra
2012-04-02 17:08 ` Eli Zaretskii
2012-04-03  0:04 ` Doug Evans
2012-04-03  5:54   ` Siva Chandra
2012-04-05 16:30     ` Paul_Koning
2012-04-05 18:41       ` Tom Tromey
2012-04-05 18:37     ` Tom Tromey
2012-04-05 19:56 ` Paul_Koning
2012-04-05 20:26   ` Doug Evans
2012-04-06 16:42     ` Siva Chandra
2012-04-09 17:56       ` Tom Tromey
2012-04-09 17:59         ` Doug Evans

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox