Skip to content

Commit 591ce68

Browse files
More debugging stuff
1 parent 35940c5 commit 591ce68

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

newrelic-agent/src/main/java/com/newrelic/agent/instrumentation/weaver/ClassLoaderClassTransformer.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@ public byte[] transform(ClassLoader loader, String className, Class<?> classBein
292292
String superName = reader.getClassName().equals("java/lang/ClassLoader") ? reader.getClassName() : reader.getSuperName();
293293
if (observedClassLoaders.containsKey(superName) ||
294294
classloadersToInclude.contains(reader.getClassName()) || classloadersToInclude.contains(superName)) {
295-
return transform(loader, className, classBeingRedefined, protectionDomain, classfileBuffer, null, null);
295+
byte[] finalClassBytes = transform(loader, className, classBeingRedefined, protectionDomain, classfileBuffer, null, null);
296+
//WeaveUtils.createReadableClassFileFromByteArray(finalClassBytes, true, className, "ClassLoader", "/Users/katherineanderson/Downloads");
297+
return finalClassBytes;
296298
}
297299
return null;
298300
}
@@ -322,6 +324,12 @@ public byte[] transform(ClassLoader loader, String className, Class<?> classBein
322324
// This ClassCache will only consult the map of the observedClassLoaders that we pass in, so
323325
// we don't need to worry about it possibly using findResource() when we call validate(cache).
324326
ClassCache cache = new ClassCache(new ClassLoaderClassFinder(observedClassLoaders));
327+
328+
//DEBUG TIME: how does the base classloader fare???
329+
if(className.equals("java/lang/ClassLoader")){
330+
WeaveUtils.forceVisitationOfClassFile(classfileBuffer, cache);
331+
}
332+
325333
PackageValidationResult result;
326334
if (className.equals("java/lang/ClassLoader")) {
327335
// For "java.lang.ClassLoader" we only want to instrument one of the loadClass() methods

newrelic-weaver/src/main/java/com/newrelic/weave/ClassWeave.java

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public static ClassWeave weave(PreparedMatch match,
7272
}
7373

7474
private void weave() {
75+
WeaveUtils.createReadableClassFileFromClassNode(target, false, target.name, "ClassLoader", "/Users/katherineanderson/Downloads");
7576
weavedMethods.clear();
7677
// copy target to composite without any jsr instructions
7778
target.accept(new ClassVisitor(WeaveUtils.ASM_API_LEVEL, composite) {

newrelic-weaver/src/main/java/com/newrelic/weave/utils/PatchedClassWriter.java

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* the class structure without actually loading any classes.
2222
*/
2323
class PatchedClassWriter extends ClassWriter {
24+
private final String THROWABLE_NAME = "java/lang/Throwable";
25+
private final String EXCEPTION_NAME = "java/lang/Exception";
2426
private final ClassInformationFinder classInfoFinder;
2527

2628
PatchedClassWriter(int flags, ClassInformationFinder classInfoFinder) {
@@ -55,6 +57,9 @@ class PatchedClassWriter extends ClassWriter {
5557

5658
@Override
5759
protected String getCommonSuperClass(final String type1, final String type2) {
60+
if ((type1.equals(THROWABLE_NAME) && type2.equals(EXCEPTION_NAME)) || (type2.equals(THROWABLE_NAME) && type1.equals(EXCEPTION_NAME))){
61+
System.out.println("Comparing an Exception and a Throwable.");
62+
}
5863
if (type1.equals(type2)) {
5964
return type1;
6065
}
@@ -105,6 +110,9 @@ protected String getCommonSuperClass(final String type1, final String type2) {
105110
}
106111

107112
if (null == commonSuperType) {
113+
if ((type1.equals(THROWABLE_NAME) && type2.equals(EXCEPTION_NAME)) || (type2.equals(THROWABLE_NAME) && type1.equals(EXCEPTION_NAME))) {
114+
return THROWABLE_NAME;
115+
}
108116
return WeaveUtils.JAVA_LANG_OBJECT_NAME;
109117
}
110118

newrelic-weaver/src/main/java/com/newrelic/weave/utils/WeaveUtils.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,12 @@ public static void printClassFrames(byte [] classBytes) {
603603

604604
//TEMPORARY
605605

606+
public static void forceVisitationOfClassFile(byte[] classBytes, ClassInformationFinder classInfoFinder) {
607+
ClassReader reader = new ClassReader(classBytes);
608+
ClassWriter cw = new PatchedClassWriter(ClassWriter.COMPUTE_FRAMES, classInfoFinder);
609+
reader.accept(cw, ClassReader.EXPAND_FRAMES);
610+
}
611+
606612
public static void getInstructionAtBCI(int bci, byte [] classBytes) {
607613
int bciMargin = 3;
608614
ClassReader reader = new ClassReader(classBytes);
@@ -629,21 +635,22 @@ public static void getInstructionAtBCI(int bci, byte [] classBytes) {
629635
}
630636
}
631637

632-
public static void createReadableClassFileFromClassNode(ClassNode cn, String originalName, String targetName, String destDir) {
638+
public static void createReadableClassFileFromClassNode(ClassNode cn, boolean isNew, String originalName, String targetName, String destDir) {
633639
if (targetName == null || originalName.contains(targetName)) {
634640
System.out.println("Weaved composite ClassLoader");
635641
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
636642
cn.accept(cw);
637643
byte[] classBytes = cw.toByteArray();
638-
createReadableClassFileFromByteArray(classBytes, originalName, null, destDir);
644+
createReadableClassFileFromByteArray(classBytes, isNew, originalName, null, destDir);
639645
}
640646
}
641647

642-
public static void createReadableClassFileFromByteArray(byte[] classBytes, String originalName, String targetName, String destDir){
648+
public static void createReadableClassFileFromByteArray(byte[] classBytes, boolean isNew, String originalName, String targetName, String destDir){
649+
String suffix = isNew ? ".new" : ".old";
643650
if (targetName == null || originalName.contains(targetName)) {
644651
final File MY_DIRECTORY = new File(destDir);
645652
try {
646-
File newFile = File.createTempFile(originalName.replace('/', '_'), ".new", MY_DIRECTORY);
653+
File newFile = File.createTempFile(originalName.replace('/', '_'), suffix, MY_DIRECTORY);
647654
PrintWriter pw = new PrintWriter(newFile);
648655
ClassReader cr = new ClassReader(classBytes);
649656
org.objectweb.asm.util.TraceClassVisitor mv = new org.objectweb.asm.util.TraceClassVisitor(pw);

0 commit comments

Comments
 (0)