新聞中心
穩(wěn)定性: 3 - 穩(wěn)定
V8提供了強(qiáng)大的調(diào)試工具,可以通過TCP protocol從外部訪問。node內(nèi)置這個(gè)調(diào)試工具客戶端。使用這個(gè)調(diào)試器的方法是,以debug參數(shù)啟動(dòng)Node.js,將會(huì)出現(xiàn)提示,指示調(diào)試器成功啟動(dòng):

% 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的調(diào)試器不支持所有的命令,但是簡(jiǎn)單的步進(jìn)和檢查還是可以的。在代碼里嵌入debugger;,可以設(shè)置斷點(diǎn)。
例:myscript.js代碼如下:
// myscript.js
x = 5;
setTimeout(function () {
debugger;
console.log("world");
}, 1000);
console.log("hello");
如果啟動(dòng)debugger,它會(huì)斷在第四行:
% 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命令能執(zhí)行遠(yuǎn)程代碼;next能步進(jìn)到下一行。此外可以輸入help查看哪些命令可用。
監(jiān)視器-Watchers
調(diào)試的時(shí)候可以查看表達(dá)式和變量。每個(gè)斷點(diǎn)處,監(jiān)視器都會(huì)顯示上下文。
輸入watch("my_expression")開始監(jiān)視表達(dá)式,watchers顯示活躍的監(jiān)視器。輸入unwatch("my_expression")可以移除監(jiān)視器。
命令參考-Commands reference
步進(jìn)-Stepping
cont,c- 繼續(xù)執(zhí)行next,n- Step nextstep,s- Step inout,o- Step outpause- 暫停 (類似開發(fā)工具的暫停按鈕)
斷點(diǎn)Breakpoints
setBreakpoint(),sb()- 當(dāng)前行設(shè)置斷點(diǎn)setBreakpoint(line),sb(line)- 在指定行設(shè)置斷點(diǎn)setBreakpoint('fn()'),sb(...)- 在函數(shù)里的第一行設(shè)置斷點(diǎn)setBreakpoint('script.js', 1),sb(...)- 在 script.js 第一行設(shè)置斷點(diǎn)。clearBreakpoint,cb(...)- 清除斷點(diǎn)
也可以在尚未加載的文件里設(shè)置斷點(diǎn):
% ./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>
信息Info
backtrace,bt- 打印當(dāng)前執(zhí)行框架的backtracelist(5)- 顯示腳本代碼的5行上下文(之前5行和之后5行)watch(expr)- 監(jiān)視列表里添加表達(dá)式unwatch(expr)- 從監(jiān)視列表里刪除表達(dá)式watchers- 顯示所有的監(jiān)視器和它們的值(每個(gè)斷點(diǎn)都會(huì)自動(dòng)列出)repl- 在所調(diào)試的腳本的上下文中,打開調(diào)試器的repl
執(zhí)行控制Execution control
run- 運(yùn)行腳本 (開始調(diào)試的時(shí)候自動(dòng)運(yùn)行)restart- 重新運(yùn)行腳本kill- 殺死腳本
雜項(xiàng)Various
scripts- 列出所有已經(jīng)加載的腳本version- 顯示v8版本
高級(jí)應(yīng)用Advanced Usage
V8調(diào)試器可以用兩種方法啟用和訪問,--debug命令啟動(dòng)調(diào)試,或向已經(jīng)啟動(dòng)Node發(fā)送SIGUSR1。
一旦一個(gè)進(jìn)程進(jìn)入調(diào)試模式,它可以被node調(diào)試器連接。調(diào)試器可以通過pid或URI來連接。
node debug -p- 通過pid連接進(jìn)程node debug- 通過URI(比如localhost:5858)連接進(jìn)程w。
網(wǎng)頁名稱:創(chuàng)新互聯(lián)Node.js教程:Node.js調(diào)試器
分享鏈接:http://www.dlmjj.cn/article/cdedpep.html


咨詢
建站咨詢
