-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support agent by attaching it at build time #9668
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1736,10 +1736,15 @@ def cinterfacetutorial(args): | |
@mx.command(suite.name, 'javaagenttest', 'Runs tests for java agent with native image') | ||
def java_agent_test(args): | ||
def build_and_run(args, binary_path, native_image, agents, agents_arg): | ||
test_cp = os.pathsep.join([classpath('com.oracle.svm.test')] + agents) | ||
mx.log('Run agent with JVM as baseline') | ||
test_cp = os.pathsep.join([classpath('com.oracle.svm.test')]) | ||
java_run_cp = os.pathsep.join([test_cp, mx.dependency('org.graalvm.nativeimage').classpath_repr()]) | ||
mx.run_java( agents_arg + ['--add-exports=java.base/jdk.internal.org.objectweb.asm=ALL-UNNAMED', | ||
'-cp', java_run_cp, 'com.oracle.svm.test.javaagent.AgentTest']) | ||
test_cp = os.pathsep.join([test_cp] + agents) | ||
native_agent_premain_options = ['-XXpremain:com.oracle.svm.test.javaagent.agent1.TestJavaAgent1:test.agent1=true', '-XXpremain:com.oracle.svm.test.javaagent.agent2.TestJavaAgent2:test.agent2=true'] | ||
image_args = ['-cp', test_cp, '-J-ea', '-J-esa', '-H:+ReportExceptionStackTraces', '-H:Class=com.oracle.svm.test.javaagent.AgentTest'] | ||
native_image(image_args + svm_experimental_options(['-H:PremainClasses=' + agents_arg]) + ['-o', binary_path] + args) | ||
image_args = ['-cp', test_cp, '-J-ea', '-J-esa', '-H:+ReportExceptionStackTraces', '-J--add-exports=java.base/jdk.internal.org.objectweb.asm=ALL-UNNAMED', '-H:Class=com.oracle.svm.test.javaagent.AgentTest'] | ||
native_image(image_args + svm_experimental_options(agents_arg) + ['-o', binary_path] + args) | ||
mx.run([binary_path] + native_agent_premain_options) | ||
|
||
def build_and_test_java_agent_image(native_image, args): | ||
|
@@ -1756,18 +1761,23 @@ def build_and_test_java_agent_image(native_image, args): | |
# Note: we are not using MX here to avoid polluting the suite.py and requiring extra build flags | ||
mx.log("Building agent jars from " + test_classpath) | ||
agents = [] | ||
for i in range(1, 2): | ||
for i in range(1, 3): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. python's |
||
agent = join(tmp_dir, "testagent%d.jar" % (i)) | ||
agent_test_classpath = join(test_classpath, 'com', 'oracle', 'svm', 'test', 'javaagent', 'agent' + str(i)) | ||
class_list = [join(test_classpath, 'com', 'oracle', 'svm', 'test', 'javaagent', 'agent' + str(i), f) for f in os.listdir(agent_test_classpath) if os.path.isfile(os.path.join(agent_test_classpath, f)) and f.endswith(".class")] | ||
mx.run([mx.get_jdk().jar, 'cmf', join(test_classpath, 'resources', 'javaagent' + str(i), 'MANIFEST.MF'), agent] + class_list, cwd = tmp_dir) | ||
current_dir = os.getcwd() | ||
# Change to test classpath to create agent jar file | ||
os.chdir(test_classpath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to change the dir? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Running |
||
agent_test_classpath = join('com', 'oracle', 'svm', 'test', 'javaagent', 'agent' + str(i)) | ||
class_list = [join('com', 'oracle', 'svm', 'test', 'javaagent', 'agent' + str(i), f) for f in os.listdir(agent_test_classpath) if os.path.isfile(os.path.join(agent_test_classpath, f)) and f.endswith(".class")] | ||
class_list.append(join('com', 'oracle', 'svm', 'test', 'javaagent', 'AgentPremainHelper.class')) | ||
mx.run([mx.get_jdk().jar, 'cmf', join(test_classpath, 'resources', 'javaagent' + str(i), 'MANIFEST.MF'), agent] + class_list, cwd = test_classpath) | ||
agents.append(agent) | ||
os.chdir(current_dir) | ||
|
||
mx.log("Building images with different agent orders ") | ||
build_and_run(args, join(tmp_dir, 'agenttest1'), native_image, agents,'com.oracle.svm.test.javaagent.agent1.TestJavaAgent1,com.oracle.svm.test.javaagent.agent2.TestJavaAgent2') | ||
build_and_run(args, join(tmp_dir, 'agenttest1'), native_image, agents,[f'-javaagent:{agents[0]}=test.agent1=true', f'-javaagent:{agents[1]}=test.agent2=true']) | ||
|
||
# Switch the premain sequence of agent1 and agent2 | ||
build_and_run(args, join(tmp_dir, 'agenttest2'), native_image, agents, 'com.oracle.svm.test.javaagent.agent2.TestJavaAgent2,com.oracle.svm.test.javaagent.agent1.TestJavaAgent1') | ||
build_and_run(args, join(tmp_dir, 'agenttest2'), native_image, agents, [f'-javaagent:{agents[1]}=test.agent2=true', f'-javaagent:{agents[0]}=test.agent1=true']) | ||
|
||
native_image_context_run(build_and_test_java_agent_image, args) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -336,6 +336,11 @@ public boolean isClosedTypeWorld() { | |
return true; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the following instead: |
||
public boolean isFromJavaAgent(Class<?> clazz) { | ||
return false; | ||
} | ||
|
||
/** | ||
* Helpers to determine what analysis actions should be taken for a given Multi-Method version. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,10 +26,12 @@ | |
|
||
import static jdk.vm.ci.common.JVMCIError.unimplemented; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import com.oracle.graal.pointsto.constraints.UnresolvedElementException; | ||
import com.oracle.graal.pointsto.meta.AnalysisUniverse; | ||
import com.oracle.graal.pointsto.util.GraalAccess; | ||
|
||
import jdk.graal.compiler.api.replacements.SnippetReflectionProvider; | ||
|
@@ -41,6 +43,7 @@ | |
import jdk.vm.ci.meta.JavaType; | ||
import jdk.vm.ci.meta.ResolvedJavaMethod; | ||
import jdk.vm.ci.meta.ResolvedJavaType; | ||
import jdk.vm.ci.meta.UnresolvedJavaMethod; | ||
|
||
public class WrappedConstantPool implements ConstantPool, ConstantPoolPatch { | ||
|
||
|
@@ -117,7 +120,26 @@ public JavaMethod lookupMethod(int cpi, int opcode) { | |
@Override | ||
public JavaMethod lookupMethod(int cpi, int opcode, ResolvedJavaMethod caller) { | ||
try { | ||
return universe.lookupAllowUnresolved(wrapped.lookupMethod(cpi, opcode, OriginalMethodProvider.getOriginalMethod(caller))); | ||
JavaMethod ret = universe.lookupAllowUnresolved(wrapped.lookupMethod(cpi, opcode, OriginalMethodProvider.getOriginalMethod(caller))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For which classes does this happen? Can you provide a concrete example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Class com.oracle.svm.test.javaagent.agent1.TestJavaAgent1 has dependency on At build time,
This doesn't happen in JVM because both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cstancu : please have a look at the code part below. |
||
/** | ||
* The java agent classes are loaded by appClassloader, but their dependencies could be | ||
* loaded by NativeImageClassloader. So if the required method could not be resolved, we | ||
* look further into classes loaded by nativeImageClassloader. | ||
*/ | ||
if (ret instanceof UnresolvedJavaMethod && universe.hostVM().isFromJavaAgent(OriginalClassProvider.getJavaClass(caller.getDeclaringClass()))) { | ||
UnresolvedJavaMethod unresolvedResult = (UnresolvedJavaMethod) ret; | ||
String className = unresolvedResult.format("%H"); | ||
String methodNameWithSignature = unresolvedResult.format("%n(%P)"); | ||
try { | ||
Class<?> loadedClass = ((AnalysisUniverse) universe).getConcurrentAnalysisAccess().findClassByName(className); | ||
ResolvedJavaType resolvedType = ((AnalysisUniverse) universe).getOriginalMetaAccess().lookupJavaType(loadedClass); | ||
ResolvedJavaMethod resolvedMethod = Arrays.stream(resolvedType.getDeclaredMethods(false)).filter(m -> m.format("%n(%P)").equals(methodNameWithSignature)).findFirst().get(); | ||
return universe.lookupAllowUnresolved(resolvedMethod); | ||
} catch (Exception e) { | ||
// Could not get the resolved method, get to the unresolved path | ||
} | ||
} | ||
return ret; | ||
} catch (Throwable ex) { | ||
Throwable cause = ex; | ||
if (ex instanceof ExceptionInInitializerError && ex.getCause() != null) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,6 +202,10 @@ protected void onValueUpdate(EconomicMap<OptionKey<?>, Object> values, String ol | |
public static OptionEnabledHandler<Boolean> imageLayerEnabledHandler; | ||
public static OptionEnabledHandler<Boolean> imageLayerCreateEnabledHandler; | ||
|
||
@APIOption(name = "-javaagent", valueSeparator = ':')// | ||
@Option(help = "Enable the specified java agent in native image. Usage: -javaagent:<jarpath>[=<options>]. It's the same as using javaagent in JVM", type = User, stability = OptionStability.EXPERIMENTAL)// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This option needs a longer explanation because its behavior is not the same as on HotSpot:
|
||
public static final HostedOptionKey<AccumulatingLocatableMultiOptionValue.Strings> JavaAgent = new HostedOptionKey<>(AccumulatingLocatableMultiOptionValue.Strings.build()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This partially conflicts with the basic JVMTI runtime support that was merged a couple weeks ago (see #9558). I will need to think about that a bit and I will try to come up with an approach to unify the JVMTI build- and runtime support. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does the conflict come from? Feels to me that with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there is a conflict, as you said they are working on different phases. Class transformation has nothing to do with JVMTI events. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both JVMTI and Java agents can be used to instrument Java classes. I was worried about those approaches conflicting. However, I think that you are right, this probably shouldn't result in worse situations than on HotSpot. Besides that, the same restrictions apply to both instrumentation approaches, so there is also no difference in terms of behavior there:
|
||
|
||
@Fold | ||
public static boolean getSourceLevelDebug() { | ||
return SourceLevelDebug.getValue(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
package com.oracle.svm.hosted; | ||
|
||
import com.oracle.svm.core.PreMainSupport; | ||
import com.oracle.svm.core.SubstrateOptions; | ||
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature; | ||
import com.oracle.svm.core.feature.InternalFeature; | ||
import com.oracle.svm.core.option.AccumulatingLocatableMultiOptionValue; | ||
|
@@ -39,10 +40,12 @@ | |
import org.graalvm.nativeimage.ImageSingletons; | ||
import org.graalvm.nativeimage.hosted.Feature; | ||
|
||
import java.io.IOException; | ||
import java.lang.instrument.Instrumentation; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.jar.JarFile; | ||
|
||
/** | ||
* This feature supports instrumentation in native image. | ||
|
@@ -73,10 +76,10 @@ public void afterRegistration(AfterRegistrationAccess access) { | |
FeatureImpl.AfterRegistrationAccessImpl a = (FeatureImpl.AfterRegistrationAccessImpl) access; | ||
cl = a.getImageClassLoader().getClassLoader(); | ||
ImageSingletons.add(PreMainSupport.class, preMainSupport = new PreMainSupport()); | ||
if (Options.PremainClasses.hasBeenSet()) { | ||
List<String> premains = Options.PremainClasses.getValue().values(); | ||
for (String premain : premains) { | ||
addPremainClass(premain); | ||
if (SubstrateOptions.JavaAgent.hasBeenSet()) { | ||
List<String> agentOptions = SubstrateOptions.JavaAgent.getValue().values(); | ||
for (String agentOption : agentOptions) { | ||
addPremainClass(agentOption); | ||
} | ||
} | ||
} | ||
|
@@ -94,12 +97,34 @@ public void afterRegistration(AfterRegistrationAccess access) { | |
* is absent. <br> | ||
* So this method looks for them in the same order. | ||
*/ | ||
private void addPremainClass(String premainClass) { | ||
private void addPremainClass(String javaagentOption) { | ||
int separatorIndex = javaagentOption.indexOf("="); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can probably use |
||
String agent; | ||
String premainClass = null; | ||
String options = ""; | ||
// Get the agent file | ||
if (separatorIndex == -1) { | ||
agent = javaagentOption; | ||
} else { | ||
agent = javaagentOption.substring(0, separatorIndex); | ||
options = javaagentOption.substring(separatorIndex + 1); | ||
} | ||
// Read MANIFEST in agent jar | ||
try { | ||
JarFile agentJarFile = new JarFile(agent); | ||
premainClass = agentJarFile.getManifest().getMainAttributes().getValue("Premain-Class"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens when there is no Manifest? There should be a special error message for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this moment, the java agent has been already loaded and run by JVM. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, HotSpot already executed the method I think that this will only work for a very small number of Java agents (i.e., agents that contain Native Image-specific logic or that hardly execute any code in Ideally, the agent manifest would contained some information to identify if the |
||
} catch (IOException e) { | ||
// This shall not happen, because at this moment GraalVM is running with -javaagent. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// If the agent doesn't exist, the JVM shall fail to start. | ||
UserError.abort(e, "Can't read the agent jar %s. Please check option %s", agent, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
SubstrateOptionsParser.commandArgument(SubstrateOptions.JavaAgent, "")); | ||
} | ||
|
||
try { | ||
Class<?> clazz = Class.forName(premainClass, false, cl); | ||
Method premain = null; | ||
List<Object> args = new ArrayList<>(); | ||
args.add(""); // First argument is options which will be set at runtime | ||
args.add(options); // First argument is options which will be set at runtime | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the options be overridden at run time? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Options set at runtime can override the build time options. |
||
try { | ||
premain = clazz.getDeclaredMethod("premain", String.class, Instrumentation.class); | ||
args.add(new PreMainSupport.NativeImageNoOpRuntimeInstrumentation()); | ||
|
@@ -108,13 +133,13 @@ private void addPremainClass(String premainClass) { | |
premain = clazz.getDeclaredMethod("premain", String.class); | ||
} catch (NoSuchMethodException e1) { | ||
UserError.abort(e1, "Can't register agent premain method, because can't find the premain method from the given class %s. Please check your %s setting.", premainClass, | ||
SubstrateOptionsParser.commandArgument(Options.PremainClasses, "")); | ||
SubstrateOptionsParser.commandArgument(SubstrateOptions.JavaAgent, "")); | ||
} | ||
} | ||
preMainSupport.registerPremainMethod(premainClass, premain, args.toArray(new Object[0])); | ||
} catch (ClassNotFoundException e) { | ||
UserError.abort(e, "Can't register agent premain method, because the given class %s is not found. Please check your %s setting.", premainClass, | ||
SubstrateOptionsParser.commandArgument(Options.PremainClasses, "")); | ||
SubstrateOptionsParser.commandArgument(SubstrateOptions.JavaAgent, "")); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need
-J--add-exports=java.base/jdk.internal.org.objectweb.asm=ALL-UNNAMED
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I transformed class in test agent with ASM.