当前位置:   article > 正文

在docker中使用JDK工具包报错_sun.jvm.hotspot.debugger.debuggerexception: cannot

sun.jvm.hotspot.debugger.debuggerexception: cannot open binary file

目录

第一种报错:

 原因

解决

第二种报错:

原因

解决

第三种报错:

原因

解决


第一种报错:

  1. jinfo -flags 666
  2. Attaching to process ID 666, please wait...
  3. Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process
  4. sun.jvm.hotspot.debugger.DebuggerException: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process
  5. at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$LinuxDebuggerLocalWorkerThread.execute(LinuxDebuggerLocal.java:163)
  6. at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach(LinuxDebuggerLocal.java:278)
  7. at sun.jvm.hotspot.HotSpotAgent.attachDebugger(HotSpotAgent.java:671)
  8. at sun.jvm.hotspot.HotSpotAgent.setupDebuggerLinux(HotSpotAgent.java:611)
  9. at sun.jvm.hotspot.HotSpotAgent.setupDebugger(HotSpotAgent.java:337)
  10. at sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:304)
  11. at sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:140)
  12. at sun.jvm.hotspot.tools.Tool.start(Tool.java:185)
  13. at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
  14. at sun.jvm.hotspot.tools.JInfo.main(JInfo.java:138)
  15. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  16. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  17. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  18. at java.lang.reflect.Method.invoke(Method.java:483)
  19. at sun.tools.jinfo.JInfo.runTool(JInfo.java:108)
  20. at sun.tools.jinfo.JInfo.main(JInfo.java:76)
  21. Caused by: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process
  22. at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach0(Native Method)
  23. at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.access$100(LinuxDebuggerLocal.java:62)
  24. at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$1AttachTask.doit(LinuxDebuggerLocal.java:269)
  25. at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$LinuxDebuggerLocalWorkerThread.run(LinuxDebuggerLocal.java:138)

 原因

这是因为新版的Linux系统加入了 ptrace-scope 机制. 这种机制为了防止用户访问当前正在运行的进程的内存和状态, 而一些调试软件本身就是利用 ptrace 来进行获取某进程的内存状态的(包括GDB),所以在新版本的Linux系统, 默认情况下不允许再访问了. 可以临时开启. 如:

1

echo 0 | tee /proc/sys/kernel/yama/ptrace_scope

永久写到文件来持久化:

1

2

3

4

emacs /etc/sysctl.d/10-ptrace.conf

添加或修改为以下这一句:(0:允许, 1:不允许)

kernel.yama.ptrace_scope = 0

解决

  1. echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
  2. #再次执行时就可以得到结果了
  3. jinfo -flags 666
  4. Attaching to process ID 666, please wait...
  5. Debugger attached successfully.
  6. Server compiler detected.
  7. JVM version is 25.5-b02
  8. Non-default VM flags: -XX:InitialHeapSize=41943040 -XX:MaxHeapSize=536870912 -XX:MaxNewSize=178782208 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=1572864 -XX:OldSize=40370176 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC
  9. Command line: -Dosgi.requiredJavaVersion=1.6 -XX:MaxPermSize=256m -Xms40m -Xmx512m

第二种报错:

Caused by: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 6: Operation not permitted

原因

docker中出现时:问题分析

发现docker的官方文档提到了,这不是什么 docker 或者jmap的Bug,而是 Docker 自 1.10 版本开始加入的安全特性。 jmap 这类 JDK 工具依赖于 Linux 的 PTRACE_ATTACH,而 Docker 自 1.10 版本开始,默认的 seccomp 配置文件中禁用了 ptrace。



解决

加参数 :–security-opt seccomp=unconfined

直接关闭 seccomp 配置或者将ptrace添加到允许的名单中。用法:

  1. #docker运行时加入“--security-opt seccomp=unconfined”参数
  2. docker run -d --name ${DOCKER_NAME} \
  3. --restart=unless-stopped \
  4. --hostname ${HOSTNAME} \
  5. --security-opt seccomp=unconfined \
  6. -p 8803:8803 \
  7. -v /opt/tcp.jar:/opt/tcp.jar \
  8. -v /opt/config/:/opt/config/ \
  9. -v /opt/logs/:/opt/logs/ \
  10. ImageName:TAG
  11. docker官网文档:https://docs.docker.com/engine/security/seccomp/
  12. demo:
  13. Run without the default seccomp profile
  14. You can pass unconfined to run a container without the default seccomp profile.
  15. $ docker run --rm -it --security-opt seccomp=unconfined debian:jessie \
  16. unshare --map-root-user --user sh -c whoami

第三种报错:

  1. Attaching to core -heap from executable 7, please wait...
  2. Error attaching to core file: cannot open binary file
  3. sun.jvm.hotspot.debugger.DebuggerException: cannot open binary file
  4. at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach0(Native Method)
  5. at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach(LinuxDebuggerLocal.java:286)
  6. at sun.jvm.hotspot.HotSpotAgent.attachDebugger(HotSpotAgent.java:673)
  7. at sun.jvm.hotspot.HotSpotAgent.setupDebuggerLinux(HotSpotAgent.java:611)
  8. at sun.jvm.hotspot.HotSpotAgent.setupDebugger(HotSpotAgent.java:337)
  9. at sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:304)
  10. at sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:156)
  11. at sun.jvm.hotspot.tools.Tool.start(Tool.java:191)
  12. at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
  13. at sun.jvm.hotspot.tools.PMap.main(PMap.java:72)
  14. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  15. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  16. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  17. at java.lang.reflect.Method.invoke(Method.java:498)
  18. at sun.tools.jmap.JMap.runTool(JMap.java:201)
  19. at sun.tools.jmap.JMap.main(JMap.java:130)

原因

命令格式不正确导致

  1. [root@localhost tcp-client1.1]# ../jdk1.8.0_211/bin/jmap 7 -heap
  2. Attaching to core -heap from executable 7, please wait...
  3. Error attaching to core file: cannot open binary file
  4. sun.jvm.hotspot.debugger.DebuggerException: cannot open binary file
  5. at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach0(Native Method)
  6. at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach(LinuxDebuggerLocal.java:286)
  7. at sun.jvm.hotspot.HotSpotAgent.attachDebugger(HotSpotAgent.java:673)
  8. at sun.jvm.hotspot.HotSpotAgent.setupDebuggerLinux(HotSpotAgent.java:611)
  9. at sun.jvm.hotspot.HotSpotAgent.setupDebugger(HotSpotAgent.java:337)
  10. at sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:304)
  11. at sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:156)
  12. at sun.jvm.hotspot.tools.Tool.start(Tool.java:191)
  13. at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
  14. at sun.jvm.hotspot.tools.PMap.main(PMap.java:72)
  15. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  16. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  17. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  18. at java.lang.reflect.Method.invoke(Method.java:498)
  19. at sun.tools.jmap.JMap.runTool(JMap.java:201)
  20. at sun.tools.jmap.JMap.main(JMap.java:130)

解决

使用命令command --help查看命令使用说明

  1. [root@localhost tcp-client1.1]# ../jdk1.8.0_211/bin/jmap --help
  2. Usage:
  3. jmap [option] <pid>
  4. (to connect to running process)
  5. jmap [option] <executable <core>
  6. (to connect to a core file)
  7. jmap [option] [server_id@]<remote server IP or hostname>
  8. (to connect to remote debug server)
  9. where <option> is one of:
  10. <none> to print same info as Solaris pmap
  11. -heap to print java heap summary
  12. -histo[:live] to print histogram of java object heap; if the "live"
  13. suboption is specified, only count live objects
  14. -clstats to print class loader statistics
  15. -finalizerinfo to print information on objects awaiting finalization
  16. -dump:<dump-options> to dump java heap in hprof binary format
  17. dump-options:
  18. live dump only live objects; if not specified,
  19. all objects in the heap are dumped.
  20. format=b binary format
  21. file=<file> dump heap to <file>
  22. Example: jmap -dump:live,format=b,file=heap.bin <pid>
  23. -F force. Use with -dump:<dump-options> <pid> or -histo
  24. to force a heap dump or histogram when <pid> does not
  25. respond. The "live" suboption is not supported
  26. in this mode.
  27. -h | -help to print this help message
  28. -J<flag> to pass <flag> directly to the runtime system
  29. [root@localhost tcp-client1.1]# ../jdk1.8.0_211/bin/jmap -heap 7
  30. Attaching to process ID 7, please wait...
  31. Debugger attached successfully.
  32. Server compiler detected.
  33. JVM version is 25.211-b12
  34. using thread-local object allocation.
  35. Garbage-First (G1) GC with 63 thread(s)
  36. Heap Configuration:
  37. MinHeapFreeRatio = 40
  38. MaxHeapFreeRatio = 70
  39. MaxHeapSize = 17179869184 (16384.0MB)
  40. NewSize = 1363144 (1.2999954223632812MB)
  41. MaxNewSize = 10301210624 (9824.0MB)
  42. OldSize = 5452592 (5.1999969482421875MB)
  43. NewRatio = 2
  44. SurvivorRatio = 8
  45. MetaspaceSize = 21807104 (20.796875MB)
  46. CompressedClassSpaceSize = 1073741824 (1024.0MB)
  47. MaxMetaspaceSize = 17592186044415 MB
  48. G1HeapRegionSize = 8388608 (8.0MB)
  49. Heap Usage:
  50. G1 Heap:
  51. regions = 2048
  52. capacity = 17179869184 (16384.0MB)
  53. used = 412985176 (393.85335540771484MB)
  54. free = 16766884008 (15990.146644592285MB)
  55. 2.403890108689666% used
  56. G1 Young Generation:
  57. Eden Space:
  58. regions = 44
  59. capacity = 880803840 (840.0MB)
  60. used = 369098752 (352.0MB)
  61. free = 511705088 (488.0MB)
  62. 41.904761904761905% used
  63. Survivor Space:
  64. regions = 3
  65. capacity = 25165824 (24.0MB)
  66. used = 25165824 (24.0MB)
  67. free = 0 (0.0MB)
  68. 100.0% used
  69. G1 Old Generation:
  70. regions = 3
  71. capacity = 16273899520 (15520.0MB)
  72. used = 18720600 (17.853355407714844MB)
  73. free = 16255178920 (15502.146644592285MB)
  74. 0.11503450649300802% used
  75. 22756 interned Strings occupying 2059192 bytes.
  76. [root@localhost tcp-client1.1]#

其他参考借鉴:

Docker 中无法使用 JDK jmap之 Can't attach to the process: ptrace(PTRACE_ATTACH问题_russle的专栏-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/461425
推荐阅读
相关标签
  

闽ICP备14008679号