Merge remote-tracking branch 'origin/v0.6'
[platform/upstream/nodejs.git] / doc / api / debugger.markdown
1 ## Debugger
2
3 V8 comes with an extensive debugger which is accessible 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     < debugger listening on port 5858
10     connecting... ok
11     break in /home/indutny/Code/git/indutny/myscript.js:1
12       1 x = 5;
13       2 setTimeout(function () {
14       3   debugger;
15     debug>
16
17 Node's debugger client doesn't support the full range of commands, but
18 simple step and inspection is possible. By putting the statement `debugger;`
19 into the source code of your script, you will enable a breakpoint.
20
21 For example, suppose `myscript.js` looked like this:
22
23     // myscript.js
24     x = 5;
25     setTimeout(function () {
26       debugger;
27       console.log("world");
28     }, 1000);
29     console.log("hello");
30
31 Then once the debugger is run, it will break on line 4.
32
33     % node debug myscript.js
34     < debugger listening on port 5858
35     connecting... ok
36     break in /home/indutny/Code/git/indutny/myscript.js:1
37       1 x = 5;
38       2 setTimeout(function () {
39       3   debugger;
40     debug> cont
41     < hello
42     break in /home/indutny/Code/git/indutny/myscript.js:3
43       1 x = 5;
44       2 setTimeout(function () {
45       3   debugger;
46       4   console.log("world");
47       5 }, 1000);
48     debug> next
49     break in /home/indutny/Code/git/indutny/myscript.js:4
50       2 setTimeout(function () {
51       3   debugger;
52       4   console.log("world");
53       5 }, 1000);
54       6 console.log("hello");
55     debug> repl
56     Press Ctrl + C to leave debug repl
57     > x
58     5
59     > 2+2
60     4
61     debug> next
62     < world
63     break in /home/indutny/Code/git/indutny/myscript.js:5
64       3   debugger;
65       4   console.log("world");
66       5 }, 1000);
67       6 console.log("hello");
68       7
69     debug> quit
70     %
71
72
73 The `repl` command allows you to evaluate code remotely. The `next` command
74 steps over to the next line. There are a few other commands available and more
75 to come. Type `help` to see others.
76
77 ### Watchers
78
79 You can watch expression and variable values while debugging your code.
80 On every breakpoint each expression from the watchers list will be evaluated
81 in the current context and displayed just before the breakpoint's source code
82 listing.
83
84 To start watching an expression, type `watch("my_expression")`. `watchers`
85 prints the active watchers. To remove a watcher, type
86 `unwatch("my_expression")`.
87
88 ### Commands reference
89
90 #### Stepping
91
92 * `cont`, `c` - Continue execution
93 * `next`, `n` - Step next
94 * `step`, `s` - Step in
95 * `out`, `o` - Step out
96 * `pause` - Pause running code (like pause button in Developer TOols)
97
98 #### Breakpoints
99
100 * `setBreakpoint()`, `sb()` - Set breakpoint on current line
101 * `setBreakpoint(line)`, `sb(line)` - Set breakpoint on specific line
102 * `setBreakpoint('fn()')`, `sb(...)` - Set breakpoint on a first statement in
103 functions body
104 * `setBreakpoint('script.js', 1)`, `sb(...)` - Set breakpoint on first line of
105 script.js
106 * `clearBreakpoint`, `cb(...)` - Clear breakpoint
107
108 #### Info
109
110 * `backtrace`, `bt` - Print backtrace of current execution frame
111 * `list(5)` - List scripts source code with 5 line context (5 lines before and
112 after)
113 * `watch(expr)` - Add expression to watch list
114 * `unwatch(expr)` - Remove expression from watch list
115 * `watchers` - List all watchers and their values (automatically listed on each
116 breakpoint)
117 * `repl` - Open debugger's repl for evaluation in debugging script's context
118
119 #### Execution control
120
121 * `run` - Run script (automatically runs on debugger's start)
122 * `restart` - Restart script
123 * `kill` - Kill script
124
125 #### Various
126
127 * `scripts` - List all loaded scripts
128 * `version` - Display v8's version
129
130 ### Advanced Usage
131
132 The V8 debugger can be enabled and accessed either by starting Node with
133 the `--debug` command-line flag or by signaling an existing Node process
134 with `SIGUSR1`.
135
136