JVM 可以检索已通过附加 API 加载到其中的代理列表吗?

发布于 2024-11-27 12:30:12 字数 119 浏览 1 评论 0原文

是否可以通过 Java 1.6 Attach api 获取加载到当前 JVM 中的代理列表?如果是这样怎么办?

启动时加载的代理可以通过 RuntimeMXBean 确定,但我看不到如何处理启动后添加的代理。

Is it possible to get a list of agents loaded into the current JVM by the Java 1.6 attach api? If so how?

Agents loaded at launch can be determined via RuntimeMXBean but I can't see a way to get a handle on ones added after launch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2024-12-04 12:30:12

(这个问题类似于如何查找与正在运行的 JVM 附加的 java 代理列表?。为了方便起见为了完整性,我将在这两个问题中添加这个答案。)


检查使用 Attach API 添加的代理:

如果您对在运行时使用 Attach API 添加到应用程序的代理感兴趣,你可以使用DiagnosticCommandMBean
该 bean 提供了一个名为 vmDynlib 的诊断命令,这是一个无参数方法,返回一个列出所有动态加载库的 String

下面是一个打印应用程序 VM 加载的所有动态库的代码片段:

ObjectName diagnosticsCommandName = new ObjectName("com.sun.management:type=DiagnosticCommand");
String operationName = "vmDynlibs";
String result = (String) ManagementFactory.getPlatformMBeanServer().invoke(diagnosticsCommandName, operationName, null, null);
System.out.println(result);

这会产生与此类似的输出:

Dynamic libraries:
0x00007ff7b8600000 - 0x00007ff7b8637000     C:\Program Files\Java\jdk1.8.0_181\bin\java.exe
0x00007ffdfeb00000 - 0x00007ffdfecf0000     C:\WINDOWS\SYSTEM32\ntdll.dll
0x00007ffdfe300000 - 0x00007ffdfe3b2000     C:\WINDOWS\System32\KERNEL32.DLL
0x00007ffdfbb30000 - 0x00007ffdfbdd3000     C:\WINDOWS\System32\KERNELBASE.dll
0x00007ffdfe950000 - 0x00007ffdfe9f3000     C:\WINDOWS\System32\ADVAPI32.dll
...

然后,您可以检查此文本是否包含特定的 .so。 dll 文件。


可以非编程方式执行相同的检查。

为此,您可以使用 jconsole< /code>工具。

连接到虚拟机,切换到选项卡 MBeans,选择 com.sun.management,选择 DiagnosticCommand,选择 Operations >,选择 vmDynlibs 并调用它。

vmDynlibs 输出

在图像中,您可以看到我的测试代理之一附加到应用程序。
使用 附加 API,因此通过检查应用程序的命令行参数,该代理将不可见(即,不会有 -agentpath=...被看见在参数上),但仅作为动态加载的库可见。

检查已通过命令行添加的代理:

为了获得完整的参考,我还将发布如何检测已通过命令行添加的代理。
您可以使用 来检查它们RuntimeMXBean

该 bean 提供了方法 getInputArguments,该方法返回所有 VM 参数的列表。
您可以迭代该列表并检查参数 agentpathagentlibjavaagent,类似于以下代码片段:

    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    List<String> jvmArgs = runtimeMXBean.getInputArguments();
    System.out.println("JVM arguments:");
    for (String arg : jvmArgs) {
        if (arg.startsWith("-agentpath") || arg.startsWith("-agentlib") || arg.startsWith("-javaagent")) {
            System.out.print("***** ");
        }

        System.out.print(arg);

        if (arg.startsWith("-agentpath") || arg.startsWith("-agentlib") || arg.startsWith("-javaagent")) {
            System.out.println(" *****");
        } else {
            System.out.println();
        }
    }

(This question is similar to How to find list of java agents attached with a running JVM?. For the sake of completeness, I will add this answer to both questions.)


Checking agents that have been added using the Attach API:

If you are interested in the agents that have been added to an application at run time using the Attach API, you can use the DiagnosticCommandMBean.
This bean offers a diagnostic command called vmDynlib, a parameterless method that returns a String that list all dynamically loaded libraries.

Here is a snippet that prints all dynamic libraries loaded by the application's VM:

ObjectName diagnosticsCommandName = new ObjectName("com.sun.management:type=DiagnosticCommand");
String operationName = "vmDynlibs";
String result = (String) ManagementFactory.getPlatformMBeanServer().invoke(diagnosticsCommandName, operationName, null, null);
System.out.println(result);

This results in an output similar to this one:

Dynamic libraries:
0x00007ff7b8600000 - 0x00007ff7b8637000     C:\Program Files\Java\jdk1.8.0_181\bin\java.exe
0x00007ffdfeb00000 - 0x00007ffdfecf0000     C:\WINDOWS\SYSTEM32\ntdll.dll
0x00007ffdfe300000 - 0x00007ffdfe3b2000     C:\WINDOWS\System32\KERNEL32.DLL
0x00007ffdfbb30000 - 0x00007ffdfbdd3000     C:\WINDOWS\System32\KERNELBASE.dll
0x00007ffdfe950000 - 0x00007ffdfe9f3000     C:\WINDOWS\System32\ADVAPI32.dll
...

You can then check this text if it contains a certain .so or .dll file.


The same inspection can be performed non-programatically.

For this, you can use the jconsole tool.

Connect to a VM, switch to the tab MBeans, select com.sun.management, select DiagnosticCommand, select Operations, select vmDynlibs, and invoke it.

vmDynlibs output

In the image, you can see one of my test agents attached to the application.
The agent was attached using the Attach API, thus this agent would not be visible by checking the application's command line arguments (i.e., no -agentpath=... would be seen on the arguments) but is only visible as dynamically loaded library.

Checking agents that have been added via command-line:

To have the complete reference, I will also post how to detect agents that have been added via the command line.
You can check them by using the RuntimeMXBean.

This bean offers the method getInputArguments, which returns a list of all VM arguments.
You can iterate over the list and check it for the arguments agentpath, agentlib and javaagent, similar to the following code snippet:

    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    List<String> jvmArgs = runtimeMXBean.getInputArguments();
    System.out.println("JVM arguments:");
    for (String arg : jvmArgs) {
        if (arg.startsWith("-agentpath") || arg.startsWith("-agentlib") || arg.startsWith("-javaagent")) {
            System.out.print("***** ");
        }

        System.out.print(arg);

        if (arg.startsWith("-agentpath") || arg.startsWith("-agentlib") || arg.startsWith("-javaagent")) {
            System.out.println(" *****");
        } else {
            System.out.println();
        }
    }
鸵鸟症 2024-12-04 12:30:12

不,我认为没有一种可移植的方法来了解代理。你想实现什么目标?也许还有另一种方法...

No, I don't think there is a portable way to find out about the agents. What are you trying to accomplish? Maybe there is another approach...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文