Launch
和 attach的区别
Launch的话是直接以debug模式运行一个js文件,遇到debugger后会停止
而
Attach需要先开启一个运行在调试模式开启了debug端口的nodejs项目
资料:
https://code.visualstudio.com/docs/nodejs/nodejs-debugging
Debugger是如何工作的:
有系统调用叫ptrace,可以用一个进程去监视控制另一个进程的执行,甚至让他暂停。获取另一个进程执行的调用栈.. 然后就可以构造出一个debugger。
https://www.cnblogs.com/moonz-wu/archive/2012/01/15/2322120.html
https://i5ting.github.io/node-debug-tutorial/
https://stackoverflow.com/questions/42563900/how-does-the-visual-studio-attach-to-process-work
根据nodejs文档描述,
Node.js includes an out-of-process debugging utility accessible via a V8Inspector and built-in debugging client. To use it, start Node.js with the inspect
argument followed by the path to the script to debug; a prompt will be displayed indicating successful launch of the debugger:
nodejs集成了V8 inspector(可以和Chrome devltools protocol的debug客户端来交互)
还内置了一个内建debug客户端。
1.Nodejs内置了一个简易的debugger客户端
使用node inspect xxx.js 开始进行调试
2.如果想使用功能更全的debugger客户端
先使用—inspect标记 开启V8 inspector,
此时就可以和一个实现了Chrome DevTools Protocol的debugger客户端来交互
(例如VsCode中的 attach 调试方式)
或直接打开的Chrome浏览器客户端
资料:
1.https://nodejs.org/en/docs/guides/debugging-getting-started/
Enable Inspector
When started with the --inspect
switch, a Node.js process listens for a debugging client. By default, it will listen at host and port 127.0.0.1:9229. Each process is also assigned a unique UUID.
Inspector clients must know and specify host address, port, and UUID to connect. A full URL will look something like ws://127.0.0.1:9229/0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e
.
Node.js will also start listening for debugging messages if it receives a SIGUSR1
signal. (SIGUSR1
is not available on Windows.) In Node.js 7 and earlier, this activates the legacy Debugger API. In Node.js 8 and later, it will activate the Inspector API.
2.https://nodejs.org/api/debugger.html
3.https://juejin.im/post/5e042967e51d45584d239e86