--- title: Debugger-API slug: Tools/Debugger-API translation_of: Tools/Debugger-API ---
{{ToolsSidebar}}

Debugger 接口

Mozilla 的JS引擎, SpiderMonkey, 提供了一个名为Debugger 的调试接口,开发者能够通过该接口使用JS代码观测和操纵另一端JS代码的运行. Firefox内置的开发者工具和 Firebug 插件都使用Debugger 来是实现它们的JavaScript调试器.同时,Debugger 也被广泛应用与实现其他类型的工具,例如tracers, coverage analysis, patch-and-continue等。

Debugger 有三个基本的特点:

In Gecko, the Debugger API is available to chrome code only. By design, it ought not to introduce security holes, so in principle it could be made available to content as well; but it is hard to justify the security risks of the additional attack surface.

The Debugger API cannot currently observe self-hosted JavaScript. This is not inherent in the API’s design, but simply that the self-hosting infrastructure isn’t prepared for the kind of invasions the Debugger API can perform.

Debugger 实例与影子对象

Debugger reflects every aspect of the debuggee’s state as a JavaScript value—not just actual JavaScript values like objects and primitives, but also stack frames, environments, scripts, and compilation units, which are not normally accessible as objects in their own right.

Here is a JavaScript program in the process of running a timer callback function:

A running JavaScript program and its Debugger shadows

A running JavaScript program and its Debugger shadows

This diagram shows the various types of shadow objects that make up the Debugger API (which all follow some general conventions):

The Debugger instance itself is not really a shadow of anything in the debuggee; rather, it maintains the set of global objects which are to be considered debuggees. A Debugger observes only execution taking place in the scope of these global objects. You can set functions to be called when new stack frames are pushed; when new code is loaded; and so on.

Omitted from this picture are Debugger.Source instances, which represent JavaScript compilation units. A Debugger.Source can furnish a full copy of its source code, and explain how the code entered the system, whether via a call to eval, a <script> element, or otherwise. A Debugger.Script points to the Debugger.Source from which it is derived.

Also omitted is the Debugger’s Debugger.Memory instance, which holds methods and accessors for observing the debuggee’s memory use.

All these types follow some general conventions, which you should look through before drilling down into any particular type’s specification.

All shadow objects are unique per Debugger and per referent. For a given Debugger, there is exactly one Debugger.Object that refers to a particular debuggee object; exactly one Debugger.Frame for a particular stack frame; and so on. Thus, a tool can store metadata about a shadow’s referent as a property on the shadow itself, and count on finding that metadata again if it comes across the same referent. And since shadows are per-Debugger, tools can do so without worrying about interfering with other tools that use their own Debugger instances.

Examples

Here are some things you can try out yourself that show off some of Debugger’s features:

Gecko-specific features

While the Debugger core API deals only with concepts common to any JavaScript implementation, it also includes some Gecko-specific features:

Source Metadata

Generated from file:
js/src/doc/Debugger/Debugger-API.md
Watermark:
sha256:6ee2381145a0d2e53d2f798f3f682e82dd7ab0caa0ac4dd5e56601c2e49913a7
Changeset:
ffa775dd5bd4