首页 > 解决方案 > jmap没有file参数,所以执行异常。但是添加file参数后执行正常

问题描述

具体异常信息如下:

root@test-6cf7db85b7-sxk8h:/# jmap 7
Attaching to process ID 7, please wait...
ERROR: ptrace(PTRACE_ATTACH, ..) failed for 7: Operation not permitted
Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 7: Operation not permitted
sun.jvm.hotspot.debugger.DebuggerException: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 7: Operation not permitted
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$LinuxDebuggerLocalWorkerThread.execute(LinuxDebuggerLocal.java:163)
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach(LinuxDebuggerLocal.java:278)
        at sun.jvm.hotspot.HotSpotAgent.attachDebugger(HotSpotAgent.java:671)
        at sun.jvm.hotspot.HotSpotAgent.setupDebuggerLinux(HotSpotAgent.java:611)
        at sun.jvm.hotspot.HotSpotAgent.setupDebugger(HotSpotAgent.java:337)
        at sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:304)
        at sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:140)
        at sun.jvm.hotspot.tools.Tool.start(Tool.java:185)
        at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
        at sun.jvm.hotspot.tools.PMap.main(PMap.java:72)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.tools.jmap.JMap.runTool(JMap.java:201)
        at sun.tools.jmap.JMap.main(JMap.java:130)
Caused by: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 7: Operation not permitted
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach0(Native Method)
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.access$100(LinuxDebuggerLocal.java:62)
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$1AttachTask.doit(LinuxDebuggerLocal.java:269)
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$LinuxDebuggerLocalWorkerThread.run(LinuxDebuggerLocal.java:138)

root@test-6cf7db85b7-sxk8h:/# ^C
root@test-6cf7db85b7-sxk8h:/# 

两个命令如下:

// error
jmap 7
// success
jmap -dump:file=test.hprof 7

爪哇资料:

openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)

jmap没有file参数,所以执行异常。但是添加file参数后执行正常。

标签: javajvmjmap

解决方案


在 JDK 8 中,jmap实际上jmap -dump是两个不同的实用程序,被轻率地打包在一个可执行文件中。

它们服务于不同的目的,并在底层使用不同的机制:

  • jmap(无参数)基于 HotSpot Serviceability Agent;
  • jmap -dump使用动态附加。

有关可维护性代理和动态附加的详细信息,请参阅此答案,或阅读本文

虽然动态附加通常在大多数容器中都可以正常工作,但可服务性代理需要CAP_SYS_PTRACE功能,该功能通常仅在特权容器中可用。

自 JDK 9 以来,这一疏忽终于得到解决,现在可服务性代理工具捆绑在一个名为jhsdb.


推荐阅读