# 調(diào)試器
~~~
穩(wěn)定度: 3 - 穩(wěn)定
~~~
V8 提供了一個(gè)強(qiáng)大的調(diào)試器,可以通過(guò) [TCP 協(xié)議](http://code.google.com/p/v8/wiki/DebuggerProtocol)從外部訪問(wèn)。Node 內(nèi)建了這個(gè)調(diào)試器的客戶端。要使用調(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)和檢查是可行的。通過(guò)腳本的源代碼中放置 `debugger;` 語(yǔ)句,您便可啟用一個(gè)斷點(diǎn)。
比如,假設(shè)有一個(gè)類似這樣的 `myscript.js`:
~~~
// myscript.js
x = 5;
setTimeout(function () {
debugger;
console.log("world");
}, 1000);
console.log("hello");
~~~
那么,當(dāng)調(diào)試器運(yùn)行時(shí),它會(huì)在第 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` 命令允許您遠(yuǎn)程執(zhí)行代碼;`next` 命令步進(jìn)到下一行。此外還有一些其它命令,輸入 `help` 查看。
### 監(jiān)視器
調(diào)試代碼時(shí)您可以監(jiān)視表達(dá)式或變量值。在每個(gè)斷點(diǎn)中監(jiān)視器列表中的各個(gè)表達(dá)式會(huì)被以當(dāng)前上下文執(zhí)行,并在斷點(diǎn)的源代碼前顯示。
輸入 `watch("my_expression")` 開(kāi)始監(jiān)視一個(gè)表達(dá)式;`watchers` 顯示活動(dòng)監(jiān)視器;`unwatch("my_expression")` 移除一個(gè)監(jiān)視器。
### 命令參考
### 步進(jìn)
- `cont`, `c` - 繼續(xù)執(zhí)行
- `next`, `n` - Step next
- `step`, `s` - Step in
- `out`, `o` - Step out
- `pause` - 暫停執(zhí)行代碼(類似開(kāi)發(fā)者工具中的暫停按鈕)
### 斷點(diǎn)
- `setBreakpoint()`, `sb()` - 在當(dāng)前行設(shè)置斷點(diǎn)
- `setBreakpoint(line)`, `sb(line)` - 在指定行設(shè)置斷點(diǎn)
- `setBreakpoint('fn()')`, `sb(...)` - 在函數(shù)體的第一條語(yǔ)句設(shè)置斷點(diǎn)
- `setBreakpoint('script.js', 1)`, `sb(...)` - 在 script.js 的第一行設(shè)置斷點(diǎn)
- `clearBreakpoint`, `cb(...)` - 清除斷點(diǎn)
在一個(gè)尚未被加載的文件(模塊)中設(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í)行框架的回溯
- `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 執(zhí)行代碼
### 執(zhí)行控制
- `run` - 運(yùn)行腳本(調(diào)試器開(kāi)始時(shí)自動(dòng)運(yùn)行)
- `restart` - 重新運(yùn)行腳本
- `kill` - 終止腳本
### 雜項(xiàng)
- `scripts` - 列出所有已加載的腳本
- `version` - 顯示 V8 的版本
### 高級(jí)使用
V8 調(diào)試器可以從兩種方式啟用和訪問(wèn):以 `--debug` 命令行標(biāo)志啟動(dòng) Node;或者向已存在的 Node 進(jìn)程發(fā)送 `SIGUSR1` 信號(hào)。
一旦一個(gè)進(jìn)程進(jìn)入了調(diào)試模式,它便可被 Node 調(diào)試器連接。調(diào)試器可以通過(guò) `pid` 或 URI 來(lái)連接,語(yǔ)法是:
- `node debug -p <pid>` - 通過(guò) `pid` 連接進(jìn)程
- `node debug <URI>` - 通過(guò)類似 localhost:5858 的 URI 連接進(jìn)程
- 關(guān)于本文檔
- 概述
- 斷言 (assert)
- Buffer
- Addons插件
- 子進(jìn)程
- 集群
- 控制臺(tái)
- 加密(Crypto)
- 調(diào)試器
- DNS
- 域
- 事件 (Events)
- File System
- 全局對(duì)象
- HTTP
- HTTPS
- Modules
- net
- 操作系統(tǒng)
- 路徑 (Path)
- process
- punycode
- Query String
- Readline
- REPL
- Smalloc
- 流
- 字符串解碼器
- 定時(shí)器
- TLS (SSL)
- TTY
- UDP / 數(shù)據(jù)報(bào)套接字
- URL
- utils
- 執(zhí)行 JavaScript
- Zlib
- 進(jìn)度
- 感謝
