Merge branch 'debugger'
[platform/upstream/nodejs.git] / doc / api / debugger.markdown
1 ## Debugger
2
3 V8 comes with an extensive debugger which is accessable out-of-process via a
4 simple [TCP protocol](http://code.google.com/p/v8/wiki/DebuggerProtocol).
5 Node has a built-in client for this debugger. To use this, start Node with the
6 `debug` argument; a prompt will appear:
7
8     % node debug myscript.js
9     debug>
10
11 At this point `myscript.js` is not yet running. To start the script, enter
12 the command `run`. If everything works okay, the output should look like
13 this:
14
15     % node debug myscript.js
16     debug> run
17     debugger listening on port 5858
18     connecting...ok
19
20 Node's debugger client doesn't support the full range of commands, but
21 simple step and inspection is possible. By putting the statement `debugger;`
22 into the source code of your script, you will enable a breakpoint.
23
24 For example, suppose `myscript.js` looked like this:
25
26     // myscript.js
27     x = 5;
28     setTimeout(function () {
29       debugger;
30       console.log("world");
31     }, 1000);
32     console.log("hello");
33
34 Then once the debugger is run, it will break on line 4.
35
36     % ./node debug myscript.js
37     debug> run
38     debugger listening on port 5858
39     connecting...ok
40     hello
41     break in #<an Object>._onTimeout(), myscript.js:4
42       debugger;
43       ^
44     debug> next
45     break in #<an Object>._onTimeout(), myscript.js:5
46       console.log("world");
47       ^
48     debug> print x
49     5
50     debug> print 2+2
51     4
52     debug> next
53     world
54     break in #<an Object>._onTimeout() returning undefined, myscript.js:6
55     }, 1000);
56     ^
57     debug> quit
58     A debugging session is active. Quit anyway? (y or n) y
59     %
60
61
62 The `print` command allows you to evaluate variables. The `next` command steps
63 over to the next line. There are a few other commands available and more to
64 come type `help` to see others.
65
66
67 ### Advanced Usage
68
69 The V8 debugger can be enabled and accessed either by starting Node with
70 the `--debug` command-line flag or by signaling an existing Node process
71 with `SIGUSR1`.
72
73