【问题标题】:VirtualMachine.attach(pid) fails with java.io.IOException: Can not attach to current VMVirtualMachine.attach(pid) 失败并出现 java.io.IOException:无法附加到当前 VM
【发布时间】:2019-06-27 09:16:20
【问题描述】:

经过this 讨论后,我相信附加到同一 VM 的选项,默认情况下在 OpenJDK11 中已禁用。

我正在尝试将 java 代理升级到 OpenJDK11,在调用 VirtualMachine.attach(pid) 的测试用例期间,我看到它失败并出现以下错误。处理这种情况的正确方法是什么?

完整的堆栈跟踪:

java.io.IOException: Can not attach to current VM

at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.<init>(HotSpotVirtualMachine.java:75)
at jdk.attach/sun.tools.attach.VirtualMachineImpl.<init>(VirtualMachineImpl.java:48)
at jdk.attach/sun.tools.attach.AttachProviderImpl.attachVirtualMachine(AttachProviderImpl.java:69)
at jdk.attach/com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:207)
at org.kantega.notsoserial.WithAgentIT.attachAgent(WithAgentIT.java:76)
at org.kantega.notsoserial.WithAgentIT.attackShouldBePreventedWithAgent(WithAgentIT.java:47)
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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

【问题讨论】:

  • 链接讨论在哪里说 -Djdk.attach.allowAttachSelf=true 在 JDK11 下将不再工作?
  • 您需要在VM启动时设置该属性,而不是在运行时设置,否则将被忽略。否则,请查看可以解决限制的 byte-buddy-agent。
  • @Holger 它没有。但是如何在其中一个测试用例运行期间设置参数,该测试用例试图将代理附加到由 Maven 创建的 JVM IIUC。
  • @RafaelWinterhalter 我不确定我是否遵循,如何在 VM 启动之前从测试用例中设置属性?是的,我正在查看 byte-buddy-agent 代码并尝试从辅助进程中进行附加。作为菜鸟,我还没有设法让它发挥作用。
  • 您不能,您需要使用该属性集启动虚拟机,否则,限制将毫无意义。 byte-buddy-agent 只需要您调用 install 来获取检测实例。如果它不起作用,请告诉我您遇到的问题。

标签: java java-11 javaagents


【解决方案1】:

JDK-8180425 : Release Note: Attach API cannot be used to attach to the current VM by default:

附加 API 的实现在 JDK 9 中已更改,默认情况下不允许附加到当前 VM。此更改应该不会影响使用 Attach API 附加到正在运行的 VM 的工具。它可能会影响那些滥用此 API 作为获取 java.lang.instrument API 的方式的库。可以在命令行上设置系统属性 jdk.attach.allowAttachSelf 以减轻与此更改的任何兼容性。

【讨论】:

  • 我们从我们的应用程序中启动一个虚拟机。我可以证明将 -Djdk.attach.allowAttachSelf=true 添加到 VM args 确实允许它附加并且我可以获得远程数据转储。我们使用它来查找每个 java 线程的操作系统线程 ID,以便我们可以知道哪个线程正在运行。
【解决方案2】:

我不确定这是否对每个人都有帮助,但就我而言,这是一个测试代理是否正确连接到 JDK 的测试用例(当代理实际连接到JDK,即实际运行时不是测试用例)。

根据@Holger 的建议,在 cmets 中,我修改了我的 maven-failsafe-plugin 以允许自我附加。

        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                    <configuration>
                        <argLine>-Djdk.attach.allowAttachSelf=true</argLine>
                        <forkMode>once</forkMode>
                    </configuration>
                </execution>
            </executions>
        </plugin>

【讨论】:

  • 这对我没有帮助。
  • 这个修复对我不起作用,任何帮助将不胜感激!我面临与 OpenJDK11 相同的问题
  • @MansinghPatel 我会尽力提供帮助,但您需要分享更多关于您正在尝试做的事情的详细信息,如果它是广泛的并且与我的场景不同,可能会发布一个新问题。
  • 感谢@Shanky,幸运的是我通过添加 javaagent 得到了修复
  • @MansinghPatel 能否提供更多详细信息,如何通过添加 javaagent 来解决?
【解决方案3】:

通过在 maven-surefire-plugin 中添加 javaagent 这对我有用

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${maven-surefire-plugin.version}</version>
        <configuration>
          <!-- Centralize test reports in parent project -->
          <reportsDirectory>${basedir}/../target/surefire-reports</reportsDirectory>
          <!-- Sets the VM argument line used for Jacoco when unit tests are run. -->
          <argLine>
             -javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar ${surefireArgLine}
          </argLine>
        </configuration> 
</plugin>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 2016-10-01
    • 2016-10-31
    • 2017-03-17
    • 1970-01-01
    相关资源
    最近更新 更多