Can a JVM retrieve a list of agents that have been loaded into it via the attach api?

╄→гoц情女王★ 提交于 2019-12-10 03:04:04

问题


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.


回答1:


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...




回答2:


(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.

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();
        }
    }


来源:https://stackoverflow.com/questions/6904728/can-a-jvm-retrieve-a-list-of-agents-that-have-been-loaded-into-it-via-the-attach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!