标签:博客 csdn api javascript node.js
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 的调试器客户端并未完整支持所有命令,但简单的步进和检查是可行的。通过脚本的源代码中放置debugger; 语句,您便可启用一个断点。例如,假设有一个类似这样的 myscript.js:
// myscript.js
x = 5;
setTimeout(function () {
debugger;
console.log("world");
}, 1000);
console.log("hello");
那么,当调试器运行时,它会在第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
%
repl 命令允许您远程执行代码;next 命令步进到下一行。此外还有一些其它命令,输入 help 查看。
输入 watch("my_expression") 开始监视一个表达式;watchers 显示活动监视器;unwatch("my_expression") 移除一个监视器。
cont, c -
继续执行next, n -
Step nextstep, s -
Step inout, o -
Step outpause - 暂停执行代码(类似开发者工具中的暂停按钮)setBreakpoint(), sb() -
在当前行设置断点setBreakpoint(line), sb(line) -
在指定行设置断点setBreakpoint(‘fn()‘), sb(...) -
在函数体的第一条语句设置断点setBreakpoint(‘script.js‘, 1), sb(...) -
在 script.js 的第一行设置断点clearBreakpoint, cb(...) -
清除断点
% ./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 -
显示当前执行框架的回溯list(5) - 显示脚本源代码的 5 行上下文(之前 5 行和之后 5 行)watch(expr) - 向监视列表添加表达式unwatch(expr) - 从监视列表移除表达式watchers - 列出所有监视器和它们的值(每个断点会自动列出)repl - 在所调试的脚本的上下文中打开调试器的 repl 执行代码run - 运行脚本(调试器开始时自动运行)restart - 重新运行脚本kill - 终止脚本scripts - 列出所有已加载的脚本version - 显示 V8 的版本--debug 命令行标志启动
Node;或者向已存在的 Node 进程发送SIGUSR1 信号。一旦一个进程进入了调试模式,它便可被
Node 调试器连接。调试器可以通过 pid 或
URI 来连接,语法是:
node debug -p <pid> - 通过 pid 连接进程node debug <URI> - 通过类似 localhost:5858 的 URI 连接进程Node.js v0.10.31API手册-Debugger
标签:博客 csdn api javascript node.js
原文地址:http://blog.csdn.net/livialiu1234/article/details/40084393