[pypy-commit] pypy default: automatically find typeids.txt based on the exe name, and cache the result of load_typeids

antocuni noreply at buildbot.pypy.org
Thu Aug 4 17:02:48 CEST 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: 
Changeset: r46273:9fca7652288f
Date: 2011-08-04 15:08 +0200
http://bitbucket.org/pypy/pypy/changeset/9fca7652288f/

Log:	automatically find typeids.txt based on the exe name, and cache the
	result of load_typeids

diff --git a/pypy/tool/gdb_pypy.py b/pypy/tool/gdb_pypy.py
--- a/pypy/tool/gdb_pypy.py
+++ b/pypy/tool/gdb_pypy.py
@@ -9,20 +9,9 @@
 """
 
 import sys
+import os.path
 import gdb
 
-def load_typeids():
-    """
-    Returns a mapping offset --> description
-    """
-    typeids = {}
-    for line in open('typeids.txt'):
-        member, descr = map(str.strip, line.split(None, 1))
-        expr = "((char*)(&pypy_g_typeinfo.%s)) - (char*)&pypy_g_typeinfo" % member
-        offset = int(gdb.parse_and_eval(expr))
-        typeids[offset] = descr
-    return typeids
-
 class RPyType (gdb.Command):
     """
     Prints the RPython type of the expression (remember to dereference it!)
@@ -32,30 +21,57 @@
     (gdb) rpy_type *l_v123
     GcStruct pypy.foo.Bar { super, inst_xxx, inst_yyy }
     """
+
+    prog2typeids = {}
  
     def __init__(self):
         gdb.Command.__init__(self, "rpy_type", gdb.COMMAND_NONE)
 
-    ## # some magic code to automatically reload the python file while developing
-    ## def invoke(self, arg, from_tty):
-    ##     sys.path.insert(0, '')
-    ##     import gdb_pypy
-    ##     reload(gdb_pypy)
-    ##     self.__class__ = gdb_pypy.RPyType
-    ##     self.do_invoke(arg, from_tty)
+    # some magic code to automatically reload the python file while developing
+    def invoke(self, arg, from_tty):
+        from pypy.tool import gdb_pypy
+        reload(gdb_pypy)
+        gdb_pypy.RPyType.prog2typeids = self.prog2typeids # persist the cache
+        self.__class__ = gdb_pypy.RPyType
+        self.do_invoke(arg, from_tty)
 
-    def invoke(self, arg, from_tty):
+    def do_invoke(self, arg, from_tty):
         obj = gdb.parse_and_eval(arg)
         hdr = self.get_gc_header(obj)
         tid = hdr['h_tid']
         offset = tid & 0xFFFFFFFF # 64bit only
         offset = int(offset) # convert from gdb.Value to python int
-        typeids = load_typeids()
+        typeids = self.get_typeids()
         if offset in typeids:
             print typeids[offset]
         else:
             print 'Cannot find the type with offset %d' % offset
 
+    def get_typeids(self):
+        progspace = gdb.current_progspace()
+        try:
+            return self.prog2typeids[progspace]
+        except KeyError:
+            typeids = self.load_typeids(progspace)
+            self.prog2typeids[progspace] = typeids
+            return typeids
+
+    def load_typeids(self, progspace):
+        """
+        Returns a mapping offset --> description
+        """
+        exename = progspace.filename
+        root = os.path.dirname(exename)
+        typeids_txt = os.path.join(root, 'typeids.txt')
+        print 'loading', typeids_txt
+        typeids = {}
+        for line in open('typeids.txt'):
+            member, descr = map(str.strip, line.split(None, 1))
+            expr = "((char*)(&pypy_g_typeinfo.%s)) - (char*)&pypy_g_typeinfo" % member
+            offset = int(gdb.parse_and_eval(expr))
+            typeids[offset] = descr
+        return typeids
+
     def get_first_field_if(self, obj, suffix):
         ctype = obj.type
         field = ctype.fields()[0]


More information about the pypy-commit mailing list