diff --git a/JavassistTest/src/tracer/Tracer.java b/JavassistTest/src/tracer/Tracer.java index 9ae1bc5..c63132b 100644 --- a/JavassistTest/src/tracer/Tracer.java +++ b/JavassistTest/src/tracer/Tracer.java @@ -55,21 +55,57 @@ CtClass cc; try { cc = cp.get(className); - for (CtMethod m : cc.getDeclaredMethods()) { + for (final CtMethod m : cc.getDeclaredMethods()) { ControlFlow cf = new ControlFlow(m); - String declaredClassName = cc.getName(); + final String declaredClassName = cc.getName(); String longName = m.getReturnType().getName() + " " + m.getLongName(); String shortName = cc.getSimpleName() + "." + m.getName() + "()"; String methodSignature = m.getSignature(); + final String lineAndThread = "\":Line 0\" + \":ThreadNo \" + Thread.currentThread().getId()"; m.instrument(new ExprEditor() { public void edit(FieldAccess f) throws CannotCompileException { if (f.isReader()) { - f.replace( - "{$_ = $proceed(); System.out.println(\"FieldGet \" + System.identityHashCode($0) + \",\" + System.identityHashCode($_));}"); + String thisOutput = ""; + if ((m.getModifiers() & Modifier.STATIC) == 0) { + thisOutput = "System.out.print(\"get:\" + this.getClass().getName() + \":\" + System.identityHashCode(this) + \":\"); "; + } else { + thisOutput = "System.out.print(\"get:" + declaredClassName + ":0:\"); "; + } + f.replace("{$_ = $proceed(); " + + thisOutput + + "if ($0 != null) {" + // target + "System.out.print($0.getClass().getName() + \":\" + System.identityHashCode($0) + \":\"); " + + "} else {" + + "System.out.print(\"---:0:\"); " + + "} " + + "if ($_ != null) {" + // o + "System.out.println($_.getClass().getName() + \":\" + System.identityHashCode($_) + " + lineAndThread + ");" + + "} else {" + + "System.out.println(\"---:0\" + " + lineAndThread + ");" + + "} " + "}"); +// } else { +// // static�ϐ������get +// f.replace("{System.out.print(\"get:" + declaredClassName + ":0:\"); " + +// "if ($_ != null) {" + +// "System.out.println($_.getClass().getName() + \":\" + System.identityHashCode($_) + " + lineAndThread + ");" + +// "} else {" + +// "System.out.println(\"---:0\" + " + lineAndThread + ");" + +// "} " + +// "$_ = $proceed();}"); +// } } else { - f.replace( - "$proceed($$); System.out.println(\"FieldSet \" + System.identityHashCode($0) + \",\" + System.identityHashCode($1));"); + f.replace("{$proceed($$); " + + "if ($0 != null) {" + // target + "System.out.print(\"set:\" + $0.getClass().getName() + \":\" + System.identityHashCode($0) + \":\"); " + + "} else {" + + "System.out.print(\"set:" + declaredClassName + ":0:\"); " + + "} " + + "if ($1 != null) {" + // o[0] + "System.out.println($1.getClass().getName() + \":\" + System.identityHashCode($1) + " + lineAndThread + ");" + + "} else {" + + "System.out.println(\"---:0\" + " + lineAndThread + ");" + + "}" + "}"); } } }); @@ -77,39 +113,49 @@ String methodOutput = ""; String classOutput = ""; String argsOutput = ""; + String returnOutput = ""; String delimiter = "System.out.println(\"Args:\" + "; CtClass parameterClasses[] = m.getParameterTypes(); int p = 0; for (CtClass c : parameterClasses) { - p++; if (!c.isPrimitive()) { - argsOutput += delimiter + "$args[" + (p - 1) + "].getClass().getName() + " + "\":\" + System.identityHashCode($" + p + ")"; + argsOutput += delimiter + "$args[" + p + "].getClass().getName() + " + "\":\" + System.identityHashCode($" + (p + 1) + ")"; } else { - argsOutput += delimiter + "$args[" + (p - 1) + "].getClass().getName() + " + "\":\" + $" + p; + argsOutput += delimiter + "$args[" + p + "].getClass().getName() + " + "\":\" + $" + (p + 1); } + p++; delimiter = " + \":\" + "; } if (p > 0) { - argsOutput += " + \":Line 0:\" + \":ThreadNo \" + Thread.currentThread().getId());"; + argsOutput += " + \":Line 0:\" + \"ThreadNo \" + Thread.currentThread().getId());"; } if ((m.getModifiers() & Modifier.STATIC) == 0) { methodOutput = "System.out.println(\"Method \" + this.getClass().getName() + \"," + longName + ":\" + System.identityHashCode(this) + \":Line 0:\" + System.nanoTime() + \":ThreadNo \" + Thread.currentThread().getId());"; - classOutput = "System.out.println(\"Class \" + this.getClass().getName() + \":\" + System.identityHashCode(this) + \":Line 0\" + \":ThreadNo \" + Thread.currentThread().getId());"; + classOutput = "System.out.println(\"Class \" + this.getClass().getName() + \":\" + System.identityHashCode(this) + " + lineAndThread + ");"; } else { longName = "static " + longName; methodOutput = "System.out.println(\"Method " + declaredClassName + "," + longName + ":\" + 0 + \":Line 0:\" + System.nanoTime() + \":ThreadNo \" + Thread.currentThread().getId());"; - classOutput = "System.out.println(\"Class " + declaredClassName + ":\" + 0 + \":Line 0\" + \":ThreadNo \" + Thread.currentThread().getId());"; + classOutput = "System.out.println(\"Class " + declaredClassName + ":\" + 0 + " + lineAndThread + ");"; } + + String thisOutput = ""; + if ((m.getModifiers() & Modifier.STATIC) == 0) { + thisOutput = "System.identityHashCode(this)"; + } else { + thisOutput = "\"0\""; + } + returnOutput = "if ($_ != null) {" + + "System.out.print(\"Return execution(" + shortName + "):\" + $_.getClass().getName() + \":\" + System.identityHashCode($_) + \":\");" + + "} else {" + + "System.out.print(\"Return execution(" + shortName + "):void:0:\");" + + "} " + + "System.out.println(\"\" + " + thisOutput + " + " + lineAndThread + ");"; m.insertBefore("{" + methodOutput + classOutput + argsOutput + "}"); - m.insertAfter("if ($_ != null) {" - + "System.out.println(\"Return execution(" + shortName + "):\" + $_.getClass().getName() + \":\" + System.identityHashCode($_) + \":Line 0\" + \":ThreadNo \" + Thread.currentThread().getId());" - + "} else {" - + "System.out.println(\"Return execution(" + shortName + "):void:0\" + \":Line 0\" + \":ThreadNo \" + Thread.currentThread().getId());" - + "}"); + m.insertAfter("{" + returnOutput + "}"); // CodeConverter conv = new CodeConverter(); // conv.replaceArrayAccess(cc, new // ArrayAccessReplacementMethodNames() {