Node.js v0.11.11 マニュアル & ドキュメンテーション
Table of Contents
Debugger#
Stability: 3 - Stable
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 をタイプしてください。
Watchers#
デバッグ中に式や変数の値をウォッチすることができます。 全てのブレークポイントにおいて、ウォッチリストのそれぞれの式は 現在のコンテキストで評価され、ブレークポイントのソースコードの前に 表示されます。
式のウォッチを開始するには、watch("my_expression") をタイプします。
watchers はアクティブなウォッチの一覧を表示します。
ウォッチを解除するには、unwatch("my_expression") とタイプします。
Commands reference#
Stepping#
cont,c- 実行を継続します。next,n- 次の行へステップオーバーします。step,s- ステップインします。out,o- ステップアウトします。pause- コードの実行を中断します (Developer Tools の pause ボタンと同じです。
Breakpoints#
setBreakpoint(),sb()- 現在行にブレークポイントを設定します。setBreakpoint(line),sb(line)- 指定した行にブレークポイントを設定します。setBreakpoint('fn()'),sb(...)- 指定した関数の先頭行にブレークポイントを設定しますsetBreakpoint('script.js', 1),sb(...)- 指定したスクリプトファイルの指定した行にブレークポイントを設定します。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>
Info#
backtrace,bt- 現在の実行フレームのバックトレースを表示します。list(5)- 現在の行の前後のソースコードを表示します (例では前後とも 5 行が表示されます)。watch(expr)- 式をウォッチリストに追加します。unwatch(expr)- 式をウォッチリストから削除します。watchers- ウォッチしている全ての式とその値を表示します (各ブレークポイントで自動的に表示されます)。repl- デバッグしているスクリプトをコンテキストとする REPL を開きます。
Execution control#
run- スクリプトを実行します (デバッガを開始すると自動的に実行します)。restart- スクリプトを再実行します。kill- スクリプトを終了します。
Various#
scripts- ロードされている全スクリプトの一覧を表示します。version- v8 のバージョンを表示します。
Advanced Usage#
V8 デバッガは Node をコマンドラインの --debug フラグで起動したり、起動済みの Node プロセスに SIGUSR1 シグナルを送ることでも有効にできます。
これによって一度デバッグモードに設定されたプロセスは、
pid または URI のどちらでも node デバッガに接続することができます。
形式は:
node debug -p <pid>-pidを通じてプロセスに接続node debug <URI>-localhost:585のような URI を通じてプロセスに接続