📄 stack.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Writing Advanced Applications, Chapter 7: Analyzing Stack Traces</TITLE>
<META NAME="AUTHOR" CONTENT="Monica Pawlan and Calvin Austin">
<META NAME="KEYWORDS" CONTENT="programming, advanced, Java 2">
<META NAME="OWNER" CONTENT="Editorial/JDC">
<META NAME="revision" CONTENT="@(#)stack.src 1.16 09/21/99 JDC">
</HEAD>
<!-- Start Body Insert-->
<BODY BGCOLOR="#ffffff">
<!-- End Body Insert-->
<!-- Start PageTop Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR ALIGN="CENTER" VALIGN="TOP">
<TD WIDTH="157" ALIGN="LEFT">
<IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">
<A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A>
<BR>
<IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>
<TD>
<FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD ALIGN="RIGHT">
<IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD WIDTH="100%">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD BGCOLOR="#CC9966" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="LEFT">
<IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="CENTER">
<A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>
<TD VALIGN="CENTER">
<FONT FACE="Helvetica" SIZE="1">
<INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>
<TD VALIGN="CENTER">
<INPUT TYPE="image" SRC="/images/search.button.gif"
value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
<P>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
<TR VALIGN="TOP">
<TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55"
HEIGHT="18" ALT=""></TD>
<TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">
<A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" ALT="Online Training" BORDER=0></A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<!-- End PageTop Insert -->
<!-- Start NavBar Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT">
<!-- tab categories -->
<TR>
<TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News & Events from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD>
</TR>
<TR><TD> </TD></TR>
<!-- End NavBar Insert -->
<!-- START SUB-NAV -->
<TR>
<TD><!-- INSERT SUB-NAV INFO -->
</TD>
</TR>
<!-- END SUB-NAV -->
</TABLE>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD>
<!-- Template Version 2.0 -->
<!-- ================== -->
<!-- Start Main Content -->
<!-- ================== -->
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="TOP">
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="top"></A>
<DIV ALIGN="RIGHT">
<FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT>
<H2>Writing Advanced Applications<BR>
<FONT SIZE="3">Chapter 7 Continued: Analyzing Stack Traces</FONT></H2>
<FONT SIZE="-1">[<A HREF="awt.html"><<BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="version.html">NEXT>></A>]</FONT></DIV>
<P>
Stack traces have often been considered a mystery to developers.
There is little or no documentation available, and when you get one
or need to generate one, time is always at a premium. The next
sections uncover the secrets to debugging stack traces, and by the end,
you might consider a stack trace to be a helpful tool for analyzing other
programs--not just broken ones!
<P>
What is a stack trace produced by the Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> platform? It is a user
friendly snapshot
of the threads and monitors in a Java<A HREF="#TJVM"><SUP>1</SUP></A> VM. Depending on how complex your
application or applet is, a stack trace can range from fifty lines to
thousands of lines of diagnostics.
<P>
Regardless of the size of the stack trace, there are a few key
things that anyone can find to help diagnose most software problems,
whether you are an expert or very new to the Java platform.
<P>
There are three popular ways to generate a stack trace: sending
a signal to the Java VM; the Java VM generates a stack
trace for you; or using debugging tools or API calls.
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#signal">Sending a Signal to the Java VM</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#generate">The Java VM Generates a Stack Trace</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#tools">Using Debugging Tools or API Calls</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#first">What to Look For First</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#release">Which Release Generated the Stack Trace?</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#platform">Which Platform Generated the Stack Trace?</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#package">Which Thread Package Was Used?</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#states">What are the Thread States</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#monitors">Examining Monitors</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#practice">Putting the Steps Into Practice</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#checklist">Expert's Checklist</A></FONT>
</UL>
<HR>
<A NAME="signal"></A>
<H3>Sending a signal to the Java VM</H3>
On UNIX platforms you can send a signal to a program with the kill
command. This is the quit signal, which is handled by the Java
Virtual Machine (VM).
<P>
<STRONG>Unix Systems:</STRONG>
<P>
For example, on the Solaris<FONT SIZE="-2"><SUP>TM</SUP></FONT>
platform, you can use the <CODE>kill -QUIT process_id</CODE> command,
where <CODE>process_id</CODE> is the process number of your program.
<P>
Alternately, you can enter the key sequence <CODE><ctrl>\ </CODE>in
the window where the program started.
<P>
Sending this signal instructs a signal handler in the Java VM to recursively
print out all the information on the threads and monitor inside the Java
VM.
<P>
<STRONG>Windows 95/NT:</STRONG>
<P>
To generate a stack trace on the Windows 95 or Windows NT platforms, enter
the key sequence <CODE><ctrl><break></CODE> in the window where the
program is running.
<A NAME="generate"></A>
<H3>The Java VM Generates a Stack Trace</H3>
If the Java VM experienced an internal error such as a segmentation violation
or an illegal page fault, it calls its own signal handler to print out the
threads and monitor information.
<A NAME="tools"></A>
<H3>Using Debugging Tools or API Calls</H3>
You can generate a partial stack trace, (which in this case is
only the
threads information) by using the <CODE>Thread.dumpStack</CODE> method,
or the <CODE>printStackTrace</CODE> method of the Throwable class.
<P>
You can also obtain similar information by entering
<CODE>where</CODE> inside the Java debugger.
<P>
If you are successful at generating a stack trace, you should see
something similar to this <A HREF="trace1.trc">stack trace</A>.
</FONT>
<PRE>
strings core | grep JAVA_HOME
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
In the Java 2 software release, threads that called methods resulting
in a call to native code are indicated in the stack trace.
<A NAME="release"></A>
<H3>Which Release Generated The Stack Trace?</H3>
In the Java 2 release the stack trace contains the Java Virtual Machine version string, the same information you see when using the <CODE>-version</CODE> paramater.
<P>
However if there is no version string, you can stilltake a pretty good guess at
which release this stack trace came from. Obviously, if you generated
the stack trace yourself this should not be much of an issue, but you may
see a stack trace posted on a newsgroup or in an email article.
<P>
First identify where the Registered Monitor Dump section is in the stack
trace:
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">If you see a <CODE>utf8 hash table lock</CODE> in the Registered
Monitor Dump, this is a Java 2 platform stack trace. The final release of
the Java 2 platform
also contains a version string so if a version string is missing this stack
trace may be from a Java 2 beta release.</FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">If you see a <CODE>JNI pinning lock </CODE>and
no <CODE>utf8 hash lock</CODE>, this is a JDK 1.1+ release.</FONT>
</UL>
If neither of these appears in the Registered Monitor Dump, it
is probably a JDK 1.0.2 release.
<A NAME="platform"></A>
<H3>Which Platform Generated the Stack Trace?</H3>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -