/doc/api/debugger.markdown

https://gitlab.com/MichelZuniga/node · Markdown · 170 lines · 139 code · 31 blank · 0 comment · 0 complexity · de9e138d475bf0009d3ef5cf561b57e9 MD5 · raw file

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