是否有必要以root身份运行jstack -F(在linux上),如果是这样,为什么?
尝试jstack -F我的拥有进程时出现以下错误。
附加到进程的错误:sun.jvm.hotspot.debugger.DebuggerException:无法附加到进程
如果我用sudo运行它,jstack -F工作正常。
答案 0 :(得分:11)
这是因为jstack -F
使用ptrace(2)
系统调用来尝试访问JVM数据,如果您没有权限,则会失败:
$ strace -e all -f jstack -F 26846
...
[pid 27653] ptrace(PTRACE_ATTACH, 26846, 0, 0) = -1 EPERM (Operation not permitted)
...
EPERM The specified process cannot be traced. This could be because the parent has insufficient privileges (the required capability is CAP_SYS_PTRACE); unprivileged processes cannot trace processes that they cannot send signals to or those running set-user-ID/set-group-ID programs, for obvious reasons. Alternatively, the process may already be being traced, or be init(8) (PID 1).
另见capabilities(7)。使用sudo
,您将获得root的功能,其中包括CAP_SYS_PTRACE
。