【问题标题】:Adding conditional breakpoint in NodeJS debugger在 NodeJS 调试器中添加条件断点
【发布时间】:2014-02-04 14:16:53
【问题描述】:

node.js help 命令输出如下:

debug> help
Commands: run (r), cont (c), next (n), step (s), out (o), backtrace (bt), setBreakpoint (sb), clearBreakpoint (cb),
watch, unwatch, watchers, repl, restart, kill, list, scripts, breakOnException, breakpoints, version

我可以使用setBreakpoint添加一个新断点:

debug> setBreakpoint(12)
...

但是我可以让它有条件吗?例如:

*only if `foo() === true`, stop here*

替代方法是将其添加为脚本中的if

if (foo()) { debugger; }

这可以通过 NodeJS 调试器实现吗?

【问题讨论】:

    标签: node.js debugging conditional-breakpoint


    【解决方案1】:

    我相信这可以通过使用watchers 来实现。我不太熟悉它的用例或语法,所以我没有任何示例。

    文档似乎用变量名描述了这种用法。它还可能取决于我们正在谈论的 node.js 版本。

    https://nodejs.org/api/debugger.html#debugger_info

    【讨论】:

    • 嗯,使用watch 显示变量值,而不是添加断点。还是我错过了什么?
    【解决方案2】:

    使用Object.defineProperty。例如:

    function setter () { debugger; }
    
    Object.defineProperty(Math, 'name', { set: setter });
    

    如果在Math 对象上设置了name 属性,则将添加一个调试器语句。

    参考文献

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多