Node.js,如何使用GDB进行调试

12

我在谷歌上搜索后,发现以下方法可以对Node.js应用程序进行GDB调试, 使用"./configure --debug"选项构建Node.js,然后执行

  gdb --args ~/node_g start.js

我正在使用这个工具来调试一个小程序,但是在设置了断点之后,我无法看到它是否在那个函数中停止。

我的简单程序gdb_node.js如下:

 function abc() {
    console.log("In abc");
 }

 function bcd() {
    abc();
    console.log("Done abc");
 }

 bcd();

现在我正在使用gdb:

(gdb) b bcd
 Function "bcd" not defined.
 Make breakpoint pending on future shared library load? (y or [n]) y
 Breakpoint 1 (bcd) pending.
 (gdb) run
 Starting program: /Users/mayukh/node_g gdb_node.js
 Reading symbols for shared libraries  

++++......................................................................................................................................... 完成

 In abc
 Done abc

 Program exited normally.
 (gdb) 

有人能告诉我这里我错过了什么吗?

谢谢, -M-

2个回答

15

gdb 尝试在从 C++ 源代码生成的调试信息中查找 bcd 符号。似乎您实际上希望调试的是 JavaScript 而不是 C++。

V8 内置了调试器,而 node.js 有一个调试器客户端,可用于调试协议

要启动已连接到程序的调试器客户端的 node.js:

node inspect test.js

你可以使用调试器命令设置断点:

sh-3.2$ node inspect test.js
< debugger listening on port 5858
connecting... ok
break in test.js:10
  8 }
  9
 10 bcd();
 11
 12 });
debug> sb(6)
  5 function bcd() {
* 6   abc();
  7   console.log("Done abc");
  8 }
  9
 10 bcd();
 11
 12 });
debug>

或使用debugger关键字:

 function abc() {
    console.log("In abc");
 }

 function bcd() {
    debugger;
    abc();
    console.log("Done abc");
 }

 bcd();
sh-3.2$ node inspect test.js
< debugger listening on port 5858
connecting... ok
break in test.js:11
  9 }
 10
 11 bcd();
 12
 13 });
debug> c
break in test.js:6
  4
  5 function bcd() {
  6   debugger;
  7   abc();
  8   console.log("Done abc");
debug>

对于V8调试器也存在GUI客户端:node-webkit-agentnode-inspectoreclipse等。


Node v8 建议使用 node inspect 代替 node debug - morhook

1

试试Trepan-ni。它是基于Node Inspector的类似GDB的调试器。我已经使用它超过一年了,效果很好。我可以确认它适用于NodeJS v8到v14。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接