Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

在Venkman中使用断点

这篇文章包含在以Venkman入门开篇的一系列关于Venkman的文档中。

调试任何一种编程语言的一个基本任务是设置“断点”。断点表示了代码的执行在什么位置暂停。当你在像Venkman这类调试程序中设置断点时,你可以暂停程序的执行,来检查变量,对象和其它的运行参数。

这篇文章描述了在JavaScript中的断点,和如何在Venkman中设置和使用断点。

基本的断点

“Stop”按钮和debugger命令是JavaScript调试器中很关键的要素,但是当你深入调试极其复杂的代码时,你会发现使用基本的工具已经无能为力,这时你需要断点来帮忙了。

断点的类型

Venkman有两种类型的断点。第一种,也是最常见的,叫做“硬断点”。A hard breakpoint represents an actual trap instruction included in the pseudocode of a compiled function. Hard breakpoints can only exist in the context of a function currently "live" in the browser. Hard breakpoints are the ones that actually stop program execution.

第二种类型的断点,“将来断点”,represents a promise from Venkman to set a hard breakpoint as soon as it is possible. Future breakpoints are used when you want to stop in a script that has not yet been compiled. The most common use of future breakpoints is to stop in a "top level" script (script that executes outside of any function), or script that executes during the onLoad event of a page. When a script is loaded that matches the URL of a future breakpoint, and has executable code at the specified line, Venkman will automatically set a future breakpoint.

除了下面提到的这个差别以外,断点工作在Venkman和工作在其它调试器中的情况是很相似的。你在源代码视图左边框看到的点显示了哪一行包含了可执行的代码,同时也表明了那一行可以设置硬断点。

“Figure 1” Venkman“源代码”视图中标明的可执行代码

设置断点

在源代码视图中点击一个圆点,就会在这一行设置一个断点。Venkman会在这行代码执行前停止代码的运行。当一个断点被设置后,这一行左侧的边沿就会变成字母"B",如Figure 2。如果你在设置完断点后再次执行代码,Venkman会在第81行停止运行。

“Figure 2” 设置断点

Venkman会在“已加载脚本”视图中显示一个或更多已经设置好的断点,一个红色的圆点会出现在已设置有断点的文件旁,后面还会显示断点所在的函数的行号。

“Figure 3” 在“已加载脚本”视图中的带有断点的文件

使用断点进行调试

当你设置一个断点后,你就给了Venkman(或是任何你使用的调试器)显示执行环境信息的机会。 调试脚本或程序,最重要的表现是,它可以检查函数返回值,错误,计数器,变量作用范围,这些改变了脚本执行的路线。

在Figure 1中的onFlipX函数,例如,使用变量newSign来指明用什么图片表示fish。当你在第81行设置一个断点,你可以激活浏览器窗口的页面,Venkman停止代码的执行,当他加入了onFlipX函数。停在那里,Venkman在本地变量视图中显示了变量newSign的值"1"。

“Figure 4” 在断点处的本地变量

使用断点和交互视图,你可以改变Venkman显示的变量值(仅仅是在调试环境的上下文中),还可以观察这些改变怎样影响代码的执行。

“Figure 5” 在断点处与脚本结合

如果你想获得更多关于“调试动作”的信息,请浏览Venkman入门的调试基础章节。

清除断点

点击两次边框来清除断点。第一次单击会清除硬断点,只保留一个用字母“F”表示的将来断点。第二次单击将会清除将来断点。

“Figure 6” 将来断点

高级断点

Venkman允许你将一个断点和一段脚本关联在一起:当遇到断点时,这个脚本就会被执行。这个针对关联脚本的高级特性和其他一些选项,你可以在将来断点属性对话框中找到他们,你只需要右键单击一个断点,选择“属性”。

“Figure 7” 将来断点属性对话框

一旦你创建了一个与断点相关联的脚本,你就可以从将来断点属性对话框中选择一些不同的选项,这些选项决定了Venkman如何处理关联脚本的输出。下面是经常用到的选项:

  • “Continue regardless of result” 使Venkman在运行完断点脚本后继续执行正常的脚本。这对于试验不工作的额外脚本非常有用。
  • “Stop regardless of result” 使Venkman在运行完断点脚本后停止执行正常的脚本,这允许你检查程序的状态。
  • “Stop if result is true” 非常有效的将它当作一个条件断点。如果断点脚本返回true值(它并不必须是一个严格的布尔值true,比如非null、非空字符串、非零、非undefined、非false都是可以接受的),脚本执行会继续下去。如果返回false值,Venkman会在断点处停止脚本的执行。
  • “Early return from caller with result” will cause the function that the breakpoint is set in to return the value of the breakpoint script as its result, immediatley after the breakpoint script completes.
  • The “Pass exceptions to caller” checkbox allows you to pass exceptions thrown by the breakpoint script directly to the caller. Normally, if the breakpoint script generates an exception, Venkman assumes you made a mistake and stops execution after displaying the exception. If you would like to see what your code does when exceptions are thrown at it, check "Pass exceptions to caller", and thrown an exception from the breakpoint script.
  • The “Log result” checkbox tells Venkman you want the result of the script to show up in the Interactive Session View. When used with the “Continue regardless of result” option, the breakpoint can be used as a simple log message.
  • The number of times the breakpoint has been hit is passed in as a parameter to the breakpoint script. To reset the count, enter a 0 in the "Trigger count" field.

关键性注释

You can also embed scripted breakpoints directly into the souce code you are debugging by using a Venkman facility called meta comments . Meta comments are specially formatted comments that pull in the script named after the comment and specify how to treat the output of that script. The following meta comment types are available:

  • The //@JSD_LOG comment will insert a breakpoint which is set to execute the script that follows without stopping. The result of the evaluation will be logged to the Interactive Session.
  • The //@JSD_BREAK comment will insert a breakpoint which is set to execute the script that follows and stop if the result is true.
  • The //@JSD_EVAL command will insert a breakpoint which is set to execute the script that follows without stopping and without logging the result.
  • These meta comments can be used to insert debug-only code in your scripts with zero impact on production code.

To enable meta comments in a script, select "Scan for Meta Comments" from the context menu of the file in the Loaded Scripts view.

When you add a meta comment, a normal breakpoint is created. You can change or delete this breakpoint just as you would a breakpoint created by hand.

Resources

Original Document Information

  • Authors: Robert Ginda, Ian Oeschger
  • Published 02 May 2003

--AndyYard 01:46 2007年11月15日 (PST)

文档标签和贡献者

标签: 
 此页面的贡献者: ziyunfei, Andyyard
 最后编辑者: ziyunfei,