doc refactor: debugger
[platform/upstream/nodejs.git] / doc / api / debugger.markdown
1 # Debugger
2
3 <!-- type=concept -->
4
5 V8 comes with an extensive debugger which is accessible out-of-process via a
6 simple [TCP protocol](http://code.google.com/p/v8/wiki/DebuggerProtocol).
7 Node has a built-in client for this debugger. To use this, start Node with the
8 `debug` argument; a prompt will appear:
9
10     % node debug myscript.js
11     < debugger listening on port 5858
12     connecting... ok
13     break in /home/indutny/Code/git/indutny/myscript.js:1
14       1 x = 5;
15       2 setTimeout(function () {
16       3   debugger;
17     debug>
18
19 Node's debugger client doesn't support the full range of commands, but
20 simple step and inspection is possible. By putting the statement `debugger;`
21 into the source code of your script, you will enable a breakpoint.
22
23 For example, suppose `myscript.js` looked like this:
24
25     // myscript.js
26     x = 5;
27     setTimeout(function () {
28       debugger;
29       console.log("world");
30     }, 1000);
31     console.log("hello");
32
33 Then once the debugger is run, it will break on line 4.
34
35     % node debug myscript.js
36     < debugger listening on port 5858
37     connecting... ok
38     break in /home/indutny/Code/git/indutny/myscript.js:1
39       1 x = 5;
40       2 setTimeout(function () {
41       3   debugger;
42     debug> cont
43     < hello
44     break in /home/indutny/Code/git/indutny/myscript.js:3
45       1 x = 5;
46       2 setTimeout(function () {
47       3   debugger;
48       4   console.log("world");
49       5 }, 1000);
50     debug> next
51     break in /home/indutny/Code/git/indutny/myscript.js:4
52       2 setTimeout(function () {
53       3   debugger;
54       4   console.log("world");
55       5 }, 1000);
56       6 console.log("hello");
57     debug> repl
58     Press Ctrl + C to leave debug repl
59     > x
60     5
61     > 2+2
62     4
63     debug> next
64     < world
65     break in /home/indutny/Code/git/indutny/myscript.js:5
66       3   debugger;
67       4   console.log("world");
68       5 }, 1000);
69       6 console.log("hello");
70       7
71     debug> quit
72     %
73
74
75 The `repl` command allows you to evaluate code remotely. The `next` command
76 steps over to the next line. There are a few other commands available and more
77 to come. Type `help` to see others.
78
79 ## Watchers
80
81 You can watch expression and variable values while debugging your code.
82 On every breakpoint each expression from the watchers list will be evaluated
83 in the current context and displayed just before the breakpoint's source code
84 listing.
85
86 To start watching an expression, type `watch("my_expression")`. `watchers`
87 prints the active watchers. To remove a watcher, type
88 `unwatch("my_expression")`.
89
90 ## Commands reference
91
92 ### Stepping
93
94 * `cont`, `c` - Continue execution
95 * `next`, `n` - Step next
96 * `step`, `s` - Step in
97 * `out`, `o` - Step out
98
99 ### Breakpoints
100
101 * `setBreakpoint()`, `sb()` - Set breakpoint on current 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`.