mac 下 Can't attach to the process. Could be caused by an incorrect pid or lack of privileges. 解决
问题
报错如下
ERROR: attach: task_for_pid(4060) failed: '(os/kern) failure' (5)
Error attaching to process: Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.
sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.
at jdk.hotspot.agent/sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal$BsdDebuggerLocalWorkerThread.execute(BsdDebuggerLocal.java:168)
at jdk.hotspot.agent/sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal.attach(BsdDebuggerLocal.java:282)
at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.attachDebugger(HotSpotAgent.java:648)
at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.setupDebuggerDarwin(HotSpotAgent.java:636)
at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.setupDebugger(HotSpotAgent.java:371)
at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:336)
at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:141)
at jdk.hotspot.agent/sun.jvm.hotspot.tools.Tool.start(Tool.java:203)
at jdk.hotspot.agent/sun.jvm.hotspot.tools.Tool.execute(Tool.java:134)
at com.serveree.uselesscode.SAMethodDemo_jdk17.main(SAMethodDemo_jdk17.java:34)
Caused by: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.
at jdk.hotspot.agent/sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal.attach0(Native Method)
at jdk.hotspot.agent/sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal\(1AttachTask.doit(BsdDebuggerLocal.java:273) at jdk.hotspot.agent/sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal\)BsdDebuggerLocalWorkerThread.run(BsdDebuggerLocal.java:143)
原因
应该是 mac 间进程通信的问题,导致 java 程序缺少必要的权限
解决
有两种解决方法
命令行执行命令前加上 sudo
使用 sudo 运行 java 程序能够给它必要的权限
禁用 mac 的 SIP(System Integrity Protection, 系统完整性保护)
参考:https://blog.csdn.net/gusijin/article/details/126677058
注意: intel芯片的 mac 进入安全模式的方法与 m 系列芯片 mac 不同,intel 芯片为亮屏后长按 command + R