Merge remote-tracking branch 'joyent/v0.12' into v1.x
[platform/upstream/nodejs.git] / doc / api / debugger.markdown
1 # Debugger
2
3     Stability: 3 - Stable
4
5 <!-- type=misc -->
6
7 V8 comes with an extensive debugger which is accessible out-of-process via a
8 simple [TCP protocol](http://code.google.com/p/v8/wiki/DebuggerProtocol).
9 Node has a built-in client for this debugger. To use this, start Node with the
10 `debug` argument; a prompt will appear:
11
12     % node debug myscript.js
13     < debugger listening on port 5858
14     connecting... ok
15     break in /home/indutny/Code/git/indutny/myscript.js:1
16       1 x = 5;
17       2 setTimeout(function () {
18       3   debugger;
19     debug>
20
21 Node's debugger client doesn't support the full range of commands, but
22 simple step and inspection is possible. By putting the statement `debugger;`
23 into the source code of your script, you will enable a breakpoint.
24
25 For example, suppose `myscript.js` looked like this:
26
27     // myscript.js
28     x = 5;
29     setTimeout(function () {
30       debugger;
31       console.log("world");
32     }, 1000);
33     console.log("hello");
34
35 Then once the debugger is run, it will break on line 4.
36
37     % node debug myscript.js
38     < debugger listening on port 5858
39     connecting... ok
40     break in /home/indutny/Code/git/indutny/myscript.js:1
41       1 x = 5;
42       2 setTimeout(function () {
43       3   debugger;
44     debug> cont
45     < hello
46     break in /home/indutny/Code/git/indutny/myscript.js:3
47       1 x = 5;
48       2 setTimeout(function () {
49       3   debugger;
50       4   console.log("world");
51       5 }, 1000);
52     debug> next
53     break in /home/indutny/Code/git/indutny/myscript.js:4
54       2 setTimeout(function () {
55       3   debugger;
56       4   console.log("world");
57       5 }, 1000);
58       6 console.log("hello");
59     debug> repl
60     Press Ctrl + C to leave debug repl
61     > x
62     5
63     > 2+2
64     4
65     debug> next
66     < world
67     break in /home/indutny/Code/git/indutny/myscript.js:5
68       3   debugger;
69       4   console.log("world");
70       5 }, 1000);
71       6 console.log("hello");
72       7
73     debug> quit
74     %
75
76
77 The `repl` command allows you to evaluate code remotely. The `next` command
78 steps over to the next line. There are a few other commands available and more
79 to come. Type `help` to see others.
80
81 ## Watchers
82
83 You can watch expression and variable values while debugging your code.
84 On every breakpoint each expression from the watchers list will be evaluated
85 in the current context and displayed just before the breakpoint's source code
86 listing.
87
88 To start watching an expression, type `watch("my_expression")`. `watchers`
89 prints the active watchers. To remove a watcher, type
90 `unwatch("my_expression")`.
91
92 ## Commands reference
93
94 ### Stepping
95
96 * `cont`, `c` - Continue execution
97 * `next`, `n` - Step next
98 * `step`, `s` - Step in
99 * `out`, `o` - Step out
100 * `pause` - Pause running code (like pause button in Developer Tools)
101
102 ### Breakpoints
103
104 * `setBreakpoint()`, `sb()` - Set breakpoint on current line
105 * `setBreakpoint(line)`, `sb(line)` - Set breakpoint on specific line
106 * `setBreakpoint('fn()')`, `sb(...)` - Set breakpoint on a first statement in
107 functions body
108 * `setBreakpoint('script.js', 1)`, `sb(...)` - Set breakpoint on first line of
109 script.js
110 * `clearBreakpoint('script.js', 1)`, `cb(...)` - Clear breakpoint in script.js
111 on line 1
112
113 It is also possible to set a breakpoint in a file (module) that
114 isn't loaded yet:
115
116     % ./node debug test/fixtures/break-in-module/main.js
117     < debugger listening on port 5858
118     connecting to port 5858... ok
119     break in test/fixtures/break-in-module/main.js:1
120       1 var mod = require('./mod.js');
121       2 mod.hello();
122       3 mod.hello();
123     debug> setBreakpoint('mod.js', 23)
124     Warning: script 'mod.js' was not loaded yet.
125       1 var mod = require('./mod.js');
126       2 mod.hello();
127       3 mod.hello();
128     debug> c
129     break in test/fixtures/break-in-module/mod.js:23
130      21
131      22 exports.hello = function() {
132      23   return 'hello from module';
133      24 };
134      25
135     debug>
136
137 ### Info
138
139 * `backtrace`, `bt` - Print backtrace of current execution frame
140 * `list(5)` - List scripts source code with 5 line context (5 lines before and
141 after)
142 * `watch(expr)` - Add expression to watch list
143 * `unwatch(expr)` - Remove expression from watch list
144 * `watchers` - List all watchers and their values (automatically listed on each
145 breakpoint)
146 * `repl` - Open debugger's repl for evaluation in debugging script's context
147
148 ### Execution control
149
150 * `run` - Run script (automatically runs on debugger's start)
151 * `restart` - Restart script
152 * `kill` - Kill script
153
154 ### Various
155
156 * `scripts` - List all loaded scripts
157 * `version` - Display v8's version
158
159 ## Advanced Usage
160
161 The V8 debugger can be enabled and accessed either by starting Node with
162 the `--debug` command-line flag or by signaling an existing Node process
163 with `SIGUSR1`.
164
165 Once a process has been set in debug mode with this it can be connected to
166 with the node debugger. Either connect to the `pid` or the URI to the debugger.
167 The syntax is:
168
169 * `node debug -p <pid>` - Connects to the process via the `pid`
170 * `node debug <URI>` - Connects to the process via the URI such as localhost:5858