Giter Club home page Giter Club logo

asm-method-call-log's Introduction

ASM Method Call Log

Task


Use the ASM library for Java to print the name of each method when it's executed.

Usage


sh printmethods.sh NAME

where NAME refers to the name (without the extension) of the .java file we would like to use the tool on.

The script compiles the NAME.java file, compiles MethodCallLog, runs it on NAME.class to modify it such that methods print their name at the start of their execution, and finally runs the modified NAME.class .

Note that as it stands, the script is configured to use the ASM 6.1.1 jar file.

Method


I first ran the test Java class through the ASMifier to see what it looked like in ASM format. Here's a snippet showing what the methods looked like.

        {
            mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
            mv.visitCode();
            mv.visitMethodInsn(INVOKESTATIC, "C", "m", "()V", false);
            mv.visitMethodInsn(INVOKESTATIC, "C", "n", "()V", false);
            mv.visitMethodInsn(INVOKESTATIC, "C", "m", "()V", false);
            mv.visitMethodInsn(INVOKESTATIC, "C", "m", "()V", false);
            mv.visitInsn(RETURN);
            mv.visitMaxs(0, 1);
            mv.visitEnd();
        }
        {
            mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "m", "()V", null, null);
            mv.visitCode();
            mv.visitInsn(RETURN);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
        {
            mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "n", "()V", null, null);
            mv.visitCode();
            mv.visitInsn(RETURN);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }

It appeared that the printing code would have to be added right before the MethodVisitor's visitCode() method.

Consequently, I implemented an Adapter for MethodVisitor which overrides the class' visitCode() method to do just that:

class MethodAdapter extends MethodVisitor implements Opcodes {
  
  /** The name of the method receiving the visit 
   */
  final String name;

  public MethodAdapter(final MethodVisitor mv, final String _name) {
    super(ASM6, mv);
    name = _name;
  }

  @Override
  public void visitCode() {
    // Print the method's name : System.out.println(name) in asm format
    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    mv.visitLdcInsn( name );
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
    
    mv.visitCode();
  }
}

where I got the ASM format for System.out.println(name) by running it through the ASMifier:

 mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
 mv.visitLdcInsn("main");
 mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);

Finally, to make sure that MethodAdapter is used, I implemented an adapter for ClassVisitor called ClassAdapter which returns a MethodAdapter when visitMethod(...) is called, as shown in the ASM code above.

class ClassAdapter extends ClassVisitor implements Opcodes {
  
  public ClassAdapter(final ClassVisitor cv) {
    super(ASM6, cv);
  }

  @Override
  public MethodVisitor visitMethod(final int access, final String name,
          final String desc, final String signature, final String[] exceptions) {
    MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
    return mv == null ? null : new MethodAdapter(mv, name);
  }
}

The modification of a target java class happens in the MethodCallLog class shown below:

public class MethodCallLog {

  public static void main(final String args[]) throws Exception {
    FileInputStream is = new FileInputStream(args[0]); // Open .class file taken as the first argument
    
    ClassReader cr = new ClassReader(is); 
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    ClassAdapter ca = new ClassAdapter(cw);
    cr.accept(ca, 0);

    FileOutputStream fos = new FileOutputStream(args[1]); // Write to .class file name taken as the second argument
    fos.write(cw.toByteArray());
    fos.close();
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.