Node.js Debugger模块

  • Debugger
    • Watchers
    • Command reference
      • Stepping
      • Breakpoints
      • Information
      • Execution control
      • Various
    • Advanced Usage
    • V8 Inspector Integration for Node.js

Node.js includes a full-featured out-of-process debugging utility accessible via a simple TCP-based protocol and built-in debugging client. To use it, start Node.js with the debug argument followed by the path to the script to debug; a prompt will be displayed indicating successful launch of the debugger:

Node.js包含一个全特性的进程外的调试工具,可以通过一个简单的基于TCP协议和编译的调试客户端来访问。为了使用它,需要带debug参数地来启动Node.js,接着跟着待调试的脚本路径,会显示一个提示符来指示调试器是否启动成功。

$ node debug myscript.js
< debugger listening on port 5858
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
  1 x = 5;
  2 setTimeout(() => {
  3   debugger;
debug>

Node.js’s debugger client is not a full-featured debugger, but simple step and inspection are possible.

Inserting the statement debugger; into the source code of a script will enable a breakpoint at that position in the code:

Node.js调试器的客户端不是一个全特性的调试器,但是支持单步和检查。

插入debugger语句到脚本的源码中,可以在代码中的该位置启用断点。

// myscript.js
var x = 5;
setTimeout(() => {
  debugger;
  console.log('world');
}, 1000);
console.log('hello');

Once the debugger is run, a breakpoint will occur at line 4:

一旦调试器启动,断点就会在第4行被触发。

$ node debug myscript.js
< debugger listening on port 5858
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
  1 //myscript.js
  2 var x = 5;
  3 setTimeout(() => {
  4   debugger;
debug> cont
< hello
break in /home/indutny/Code/git/indutny/myscript.js:4
  2 var x = 5;
  3 setTimeout(() => {
  4   debugger;
  5   console.log('world');
  6 }, 1000);
debug> backtrace
#0 setTimeout myscript.js:4:3
debug> list(1)
  3 setTimeout(() => {
  4   debugger;
  5   console.log('world');
debug> list(2)
  2 var x = 5;
  3 setTimeout(() => {
  4   debugger;
  5   console.log('world');
  6 }, 1000);
debug> next
break in /home/indutny/Code/git/indutny/myscript.js:4
  2 setTimeout(() => {
  3   debugger;
  4   console.log('world');
  5 }, 1000);
  6 console.log('hello');
debug> repl
Press Ctrl + C to leave debug repl
> x
5
> 2+2
4
debug> next
< world
break in /home/indutny/Code/git/indutny/myscript.js:5
  3   debugger;
  4   console.log('world');
  5 }, 1000);
  6 console.log('hello');
  7
debug> quit

The repl command allows code to be evaluated remotely. The next command steps to the next line. Type help to see what other commands are available.

Pressing enter without typing a command will repeat the previous debugger command.

repl命令允许代码在远程被执行。next命令执行到下一行。键入help命令来查看有什么其它可用的命令。

按下回车键而不键入命令将会重发先前的调试命令。

Watchers

It is possible to watch expression and variable values while debugging. On every breakpoint, each expression from the watchers list will be evaluated in the current context and displayed immediately before the breakpoint’s source code listing.

To begin watching an expression, type watch('my_expression'). The command watchers will print the active watchers. To remove a watcher, type unwatch('my_expression').

当调试的时候,查看表达式或者变量值也是可以得。在每一个断点处,观察列表中当前上下文的每一个表达式都会在列出断点源码之前被计算并立即显示。

要查看某个表达式,需要键入watch('my_expression')。命令观察者就会打印出有效的观察者。移除观察者可以键入unwatch('my_expression')

Command reference

Stepping

  • cont, c - Continue execution
  • next, n - Step next
  • step, s - Step in
  • out, o - Step out
  • pause - Pause running code (like pause button in Developer Tools)

  • cont, c - 继续执行

  • next, n - 调到下一步
  • step, s - 跳进语句块
  • out, o - 跳出语句块
  • pause - 暂停正在执行的代码 (就像开发工具中的暂停按钮)

Breakpoints

  • setBreakpoint(), sb() - Set breakpoint on current line
  • setBreakpoint(line), sb(line) - Set breakpoint on specific line
  • setBreakpoint('fn()'), sb(…)` - Set breakpoint on a first statement in functions body
  • setBreakpoint('script.js', 1), sb(...) - Set breakpoint on first line of script.js
  • clearBreakpoint('script.js', 1), cb(...) - Clear breakpoint in script.js on line 1

It is also possible to set a breakpoint in a file (module) that isn’t loaded yet:

  • setBreakpoint(), sb() - 将断点设置在当前行。
  • setBreakpoint(line), sb(line) - 将断点设置在指定的行。
  • setBreakpoint('fn()'), sb(...) - 在函数体的第一条语句设置断点。
  • setBreakpoint('script.js', 1), sb(...) - 在script.js的第1行设置断点。
  • clearBreakpoint('script.js', 1), cb(...) - 清除script.js第1行的断点。

也可以将断点设置在一个未加载的文件(或模块)。

$ ./node debug test/fixtures/break-in-module/main.js
< debugger listening on port 5858
connecting to port 5858... ok
break in test/fixtures/break-in-module/main.js:1
  1 var mod = require('./mod.js');
  2 mod.hello();
  3 mod.hello();
debug> setBreakpoint('mod.js', 23)
Warning: script 'mod.js' was not loaded yet.
  1 var mod = require('./mod.js');
  2 mod.hello();
  3 mod.hello();
debug> c
break in test/fixtures/break-in-module/mod.js:23
 21
 22 exports.hello = () => {
 23   return 'hello from module';
 24 };
 25
debug>

Information

  • backtrace, bt - Print backtrace of current execution frame
  • list(5) - List scripts source code with 5 line context (5 lines before and after)
  • watch(expr) - Add expression to watch list
  • unwatch(expr) - Remove expression from watch list
  • watchers - List all watchers and their values (automatically listed on each breakpoint)
  • repl - Open debugger’s repl for evaluation in debugging script’s context
  • exec expr - Execute an expression in debugging script’s context
  • backtrace, bt - 打印当前执行框架的轨道信息。
  • list(5) - 列出当前断点的脚本代码的5行上下文(列出前5行和后5行)
  • watch(expr) - 将表达式添加到观察列表中
  • unwatch(expr) - 从观察列表中移除某个表达式
  • watchers - 列出所有观察值和它们的值(自动列出每一个断点)
  • repl - 在调试脚本上下文环境中打开调试器repl进行求值(repl中变量的生命周期仅仅在repl中,不会污染源代码上下文)
  • exec expr - 在调试脚本上下文中执行某个表达式(该值的生命周期仅仅在该条语句,不会污染源代码上下文)

Execution control

  • run - Run script (automatically runs on debugger’s start)
  • restart - Restart script
  • kill - Kill script
  • run - 运行脚本 (在调试器启动时会自动运行)
  • restart - 重新启动脚本
  • kill - 杀死脚本

Various

- scripts - List all loaded scripts
- version - Display V8’s version

  • scripts - 显示所有加载的脚本
  • version - 显示V8的版本

Advanced Usage

An alternative way of enabling and accessing the debugger is to start Node.js with the --debug command-line flag or by signaling an existing Node.js process with SIGUSR1.

Once a process has been set in debug mode this way, it can be inspected using the Node.js debugger by either connecting to the pid of the running process or via URI reference to the listening debugger:

  • node debug -p <pid> - Connects to the process via the pid
  • node debug <URI> - Connects to the process via the URI such as localhost:5858

一个备选的方式来启用和访问调试器是带--debug命令行标志的方式启动Node.js或者发送给一个已经存在的Node.js进程SIGUSR1信号。

一旦一个进程被使用该方法设置为调试模式,它就可以被连接到该pid的运行中的线程,或通过URI引用该监听的调试器检查。

  • node debug -p <pid> 通过pid连接到相应进程。
  • node debug <URI> 通过诸如localhost:5858的URI连接到进程上。

SIGUSR1/2: 用户定义的信号。

V8 Inspector Integration for Node.js

NOTE: This is an experimental feature.

V8 Inspector integration allows attaching Chrome DevTools to Node.js instances for debugging and profiling.

V8 Inspector can be enabled by passing the --inspect flag when starting a Node.js application. It is also possible to supply a custom port with that flag, e.g. --inspect=9222 will accept DevTools connections on port 9222.

To break on the first line of the application code, provide the --debug-brk flag in addition to --inspect.

注意:这是一个实验特性。

V8检察一体化工具允许附加到Chrome浏览器的开发工具的Node.js实例上来调试和分析。

在启动一个Node.js应用时,V8检查器可以通过传递--inspect标志来启用。这个标记也可以提供一个自定义的端口,例如--inspet=9222,它会接受开发工具连接到9222端口上。

在应用代码的首行中断,需要在--inspect的基础上添加--debug-brk标记。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值