返回首页 io.js API 中文文档
DNS
Net
OS
TTY
URL
V8
VM

Debugger

稳定度: 2 - 稳定

V8 自带了一个强大的调试器,可以从外部通过 TCP 协议访问。io.js为这个调试器内建了一个客户端。要使用它的话,使用debug参数启动io.js;会出现提示符:

% iojs 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>

io.js的调试器客户端并未支持所有的命令,但是简单的步进和调试都是可以的。通过在源代码就放置debugger;语句,你可以启用一个断点。

例如,假设又一个这样的myscript.js

// myscript.js
x = 5;
setTimeout(function () {
  debugger;
  console.log("world");
}, 1000);
console.log("hello");

那么一旦你打开调试器,它会在第四行中断。

% iojs 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
%

repl命令允许你远程地执行代码。next命令步进到下一行。还有一些其他的可用命令,输入help查看它们。

Watchers

在调试代码时,你可监视表达式和变量的值。在每个断点,监视器列表上的每个表达式会被在当前上下文执行,并且断点的源代码前展示。

为了开始监视一个表达式,输入watch("my_expression")watchers打印可用的监视器。为了移除一个监视器,输入unwatch("my_expression")

命令参考

Stepping

  • cont, c - 继续执行
  • next, n - 下一步
  • step, s - 介入(Step in)
  • out, o - 离开(Step out)
  • pause - 暂停代码执行(类似开发者工具中的暂停按钮)

Breakpoints

  • setBreakpoint(), sb() - 在当前行设置一个断点
  • setBreakpoint(line), sb(line) - 在指定行设置一个断点
  • setBreakpoint('fn()'), sb(...) - 在函数体的第一个语句上设置断点
  • setBreakpoint('script.js', 1), sb(...) - 在script.js的第一行设置断点
  • clearBreakpoint('script.js', 1), cb(...) - 清除script.js第一行的断点

同样也可以在一个还未载入的文件(模块)中设置断点:

% ./iojs 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>

Info

  • backtrace, bt - 打印当前执行框架的回溯
  • list(5) - 列出脚本源代码的5行上下文(前5行和后5行)
  • watch(expr) - 为监视列表添加表达式
  • unwatch(expr) - 从监视列表中移除表达式
  • watchers - 列出所有的监视器和它们的值(会在每一个断点自动列出)
  • repl - 在所调试的脚本上下文中打开调试器的repl

Execution control

  • run - 运行脚本(在调试器开始时自动运行)
  • restart - 重启脚本
  • kill - 结束脚本

Various

  • scripts - 列出所有载入的脚本
  • version - 展示 V8 版本

高级使用

V8调试器可以通过 使用--debug命令行参数打开io.js 或 向一个已存在的io.js进程发送SIGUSR1信号 来启用。

一旦一个进程被设置为了调试模式,它就可以被连接到io.js调试器。可以通过 pid 或 URI 来连接,语法为:

  • iojs debug -p - 通过 pid 连接进程
  • iojs debug - 通过 URI(如localhost:5858)连接进程
上一篇: Crypto 下一篇: DNS