Ejemplo n.º 1
0
    def fn_pretty(self, c):
        from javatools import opcodes

        pr = list()
        for offset, code, args in self.fn_data(c):
            name = opcodes.get_opname_by_code(code)
            pr.append((offset, name, args))

        return pr
Ejemplo n.º 2
0
    def fn_pretty(self, c):
        from javatools import opcodes

        if not self.offsets:
            return None

        pr = list()

        for offset, code, args in c.disassemble():
            if offset in self.offsets and opcodes.has_const_arg(code):
                name = opcodes.get_opname_by_code(code)
                data = c.cpool.pretty_deref_const(args[0])
                pr.append((offset, name, data))

        return pr
Ejemplo n.º 3
0
def print_method(options, method):
    if options.indent:
        print("   ", end="")

    print("%s;" % method.pretty_descriptor())

    if options.sigs:
        print("  Signature:", method.get_signature())

    if method.get_annotations():
        print("  RuntimeVisibleAnnotations:")
        index = 0
        for anno in method.get_annotations():
            print("  %i: %s" % (index, anno.pretty_annotation()))

    code = method.get_code()
    if options.disassemble and code:

        print("  Code:")

        if options.verbose:
            # the arg count is the number of arguments consumed from
            # the stack when this method is called. non-static methods
            # implicitly have a "this" argument that's not in the
            # descriptor
            argsc = len(method.get_arg_type_descriptors())
            if not method.is_static():
                argsc += 1

            print("   Stack=%i, Locals=%i, Args_size=%i" %
                  (code.max_stack, code.max_locals, argsc))

        for line in code.disassemble():
            opname = opcodes.get_opname_by_code(line[1])
            args = line[2]
            if args:
                args = ", ".join(str(arg) for arg in args)
                print("   %i:\t%s\t%s" % (line[0], opname, args))
            else:
                print("   %i:\t%s" % (line[0], opname))

        exps = code.exceptions
        if exps:
            print("  Exception table:")
            print("   from\tto\ttarget\ttype")
            for e in exps:
                ctype = e.pretty_catch_type()
                print("  % 4i\t% 4i\t% 4i\t%s" %
                      (e.start_pc, e.end_pc, e.handler_pc, ctype))

    if options.verbose:
        if method.is_deprecated():
            print("  Deprecated: true")

        if method.is_synthetic():
            print("  Synthetic: true")

        if method.is_bridge():
            print("  Bridge: true")

        if method.is_varargs():
            print("  Varargs: true")

    if options.lines and code:
        lnt = method.get_code().get_linenumbertable()
        if lnt:
            print("  LineNumberTable:")
            for o, l in lnt:
                print("   line %i: %i" % (l, o))

    if options.locals and code:
        if method.cpool:
            cval = method.cpool.deref_const
        else:
            cval = str

        lvt = method.get_code().get_localvariabletable()
        lvtt = method.get_code().get_localvariabletypetable()

        if lvt:
            print("  LocalVariableTable:")
            print("   Start  Length  Slot\tName\tDescriptor")
            for o, l, n, d, i in lvt:
                line = (str(o), str(l), str(i), cval(n), cval(d))
                print("   %s" % "\t".join(line))

        if lvtt:
            print("  LocalVariableTypeTable:")
            print("   Start  Length  Slot\tName\tSignature")
            for o, l, n, s, i in lvtt:
                line = (str(o), str(l), str(i), cval(n), cval(s))
                print("   %s" % "\t".join(line))

    if options.verbose:
        exps = method.pretty_exceptions()
        if exps:
            print("  Exceptions:")
            for e in exps:
                print("   throws", e)

        print()
Ejemplo n.º 4
0
def print_method(options, method):
    if options.indent:
        print("   ", end="")

    print("%s;" % method.pretty_descriptor())

    if options.sigs:
        print("  Signature:", method.get_signature())

    if method.get_annotations():
        print("  RuntimeVisibleAnnotations:")
        index = 0
        for anno in method.get_annotations():
            print("  %i: %s" % (index, anno.pretty_annotation()))

    code = method.get_code()
    if options.disassemble and code:

        print("  Code:")

        if options.verbose:
            # the arg count is the number of arguments consumed from
            # the stack when this method is called. non-static methods
            # implicitly have a "this" argument that's not in the
            # descriptor
            argsc = len(method.get_arg_type_descriptors())
            if not method.is_static():
                argsc += 1

            print("   Stack=%i, Locals=%i, Args_size=%i" %
                  (code.max_stack, code.max_locals, argsc))

        for line in code.disassemble():
            opname = opcodes.get_opname_by_code(line[1])
            args = line[2]
            if args:
                args = ", ".join(str(arg) for arg in args)
                print("   %i:\t%s\t%s" % (line[0], opname, args))
            else:
                print("   %i:\t%s" % (line[0], opname))

        exps = code.exceptions
        if exps:
            print("  Exception table:")
            print("   from\tto\ttarget\ttype")
            for e in exps:
                ctype = e.pretty_catch_type()
                print("  % 4i\t% 4i\t% 4i\t%s" %
                      (e.start_pc, e.end_pc, e.handler_pc, ctype))

    if options.verbose:
        if method.is_deprecated():
            print("  Deprecated: true")

        if method.is_synthetic():
            print("  Synthetic: true")

        if method.is_bridge():
            print("  Bridge: true")

        if method.is_varargs():
            print("  Varargs: true")

    if options.lines and code:
        lnt = method.get_code().get_linenumbertable()
        if lnt:
            print("  LineNumberTable:")
            for o, l in lnt:
                print("   line %i: %i" % (l, o))

    if options.locals and code:
        if method.cpool:
            cval = method.cpool.deref_const
        else:
            cval = str

        lvt = method.get_code().get_localvariabletable()
        lvtt = method.get_code().get_localvariabletypetable()

        if lvt:
            print("  LocalVariableTable:")
            print("   Start  Length  Slot\tName\tDescriptor")
            for o, l, n, d, i in lvt:
                line = (str(o), str(l), str(i), cval(n), cval(d))
                print("   %s" % "\t".join(line))

        if lvtt:
            print("  LocalVariableTypeTable:")
            print("   Start  Length  Slot\tName\tSignature")
            for o, l, n, s, i in lvtt:
                line = (str(o), str(l), str(i), cval(n), cval(s))
                print("   %s" % "\t".join(line))

    if options.verbose:
        exps = method.pretty_exceptions()
        if exps:
            print("  Exceptions:")
            for e in exps:
                print("   throws", e)

        print()
Ejemplo n.º 5
0
    def handle_method(self, method):
        code = method.get_code()
        if code is None:
            return

        m = None
        for ann in method.get_invisible_annotations():
            t = ann.pretty_type().split('.')[-1]
            if t in ['replace', 'prepend']:
                m = Method(method, t)
                break
        else:
            return

        cpool = code.cpool
        for offset, opcode, args in code.disassemble():
            name = opcodes.get_opname_by_code(opcode).upper()
            if m.action_type != 'replace' and 'RETURN' in name:
                continue
            if name in ['NOP', 'ACONST_NULL', 'ICONST_M1', 'ICONST_0',
                    'ICONST_1', 'ICONST_2', 'ICONST_3', 'ICONST_4', 'ICONST_5',
                    'LCONST_0', 'LCONST_1', 'FCONST_0', 'FCONST_1', 'FCONST_2',
                    'DCONST_0', 'DCONST_1', 'IALOAD', 'LALOAD', 'FALOAD',
                    'DALOAD', 'AALOAD', 'BALOAD', 'CALOAD', 'SALOAD', 'IASTORE',
                    'LASTORE', 'FASTORE', 'DASTORE', 'AASTORE', 'BASTORE',
                    'CASTORE', 'SASTORE', 'POP', 'POP2', 'DUP', 'DUP_X1',
                    'DUP_X2', 'DUP2', 'DUP2_X1', 'DUP2_X2', 'SWAP', 'IADD',
                    'LADD', 'FADD', 'DADD', 'ISUB', 'LSUB', 'FSUB', 'DSUB',
                    'IMUL', 'LMUL', 'FMUL', 'DMUL', 'IDIV', 'LDIV', 'FDIV',
                    'DDIV', 'IREM', 'LREM', 'FREM', 'DREM', 'INEG', 'LNEG',
                    'FNEG', 'DNEG', 'ISHL', 'LSHL', 'ISHR', 'LSHR', 'IUSHR',
                    'LUSHR', 'IAND', 'LAND', 'IOR', 'LOR', 'IXOR', 'LXOR',
                    'I2L', 'I2F', 'I2D', 'L2I', 'L2F', 'L2D', 'F2I', 'F2L',
                    'F2D', 'D2I', 'D2L', 'D2F', 'I2B', 'I2C', 'I2S', 'LCMP',
                    'FCMPL', 'FCMPG', 'DCMPL', 'DCMPG', 'IRETURN', 'LRETURN',
                    'FRETURN', 'DRETURN', 'ARETURN', 'RETURN', 'ARRAYLENGTH',
                    'ATHROW', 'MONITORENTER', 'MONITOREXIT']:
                m.code.append('visitInsn(Opcodes.%s)' % name)
            elif name in ['BIPUSH', 'SIPUSH']:
                m.code.append('visitIntInsn(Opcodes.%s, %d)' % args[0])
            elif 'LDC' in name:
                t, v = cpool.get_const(args[0])
                const = cpool.deref_const(args[0])
                if t == javatools.CONST_String:
                    m.code.append('visitLdcInsn("%s")' % const)
                elif t == javatools.CONST_Long:
                    m.code.append('visitLdcInsn(%sL)' % const)
                elif t == javatools.CONST_Double:
                    m.code.append('visitLdcInsn(%sd)' % const)
                else: # TODO
                    m.code.append('visitLdcInsn(%s)' % const)
            elif name[1:-2] in ['LOAD', 'STORE']:
                m.code.append('visitVarInsn(Opcodes.%s, %s)' % (name[:-2], name[-1]))
            elif name in ['GETFIELD', 'PUTFIELD', 'GETSTATIC', 'PUTSTATIC']:
                field = cpool.deref_const(args[0])
                m.code.append('visitFieldInsn(Opcodes.%s, "%s", "%s", "%s")' % (name,
                    field[0], field[1][0], field[1][1]))
            elif 'INVOKE' in name:
                target = cpool.deref_const(args[0])
                m.code.append('visitMethodInsn(Opcodes.%s, "%s", "%s", "%s")' % (name,
                    target[0], target[1][0], target[1][1]))
            elif name in ['NEW', 'ANEWARRAY', 'CHECKCAST', 'INSTANCEOF']:
                t = cpool.deref_const(args[0])
                m.code.append('visitTypeInsn(Opcodes.%s, "%s")' % (name, t))
            else:
                raise Exception('unhandled opcode ' + name)

        return m