ivmdump.py :  » Development » Psyco » psyco-dist » py-utils » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Psyco 
Psyco » psyco dist » py utils » ivmdump.py
import sys, os
from struct import unpack,calcsize,error


class argtypes:
    char = "b", "0x%x"
    byte = code_t = "B", "0x%x"
    int  = "i", "0x%x"
    long = word_t = "l", "0x%x"
    def stack(t):
        if isinstance(t, tuple):
            return t[0], "[%d]"
        else:
            return "", "[%s]" % t, t
    def indirect(t):
        return t

fn = os.path.join(os.path.dirname(__file__), '../c/ivm/prolog/insns-table.py')
execfile(fn, argtypes.__dict__, globals())


class Mode:
    
    def __init__(self, opcode):
        self.opcode = opcode
        if opcode in insntable:
            self.insns = insntable[opcode]
        else:
            self.insns = [("<%d>" % opcode,)]
        if len(self.insns) == 1:
            self.singleinsn = self.insns[0][0]
        else:
            self.singleinsn = None
        self.stackpushes = stackpushes.get(opcode)
        self.unpackfmt = "="
        self.template = ""
        self.constantargs = []
        i = 0
        line = '%10x\t'
        for insn in self.insns:
            args = []
            for arg in insn[1:]:
                if isinstance(arg, tuple):
                    self.unpackfmt += arg[0]
                    args.append(arg[1])
                    if len(arg)>2:
                        self.constantargs.append((i, arg[2]))
                else:
                    args.append(str(arg))
                    self.constantargs.append((i, arg))
                i += 1
            line += '%-11s %s' % (insn[0], ', '.join(args))
            self.template += line
            line = '\n          \t'
        self.unpacksize = calcsize(self.unpackfmt)
        
    def dump(self, data, address, position):
        data = data[position:position+self.unpacksize]
        args = unpack(self.unpackfmt, data)
        result = self.template % ((address,) + args)
        return position+self.unpacksize, result.split('\n'), args

    def getargs(self, data, position):
        data = data[position:position+self.unpacksize]
        args = list(unpack(self.unpackfmt, data))
        for i, value in self.constantargs:
            args.insert(i, value)
        return args

insnlist = [Mode(opcode) for opcode in range(256)]


def dump(data, originaddr):
    l = len(data)
    if l>8 and data[-4:] == '\x00\x00\x00\x00':
        p, = unpack("l", data[-8:-4])
        queue = ["", "          (promotion chained list: 0x%x)" % p]
        l -= 4
    else:
        queue = []
    depth = None
    result = []
    p = 0
    try:
        while p < l:
            mode = insnlist[ord(data[p])]
            p, lines, args = mode.dump(data, originaddr+p, p+1)
            if depth is not None:
                if mode.stackpushes is None:
                    depth = None
                else:
                    depth += mode.stackpushes
                    lines[-1] = '%-40s [%d]' % (lines[-1], depth)
            if mode.singleinsn == 'assertdepth':
                asserteddepth = args[0]/4
                if depth is not None and asserteddepth != depth:
                    err = '************* assertion error **************'
                    lines.append(err)
                    print >> sys.stderr, err
                    print >> sys.stderr, originaddr
                else:
                    lines = [(s+' ')[:s.find('assertdepth')] for s in lines]
                depth = asserteddepth
            result += lines
    except error:
        while p < l:
            result.append("  %10x\t<%d>" % (originaddr+p, ord(data[p])))
            p += 1
    result += queue
    return result
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.