Does this support OpenJdk11?
btrace WARNING: BTrace system classpath resource [C:\Program Files\Java\jdk-11.0.1/../lib/tools.jar] does not exist.
Exception in thread "Attach Listener" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:513)
at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndCallAgentmain(InstrumentationImpl.java:535)
Caused by: java.lang.NoClassDefFoundError: sun/reflect/Reflection
at com.sun.btrace.BTraceRuntime.<clinit>(BTraceRuntime.java:211)
at com.sun.btrace.agent.Main.main(Main.java:134)
at com.sun.btrace.agent.Main.agentmain(Main.java:93)
... 6 more
Thanks for raising this. I ran into the same issue recently. This is gonna requires at least 2 fixes to get it working with jdk11:
- (easy) use security manager (or StackWalker in jdk11 or jdk.internal.reflect.Reflection.getCallerClass) for getCallerClass() as in https://github.com/eclipse/jetty.project/pull/2922
- (harder) replace sun.misc.Unsafe.defineClass() with some new implementation: -- use maybe jdk.internal.misc.Unsafe.defineClass -- we do need to think about the finer issues raised by Rafael in this posting: http://mail.openjdk.java.net/pipermail/jigsaw-dev/2018-April/013720.html
The changes will require either splitting the versions to one supporting JDK 9+ features and one supporting JDK 7/8 or complex runtime checks with hacks to call the correct classes/methods.
In addition to what @zeocio mentions we also need to figure out how to easily open up the modules with instrumented classes to the BTrace runtime code. Should be doable but it is not my personal priority right now so either someone will take up the challenge and raise a PR or this will have to wait until I need this for my clients - which might be anywhere between 1-2 years from now given the 'rapid' adoption in enterprise space.
Actually, the same effort is already tracked as #292 - so far no volunteers there.
Just thinking out aloud here, would it be beneficial to refactor some of the agent instrumentation code to leverage the jdk11 work already done in bytebuddy (see https://github.com/raphw/byte-buddy/search?q=%22jdk+11%22&type=Issues)? The project's primary developers are actively involved in both maintaining and advocating current instrumentation use cases as well as relevant jdk api standardization discussions (http://mail.openjdk.java.net/pipermail/core-libs-dev/2018-October/055901.html) which is a good sign for api stability going forward.
Disclaimer: I don't know much about bytebuddy, just found out about it googling unsafe and jdk11 issues. Con: this is potentially a big change introducing additional dependencies and test surfaces with limited gains. Pro: bytebuddy is used in many mock/test/proxy frameworks and probably gets more testing under different scenarios than btrace.
The initial support is available in 'develop' branch. If you can give it a test ride it would be awesome.
Thanks for the work and update!
I ran a quick test and had some issues building it:
- need to set JAVA_8_HOME, JAVA_11_HOME and JAVA_HOME
- couldn't find jdk9 distro and so removed java9 sections from btrace-runtime/build.gradle
- gradlew build gave 1 failed test: ProbeLoaderUpgradeTest#testPersistedProbeLoad
At this point should I take btrace-dist/build/distributions/btrace-bin.zip and use that as binary distro? I ported my old btrace scripts (using new package names in imports) but got this:
java.lang.ExceptionInInitializerError
at org.openjdk.btrace.instr.Verifier.reportError(Verifier.java:68)
at org.openjdk.btrace.instr.Verifier.reportSafetyError(Verifier.java:202)
at org.openjdk.btrace.instr.Verifier.reportSafetyError(Verifier.java:198)
at org.openjdk.btrace.instr.Verifier$1.visit(Verifier.java:131)
at org.openjdk.btrace.libs.org.objectweb.asm.ClassReader.readElementValue(ClassReader.java:2829)
at org.openjdk.btrace.libs.org.objectweb.asm.ClassReader.readElementValues(ClassReader.java:2760)
at org.openjdk.btrace.libs.org.objectweb.asm.ClassReader.accept(ClassReader.java:572)
at org.openjdk.btrace.libs.org.objectweb.asm.ClassReader.accept(ClassReader.java:400)
at org.openjdk.btrace.instr.BTraceProbeNode.initialize(BTraceProbeNode.java:409)
at org.openjdk.btrace.instr.BTraceProbeNode.initialize(BTraceProbeNode.java:396)
at org.openjdk.btrace.instr.BTraceProbeNode.<init>(BTraceProbeNode.java:87)
at org.openjdk.btrace.instr.BTraceProbeFactory.createProbe(BTraceProbeFactory.java:106)
at org.openjdk.btrace.agent.Client.load(Client.java:436)
at org.openjdk.btrace.agent.Client.loadClass(Client.java:292)
at org.openjdk.btrace.agent.Client.loadClass(Client.java:285)
at org.openjdk.btrace.agent.RemoteClient.<init>(RemoteClient.java:64)
at org.openjdk.btrace.agent.Main.startServer(Main.java:740)
at org.openjdk.btrace.agent.Main.access$000(Main.java:71)
at org.openjdk.btrace.agent.Main$2.run(Main.java:132)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.util.MissingResourceException: Can't find bundle for base name org.openjdk.btrace.core.messages, locale en_US
at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2055)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1689)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1593)
at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:1284)
at org.openjdk.btrace.core.Messages.<clinit>(Messages.java:36)
... 20 more
Additional info relevant to the error above:
- both the build env of btrace-dist and the test env use en_US locale
- target test app runs spring boot application, which has its own class/resource loaders
JDK 9, 10 and 11 are now supported in the develop branch.
Thanks for making btrace work with Java 9/10/11, this is awesome!
I managed to make it work fine, but after executing the same btrace script the second time, it fails with (example with OpenJDK 11 using the OnThrow script from the samples):
! ERROR
java.lang.LinkageError: loader 'bootstrap' attempted duplicate class definition for org.openjdk.btrace.runtime.aux.OnThrow. (org.openjdk.btrace.runtime.aux.OnThrow is in unnamed module of loader 'bootstrap')
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.System$2.defineClass(System.java:2126)
at java.base/java.lang.invoke.MethodHandles$Lookup.defineClass(MethodHandles.java:962)
at org.openjdk.btrace.runtime.BTraceRuntimeImpl_11.defineClass(BTraceRuntimeImpl_11.java:124)
at org.openjdk.btrace.instr.BTraceProbeSupport.defineClass(BTraceProbeSupport.java:221)
at org.openjdk.btrace.instr.BTraceProbePersisted.register(BTraceProbePersisted.java:487)
at org.openjdk.btrace.agent.Client.loadClass(Client.java:335)
at org.openjdk.btrace.agent.Client.loadClass(Client.java:285)
at org.openjdk.btrace.agent.RemoteClient.<init>(RemoteClient.java:64)
at org.openjdk.btrace.agent.Main.startServer(Main.java:746)
at org.openjdk.btrace.agent.Main.access$000(Main.java:71)
at org.openjdk.btrace.agent.Main$2.run(Main.java:136)
at java.base/java.lang.Thread.run(Thread.java:834)
Reflective accesses do not seem to be working as well.
Code:
private static Field fdField = field("java.io.FileInputStream", "fd");
Output:
! ERROR
java.lang.NoClassDefFoundError: sun/reflect/Reflection
at org.openjdk.btrace.core.BTraceUtils.field(BTraceUtils.java:639)
at org.openjdk.btrace.runtime.aux.Leak8.<clinit>(Unknown Source)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.openjdk.btrace.runtime.BTraceRuntimeImpl_11.defineClass(BTraceRuntimeImpl_11.java:126)
at org.openjdk.btrace.instr.BTraceProbeSupport.defineClass(BTraceProbeSupport.java:221)
at org.openjdk.btrace.instr.BTraceProbePersisted.register(BTraceProbePersisted.java:487)
at org.openjdk.btrace.agent.Client.loadClass(Client.java:335)
at org.openjdk.btrace.agent.Client.loadClass(Client.java:285)
at org.openjdk.btrace.agent.RemoteClient.<init>(RemoteClient.java:64)
at org.openjdk.btrace.agent.Main.startServer(Main.java:746)
at org.openjdk.btrace.agent.Main.access$000(Main.java:71)
at org.openjdk.btrace.agent.Main$2.run(Main.java:136)
at java.base/java.lang.Thread.run(Thread.java:834)
@estaban The first issue with duplicate class definition is fully fixed in https://github.com/btraceio/btrace/commit/447ee3b6cd3877fa9b1dce322504b7e230ad79b0 (discovered this problem when fixing other issue).
The reflection problem must be fixed. Let me take a look at it.
Filed #400 to track the reflective access problems.