⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch07.html

📁 java2高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
Button</EM>, use <EM CLASS="CODE">stop in java.awt.Button.&lt;init&gt;(java.lang.String)</EM>. </P><P CLASS="Body"><A NAME="pgfId-1087680"></A>Later releases of <EM CLASS="CODE">jdb</EM> let you choose the desired method as a numbered option.</P><P CLASS="Body"><A NAME="pgfId-1087682"></A><EM CLASS="Bold">The </EM><EM CLASS="C-Code">cont</EM><EM CLASS="Bold"> Command. </EM><A NAME="marker-1087681"></A>To continue the <EM CLASS="CODE">jdb</EM> session, use the <EM CLASS="CODE">cont</EM> command. The next time the program creates a <EM CLASS="CODE">Button</EM> with a <EM CLASS="CODE">String</EM> as the constructor, <EM CLASS="CODE">jdb</EM> stops so that you can examine the output. </P><PRE CLASS="CODE"><A NAME="pgfId-1087683"></A>main[1] cont </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087684"></A>main[1] </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087685"></A>Breakpoint hit: java.awt.Button.&lt;init&gt; (Button:130)</PRE><P CLASS="Body"><A NAME="pgfId-1087686"></A>If the <EM CLASS="CODE">Button</EM> class had not been recompiled with debug information as described earlier, you would not see the internal fields from the <EM CLASS="CODE">print</EM> command. </P><P CLASS="Body"><A NAME="pgfId-1087689"></A><EM CLASS="Bold">Clearing Breakpoints. </EM><A NAME="marker-1087687"></A><A NAME="marker-1087688"></A>To clear this breakpoint and not stop every time a <EM CLASS="CODE">Button</EM> is created, use the <EM CLASS="CODE">clear</EM> command. This example uses the <EM CLASS="CODE">clear</EM> command with no arguments to display the list of current breakpoints and the <EM CLASS="CODE">clear</EM> command with the <EM CLASS="CODE">java.awt.Button:130</EM> argument to clear the <EM CLASS="CODE">java.awt.Button:130</EM> breakpoint. </P><PRE CLASS="CODE"><A NAME="pgfId-1087690"></A>main[1] clearCurrent breakpoints set:SimpleJdbTest:10java.awt.Button:130main[1] clear java.awt.Button:130Breakpoint cleared at java.awt.Button: 130</PRE><P CLASS="Body"><A NAME="pgfId-1087692"></A><EM CLASS="Bold">Displaying Object Details. </EM><A NAME="marker-1087691"></A>To display details about an object, use the <EM CLASS="CODE">print</EM> command to call the object's <EM CLASS="CODE">toString</EM> method, or use the <EM CLASS="CODE">dump</EM> command to display the object's fields and values. </P><P CLASS="Body"><A NAME="pgfId-1087693"></A>This example puts a breakpoint at line 17 and uses the <EM CLASS="CODE">print</EM> and <EM CLASS="CODE">dump</EM> commands to print and dump the first <EM CLASS="CODE">Button</EM> object in the array of <EM CLASS="CODE">Button</EM> objects. The <EM CLASS="CODE">dump</EM> command output has been abbreviated. </P><PRE CLASS="CODE"><A NAME="pgfId-1087694"></A>main[1] stop at SimpleJdbTest:17Breakpoint set at SimpleJdbTest:17main[1] contmain[1]Breakpoint hit: SimpleJdbTest.setup (SimpleJdbTest:17)main[1] print b[0]b[0] = java.awt.Button[button1,0,0,0x0,invalid,label=press]main[1] dump b[0]b[0] = (java.awt.Button)0x163 {private int componentSerializedDataVersion = 2boolean isPacked = falseprivate java.beans.PropertyChangeSupport changeSupport = nulllong eventMask = 4096transient java.awt.event.InputMethodListener 			inputMethodListener = null....java.lang.String actionCommand = nulljava.lang.String label = press}</PRE><P CLASS="Body"><A NAME="pgfId-1087696"></A><EM CLASS="Bold">Ending the Session. </EM><A NAME="marker-1087695"></A>That finishes the simple <EM CLASS="CODE">jdb</EM> examples. To terminate the <EM CLASS="CODE">jdb</EM> session, use the <EM CLASS="CODE">quit</EM> command: </P><PRE CLASS="CODE"><A NAME="pgfId-1087697"></A>0xee2f9820:class(SimpleJdbTest)&gt; quit</PRE></DIV></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087699"></A><A NAME="58498"></A>Remote Debugging</H5><P CLASS="Body"><A NAME="pgfId-1087702"></A><A NAME="marker-1087700"></A><A NAME="marker-1087701"></A>The <EM CLASS="CODE">jdb</EM> tool is an external process debugger, which means it debugs the program by sending messages to and from a helper inside the Java virtual machine. This makes it easy to debug a running program and helps you debug a program that interacts with the end user. A remote debug session from the command line does not interfere with the normal operation of the application. </P><P CLASS="Body"><A NAME="pgfId-1087703"></A><EM CLASS="Bold">Starting the Session. </EM>Before the Java 2 release, the only thing required to enable remote debugging was to start the program with the <EM CLASS="CODE">-debug</EM> flag as the first argument, and if the application uses native libraries, make the library name end in <EM CLASS="CODE">_g</EM>. For example, you would need to copy <EM CLASS="CODE">nativelib.dll</EM> to <EM CLASS="CODE">nativelib_g.dll</EM> to debug with that library. </P><P CLASS="Body"><A NAME="pgfId-1087704"></A>In Java 2, choices are a little more complicated. You need to tell the Java virtual machine where the <EM CLASS="CODE">tools.jar</EM> file is with the <EM CLASS="CODE">CLASSPATH</EM> variable. The <EM CLASS="CODE">tools.jar</EM> file contains noncore class files to support tools and utilities in the SDK. It is normally found in the <EM CLASS="CODE">lib</EM> directory of the Java platform installation. </P><P CLASS="Body"><A NAME="pgfId-1087706"></A>You also need to disable the <A NAME="marker-1087705"></A>Just In Time (JIT) compiler if one exists. The JIT compiler is disabled by setting the <EM CLASS="CODE">java.compiler</EM> property to <EM CLASS="CODE">NONE</EM> or to an empty string. Finally, as the <EM CLASS="CODE">-classpath</EM> option overrides any previously set user classpath, you also need to add the <EM CLASS="CODE">CLASSPATH</EM> needed by your application. </P><P CLASS="Body"><A NAME="pgfId-1087707"></A>Putting all of this together, here is the command line needed to start a program in remote debug mode. Put this all on one line and include all the classes you need on the command line. </P><DIV><H6 CLASS="D"><A NAME="pgfId-1087708"></A><EM CLASS="Bold">Windows:</EM></H6><PRE CLASS="CODE"><A NAME="pgfId-1087709"></A>$ java -debug -classpath C:&#92;java&#92;lib&#92;tools.jar;. -Djava.compiler=NONE SimpleJdbTest Agent password=4gk5hm</PRE></DIV><DIV><H6 CLASS="D"><A NAME="pgfId-1087710"></A><EM CLASS="Bold">Unix:</EM></H6><PRE CLASS="CODE"><A NAME="pgfId-1087711"></A>$ java -debug -classpath /usr/java/lib/tools.jar:. -Djava.compiler=NONE SimpleJdbTest Agent password=5ufhic</PRE><P CLASS="Body"><A NAME="pgfId-1087714"></A>The output is the <A NAME="marker-1087712"></A><A NAME="marker-1087713"></A>agent password (in this case, <EM CLASS="CODE">4gk5hm</EM>) if the program was successfully started. The agent password is supplied when starting <EM CLASS="CODE">jdb</EM> so <EM CLASS="CODE">jdb</EM> can find the corresponding application started in debug mode on that machine. </P><P CLASS="Body"><A NAME="pgfId-1087716"></A><A NAME="marker-1087715"></A>To start <EM CLASS="CODE">jdb</EM> in remote debug mode, supply a host name, which can be either the machine where the remote program was started or <EM CLASS="CODE">localhost</EM> if you are debugging on the same machine as the remote program, and the agent password. </P><PRE CLASS="CODE"><A NAME="pgfId-1087717"></A>jdb -host localhost -password 4gk5hm</PRE><P CLASS="Body"><A NAME="pgfId-1087719"></A><EM CLASS="Bold">Listing Threads. </EM><A NAME="marker-1087718"></A>Once inside the <EM CLASS="CODE">jdb</EM> session, you can list the currently active threads with the <EM CLASS="CODE">threads</EM> command and use the <EM CLASS="CODE">thread &lt;threadnumber&gt;</EM> command (for example, <EM CLASS="CODE">thread 7)</EM> to select the thread to analyze. Once the thread is selected, use the <EM CLASS="CODE">where</EM> command to see which methods have been called for this thread. </P><PRE CLASS="CODE"><A NAME="pgfId-1087720"></A>$ jdb -host arsenal -password 5ufhic</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087721"></A>Initializing jdb...</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087722"></A>&gt; threads</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087723"></A>Group system:</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087724"></A>1. (java.lang.Thread)0x9        Signal dispatcher                                cond. waiting</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087725"></A>2. (java.lang.ref.Reference     0xb Reference Handler</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087726"></A>      $ReferenceHandler)        cond. waiting</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087727"></A>3. (java.lang.ref.              Finalizer</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087728"></A>      Finalizer                cond. waiting</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087729"></A>      $FinalizerThread)0xd</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087730"></A>       </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087731"></A>4. (java.lang.Thread)0xe       Debugger agent</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087732"></A>                               running</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087733"></A>5. (sun.tools.agent.           Breakpoint handler</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087734"></A>      Handler)0x10             cond. waiting</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087735"></A>6. (sun.tools.agent.           Step handler</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087736"></A>      StepHandler)0x12         cond. waiting</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087737"></A>Group main:</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087738"></A>7. (java.awt.                  AWT-EventQueue-0</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087739"></A>       EventDispatchThread)    cond. waiting</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087740"></A>       0x19</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087741"></A>8. (sun.awt.                    PostEventQueue-0</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087742"></A>      PostEventQueue)0x1b       cond. waiting</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087743"></A>9. (java.lang.Thread)0x1c       AWT-Motif</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087744"></A>                                running</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087745"></A>10. (java.lang.Thread)0x1d      TimerQueue</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087746"></A>                                cond. waiting</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087747"></A>11. (sun.awt.                   Screen Updater</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087748"></A>       ScreenUpdater)0x1f       cond. waiting</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087749"></A>12. (java.lang.Thread)0x20      Thread-0</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087750"></A>                                cond. waiting</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087751"></A>&gt; thread 7</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087752"></A>AWT-EventQueue-0[1] where</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087753"></A>  [1] java.lang.Object.wait (native method)</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087754"></A>  [2] java.lang.Object.wait (Object:424)</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087755"></A>  [3] java.awt.EventQueue.getNextEvent (EventQueue:179)</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087756"></A>  [4] java.awt.EventDispatchThread.run (EventDispatchThread:67)</PRE><P CLASS="Body"><A NAME="pgfId-1087758"></A><EM CLASS="Bold">Listing Source. </EM><A NAME="marker-1087757"></A>To list the source, the thread needs to be suspended using the <EM CLASS="CODE">suspend</EM> command. To let this thread continue, use the <EM CLASS="CODE">resume</EM> command. The example uses <EM CLASS="CODE">resume 7.</EM> </P><PRE CLASS="CODE"><A NAME="pgfId-1087759"></A>AWT-EventQueue-0[1] suspend 7</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087760"></A>AWT-EventQueue-0[1] list </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087761"></A>Current method is native </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087762"></A>AWT-EventQueue-0[1] where</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087763"></A>   [1] java.lang.Object.wait (native method)</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087764"></A>   [2] java.lang.Object.wait (Object:424)</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087765"></A>   [3] java.awt.EventQueue.getNextEvent   (EventQueue:179)</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087766"></A>   [4] java.awt.EventDispatchThread.run (EventDispatchThread:67)</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087767"></A>AWT-EventQueue-0[1] resume 7</PRE><P CLASS="Body"><A NAME="pgfId-1087769"></A><EM CLASS="Bold">Ending the Session. </EM><A NAME="marker-1087768"></A>When you finish debugging this program remotely, clear any remaining breakpoints before quitting the debug session. To get a list of remaining breakpoints use the <EM CLASS="CODE">clear</EM> command, and to remove them, enter <EM CLASS="CODE">clear class:linenumber</EM> as follows: </P><PRE CLASS="CODE"><A NAME="pgfId-1087770"></A>main[1] clearCurrent breakpoints set:SimpleJdbTest:10main[1] clear SimpleJdbTest:10main[1] quit</PRE></DIV></DIV><DIV><H5 CLASS="B">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -