Node.js v0.10.18 手册 & 文档
调试器#
稳定度: 3 - 稳定
V8 comes with an extensive debugger which is accessible out-of-process via a
simple TCP protocol.
Node has a built-in client for this debugger. To use this, start Node with the
debug
argument; a prompt will appear:
V8 提供了一个强大的调试器,可以通过 TCP 协议从外部访问。Node 内建了这个调试器的客户端。要使用调试器,以 debug
参数启动 Node,出现提示符:
% node debug myscript.js
< debugger listening on port 5858
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
1 x = 5;
2 setTimeout(function () {
3 debugger;
debug>
Node's debugger client doesn't support the full range of commands, but
simple step and inspection is possible. By putting the statement debugger;
into the source code of your script, you will enable a breakpoint.
Node 的调试器客户端并未完整支持所有命令,但简单的步进和检查是可行的。通过脚本的源代码中放置 debugger;
语句,您便可启用一个断点。
For example, suppose myscript.js
looked like this:
比如,假设有一个类似这样的 myscript.js
:
// myscript.js
x = 5;
setTimeout(function () {
debugger;
console.log("world");
}, 1000);
console.log("hello");
Then once the debugger is run, it will break on line 4.
那么,当调试器运行时,它会在第 4 行中断:
% node debug myscript.js
< debugger listening on port 5858
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
1 x = 5;
2 setTimeout(function () {
3 debugger;
debug> cont
< hello
break in /home/indutny/Code/git/indutny/myscript.js:3
1 x = 5;
2 setTimeout(function () {
3 debugger;
4 console.log("world");
5 }, 1000);
debug> next
break in /home/indutny/Code/git/indutny/myscript.js:4
2 setTimeout(function () {
3 debugger;
4 console.log("world");
5 }, 1000);
6 console.log("hello");
debug> repl
Press Ctrl + C to leave debug repl
> x
5
> 2+2
4
debug> next
< world
break in /home/indutny/Code/git/indutny/myscript.js:5
3 debugger;
4 console.log("world");
5 }, 1000);
6 console.log("hello");
7
debug> quit
%
The repl
command allows you to evaluate code remotely. The next
command
steps over to the next line. There are a few other commands available and more
to come. Type help
to see others.
repl
命令允许您远程执行代码;next
命令步进到下一行。此外还有一些其它命令,输入 help
查看。
监视器#
You can watch expression and variable values while debugging your code. On every breakpoint each expression from the watchers list will be evaluated in the current context and displayed just before the breakpoint's source code listing.
调试代码时您可以监视表达式或变量值。在每个断点中监视器列表中的各个表达式会被以当前上下文执行,并在断点的源代码前显示。
To start watching an expression, type watch("my_expression")
. watchers
prints the active watchers. To remove a watcher, type
unwatch("my_expression")
.
输入 watch("my_expression")
开始监视一个表达式;watchers
显示活动监视器;unwatch("my_expression")
移除一个监视器。
命令参考#
步进#
cont
,c
- Continue executionnext
,n
- Step nextstep
,s
- Step inout
,o
- Step outpause
- Pause running code (like pause button in Developer Tools)cont
,c
- 继续执行next
,n
- Step nextstep
,s
- Step inout
,o
- Step outpause
- 暂停执行代码(类似开发者工具中的暂停按钮)
断点#
setBreakpoint()
,sb()
- Set breakpoint on current linesetBreakpoint(line)
,sb(line)
- Set breakpoint on specific linesetBreakpoint('fn()')
,sb(...)
- Set breakpoint on a first statement in functions bodysetBreakpoint('script.js', 1)
,sb(...)
- Set breakpoint on first line of script.jsclearBreakpoint
,cb(...)
- Clear breakpointsetBreakpoint()
,sb()
- 在当前行设置断点setBreakpoint(line)
,sb(line)
- 在指定行设置断点setBreakpoint('fn()')
,sb(...)
- 在函数体的第一条语句设置断点setBreakpoint('script.js', 1)
,sb(...)
- 在 script.js 的第一行设置断点clearBreakpoint
,cb(...)
- 清除断点
It is also possible to set a breakpoint in a file (module) that isn't loaded yet:
在一个尚未被加载的文件(模块)中设置断点也是可行的:
% ./node debug test/fixtures/break-in-module/main.js
< debugger listening on port 5858
connecting to port 5858... ok
break in test/fixtures/break-in-module/main.js:1
1 var mod = require('./mod.js');
2 mod.hello();
3 mod.hello();
debug> setBreakpoint('mod.js', 23)
Warning: script 'mod.js' was not loaded yet.
1 var mod = require('./mod.js');
2 mod.hello();
3 mod.hello();
debug> c
break in test/fixtures/break-in-module/mod.js:23
21
22 exports.hello = function() {
23 return 'hello from module';
24 };
25
debug>
信息#
backtrace
,bt
- Print backtrace of current execution framelist(5)
- List scripts source code with 5 line context (5 lines before and after)watch(expr)
- Add expression to watch listunwatch(expr)
- Remove expression from watch listwatchers
- List all watchers and their values (automatically listed on each breakpoint)repl
- Open debugger's repl for evaluation in debugging script's contextbacktrace
,bt
- 显示当前执行框架的回溯list(5)
- 显示脚本源代码的 5 行上下文(之前 5 行和之后 5 行)watch(expr)
- 向监视列表添加表达式unwatch(expr)
- 从监视列表移除表达式watchers
- 列出所有监视器和它们的值(每个断点会自动列出)repl
- 在所调试的脚本的上下文中打开调试器的 repl 执行代码
执行控制#
run
- Run script (automatically runs on debugger's start)restart
- Restart scriptkill
- Kill scriptrun
- 运行脚本(调试器开始时自动运行)restart
- 重新运行脚本kill
- 终止脚本
杂项#
scripts
- List all loaded scriptsversion
- Display v8's versionscripts
- 列出所有已加载的脚本version
- 显示 V8 的版本
高级使用#
The V8 debugger can be enabled and accessed either by starting Node with
the --debug
command-line flag or by signaling an existing Node process
with SIGUSR1
.
V8 调试器可以从两种方式启用和访问:以 --debug
命令行标志启动 Node;或者向已存在的 Node 进程发送 SIGUSR1
信号。
Once a process has been set in debug mode with this it can be connected to
with the node debugger. Either connect to the pid
or the URI to the debugger.
The syntax is:
一旦一个进程进入了调试模式,它便可被 Node 调试器连接。调试器可以通过 pid
或 URI 来连接,语法是:
node debug -p <pid>
- Connects to the process via thepid
node debug <URI>
- Connects to the process via the URI such as localhost:5858
node debug -p <pid>
- 通过pid
连接进程node debug <URI>
- 通过类似 localhost:5858 的 URI 连接进程