Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

node の debug を活用する

個人用メモです。
どこからでも確認できるように public な場所に置いていますが
特に人に見せることを意識してまとめていません。

node debug

node には debug 機能が組み込まれています。

サンプル

コード

var hoge = "hoge";
hoge += hoge;
var ary = [];
ary.push(1);
ary.push(2);

試行

$ node inspect index.js
Break on start in index.js:1
> 1 (function (exports, require, module, __filename, __dirname) { var hoge = "hoge";
  2 hoge += hoge;
  3 var ary = [];
debug> help
run, restart, r       Run the application or reconnect
kill                  Kill a running application or disconnect

cont, c               Resume execution
next, n               Continue to next line in current file
step, s               Step into, potentially entering a function
out, o                Step out, leaving the current function
backtrace, bt         Print the current backtrace
list                  Print the source around the current line where execution
                      is currently paused

setBreakpoint, sb     Set a breakpoint
clearBreakpoint, cb   Clear a breakpoint
breakpoints           List all known breakpoints
breakOnException      Pause execution whenever an exception is thrown
breakOnUncaught       Pause execution whenever an exception isn't caught
breakOnNone           Don't pause on exceptions (this is the default)

watch(expr)           Start watching the given expression
unwatch(expr)         Stop watching an expression
watchers              Print all watched expressions and their current values

exec(expr)            Evaluate the expression and print the value
repl                  Enter a debug repl that works like exec

scripts               List application scripts that are currently loaded
scripts(true)         List all scripts (including node-internals)

profile               Start CPU profiling session.
profileEnd            Stop current CPU profiling session.
profiles              Array of completed CPU profiling sessions.
profiles[n].save(filepath = 'node.cpuprofile')
                      Save CPU profiling session to disk as JSON.

takeHeapSnapshot(filepath = 'node.heapsnapshot')
                      Take a heap snapshot and save to disk as JSON.
debug> exec hoge
undefined
debug> n
break in index.js:1
> 1 (function (exports, require, module, __filename, __dirname) { var hoge = "hoge";
  2 hoge += hoge;
  3 var ary = [];
debug> exec hoge
undefined
debug> n
break in index.js:2
  1 (function (exports, require, module, __filename, __dirname) { var hoge = "hoge";
> 2 hoge += hoge;
  3 var ary = [];
  4 ary.push(1);
debug> exec hoge
'hoge'
debug> n
break in index.js:3
  1 (function (exports, require, module, __filename, __dirname) { var hoge = "hoge";
  2 hoge += hoge;
> 3 var ary = [];
  4 ary.push(1);
  5 ary.push(2);
debug> exec hoge
'hogehoge'
debug> list(1)
  2 hoge += hoge;
> 3 var ary = [];
  4 ary.push(1);
$ node --inspect-brk index.js

chrome://inspect/ を開く

f:id:tbpg:20170809144608p:plain

関連情報