Skip to content

Commit

Permalink
refactor(SimpleTracer,FastTracer): introduce AbstractTracer
Browse files Browse the repository at this point in the history
  • Loading branch information
nharrand committed Mar 1, 2019
1 parent 1007b8e commit bc7ecb4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 127 deletions.
10 changes: 0 additions & 10 deletions src/main/java/fr/inria/align/treediff/FastRemoteReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@

public class FastRemoteReader {

/*public static void main(String arg[]) throws InterruptedException {
FastRemoteReader r = new FastRemoteReader();
r.logger = FastLogger.getInstance();
File dd = new File("/home/nharrand/Documents/helloworld");
r.dir = new File(dd,"d2");
r.logger.setLogFile(new File("out.json"));
r.read();
r.logger.flush();
}*/

public FastRemoteReader(FastTracking logger, File inTraceDir) {
this.logger = logger;
this.dir= inTraceDir;
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/fr/inria/yajta/api/AbstractTracer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package fr.inria.yajta.api;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtBehavior;
import javassist.CtClass;
import javassist.NotFoundException;

import java.net.URL;

public abstract class AbstractTracer {

public boolean verbose = false;
ClassList cl;
ClassPool pool = ClassPool.getDefault();

public byte[] transform( final ClassLoader loader, final String className, final Class clazz,
final java.security.ProtectionDomain domain, final byte[] bytes ) {
//if(verbose) System.out.println("className: " + className + " ? ");
URL classURL = loader.getResource(className + ".class");
String classFilePath = classURL == null ? null : classURL.getFile().replace("file:","");
if(classFilePath == null || !cl.isInJars(classFilePath)) return bytes;
if(verbose) System.out.println("className: " + className + " -> " + cl.isToBeProcessed(className));
if( cl.isToBeProcessed(className) ) {
return doClass( className, clazz, bytes );
} else {
return bytes;
}
}

public byte[] doClass( final String name, final Class clazz, byte[] b ) {
CtClass cl = null;
try {
cl = pool.makeClass( new java.io.ByteArrayInputStream( b ) );
if( cl.isInterface() == false ) {

doClass(cl,name);

b = cl.toBytecode();

if(verbose) System.err.println( "-> Instrument " + name);
}
} catch( Exception e ) {
if(verbose) System.err.println( "Could not instrument " + name + ", exception : " + e.getMessage() );
} finally {

if( cl != null ) {
cl.detach();
}
}

return b;
}

public void doClass(CtClass cl, String name) throws NotFoundException, CannotCompileException {
CtBehavior[] methods = cl.getDeclaredBehaviors();

for( int i = 0; i < methods.length; i++ ) {

if( methods[i].isEmpty() == false ) {
doMethod( methods[i] , name);
}
}
}

private void doMethod( final CtBehavior method , String className) throws NotFoundException, CannotCompileException {
doMethod(method,className,false,null);
}

abstract void doMethod( final CtBehavior method , String className, boolean isIsotope, String isotope) throws NotFoundException, CannotCompileException;
}
60 changes: 2 additions & 58 deletions src/main/java/fr/inria/yajta/api/FastTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@
import java.lang.reflect.Method;
import java.net.URL;

public class FastTracer implements TracerI {
public class FastTracer extends AbstractTracer implements TracerI {

public boolean verbose = false;
public boolean strictIncludes = false;
ClassList cl;

String loggerInstance;
FastTracking realLoggerInstance;
boolean logValue = false;
boolean logBranch = false;
ClassPool pool = ClassPool.getDefault();


public FastTracer (ClassList cl) {
Expand Down Expand Up @@ -70,44 +67,6 @@ public boolean implementsInterface(Class cl, Class interf) {
return false;
}

public byte[] transform( final ClassLoader loader, final String className, final Class clazz,
final java.security.ProtectionDomain domain, final byte[] bytes ) {
//if(verbose) System.out.println("className: " + className + " ? ");
URL classURL = loader.getResource(className + ".class");
String classFilePath = classURL == null ? null : classURL.getFile().replace("file:","");
if(classFilePath == null || !cl.isInJars(classFilePath)) return bytes;
if(verbose) System.out.println("className: " + className + " -> " + cl.isToBeProcessed(className));
if( cl.isToBeProcessed(className) ) {
return doClass( className, clazz, bytes );
} else {
return bytes;
}
}

public byte[] doClass( final String name, final Class clazz, byte[] b ) {
CtClass cl = null;
try {
cl = pool.makeClass( new java.io.ByteArrayInputStream( b ) );
if( cl.isInterface() == false ) {

doClass(cl,name);

b = cl.toBytecode();

if(verbose) System.err.println( "-> Instrument " + name);
}
} catch( Exception e ) {
if(verbose) System.err.println( "Could not instrument " + name + ", exception : " + e.getMessage() );
} finally {

if( cl != null ) {
cl.detach();
}
}

return b;
}

@Override
public void setTrackingClass(Class<? extends Tracking> trackingClass) throws MalformedTrackingClassException {
throw new UnsupportedOperationException("FastTracer only supports TracingInstance that implements FastTracking");
Expand Down Expand Up @@ -154,21 +113,6 @@ public void setTrackingClass(Class<? extends FastTracking> trackingClass, FastTr
this.realLoggerInstance = realLoggerInstance;
}

public void doClass(CtClass cl, String name) throws NotFoundException, CannotCompileException {
CtBehavior[] methods = cl.getDeclaredBehaviors();

for( int i = 0; i < methods.length; i++ ) {

if( methods[i].isEmpty() == false ) {
doMethod( methods[i] , name);
}
}
}

private void doMethod( final CtBehavior method , String className) throws NotFoundException, CannotCompileException {
doMethod(method,className,false,null);
}

private Bytecode getBytecode(String print, CtClass cc) throws CompileError {
Javac jv = new Javac(cc);
jv.compileStmnt(print);
Expand Down Expand Up @@ -209,7 +153,7 @@ private boolean isBlockEmpty(CodeIterator iterator, int begin, int end) {
return true;
}

private void doMethod( final CtBehavior method , String className, boolean isIsotope, String isotope) throws NotFoundException, CannotCompileException {
protected void doMethod( final CtBehavior method , String className, boolean isIsotope, String isotope) throws NotFoundException, CannotCompileException {
if(!Modifier.isNative(method.getModifiers())) {
if(verbose) System.err.println("[Vanilla] " + className + " " + method.getName());
String params = "(";
Expand Down
1 change: 1 addition & 0 deletions src/main/java/fr/inria/yajta/api/FastTracking.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.inria.yajta.api;

import java.io.File;
import java.lang.invoke.MethodHandles;

/**
* This interface can be extended by the user to create new logging structure.
Expand Down
61 changes: 2 additions & 59 deletions src/main/java/fr/inria/yajta/api/SimpleTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@
import java.net.URL;
import java.util.Arrays;

//public class SimpleTracer implements ClassFileTransformer {
public class SimpleTracer implements TracerI {
public class SimpleTracer extends AbstractTracer implements TracerI {

public boolean verbose = true;
public boolean strictIncludes = false;
ClassList cl;

String loggerInstance;
boolean logValue = false;
boolean logBranch = false;
ClassPool pool = ClassPool.getDefault();


public SimpleTracer (ClassList cl) {
Expand Down Expand Up @@ -121,59 +117,6 @@ public void setFastTrackingClass(Class<? extends FastTracking> trackingClass) th
throw new UnsupportedOperationException("SimpleTracer only supports TracingInstance that implements ValueTracking or Tracking");
}

public byte[] transform( final ClassLoader loader, final String className, final Class clazz,
final java.security.ProtectionDomain domain, final byte[] bytes ) {
if(verbose) System.out.println("className: " + className + " ? ");
URL classURL = loader.getResource(className + ".class");
String classFilePath = classURL == null ? null : classURL.getFile().replace("file:","");
if(classFilePath == null || !cl.isInJars(classFilePath)) return bytes;
if(verbose) System.out.println("className: " + className + " -> " + cl.isToBeProcessed(className));
if( cl.isToBeProcessed(className) ) {
return doClass( className, clazz, bytes );
} else {
return bytes;
}
}

public byte[] doClass( final String name, final Class clazz, byte[] b ) {
CtClass cl = null;
try {
cl = pool.makeClass( new java.io.ByteArrayInputStream( b ) );
if( cl.isInterface() == false ) {

doClass(cl,name);

b = cl.toBytecode();

if(verbose) System.err.println( "-> Instrument " + name);
}
} catch( Exception e ) {
if(verbose) System.err.println( "Could not instrument " + name + ", exception : " + e.getMessage() );
} finally {

if( cl != null ) {
cl.detach();
}
}

return b;
}

public void doClass(CtClass cl, String name) throws NotFoundException, CannotCompileException {
CtBehavior[] methods = cl.getDeclaredBehaviors();

for( int i = 0; i < methods.length; i++ ) {

if( methods[i].isEmpty() == false ) {
doMethod( methods[i] , name);
}
}
}

private void doMethod( final CtBehavior method , String className) throws NotFoundException, CannotCompileException {
doMethod(method,className,false,null);
}

private Bytecode getBytecode(String print, CtClass cc) throws CompileError {
Javac jv = new Javac(cc);
jv.compileStmnt(print);
Expand Down Expand Up @@ -214,7 +157,7 @@ private boolean isBlockEmpty(CodeIterator iterator, int begin, int end) {
return true;
}

private void doMethod( final CtBehavior method , String className, boolean isIsotope, String isotope) throws NotFoundException, CannotCompileException {
protected void doMethod( final CtBehavior method , String className, boolean isIsotope, String isotope) throws NotFoundException, CannotCompileException {
//System.out.println("\t\tMethod: " + method.getLongName() + " -> " + !Modifier.isNative(method.getModifiers()));
if(!Modifier.isNative(method.getModifiers())) {
if(verbose) System.err.println("[Vanilla] " + className + " " + method.getName());
Expand Down

0 comments on commit bc7ecb4

Please sign in to comment.