是否有必要以root身份运行jstack -F(在linux上),如果是这样,为什么呢?

lal*_*666 9 java jvm jstack

是否有必要以root身份运行jstack -F(在linux上),如果是这样,为什么呢?

当尝试jstack -F我自己的进程时,我收到以下错误.

附加到进程的错误:sun.jvm.hotspot.debugger.DebuggerException:无法附加到进程

如果我用sudo运行它,jstack -F工作正常.

Fra*_*eau 12

这是因为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)
...
Run Code Online (Sandbox Code Playgroud)

来自ptrace(2)男人:

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).
Run Code Online (Sandbox Code Playgroud)

另见功能(7).使用sudo,你获得root的功能,包括CAP_SYS_PTRACE.