-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject coverage store field into Thread class
- Loading branch information
1 parent
0584623
commit 50a5b2e
Showing
28 changed files
with
507 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...in/java/datadog/trace/civisibility/coverage/instrumentation/store/CoreJvmClassReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package datadog.trace.civisibility.coverage.instrumentation.store; | ||
|
||
import datadog.communication.util.IOThrowingFunction; | ||
import datadog.trace.civisibility.config.JvmInfo; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Path; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
|
||
public class CoreJvmClassReader { | ||
|
||
private static final String JAVA_BASE_MODULE_RELATIVE_PATH = "jmods/java.base.jmod"; | ||
|
||
// java.home contains the "/jre" segment for Java 8 distributions | ||
private static final String RT_JAR_RELATIVE_PATH = "lib/rt.jar"; | ||
|
||
/** | ||
* Performs an action on the bytecode of a core JVM class | ||
* | ||
* @param jvm The JVM distribution | ||
* @param className The name of the class (as returned by {@link Class#getName}, e.g. {@code | ||
* java.lang.Thread}) | ||
* @param action The action to be performed on the class bytecode stream | ||
* @return The return value of the action | ||
* @param <T> The type of the return value | ||
* @throws IOException If the stream could not be retrieved | ||
*/ | ||
public <T> T withClassStream( | ||
JvmInfo jvm, String className, IOThrowingFunction<InputStream, T> action) throws IOException { | ||
if (jvm.isModular()) { | ||
return withClassStreamModularJdk(jvm.getHome(), className, action); | ||
} else { | ||
return withClassStreamPreModularJdk(jvm.getHome(), className, action); | ||
} | ||
} | ||
|
||
private <T> T withClassStreamModularJdk( | ||
Path jvmHome, String className, IOThrowingFunction<InputStream, T> action) | ||
throws IOException { | ||
Path javaBaseModule = jvmHome.resolve(JAVA_BASE_MODULE_RELATIVE_PATH); | ||
return withZipEntry( | ||
javaBaseModule, "classes/" + className.replace('.', '/') + ".class", action); | ||
} | ||
|
||
private <T> T withClassStreamPreModularJdk( | ||
Path jvmHome, String className, IOThrowingFunction<InputStream, T> action) | ||
throws IOException { | ||
Path rtJar = jvmHome.resolve(RT_JAR_RELATIVE_PATH); | ||
return withZipEntry(rtJar, className.replace('.', '/') + ".class", action); | ||
} | ||
|
||
private static <T> T withZipEntry( | ||
Path zipFilePath, String entryName, IOThrowingFunction<InputStream, T> action) | ||
throws IOException { | ||
try (ZipFile zipFile = new ZipFile(zipFilePath.toFile())) { | ||
ZipEntry entry = zipFile.getEntry(entryName); | ||
if (entry == null) { | ||
throw new IOException("Entry " + entryName + " not found in zip file " + zipFilePath); | ||
} | ||
try (InputStream entryStream = zipFile.getInputStream(entry)) { | ||
return action.apply(entryStream); | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...datadog/trace/civisibility/coverage/instrumentation/store/CoverageStoreFieldInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package datadog.trace.civisibility.coverage.instrumentation.store; | ||
|
||
import static datadog.trace.api.civisibility.coverage.CoverageBridge.COVERAGE_STORE_FIELD_NAME; | ||
|
||
import datadog.trace.api.civisibility.coverage.CoverageBridge; | ||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
|
||
/** | ||
* A {@link ClassVisitor} that injects an instance field named {@link | ||
* CoverageBridge#COVERAGE_STORE_FIELD_NAME} of type {@link Object} into the visited class | ||
*/ | ||
public class CoverageStoreFieldInjector extends ClassVisitor { | ||
|
||
public CoverageStoreFieldInjector(ClassVisitor cv) { | ||
super(Opcodes.ASM9, cv); | ||
} | ||
|
||
@Override | ||
public void visitEnd() { | ||
cv.visitField(Opcodes.ACC_PUBLIC, COVERAGE_STORE_FIELD_NAME, "Ljava/lang/Object;", null, null) | ||
.visitEnd(); | ||
super.visitEnd(); | ||
} | ||
} |
Oops, something went wrong.