jstack无法在服务器上运行

时间:2011-11-16 02:17:57

标签: java jstack

我们在服务器上使用jstack来检测java应用程序是否正在陷入僵局。它不适用于我们的Linux服务器之一。我认为O / S版本是:

$cat /etc/issue.net
Red Hat Enterprise Linux Server release 5.6 (Tikanga)
Kernel \r on an \m

在服务器上运行的Java版本:

java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)

当我尝试:

jstack 19114

我明白了:

19114: Unable to open socket file: target process not responding or HotSpot VM not loaded
The -F option can be used when the target process is not responding

当我尝试:

jstack -F 19114

我明白了:

Attaching to process ID 19114, please wait...
Debugger attached successfully.

Deadlock Detection:

No deadlocks found.

Thread 19180: (state = BLOCKED)
Error occurred during stack walking:
sun.jvm.hotspot.debugger.DebuggerException: sun.jvm.hotspot.debugger.DebuggerException: get_thread_regs failed for a lwp
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$LinuxDebuggerLocalWorkerThread.execute(LinuxDebuggerLocal.java:152)
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.getThreadIntegerRegisterSet(LinuxDebuggerLocal.java:466)
        at sun.jvm.hotspot.debugger.linux.LinuxThread.getContext(LinuxThread.java:65)
        at sun.jvm.hotspot.runtime.linux_amd64.LinuxAMD64JavaThreadPDAccess.getCurrentFrameGuess(LinuxAMD64JavaThreadPDAccess.java:92)
        at sun.jvm.hotspot.runtime.JavaThread.getCurrentFrameGuess(JavaThread.java:256)
        at sun.jvm.hotspot.runtime.JavaThread.getLastJavaVFrameDbg(JavaThread.java:218)
        at sun.jvm.hotspot.tools.StackTrace.run(StackTrace.java:76)
        at sun.jvm.hotspot.tools.StackTrace.run(StackTrace.java:45)
        at sun.jvm.hotspot.tools.JStack.run(JStack.java:60)
        at sun.jvm.hotspot.tools.Tool.start(Tool.java:221)
        at sun.jvm.hotspot.tools.JStack.main(JStack.java:86)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at sun.tools.jstack.JStack.runJStackTool(JStack.java:118)
        at sun.tools.jstack.JStack.main(JStack.java:84)
Caused by: sun.jvm.hotspot.debugger.DebuggerException: get_thread_regs failed for a lwp
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.getThreadIntegerRegisterSet0(Native Method)
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.access$800(LinuxDebuggerLocal.java:51)
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$1GetThreadIntegerRegisterSetTask.doit(LinuxDebuggerLocal.java:460)
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$LinuxDebuggerLocalWorkerThread.run(LinuxDebuggerLocal.java:127)

任何人都知道造成这种情况的原因是什么?

4 个答案:

答案 0 :(得分:14)

需要以启动java进程的同一用户身份运行jstack。这解决了上面的堆栈跟踪错误。有关更多详细信息,请参阅此帖子:jstack thread dump error: get_thread_regs failed for a lwp。一旦我susted jstack命令,错误就消失了。

答案 1 :(得分:4)

语法很简单:

sudo -u USERID jstack PID

示例:

sudo -u tomcat7 jstack 2498

答案 2 :(得分:2)

尝试使用kill -3 <pid>来获取VM的堆栈跟踪。

答案 3 :(得分:2)

可以使用一个为您提供类似输出的jsp。

以下jsp将线程堆栈信息打印到屏幕,但您也可以将输出更改为文件或在常规POJO类中使用。

<%@ page import="java.util.*" %><%
    Map<Thread,StackTraceElement[]> map = Thread.getAllStackTraces();
    Set tt = map.keySet();
    Iterator<Thread> ti = tt.iterator();
    Thread thrd = null;
    final String br = "<" + "br" + ">";//website does not parse it
    try{

        int cnt = 1;
        StackTraceElement[] st = null;
        while(ti.hasNext() ){
            thrd = ti.next();
            out.print(br + "<" + "hr" + ">" + br + cnt + " \"" + thrd.getName());
            out.println("\", priority :" + thrd.getPriority()  + ", state :" + thrd.getState());

            out.print(", id :" + thrd.getId() + ", hex :" +  Long.toHexString(thrd.getId()) );
            out.print(" alive :"  + thrd.isAlive() + ", daemon :" + thrd.isDaemon() );
            out.print(" interrupted  :"  + thrd.isInterrupted() + ", daemon :" + thrd.isDaemon() );
            out.print(".\n" + br);
            st = thrd.getStackTrace();
            for(int sti = 0; sti < st.length; sti++){
                out.println(br + " &nbsp; &nbsp; " + st[sti].getClassName() + "." + st[sti].getMethodName());
                out.println("(" + st[sti].getFileName());
                if(st[sti].getLineNumber() < 1){
                    out.print("Native method");
                }else{
                    out.print(":" + st[sti].getLineNumber());
                }
                out.println(")");
            }

            out.println("");
            cnt++;
        }
    }catch(Exception e){
        out.println(br + "err " + e + br);
    }




%>

示例输出:

  

121&#34; Thread-40&#34;,priority:6,state:WAITING,id:134,hex:86 alive:true,daemon:false interrupted:false,daemon:false。
< BR />

     

java.lang.Object.wait(Object.java Native method)
     java.lang.Object.wait(Object.java:485)
     org.jpos.iso.ISOMUX $ Receiver.run(ISOMUX.java:326)
     java.lang.Thread.run(Thread.java:662)

     

122&#34; Thread-48&#34;,priority:5,state:TIMED_WAITING,id:142,hex:8e alive:true,daemon:false interrupted:false,daemon:false。
< BR />

     
    

java.lang.Thread.sleep(Thread.java Native method)
    org.jpos.apps.qsp.QSP.monitorConfigFile(QSP.java:301)
    org.jpos.apps.qsp.QSP.run(QSP.java:346)
    java.lang.Thread.run(Thread.java:662)