?
This document uses PHP Chinese website manual Release
穩(wěn)定性: 3 - 穩(wěn)定
V8 提供了強大的調(diào)試工具,可以通過 <a rel="nofollow" href="http://code.google.com/p/v8/wiki/DebuggerProtocol"">TCP protocol 從外部訪問。Node 內(nèi)置這個調(diào)試工具客戶端。要使用這個調(diào)試器,以debug
參數(shù)啟動 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)試器不支持所有的命令,但是簡單的步進和檢查還是可以的。在代碼里嵌入 debugger;
,可以設置斷點。
例如, myscript.js
代碼如下:
// myscript.js x = 5; setTimeout(function () { debugger; console.log("world"); }, 1000); console.log("hello");
如果啟動 debugger,它會斷在第四行:
% 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í)行遠程代碼;next
能步進到下一行。此外可以輸入 help
查看哪些命令可用。
調(diào)試的時候可以查看表達式和變量。每個斷點處,監(jiān)視器都會顯示上下文。
輸入 watch("my_expression")
開始監(jiān)視表達式,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
- 暫停 (類似開發(fā)工具的暫停按鈕)
setBreakpoint()
, sb()
- 當前行設置斷點
setBreakpoint(line)
, sb(line)
- 在指定行設置斷點
setBreakpoint('fn()')
, sb(...)
- 在函數(shù)里的第一行設置斷點
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
- 打印當前執(zhí)行框架的backtrace
list(5)
- 顯示腳本代碼的 5 行上下文(之前 5 行和之后 5 行)
watch(expr)
- 監(jiān)視列表里添加表達式
unwatch(expr)
- 從監(jiān)視列表里刪除表達式
watchers
- 顯示所有的監(jiān)視器和它們的值(每個斷點都會自動列出)
repl
- 在所調(diào)試的腳本的上下文中,打開調(diào)試器的 repl
run
- 運行腳本 (開始調(diào)試的時候自動運行)
restart
- 重新運行腳本
kill
- 殺死腳本
scripts
- 列出所有已經(jīng)加載的腳本
version
- 顯示 v8 版本
V8 調(diào)試器可以用兩種方法啟用和訪問,--debug
命令啟動調(diào)試,或向已經(jīng)啟動 Node 發(fā)送 SIGUSR1
。
一旦一個進程進入調(diào)試模式,它可以被 node 調(diào)試器連接。調(diào)試器可以通過pid
或 URI 來連接。
node debug -p <pid>
- 通過 pid
連接進程
node debug <URI>
- 通過 URI (比如localhost:5858) 連接進程w