Merge remote-tracking branch 'origin/v0.8'
[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`, `cb(...)` - Clear breakpoint
111
112 ### Info
113
114 * `backtrace`, `bt` - Print backtrace of current execution frame
115 * `list(5)` - List scripts source code with 5 line context (5 lines before and
116 after)
117 * `watch(expr)` - Add expression to watch list
118 * `unwatch(expr)` - Remove expression from watch list
119 * `watchers` - List all watchers and their values (automatically listed on each
120 breakpoint)
121 * `repl` - Open debugger's repl for evaluation in debugging script's context
122
123 ### Execution control
124
125 * `run` - Run script (automatically runs on debugger's start)
126 * `restart` - Restart script
127 * `kill` - Kill script
128
129 ### Various
130
131 * `scripts` - List all loaded scripts
132 * `version` - Display v8's version
133
134 ## Advanced Usage
135
136 The V8 debugger can be enabled and accessed either by starting Node with
137 the `--debug` command-line flag or by signaling an existing Node process
138 with `SIGUSR1`.
139
140 Once a process has been set in debug mode with this it can be connected to
141 with the node debugger. Either connect to the `pid` or the URI to the debugger.
142 The syntax is:
143
144 * `node debug -p <pid>` - Connects to the process via the `pid`
145 * `node debug <URI> - Connects to the process via the URI such as localhost:5858