?
本文檔使用 php中文網(wǎng)手冊(cè) 發(fā)布
穩(wěn)定性: 3 - 穩(wěn)定
V8 提供了強(qiáng)大的調(diào)試工具,可以通過(guò) <a rel="nofollow" href="http://code.google.com/p/v8/wiki/DebuggerProtocol"">TCP protocol 從外部訪問(wèn)。Node 內(nèi)置這個(gè)調(diào)試工具客戶端。要使用這個(gè)調(diào)試器,以debug
參數(shù)啟動(dòng) Node,出現(xiàn)提示:
% 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.jscont < 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
查看哪些命令可用。
調(diào)試的時(shí)候可以查看表達(dá)式和變量。每個(gè)斷點(diǎn)處,監(jiān)視器都會(huì)顯示上下文。
輸入 watch("my_expression")
開(kāi)始監(jiān)視表達(dá)式,watchers
顯示活躍的監(jiān)視器。輸入unwatch("my_expression")
可以移除監(jiān)視器。
cont
, c
- 繼續(xù)執(zhí)行
next
, n
- Step next
step
, s
- Step in
out
, o
- Step out
pause
- 暫停 (類似開(kāi)發(fā)工具的暫停按鈕)
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>
backtrace
, bt
- 打印當(dāng)前執(zhí)行框架的backtrace
list(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)試的腳本的上下文中,打開(kāi)調(diào)試器的 repl
run
- 運(yùn)行腳本 (開(kāi)始調(diào)試的時(shí)候自動(dòng)運(yùn)行)
restart
- 重新運(yùn)行腳本
kill
- 殺死腳本
scripts
- 列出所有已經(jīng)加載的腳本
version
- 顯示 v8 版本
V8 調(diào)試器可以用兩種方法啟用和訪問(wèn),--debug
命令啟動(dòng)調(diào)試,或向已經(jīng)啟動(dòng) Node 發(fā)送 SIGUSR1
。
一旦一個(gè)進(jìn)程進(jìn)入調(diào)試模式,它可以被 node 調(diào)試器連接。調(diào)試器可以通過(guò)pid
或 URI 來(lái)連接。
node debug -p <pid>
- 通過(guò) pid
連接進(jìn)程
node debug <URI>
- 通過(guò) URI (比如localhost:5858) 連接進(jìn)程w