Skip to content

Commit

Permalink
refactor: catch Error instead of Exception in `DispatchTransforme…
Browse files Browse the repository at this point in the history
…r`; add more log
  • Loading branch information
oldratlee committed Dec 19, 2023
1 parent 2eccbf6 commit fdf4ac6
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void premain(final String agentArgs, @NonNull final Instrumentatio
System.out.println("[" + NAME + "Agent] Enter premain!");

if (System.getenv().containsKey("STUDY_HELLO_AGENT_THROW_EXCEPTION")) {
throw new IllegalStateException("throw exception for jvm start failure test by setting HELLO_AGENT_THROW_EXCEPTION env var!");
throw new IllegalStateException("throw exception for jvm start failure test by setting STUDY_HELLO_AGENT_THROW_EXCEPTION env var!");
}

logLoadedClasses(NAME, inst);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum StudyAgentMode {
public static StudyAgentMode getStudyAgentMode() {
final String envVarName = "STUDY_AGENT_RUN_MODE";
final String value = System.getenv(envVarName);
if (value == null || "".equals(value)) {
if (value == null) {
return NO_AGENTS;
} else if ("hello-and-world-agents".equals(value)) {
return HELLO_AND_WORLD_AGENTS;
Expand Down
2 changes: 1 addition & 1 deletion scripts/integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ for jdk_version in "${JDK_VERSIONS[@]}"; do
mvu::mvn_cmd dependency:properties exec:exec -pl main-runner 2>&1 |
tee "$tmp_output_file" &&
cu::die "jvm should fail if throw exception in agent premain!"
cu::log_then_run grep "IllegalStateException.*throw exception for jvm start failure test by setting HELLO_AGENT_THROW_EXCEPTION env var!" "$tmp_output_file" ||
cu::log_then_run grep "IllegalStateException.*throw exception for jvm start failure test by setting STUDY_HELLO_AGENT_THROW_EXCEPTION env var!" "$tmp_output_file" ||
cu::die "should contains exception message!"

cu::head_line_echo run Main under hello and world agents
Expand Down
17 changes: 12 additions & 5 deletions utils/src/main/java/io/foldright/study/agent/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ public static boolean isClassLoadedByClassLoader(@NonNull final Instrumentation

@NonNull
private static Map<String, Map<ClassLoader, Set<Class<?>>>> getLoadedClasses(@NonNull Instrumentation inst) {
return Arrays.stream((Class<?>[]) inst.getAllLoadedClasses())
.collect(groupingBy(Class::getName, groupingBy(clazz -> {
ClassLoader classLoader = clazz.getClassLoader();
return classLoader == null ? NULL_CLASS_LOADER : classLoader;
}, toSet())));
return Arrays.stream((Class<?>[]) inst.getAllLoadedClasses()).collect(
groupingBy(
Class::getName,
groupingBy(
clazz -> {
ClassLoader classLoader = clazz.getClassLoader();
return classLoader == null ? NULL_CLASS_LOADER : classLoader;
},
toSet()
)
)
);
}

@SuppressWarnings("removal")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public byte[] transform(@Nullable final ClassLoader loader, @Nullable final Stri
final String className = classFileToName(classFile);
final Transformlet tr = transformlets.get(className);
if (tr == null) return NO_TRANSFORM;
return tr.transform(loader, classFile, classBeingRedefined, protectionDomain, classFileBuffer);
} catch (Exception e) {
return tr.transform(loader, className, classBeingRedefined, protectionDomain, classFileBuffer);
} catch (Throwable e) {
logThrowable(e);
throw new IllegalStateException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public ThreadPoolExecutorTransformlet(String name) {

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws Exception {
log("transforming " + className);

final ClassInfo classInfo = new ClassInfo(THREAD_POOL_EXECUTOR_CLASS_NAME, classFileBuffer, loader);

// HACK toString method
Expand Down

0 comments on commit fdf4ac6

Please sign in to comment.