Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of Debugger in JavaScript?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the principle of Debugger in JavaScript". In daily operation, I believe that many people have doubts about the principle of Debugger in JavaScript. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "what is the principle of Debugger in JavaScript?" Next, please follow the editor to study!

What is the principle of running the code?

The way the code runs can be divided into two types: direct execution and interpretive execution.

Do not know usually you have noticed, executable files directly. / xxx can be executed, and the execution of js files need node. / xxx, the execution of python files need python. / xxx, this is the difference between compilation and execution (direct execution) and interpretation.

Direct execution

Cpu provides a set of instructions, based on which the operation of the whole computer can be controlled. The machine language code is composed of these instructions and the corresponding operands. These machine codes can be run directly on the computer, that is, they can be executed directly. The files made up of them are called executables.

Different operating systems have different formats of executable files, including pe (Portable Executable) format on windows, elf (Executable Linkable Format) format on linux and unix systems, and mash-o format on mac. They specify where different contents (.text is code, .text .bass, and so on are data) are placed in the file. But the real executable part is made up of machine instructions provided by cpu.

Compiled language will go through the stage of compilation, assembly and linking. Compilation is to convert the source code into intermediate code composed of assembly language, assembly is to turn the intermediate code into object code, and links will combine the target code into executable files. This executable file can be executed directly on the operating system. Just because it is made up of cpu machine instructions, you can directly control cpu. So it can be executed directly by. / xxx.

Interpretive execution

Compiled languages generate executable files and execute them directly on the operating system, so there is no need to install an interpreter, while the code of interpreting languages such as js and python needs to be run with an interpreter.

Why don't you need to generate machine code with an interpreter? cpu still doesn't recognize the code.

That's because the interpreter needs to be compiled into machine code, cpu knows how to execute the interpreter, and the interpreter knows how to execute the higher-level script code, so the machine code interprets and executes the interpreter, and then the interpreter interprets and executes the upper-level code, which is how scripting languages work. This is true for js, python and so on.

But after all, there is an extra layer of interpreter, so sometimes it will be compiled into machine code for direct execution, which is the JIT compiler. For example, the js engine is generally composed of parser, interpreter, JIT compiler and GC. Most of the code is interpreted and executed by the interpreter, while the hot code is compiled into machine code by the JIT compiler and executed directly on the operating system to improve performance.

Compiled into machine code to execute directly, or interpreted from the source code, the code can be executed in these two ways. Each of them has its own advantages, such as high speed of compilation and cross-platform interpretation. This is how the code runs.

Wang Yin said that the essence of a computer is an interpreter. That is to say, cpu uses circuits to interpret machine code, and the interpreter uses machine code to interpret higher-level script code, so the essence of a computer is an interpreter.

Why do you need debugger

We know that Turing's complete language can explain any computable problem, so both compiled and interpreted can describe all computable business logic.

We use different languages to describe the business logic, and then run it to see the effect. When the logic of the code is more complex, it is inevitable to make mistakes. We want to be able to run step by step or stop at a certain point. Then look at the variables in the environment at that time and execute a script. It is debugger that accomplishes this function.

There may be many junior programmers who can only use console.log to log, but the log can not fully show the environment at that time, the best way is debugger.

Uncle Wolf has said that whether or not to use debugger is a clear distinction between nodejs levels.

The principle of debugger

We know that debugger is essential for debugging programs, so how is it implemented?

Debugger of the executable file

In fact, cpu, the operating system in the design to support the ability of debugger (see the importance of debugger), cpu there are four registers can do hard interrupt, the operating system provides system calls to do soft interrupts. This is the basis for the debugger implementation of compiled languages.

Interrupt

Cpu will only continue to execute the next instruction, but in the process of running the program will inevitably have to deal with some external messages, such as io, network, exceptions and so on, so the interrupt mechanism is designed. Every time cpu executes an instruction, it will look at the interrupt flag to see if it needs to be interrupted. Just like event loop checks to see if it needs to be rendered every time it finishes loop.

INT instruction

Cpu supports INT instructions to trigger interrupts, interrupts are numbered, different numbers have different handlers, and tables that record numbers and interrupt handlers are called interrupt vector tables. Where INT 3 (interrupt No. 3) can trigger debugger, which is a convention.

So how does the executable use this No. 3 interrupt to debugger? In fact, it is to replace the content of execution at run time, and the debugger program will replace the instruction content with INT 3, that is, 0xCC, where you need to set a breakpoint, which will stop. The environment data at this time can be obtained for debugging.

By replacing the machine code with 0xcc (INT 3), the program is cut off, but how to resume execution? In fact, it is relatively simple to record the machine code replaced at that time and change it back when you need to release the breakpoint.

This is the principle of debugger of executable files, which is finally realized by the interrupt mechanism supported by cpu.

Interrupt register

The debugger implementation mentioned above is a way to modify the machine code in memory, but sometimes the code cannot be modified, such as ROM, which is done through the four DR0-DR3 provided by cpu. This is called a hard interrupt.

In short, soft interrupts in INT 3 and hard interrupts in interrupt registers are two ways for executable files to implement debugger.

Debugger of interpretive language

Because the compiled language executes directly on the operating system, it uses the interrupt mechanism and system calls of cpu and operating system to implement debugger. However, the interpretive language implements the interpretation and execution of the code itself, so it does not need that set, but the implementation idea is still the same, that is, insert a piece of code to stop, support the viewing of environmental data and the execution of the code. continue to execute when the breakpoint is released.

For example, the debugger statement is supported in javascript, and when the interpreter executes this statement, it will break.

The debugger of an interpreted language is relatively simple, and you don't need to know about cpu's INT 3 interrupts.

Debugger client

Above we saw how the debugger of code that executes directly and interprets execution is implemented respectively. We know how the code is broken, so what happens after the break? How to expose environmental data and how to execute external code?

This requires the debugger client.

For example, the V8 engine exposes the ability to set breakpoints, obtain environment information, and execute scripts through socket, and the format of the information transmitted by socket is v8 debug protocol.

For example:

Set the breakpoint:

{"seq": 117, "type": "request", "command": "setbreakpoint", "arguments": {"type": "function", "target": "f"}

Remove the breakpoint:

{"seq": 117, "type": "request", "command": "clearbreakpoint", "arguments": {"type": "function", "breakpoint": 1}}

Continue:

{"seq": 117, "type": "request", "command": "continue"}

Execute the code:

{"seq": 117, "type": "request", "command": "evaluate", "arguments": {"expression": "120th 2"}}

Interested students can go to the v8 debug protocol documentation to see all the protocols.

Based on these protocols, you can control the debugger of v8, and all those who can implement debugger are docked with this protocol, such as chrome devtools, vscode debugger and other debugger of ide.

Debugging of nodejs code

Nodejs can be debugged by adding-- inspect's option (or-- inspect-brk, which will stop on the first line).

It will serve as a websocket server for debugger, and we can debug nodejs code with vscode or chrome devtools (see nodejs debugger documentation).

➜node-- inspect test.js Debugger listening on ws://127.0.0.1:9229/db309268-623a-4abe-b19a-c4407ed8998d For help see https://nodejs.org/en/docs/inspector

The principle is to implement v8 debug protocol.

If we do our own debugging tools and ide, we have to dock with this protocol.

Debugger adaptor protocol

The v8 debug protocol described above can debug js code, so python, c # and so on must have their own debugging protocols. If you want to implement ide, it is too troublesome to dock again. So then there was an intermediate layer protocol, DAP (debugger adaptor protocol).

Debugger adaptor protocol, as its name implies, is adaptable, adapting to various debugger protocols on one end and providing a unified protocol to the client on the other. This is a good application of the adapter pattern.

At this point, the study of "what is the principle of Debugger in JavaScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report