Table of Contents
debugger principle
vscode debugger 进阶
彩蛋
总结
Home Web Front-end JS Tutorial Let's talk about the usage skills of debugger in vscode

Let's talk about the usage skills of debugger in vscode

Aug 11, 2021 am 10:08 AM
debugger vscode

Let's talk about the usage skills of debugger in vscode

Learning nodejs What is the most important thing? Maybe everyone has their own answer.

I think that in addition to mastering the basic API and some commonly used packages when learning nodejs, the most important ability is to learn to use the debugger. Because when the process is complex, breakpoint debugging can help you better clarify the logic, and when there are bugs, you can also locate the problem faster. [Recommended learning: "nodejs tutorial", "vscode tutorial"]

Uncle Wolf said that whether you can use debugger is important to distinguish a programmer's nodejs level logo.

This article shares the principles of debugger and vscode debugger usage techniques.

debugger principle

When running nodejs code, if you bring --inspect (can break points) or - -inspect-brk (can break points and break on the first line) parameter, then it will start in debugger mode:

Lets talk about the usage skills of debugger in vscode

can be seen , node started a web socket server, the address is: ws://127.0.0.1:9229/78637688-e8e0-4582-80cc-47655f4bff66

Why does the debugger start a websocket server?

The meaning of debugger is to stop somewhere, run it in a single step, and view variables in the environment. So how to set breakpoints and expose the variables of the current context is to start a websocket server. At this time, you only need to start a websocket client and connect to the server to debug the nodejs code.

v8 debug protocol

After connecting, how do the debugger server and debugger client communicate? This involves the v8 debug protocol.

Communicate through a format that can be recognized by both parties, for example:

Set a breakpoint at line 100 of test.js:

{
    "seq":118,
    "type":"request",
    "command":"setbreakpoint",
    "arguments":{
        "type":"script",
        "target":"test.js",
        "line":100
    }
}
Copy after login

Then view the variables of the current scope:

{
    "seq":117,
    "type":"request",
    "command":"scope"
}
Copy after login

Execute an expression:

{
    "seq":118,
    "type":"request",
    "command":"evaluate",
    "arguments":{
        "expression":"a()"
    }
}
Copy after login

Continue to run after:

{
    "seq":117,
    "type":"request",
    "command":"continue"
}
Copy after login

In this way, the client can tell the debugger server how to execute the code.

Lets talk about the usage skills of debugger in vscode

debugger client

debugger client usually has a ui (of course, in the command line Debugging through commands is also possible, but generally not done). Common js debugger clients include chrome devtools and vscode debugger.

We write a simple js script and run it through node --inspect-brk:

Lets talk about the usage skills of debugger in vscode

You can see that it starts on port 9229,

Then, we connect to it through two clients respectively.

chrome devtools

Enter chrome://inspect in the chrome address bar, and then click configure to configure the target port:

Lets talk about the usage skills of debugger in vscode

Fill in the port 9229 just now:

Lets talk about the usage skills of debugger in vscode

Then you can see that chrome scanned the target, click inspect to connect This debugger server.

Lets talk about the usage skills of debugger in vscode

Lets talk about the usage skills of debugger in vscode

After that, you can set breakpoints, single-step execution, execute expressions, view scope variables, etc. These functions are all through This is implemented using the v8 debug protocol.

vscode debugger

Writing code in vscode, debugging in chrome devtools is more troublesome, vscode also supports debugger, you can directly Use vscode to debug.

The way to use vscode debugging capabilities is to modify the .vscode/launch.json configuration in the project root directory.

attach

Click the button in the lower right corner to add a configuration item. Here choose the attach of nodejs:

Lets talk about the usage skills of debugger in vscode

Because the debugger server of websocket has been started through node --inspect-brk, then you only need to start the websocket client, and then attach to port 9229. .

Lets talk about the usage skills of debugger in vscode

点击左侧的按钮,就可以连上 debugger server 开始调试:

Lets talk about the usage skills of debugger in vscode

launch

这样先通过 node --inspect-brk 启动 debugger server,然后再添加 vscode debug 配置来连接上太麻烦了,能不能把这两步合并呢?

当然可以,只要添加一个 launch 的配置:

1Lets talk about the usage skills of debugger in vscode

1Lets talk about the usage skills of debugger in vscode

这里的 type 是 launch,就是启动 debgger server 并且启动一个 debugger client 连接上该 server。运行的程序是根目录下的 index2.js,还可以设置 stopOnEntry 来在首行断住。

点击调试,就可以看到能够成功的调试该 js 文件。

1Lets talk about the usage skills of debugger in vscode

vscode 会启动 debugger server,然后启动 debugger client 自动连接上该 server,这些都不需要我们去关心。

这样我们就可以成功的使用 vscode debugger 来调试 nodejs 代码。

vscode debugger 进阶

debugger client 中我们最常用的还是 vscode,这里着重讲一下 vscode debugger 的各种场景下的配置。

sourcemap

如果调试 ts 代码,肯定不能调试编译后的代码,要能够映射回源码,这个是 sourcemap 做的事情。调试工具都支持 sourcemap,比如 chrome devtools 和 vscode debugger,都支持文件末尾的 sourcemap url 的解析:

//# sourceMappingURL=index.js.map
Copy after login

这样当调试 index.js的时候,如果它是 ts 编译的出来的,就会自动找到对应的 ts。

当然,如果调试配置里面直接指定了 ts,那么要能够调试需要再配置 outFiles,告诉 vscode 去哪里找 sourcemap。

1Lets talk about the usage skills of debugger in vscode

这样,在 ts 源码中打的断点和在编译出的 js 打的断点都能生效。

多进程调试

当代码中有子进程的时候,就有了第二条控制流,需要再启动一个 debugger。

比如 vscode,它是基于 electron,需要启动一个主进程,一些渲染进程。主进程是通过 launch 启动的,而渲染进程则是后来 attach 的。

主进程启动的时候,通过 --remote-debugging-port 来指定子进程自动的时候的 debugger server 的端口。

1Lets talk about the usage skills of debugger in vscode

outFiles 来指定 sourcemap 的位置,这样才可以直接调试 ts 源码。runtimeExecutable 是用 vscode 的运行时替代掉了 nodejs(一般不需要设置)。

然后渲染进程是后面启动的,我们通过参数配置了会启动在 9222 端口,那么只要 attach 到那个端口就可以调试该进程了。

1Lets talk about the usage skills of debugger in vscode

vscode 支持多 target 调试,也就是可以在 vscode 里面同时启动 多个 debugger。可以切换不同的 debugger 来调试不同的进程。

1Lets talk about the usage skills of debugger in vscode

彩蛋

debugger 只能打断点么,不是的,它还可以这么用,加日志,不污染源码。

1Lets talk about the usage skills of debugger in vscode

1Lets talk about the usage skills of debugger in vscode

Lets talk about the usage skills of debugger in vscode

2Lets talk about the usage skills of debugger in vscode

总结

debugger 的使用是一项很重要的能力,对于 nodejs 水平的提升很有帮助。

nodejs debugger 的原理是 js 引擎会启动 debugger server(websocket),等待客户端连接,我们可以通过各种 debugger client 连上来进行调试,比如 chrome devtools、vscode debugger。

To debug nodejs code, it is better to use vscode debugger (of course, sometimes chrome devtools is also used for debugging, and memory analysis based on chrome devtools' memory is very helpful when locating memory leak problems).

The use of vscode debugger is mainly to add debugging configuration in .vscode/launch.json.

Debugging configuration is divided into launch and attach:

  • launch will start the debugger server and connect to it with the debugger client
  • attach just starts the debugger client and connects to the existing one debugger server, so you need to specify the port

Commonly used specific configuration items are:

  • outFiles specifies the location of the sourcemap, which is used to debug ts source code and other codes that need to be compiled.
  • stopOnEntry Stop on the first line
  • args to specify some command line parameters
  • runtimeExecutable needs to be specified when the runtime is not nodejs, such as vscode or some other runtimes

Based on these configurations, we can debug nodejs code in various scenarios, which need to be compiled, or multiple processes.

It is no exaggeration to say that if you are familiar with the use of debugger, it will be much easier to understand various nodejs codes. I hope this article can help everyone understand the principle of debugger and be able to use chrome devtools or vscode debugger to debug nodejs code. Know how to debug with sourcemap and multiple processes.

Original address: https://juejin.cn/post/6981820158046109703

Author: zxg_God said there must be light

More programming For related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of Let's talk about the usage skills of debugger in vscode. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
How to define header files for vscode How to define header files for vscode Apr 15, 2025 pm 09:09 PM

How to define header files using Visual Studio Code? Create a header file and declare symbols in the header file using the .h or .hpp suffix name (such as classes, functions, variables) Compile the program using the #include directive to include the header file in the source file. The header file will be included and the declared symbols are available.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

How to solve the problem of vscode Chinese annotations becoming question marks How to solve the problem of vscode Chinese annotations becoming question marks Apr 15, 2025 pm 11:36 PM

How to solve the problem that Chinese comments in Visual Studio Code become question marks: Check the file encoding and make sure it is "UTF-8 without BOM". Change the font to a font that supports Chinese characters, such as "Song Style" or "Microsoft Yahei". Reinstall the font. Enable Unicode support. Upgrade VSCode, restart the computer, and recreate the source file.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

Common commands for vscode terminal Common commands for vscode terminal Apr 15, 2025 pm 10:06 PM

Common commands for VS Code terminals include: Clear the terminal screen (clear), list the current directory file (ls), change the current working directory (cd), print the current working directory path (pwd), create a new directory (mkdir), delete empty directory (rmdir), create a new file (touch) delete a file or directory (rm), copy a file or directory (cp), move or rename a file or directory (mv) display file content (cat) view file content and scroll (less) view file content only scroll down (more) display the first few lines of the file (head)

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

See all articles