JVM 可以检索已通过附加 API 加载到其中的代理列表吗?
是否可以通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(这个问题类似于如何查找与正在运行的 JVM 附加的 java 代理列表?。为了方便起见为了完整性,我将在这两个问题中添加这个答案。)
检查使用 Attach API 添加的代理:
如果您对在运行时使用 Attach API 添加到应用程序的代理感兴趣,你可以使用
DiagnosticCommandMBean
。该 bean 提供了一个名为
vmDynlib
的诊断命令,这是一个无参数方法,返回一个列出所有动态加载库的String
。下面是一个打印应用程序 VM 加载的所有动态库的代码片段:
这会产生与此类似的输出:
然后,您可以检查此文本是否包含特定的
.so
或。 dll 文件。
可以非编程方式执行相同的检查。
为此,您可以使用
jconsole< /code>
工具。
连接到虚拟机,切换到选项卡
MBeans
,选择com.sun.management
,选择DiagnosticCommand
,选择Operations
>,选择 vmDynlibs 并调用它。在图像中,您可以看到我的测试代理之一附加到应用程序。
使用
附加 API
,因此通过检查应用程序的命令行参数,该代理将不可见(即,不会有-agentpath=...
被看见在参数上),但仅作为动态加载的库可见。检查已通过命令行添加的代理:
为了获得完整的参考,我还将发布如何检测已通过命令行添加的代理。
您可以使用
来检查它们RuntimeMXBean
。该 bean 提供了方法
getInputArguments
,该方法返回所有 VM 参数的列表。您可以迭代该列表并检查参数
agentpath
、agentlib
和javaagent
,类似于以下代码片段:(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 aString
that list all dynamically loaded libraries.Here is a snippet that prints all dynamic libraries loaded by the application's VM:
This results in an output similar to this one:
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
, selectcom.sun.management
, selectDiagnosticCommand
, selectOperations
, selectvmDynlibs
, 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
andjavaagent
, similar to the following code snippet:不,我认为没有一种可移植的方法来了解代理。你想实现什么目标?也许还有另一种方法...
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...