To fix the error ‘can’t attach to the process’ while attempting to do a jinfo or jmap, for instance, to a specific Java process id
From time to time, I have the need to check some information regarding a specific Java process, such as checking what memory is using or taking a heap dump. Lately, I have been facing some issues regarding the execution of the jinfo or jmap commands, as shown next
> jinfo <JAVA_PROCESS_ID> or jmap -heap:format=b <JAVA_PROCESS_ID>
which resulted in something similar to:
> Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process
Fortunately, after a search in Google, I found a solution which I am posting in my blog as a recipe for simple lookup for me in the near future. However, all credits go to the solution’s poster, not me! 🙂
To fix this problem, all you have to do is execute the following command:
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
And after that, you will be able to attach to the Java proces id specified.
The solution to this problem concerns the fix of the following issues, as explained in the original solution’s blog:
- The jmap process and the java process should be running in the same user id.
- There is a linux ptrace bug which will prevent jmap (or jinfo) from attaching to the java process.