[root@ebd4ba8d5b72]# ps -ef|grep java root 18 1 0 23:50 pts/0 00:00:00 java -jar math-game.jar root 136 69 0 23:52 pts/1 00:00:00 grep --color=auto java [root@ebd4ba8d5b72]# jmap -heap 18 Attaching to process ID 18, please wait... ERROR: ptrace(PTRACE_ATTACH, ..) failed for 18: Operation not permitted Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 18: Operation not permitted sun.jvm.hotspot.debugger.DebuggerException: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 18: 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.HeapSummary.main(HeapSummary.java:49) 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 18: 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)
方法一(已验证Ok)
在容器启动时添加–cap-add=SYS_PTRACE,如下:
1
docker run --cap-add=SYS_PTRACE -v /root:/root -it 7a4460957494 bash
这种方式在调试镜像时没问题。但是在线上环境会存在比较严重的安全问题。
方法二(未能验证)
方法二是修改Linux内核设置。执行以下命令:
1
echo 0 > /proc/sys/kernel/yama/ptrace_scope
或者在配置文件/etc/sysctl.d/10-ptrace.conf中修改或添加以下配置:
1
kernel.yama.ptrace_scope = 0
在我的环境中两种方式都会报以下错误信息:
1
/proc/sys/kernel/yama/ptrace_scope: No such file or directory
具体原因我没有深究,怀疑跟我的操作系统版本有关:
CentOS Linux release 7.7.1908 (Core)
Linux 4.4.222-1.el7.elrepo.x86_64 #1 SMP Mon May 4 19:25:23 EDT 2020 x86_64 x86_64 x86_64 GNU/Linux