Merge remote branch 'origin/v0.4'
[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
97 #### Breakpoints
98
99 * `setBreakpoint()`, `sb()` - Set breakpoint on current line
100 * `setBreakpoint('fn()')`, `sb(...)` - Set breakpoint on a first statement in
101 functions body
102 * `setBreakpoint('script.js', 1)`, `sb(...)` - Set breakpoint on first line of
103 script.js
104 * `clearBreakpoint`, `cb(...)` - Clear breakpoint
105
106 #### Info
107
108 * `backtrace`, `bt` - Print backtrace of current execution frame
109 * `list(5)` - List scripts source code with 5 line context (5 lines before and
110 after)
111 * `watch(expr)` - Add expression to watch list
112 * `unwatch(expr)` - Remove expression from watch list
113 * `watchers` - List all watchers and their values (automatically listed on each
114 breakpoint)
115 * `repl` - Open debugger's repl for evaluation in debugging script's context
116
117 #### Execution control
118
119 * `run` - Run script (automatically runs on debugger's start)
120 * `restart` - Restart script
121 * `kill` - Kill script
122
123 #### Various
124
125 * `scripts` - List all loaded scripts
126 * `version` - Display v8's version
127
128 ### Advanced Usage
129
130 The V8 debugger can be enabled and accessed either by starting Node with
131 the `--debug` command-line flag or by signaling an existing Node process
132 with `SIGUSR1`.
133
134